diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 458d61ee2316b1..6a14976af98935 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2521,17 +2521,18 @@ object can lead to crashing the application. -Type: Runtime +Type: End-of-Life Previous versions of Node.js supported `dns.lookup()` with a falsy host name -like `dns.lookup(false)` due to backward compatibility. -This behavior is undocumented and is thought to be unused in real world apps. -It will become an error in future versions of Node.js. +like `dns.lookup(false)` due to backward compatibility. This has been removed. ### DEP0119: `process.binding('uv').errname()` private API diff --git a/lib/dns.js b/lib/dns.js index d4c7d2c3c0e44e..d651f5ea0a2685 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -42,7 +42,6 @@ const { bindDefaultResolver, setDefaultResolver, validateHints, - emitInvalidHostnameWarning, getDefaultResultOrder, setDefaultResultOrder, errorCodes: dnsErrorCodes, @@ -199,13 +198,8 @@ function lookup(hostname, options, callback) { } if (!hostname) { - emitInvalidHostnameWarning(hostname); - if (all) { - process.nextTick(callback, null, []); - } else { - process.nextTick(callback, null, null, family === 6 ? 6 : 4); - } - return {}; + throw new ERR_INVALID_ARG_VALUE('hostname', hostname, + 'must be a non-empty string'); } const matchedFamily = isIP(hostname); diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 300052a04608ae..8f12d4aa998859 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -11,7 +11,6 @@ const { bindDefaultResolver, createResolverClass, validateHints, - emitInvalidHostnameWarning, errorCodes: dnsErrorCodes, getDefaultResultOrder, setDefaultResultOrder, @@ -135,8 +134,8 @@ function onlookupall(err, addresses) { function createLookupPromise(family, hostname, all, hints, dnsOrder) { return new Promise((resolve, reject) => { if (!hostname) { - emitInvalidHostnameWarning(hostname); - resolve(all ? [] : { address: null, family: family === 6 ? 6 : 4 }); + reject(new ERR_INVALID_ARG_VALUE('hostname', hostname, + 'must be a non-empty string')); return; } diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 85918f0d43d5c6..55f0b5268abf9d 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -269,19 +269,6 @@ function validateHints(hints) { } } -let invalidHostnameWarningEmitted = false; -function emitInvalidHostnameWarning(hostname) { - if (!invalidHostnameWarningEmitted) { - process.emitWarning( - `The provided hostname "${hostname}" is not a valid ` + - 'hostname, and is supported in the dns module solely for compatibility.', - 'DeprecationWarning', - 'DEP0118', - ); - invalidHostnameWarningEmitted = true; - } -} - function setDefaultResultOrder(value) { validateOneOf(value, 'dnsOrder', validDnsOrders); dnsOrder = value; @@ -352,7 +339,6 @@ module.exports = { validateHints, validateTimeout, validateTries, - emitInvalidHostnameWarning, getDefaultResultOrder, setDefaultResultOrder, errorCodes, diff --git a/test/internet/test-dns.js b/test/internet/test-dns.js index 84ee4f7c9ca9de..538d49064799e5 100644 --- a/test/internet/test-dns.js +++ b/test/internet/test-dns.js @@ -629,21 +629,6 @@ TEST(function test_lookup_ip_promise(done) { }); -TEST(async function test_lookup_null_all(done) { - assert.deepStrictEqual(await dnsPromises.lookup(null, { all: true }), []); - - const req = dns.lookup(null, { all: true }, (err, ips) => { - assert.ifError(err); - assert.ok(Array.isArray(ips)); - assert.strictEqual(ips.length, 0); - - done(); - }); - - checkWrap(req); -}); - - TEST(async function test_lookup_all_mixed(done) { function validateResult(result) { assert.ok(Array.isArray(result)); diff --git a/test/parallel/test-c-ares.js b/test/parallel/test-c-ares.js index 7576be0589e227..08d77ed65731ba 100644 --- a/test/parallel/test-c-ares.js +++ b/test/parallel/test-c-ares.js @@ -29,9 +29,9 @@ const dnsPromises = dns.promises; (async function() { let res; - res = await dnsPromises.lookup(null); - assert.strictEqual(res.address, null); - assert.strictEqual(res.family, 4); + await assert.rejects(dnsPromises.lookup(null), { + code: 'ERR_INVALID_ARG_VALUE', + }); res = await dnsPromises.lookup('127.0.0.1'); assert.strictEqual(res.address, '127.0.0.1'); @@ -43,10 +43,9 @@ const dnsPromises = dns.promises; })().then(common.mustCall()); // Try resolution without hostname. -dns.lookup(null, common.mustSucceed((result, addressType) => { - assert.strictEqual(result, null); - assert.strictEqual(addressType, 4); -})); +assert.throws(() => dns.lookup(null, common.mustNotCall()), { + code: 'ERR_INVALID_ARG_VALUE', +}); dns.lookup('127.0.0.1', common.mustSucceed((result, addressType) => { assert.strictEqual(result, '127.0.0.1'); diff --git a/test/parallel/test-dns-lookup.js b/test/parallel/test-dns-lookup.js index 404c5555c5fcdf..8d1526903d7d1c 100644 --- a/test/parallel/test-dns-lookup.js +++ b/test/parallel/test-dns-lookup.js @@ -29,11 +29,6 @@ common.expectWarning({ 'internal/test/binding': [ 'These APIs are for internal testing only. Do not use them.', ], - // For calling `dns.lookup` with falsy `hostname`. - 'DeprecationWarning': { - DEP0118: 'The provided hostname "false" is not a valid ' + - 'hostname, and is supported in the dns module solely for compatibility.' - } }); assert.throws(() => { @@ -145,12 +140,13 @@ assert.throws(() => dnsPromises.lookup(false, () => {}), (async function() { let res; - res = await dnsPromises.lookup(false, { + await assert.rejects(dnsPromises.lookup(false, { hints: 0, family: 0, all: true + }), { + code: 'ERR_INVALID_ARG_VALUE', }); - assert.deepStrictEqual(res, []); res = await dnsPromises.lookup('127.0.0.1', { hints: 0, @@ -167,14 +163,13 @@ assert.throws(() => dnsPromises.lookup(false, () => {}), assert.deepStrictEqual(res, { address: '127.0.0.1', family: 4 }); })().then(common.mustCall()); -dns.lookup(false, { +assert.throws(() => dns.lookup(false, { hints: 0, family: 0, all: true -}, common.mustSucceed((result, addressType) => { - assert.deepStrictEqual(result, []); - assert.strictEqual(addressType, undefined); -})); +}, common.mustNotCall()), { + code: 'ERR_INVALID_ARG_VALUE', +}); dns.lookup('127.0.0.1', { hints: 0, diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 7d67b09d58fc5d..a49b64666c3c4d 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -193,16 +193,13 @@ assert.deepStrictEqual(dns.getServers(), []); // dns.lookup should accept falsey values { - const checkCallback = (err, address, family) => { - assert.ifError(err); - assert.strictEqual(address, null); - assert.strictEqual(family, 4); - }; - ['', null, undefined, 0, NaN].forEach(async (value) => { - const res = await dnsPromises.lookup(value); - assert.deepStrictEqual(res, { address: null, family: 4 }); - dns.lookup(value, common.mustCall(checkCallback)); + await assert.rejects(dnsPromises.lookup(value), { + code: 'ERR_INVALID_ARG_VALUE', + }); + assert.throws(() => dns.lookup(value, common.mustNotCall()), { + code: 'ERR_INVALID_ARG_VALUE', + }); }); } @@ -247,52 +244,104 @@ assert.throws(() => dns.lookup('', { name: 'TypeError' }); -dns.lookup('', { family: 4, hints: 0 }, common.mustCall()); +assert.throws(() => { + dns.lookup('', { family: 4, hints: 0 }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - family: 6, - hints: dns.ADDRCONFIG -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + family: 6, + hints: dns.ADDRCONFIG + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { hints: dns.V4MAPPED }, common.mustCall()); +assert.throws(() => { + dns.lookup('', { hints: dns.V4MAPPED }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.ADDRCONFIG | dns.V4MAPPED -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.ADDRCONFIG | dns.V4MAPPED + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.ALL -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.ALL + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.V4MAPPED | dns.ALL -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.V4MAPPED | dns.ALL + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL, - family: 'IPv4' -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL, + family: 'IPv4' + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); -dns.lookup('', { - hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL, - family: 'IPv6' -}, common.mustCall()); +assert.throws(() => { + dns.lookup('', { + hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL, + family: 'IPv6' + }, common.mustNotCall()); +}, { + code: 'ERR_INVALID_ARG_VALUE', +}); (async function() { - await dnsPromises.lookup('', { family: 4, hints: 0 }); - await dnsPromises.lookup('', { family: 6, hints: dns.ADDRCONFIG }); - await dnsPromises.lookup('', { hints: dns.V4MAPPED }); - await dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED }); - await dnsPromises.lookup('', { hints: dns.ALL }); - await dnsPromises.lookup('', { hints: dns.V4MAPPED | dns.ALL }); - await dnsPromises.lookup('', { + await assert.rejects(dnsPromises.lookup('', { family: 4, hints: 0 }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { family: 6, hints: dns.ADDRCONFIG }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { hints: dns.V4MAPPED }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { hints: dns.ALL }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { hints: dns.V4MAPPED | dns.ALL }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL + }), { + code: 'ERR_INVALID_ARG_VALUE', + }); + await assert.rejects(dnsPromises.lookup('', { order: 'verbatim' }), { + code: 'ERR_INVALID_ARG_VALUE', }); - await dnsPromises.lookup('', { order: 'verbatim' }); })().then(common.mustCall()); {