diff --git a/lib/dns.js b/lib/dns.js index 44fdb1764a13c0..bef14a469e7cc6 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -31,7 +31,8 @@ const { getDefaultResolver, setDefaultResolver, Resolver, - validateHints + validateHints, + emitInvalidHostnameWarning, } = require('internal/dns/utils'); const { ERR_INVALID_ARG_TYPE, @@ -93,7 +94,7 @@ function lookup(hostname, options, callback) { // Parse arguments if (hostname && typeof hostname !== 'string') { - throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname); + throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname); } else if (typeof options === 'function') { callback = options; family = 0; @@ -114,6 +115,7 @@ function lookup(hostname, options, callback) { throw new ERR_INVALID_OPT_VALUE('family', family); if (!hostname) { + emitInvalidHostnameWarning(hostname); if (all) { process.nextTick(callback, null, []); } else { diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 408dec712d821f..c38a7c591da35c 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -2,7 +2,8 @@ const { bindDefaultResolver, Resolver: CallbackResolver, - validateHints + validateHints, + emitInvalidHostnameWarning, } = require('internal/dns/utils'); const { codes, dnsException } = require('internal/errors'); const { isIP, isIPv4, isLegalPort } = require('internal/net'); @@ -56,6 +57,7 @@ function onlookupall(err, addresses) { function createLookupPromise(family, hostname, all, hints, verbatim) { return new Promise((resolve, reject) => { if (!hostname) { + emitInvalidHostnameWarning(hostname); if (all) resolve([]); else @@ -100,7 +102,7 @@ function lookup(hostname, options) { // Parse arguments if (hostname && typeof hostname !== 'string') { - throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname); + throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname); } else if (options !== null && typeof options === 'object') { hints = options.hints >>> 0; family = options.family >>> 0; diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 2362f28a821b9a..1f72f81b2e6d4c 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -141,10 +141,19 @@ function validateHints(hints) { } } +function emitInvalidHostnameWarning(hostname) { + process.emitWarning( + `The provided hostname "${hostname}" is not a valid ` + + 'hostname, and is supported in the dns module solely for compatibility.', + 'DeprecationWarning', + ); +} + module.exports = { bindDefaultResolver, getDefaultResolver, setDefaultResolver, validateHints, - Resolver + Resolver, + emitInvalidHostnameWarning, }; diff --git a/test/parallel/test-dns-lookup.js b/test/parallel/test-dns-lookup.js index e67120b0d2521b..d5f2a9507723f4 100644 --- a/test/parallel/test-dns-lookup.js +++ b/test/parallel/test-dns-lookup.js @@ -14,13 +14,36 @@ cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT; const err = { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, - message: /^The "hostname" argument must be one of type string or falsy/ + message: /^The "hostname" argument must be of type string\. Received type number/ }; common.expectsError(() => dns.lookup(1, {}), err); common.expectsError(() => dnsPromises.lookup(1, {}), err); } +common.expectWarning({ + // For 'internal/test/binding' module. + 'internal/test/binding': [ + 'These APIs are exposed only for testing and are not ' + + 'tracked by any versioning system or deprecation process.' + ], + // For dns.promises. + 'ExperimentalWarning': [ + 'The dns.promises API is experimental' + ], + // For call `dns.lookup` with falsy `hostname`, twice. + 'DeprecationWarning': [ + [ + 'The provided hostname "false" is not a valid ' + + 'hostname, and is supported in the dns module solely for compatibility.', + ], + [ + 'The provided hostname "false" is not a valid ' + + 'hostname, and is supported in the dns module solely for compatibility.', + ] + ], +}); + common.expectsError(() => { dns.lookup(false, 'cb'); }, {