Skip to content

Commit 4528834

Browse files
committed
dns: runtime deprecate type coercion of dns.lookup options
1 parent f41893e commit 4528834

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,9 +2792,12 @@ changes:
27922792
- version: REPLACEME
27932793
pr-url: https://github.com/nodejs/node/pull/38906
27942794
description: Documentation-only deprecation.
2795+
- version: REPLACEME
2796+
pr-url: https://github.com/nodejs/node/pull/00000
2797+
description: Runtime deprecation.
27952798
-->
27962799

2797-
Type: Documentation-only
2800+
Type: Runtime
27982801

27992802
Using a non-nullish non-integer value for `family` option, a non-nullish
28002803
non-number value for `hints` option, a non-nullish non-boolean value for `all`

lib/dns.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141
Resolver,
4242
validateHints,
4343
emitInvalidHostnameWarning,
44+
emitTypeCoercionDeprecationWarning,
4445
getDefaultVerbatim,
4546
setDefaultResultOrder,
4647
} = require('internal/dns/utils');
@@ -112,15 +113,29 @@ function lookup(hostname, options, callback) {
112113
validateCallback(callback);
113114

114115
if (options !== null && typeof options === 'object') {
116+
if (options.hints != null && typeof options.hints !== 'number') {
117+
emitTypeCoercionDeprecationWarning();
118+
}
115119
hints = options.hints >>> 0;
120+
if (options.family != null && typeof options.family !== 'number') {
121+
emitTypeCoercionDeprecationWarning();
122+
}
116123
family = options.family >>> 0;
124+
if (options.all != null && typeof options.all !== 'boolean') {
125+
emitTypeCoercionDeprecationWarning();
126+
}
117127
all = options.all === true;
118128
if (typeof options.verbatim === 'boolean') {
119129
verbatim = options.verbatim === true;
130+
} else if (options.verbatim != null) {
131+
emitTypeCoercionDeprecationWarning();
120132
}
121133

122134
validateHints(hints);
123135
} else {
136+
if (options !== undefined && typeof options.family !== 'number') {
137+
emitTypeCoercionDeprecationWarning();
138+
}
124139
family = options >>> 0;
125140
}
126141
}

lib/internal/dns/promises.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
validateTimeout,
1515
validateTries,
1616
emitInvalidHostnameWarning,
17+
emitTypeCoercionDeprecationWarning,
1718
getDefaultVerbatim,
1819
} = require('internal/dns/utils');
1920
const { codes, dnsException } = require('internal/errors');
@@ -110,15 +111,29 @@ function lookup(hostname, options) {
110111
if (hostname && typeof hostname !== 'string') {
111112
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
112113
} else if (options !== null && typeof options === 'object') {
114+
if (options.hints != null && typeof options.hints !== 'number') {
115+
emitTypeCoercionDeprecationWarning();
116+
}
113117
hints = options.hints >>> 0;
118+
if (options.family != null && typeof options.family !== 'number') {
119+
emitTypeCoercionDeprecationWarning();
120+
}
114121
family = options.family >>> 0;
122+
if (options.all != null && typeof options.all !== 'boolean') {
123+
emitTypeCoercionDeprecationWarning();
124+
}
115125
all = options.all === true;
116126
if (typeof options.verbatim === 'boolean') {
117127
verbatim = options.verbatim === true;
128+
} else if (options.verbatim != null) {
129+
emitTypeCoercionDeprecationWarning();
118130
}
119131

120132
validateHints(hints);
121133
} else {
134+
if (options !== undefined && typeof options.family !== 'number') {
135+
emitTypeCoercionDeprecationWarning();
136+
}
122137
family = options >>> 0;
123138
}
124139

lib/internal/dns/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ function emitInvalidHostnameWarning(hostname) {
193193
);
194194
}
195195

196+
let typeCoercionWarningEmitted = false;
197+
function emitTypeCoercionDeprecationWarning() {
198+
if (!typeCoercionWarningEmitted) {
199+
process.emitWarning(
200+
'Type coercion of dns.lookup options is deprecated',
201+
'DeprecationWarning',
202+
'DEP0153'
203+
);
204+
typeCoercionWarningEmitted = true;
205+
}
206+
}
207+
196208
let dnsOrder = getOptionValue('--dns-result-order') || 'ipv4first';
197209

198210
function getDefaultVerbatim() {
@@ -219,6 +231,7 @@ module.exports = {
219231
validateTries,
220232
Resolver,
221233
emitInvalidHostnameWarning,
234+
emitTypeCoercionDeprecationWarning,
222235
getDefaultVerbatim,
223236
setDefaultResultOrder,
224237
};

0 commit comments

Comments
 (0)