Skip to content

Commit 0ae969f

Browse files
committed
dns: deprecate passing falsy hostname to dns.lookup
We can `dns.lookup` a falsy `hostname` like `dns.lookup(false)` for the reason of backwards compatibility long before(see nodejs#13119 for detail). This behavior is undocumented and seems useless in real world apps. We could also make invalid `hostname` throw in the future and the change might be semver-major. Fixes: nodejs#13119
1 parent 70ab310 commit 0ae969f

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

lib/dns.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const {
3131
getDefaultResolver,
3232
setDefaultResolver,
3333
Resolver,
34-
validateHints
34+
validateHints,
35+
emitInvalidHostnameWarning,
3536
} = require('internal/dns/utils');
3637
const {
3738
ERR_INVALID_ARG_TYPE,
@@ -93,7 +94,7 @@ function lookup(hostname, options, callback) {
9394

9495
// Parse arguments
9596
if (hostname && typeof hostname !== 'string') {
96-
throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname);
97+
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
9798
} else if (typeof options === 'function') {
9899
callback = options;
99100
family = 0;
@@ -114,6 +115,7 @@ function lookup(hostname, options, callback) {
114115
throw new ERR_INVALID_OPT_VALUE('family', family);
115116

116117
if (!hostname) {
118+
emitInvalidHostnameWarning(hostname);
117119
if (all) {
118120
process.nextTick(callback, null, []);
119121
} else {

lib/internal/dns/promises.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
const {
33
bindDefaultResolver,
44
Resolver: CallbackResolver,
5-
validateHints
5+
validateHints,
6+
emitInvalidHostnameWarning,
67
} = require('internal/dns/utils');
78
const { codes, dnsException } = require('internal/errors');
89
const { isIP, isIPv4, isLegalPort } = require('internal/net');
@@ -56,6 +57,7 @@ function onlookupall(err, addresses) {
5657
function createLookupPromise(family, hostname, all, hints, verbatim) {
5758
return new Promise((resolve, reject) => {
5859
if (!hostname) {
60+
emitInvalidHostnameWarning(hostname);
5961
if (all)
6062
resolve([]);
6163
else
@@ -100,7 +102,7 @@ function lookup(hostname, options) {
100102

101103
// Parse arguments
102104
if (hostname && typeof hostname !== 'string') {
103-
throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname);
105+
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
104106
} else if (options !== null && typeof options === 'object') {
105107
hints = options.hints >>> 0;
106108
family = options.family >>> 0;

lib/internal/dns/utils.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,19 @@ function validateHints(hints) {
141141
}
142142
}
143143

144+
function emitInvalidHostnameWarning(hostname) {
145+
process.emitWarning(
146+
`The provided hostname "${hostname}" is not a valid ` +
147+
'hostname, and is supported in the dns module solely for compatibility.',
148+
'DeprecationWarning',
149+
);
150+
}
151+
144152
module.exports = {
145153
bindDefaultResolver,
146154
getDefaultResolver,
147155
setDefaultResolver,
148156
validateHints,
149-
Resolver
157+
Resolver,
158+
emitInvalidHostnameWarning,
150159
};

test/parallel/test-dns-lookup.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,36 @@ cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT;
1414
const err = {
1515
code: 'ERR_INVALID_ARG_TYPE',
1616
type: TypeError,
17-
message: /^The "hostname" argument must be one of type string or falsy/
17+
message: /^The "hostname" argument must be of type string\. Received type number/
1818
};
1919

2020
common.expectsError(() => dns.lookup(1, {}), err);
2121
common.expectsError(() => dnsPromises.lookup(1, {}), err);
2222
}
2323

24+
common.expectWarning({
25+
// For 'internal/test/binding' module.
26+
'internal/test/binding': [
27+
'These APIs are exposed only for testing and are not ' +
28+
'tracked by any versioning system or deprecation process.'
29+
],
30+
// For dns.promises.
31+
'ExperimentalWarning': [
32+
'The dns.promises API is experimental'
33+
],
34+
// For call `dns.lookup` with falsy `hostname`, twice.
35+
'DeprecationWarning': [
36+
[
37+
'The provided hostname "false" is not a valid ' +
38+
'hostname, and is supported in the dns module solely for compatibility.',
39+
],
40+
[
41+
'The provided hostname "false" is not a valid ' +
42+
'hostname, and is supported in the dns module solely for compatibility.',
43+
]
44+
],
45+
});
46+
2447
common.expectsError(() => {
2548
dns.lookup(false, 'cb');
2649
}, {

0 commit comments

Comments
 (0)