Skip to content

Commit 7cee7b5

Browse files
committed
dns: rename verbatim to order and support ipv6first
1 parent 4221631 commit 7cee7b5

19 files changed

+274
-113
lines changed

doc/api/cli.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,16 +594,20 @@ added:
594594
- v16.4.0
595595
- v14.18.0
596596
changes:
597+
- version: REPLACEME
598+
pr-url: https://github.com/nodejs/node/pull/52492
599+
description: The `ipv6first` is supported now.
597600
- version: v17.0.0
598601
pr-url: https://github.com/nodejs/node/pull/39987
599602
description: Changed default value to `verbatim`.
600603
-->
601604

602-
Set the default value of `verbatim` in [`dns.lookup()`][] and
605+
Set the default value of `order` in [`dns.lookup()`][] and
603606
[`dnsPromises.lookup()`][]. The value could be:
604607

605-
* `ipv4first`: sets default `verbatim` `false`.
606-
* `verbatim`: sets default `verbatim` `true`.
608+
* `ipv4first`: sets default `order` to `ipv4first`.
609+
* `ipv6first`: sets default `order` to `ipv6first`.
610+
* `verbatim`: sets default `order` to `verbatim`.
607611

608612
The default is `verbatim` and [`dns.setDefaultResultOrder()`][] have higher
609613
priority than `--dns-result-order`.

doc/api/dns.md

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ section if a custom port is used.
179179
<!-- YAML
180180
added: v0.1.90
181181
changes:
182+
- version: REPLACEME
183+
pr-url: https://github.com/nodejs/node/pull/52492
184+
description: The `verbatim` option has been replaced with the `order` option.
182185
- version: v18.4.0
183186
pr-url: https://github.com/nodejs/node/pull/43054
184187
description: For compatibility with `node:net`, when passing an option
@@ -211,11 +214,12 @@ changes:
211214
flags may be passed by bitwise `OR`ing their values.
212215
* `all` {boolean} When `true`, the callback returns all resolved addresses in
213216
an array. Otherwise, returns a single address. **Default:** `false`.
214-
* `verbatim` {boolean} When `true`, the callback receives IPv4 and IPv6
215-
addresses in the order the DNS resolver returned them. When `false`,
216-
IPv4 addresses are placed before IPv6 addresses.
217-
**Default:** `true` (addresses are not reordered). Default value is
218-
configurable using [`dns.setDefaultResultOrder()`][] or
217+
* `order` {string} When `verbatim`, the resolved addresses are return
218+
unsorted. When `ipv4first`, the resolved addresses are sorted by placing
219+
IPv4 addresses before IPv6 addresses. When `ipv6first`, the resolved
220+
addresses are sorted by placing IPv6 addresses before IPv4 addresses.
221+
**Default:** `verbatim` (addresses are not reordered).
222+
Default value is configurable using [`dns.setDefaultResultOrder()`][] or
219223
[`--dns-result-order`][].
220224
* `callback` {Function}
221225
* `err` {Error}
@@ -775,18 +779,22 @@ added:
775779
- v16.4.0
776780
- v14.18.0
777781
changes:
782+
- version: REPLACEME
783+
pr-url: https://github.com/nodejs/node/pull/52492
784+
description: The `ipv6first` value is supported now.
778785
- version: v17.0.0
779786
pr-url: https://github.com/nodejs/node/pull/39987
780787
description: Changed default value to `verbatim`.
781788
-->
782789

783-
* `order` {string} must be `'ipv4first'` or `'verbatim'`.
790+
* `order` {string} must be `'ipv4first'`, `'ipv6first'` or `'verbatim'`.
784791

785-
Set the default value of `verbatim` in [`dns.lookup()`][] and
792+
Set the default value of `order` in [`dns.lookup()`][] and
786793
[`dnsPromises.lookup()`][]. The value could be:
787794

788-
* `ipv4first`: sets default `verbatim` `false`.
789-
* `verbatim`: sets default `verbatim` `true`.
795+
* `ipv4first`: sets default `order` to `ipv4first`.
796+
* `ipv6first`: sets default `order` to `ipv6first`.
797+
* `verbatim`: sets default `order` to `verbatim`.
790798

791799
The default is `verbatim` and [`dns.setDefaultResultOrder()`][] have higher
792800
priority than [`--dns-result-order`][]. When using [worker threads][],
@@ -799,13 +807,18 @@ dns orders in workers.
799807
added:
800808
- v20.1.0
801809
- v18.17.0
810+
changes:
811+
- version: REPLACEME
812+
pr-url: https://github.com/nodejs/node/pull/52492
813+
description: The `ipv6first` value is supported now.
802814
-->
803815

804-
Get the default value for `verbatim` in [`dns.lookup()`][] and
816+
Get the default value for `order` in [`dns.lookup()`][] and
805817
[`dnsPromises.lookup()`][]. The value could be:
806818

807-
* `ipv4first`: for `verbatim` defaulting to `false`.
808-
* `verbatim`: for `verbatim` defaulting to `true`.
819+
* `ipv4first`: for `order` defaulting to `ipv4first`.
820+
* `ipv6first`: for `order` defaulting to `ipv6first`.
821+
* `verbatim`: for `order` defaulting to `verbatim`.
809822

810823
## `dns.setServers(servers)`
811824

@@ -949,6 +962,10 @@ section if a custom port is used.
949962

950963
<!-- YAML
951964
added: v10.6.0
965+
changes:
966+
- version: REPLACEME
967+
pr-url: https://github.com/nodejs/node/pull/52492
968+
description: The verbatim option has been replaced by the `order` option.
952969
-->
953970

954971
* `hostname` {string}
@@ -961,13 +978,13 @@ added: v10.6.0
961978
flags may be passed by bitwise `OR`ing their values.
962979
* `all` {boolean} When `true`, the `Promise` is resolved with all addresses in
963980
an array. Otherwise, returns a single address. **Default:** `false`.
964-
* `verbatim` {boolean} When `true`, the `Promise` is resolved with IPv4 and
965-
IPv6 addresses in the order the DNS resolver returned them. When `false`,
966-
IPv4 addresses are placed before IPv6 addresses.
967-
**Default:** currently `false` (addresses are reordered) but this is
968-
expected to change in the not too distant future. Default value is
969-
configurable using [`dns.setDefaultResultOrder()`][] or
970-
[`--dns-result-order`][]. New code should use `{ verbatim: true }`.
981+
* `order` {string} When `verbatim`, the `Promise` is resolved with IPv4 and
982+
IPv6 addresses in the order the DNS resolver returned them. When `ipv4first`,
983+
IPv4 addresses are placed before IPv6 addresses. When `ipv6first`,
984+
IPv6 addresses are placed before IPv4 addresses.
985+
**Default:** `verbatim` (addresses are not reordered).
986+
Default value is configurable using [`dns.setDefaultResultOrder()`][] or
987+
[`--dns-result-order`][]. New code should use `{ order: 'verbatim' }`.
971988

972989
Resolves a host name (e.g. `'nodejs.org'`) into the first found A (IPv4) or
973990
AAAA (IPv6) record. All `option` properties are optional. If `options` is an
@@ -1349,18 +1366,22 @@ added:
13491366
- v16.4.0
13501367
- v14.18.0
13511368
changes:
1369+
- version: REPLACEME
1370+
pr-url: https://github.com/nodejs/node/pull/52492
1371+
description: The `ipv6first` value is supported now.
13521372
- version: v17.0.0
13531373
pr-url: https://github.com/nodejs/node/pull/39987
13541374
description: Changed default value to `verbatim`.
13551375
-->
13561376

1357-
* `order` {string} must be `'ipv4first'` or `'verbatim'`.
1377+
* `order` {string} must be `'ipv4first'`, `'ipv6first'` or `'verbatim'`.
13581378

1359-
Set the default value of `verbatim` in [`dns.lookup()`][] and
1379+
Set the default value of `order` in [`dns.lookup()`][] and
13601380
[`dnsPromises.lookup()`][]. The value could be:
13611381

1362-
* `ipv4first`: sets default `verbatim` `false`.
1363-
* `verbatim`: sets default `verbatim` `true`.
1382+
* `ipv4first`: sets default `order` to `ipv4first`.
1383+
* `ipv6first`: sets default `order` to `ipv6first`.
1384+
* `verbatim`: sets default `order` to `verbatim`.
13641385

13651386
The default is `verbatim` and [`dnsPromises.setDefaultResultOrder()`][] have
13661387
higher priority than [`--dns-result-order`][]. When using [worker threads][],

lib/dns.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const {
4343
setDefaultResolver,
4444
validateHints,
4545
emitInvalidHostnameWarning,
46-
getDefaultVerbatim,
4746
getDefaultResultOrder,
4847
setDefaultResultOrder,
4948
errorCodes: dnsErrorCodes,
@@ -89,6 +88,9 @@ const {
8988
const {
9089
GetAddrInfoReqWrap,
9190
GetNameInfoReqWrap,
91+
DNS_ORDER_VERBATIM,
92+
DNS_ORDER_IPV4_FIRST,
93+
DNS_ORDER_IPV6_FIRST,
9294
} = cares;
9395

9496
const kPerfHooksDnsLookupContext = Symbol('kPerfHooksDnsLookupContext');
@@ -141,7 +143,7 @@ function lookup(hostname, options, callback) {
141143
let hints = 0;
142144
let family = 0;
143145
let all = false;
144-
let verbatim = getDefaultVerbatim();
146+
let dnsOrder = getDefaultResultOrder();
145147

146148
// Parse arguments
147149
if (hostname) {
@@ -185,9 +187,9 @@ function lookup(hostname, options, callback) {
185187
validateBoolean(options.all, 'options.all');
186188
all = options.all;
187189
}
188-
if (options?.verbatim != null) {
189-
validateBoolean(options.verbatim, 'options.verbatim');
190-
verbatim = options.verbatim;
190+
if (options?.order != null) {
191+
validateOneOf(options.order, 'options.order', ['ipv4first', 'ipv6first', 'verbatim']);
192+
dnsOrder = options.dnsOrder;
191193
}
192194
}
193195

@@ -218,8 +220,16 @@ function lookup(hostname, options, callback) {
218220
req.hostname = hostname;
219221
req.oncomplete = all ? onlookupall : onlookup;
220222

223+
let order = DNS_ORDER_VERBATIM;
224+
225+
if (dnsOrder === 'ipv4first') {
226+
order = DNS_ORDER_IPV4_FIRST;
227+
} else if (dnsOrder === 'ipv6first') {
228+
order = DNS_ORDER_IPV6_FIRST;
229+
}
230+
221231
const err = cares.getaddrinfo(
222-
req, hostname, family, hints, verbatim,
232+
req, hostname, family, hints, order,
223233
);
224234
if (err) {
225235
process.nextTick(callback, new DNSException(err, 'getaddrinfo', hostname));
@@ -230,7 +240,7 @@ function lookup(hostname, options, callback) {
230240
hostname,
231241
family,
232242
hints,
233-
verbatim,
243+
order: dnsOrder,
234244
};
235245
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
236246
}

lib/internal/dns/promises.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
createResolverClass,
1313
validateHints,
1414
emitInvalidHostnameWarning,
15-
getDefaultVerbatim,
1615
errorCodes: dnsErrorCodes,
1716
getDefaultResultOrder,
1817
setDefaultResultOrder,
@@ -53,6 +52,9 @@ const {
5352
GetAddrInfoReqWrap,
5453
GetNameInfoReqWrap,
5554
QueryReqWrap,
55+
DNS_ORDER_VERBATIM,
56+
DNS_ORDER_IPV4_FIRST,
57+
DNS_ORDER_IPV6_FIRST,
5658
} = internalBinding('cares_wrap');
5759
const {
5860
ERR_INVALID_ARG_TYPE,
@@ -120,13 +122,13 @@ function onlookupall(err, addresses) {
120122
* @param {boolean} all - Whether to resolve with all IP addresses for the hostname.
121123
* @param {number} hints - One or more supported getaddrinfo flags (supply multiple via
122124
* bitwise OR).
123-
* @param {boolean} verbatim - Whether to use the hostname verbatim.
125+
* @param {number} dnsOrder - How to sort results. Must be `ipv4first`, `ipv6first` or `verbatim`.
124126
* @returns {Promise<DNSLookupResult | DNSLookupResult[]>} The IP address(es) of the hostname.
125127
* @typedef {object} DNSLookupResult
126128
* @property {string} address - The IP address.
127129
* @property {0 | 4 | 6} family - The IP address type. 4 for IPv4 or 6 for IPv6, or 0 (for both).
128130
*/
129-
function createLookupPromise(family, hostname, all, hints, verbatim) {
131+
function createLookupPromise(family, hostname, all, hints, dnsOrder) {
130132
return new Promise((resolve, reject) => {
131133
if (!hostname) {
132134
emitInvalidHostnameWarning(hostname);
@@ -150,7 +152,15 @@ function createLookupPromise(family, hostname, all, hints, verbatim) {
150152
req.resolve = resolve;
151153
req.reject = reject;
152154

153-
const err = getaddrinfo(req, hostname, family, hints, verbatim);
155+
let order = DNS_ORDER_VERBATIM;
156+
157+
if (dnsOrder === 'ipv4first') {
158+
order = DNS_ORDER_IPV4_FIRST;
159+
} else if (dnsOrder === 'ipv6first') {
160+
order = DNS_ORDER_IPV6_FIRST;
161+
}
162+
163+
const err = getaddrinfo(req, hostname, family, hints, order);
154164

155165
if (err) {
156166
reject(new DNSException(err, 'getaddrinfo', hostname));
@@ -159,7 +169,7 @@ function createLookupPromise(family, hostname, all, hints, verbatim) {
159169
hostname,
160170
family,
161171
hints,
162-
verbatim,
172+
order: dnsOrder,
163173
};
164174
startPerf(req, kPerfHooksDnsLookupContext, { type: 'dns', name: 'lookup', detail });
165175
}
@@ -175,14 +185,15 @@ const validFamilies = [0, 4, 6];
175185
* @param {0 | 4 | 6} [options.family=0] - The record family. Must be 4, 6, or 0 (for both).
176186
* @param {number} [options.hints] - One or more supported getaddrinfo flags (supply multiple via
177187
* bitwise OR).
178-
* @param {boolean} [options.verbatim=false] - Return results in same order DNS resolved them;
179-
* otherwise IPv4 then IPv6. New code should supply `true`.
188+
* @param {string} [options.order='verbatim'] - Return results in same order DNS resolved them;
189+
* Must be `ipv4first`, `ipv6first` or `verbatim`.
190+
* New code should supply `verbatim`.
180191
*/
181192
function lookup(hostname, options) {
182193
let hints = 0;
183194
let family = 0;
184195
let all = false;
185-
let verbatim = getDefaultVerbatim();
196+
let dnsOrder = getDefaultResultOrder();
186197

187198
// Parse arguments
188199
if (hostname) {
@@ -208,13 +219,13 @@ function lookup(hostname, options) {
208219
validateBoolean(options.all, 'options.all');
209220
all = options.all;
210221
}
211-
if (options?.verbatim != null) {
212-
validateBoolean(options.verbatim, 'options.verbatim');
213-
verbatim = options.verbatim;
222+
if (options?.order != null) {
223+
validateOneOf(options.order, 'options.order', ['ipv4first', 'ipv6first', 'verbatim']);
224+
dnsOrder = options.order;
214225
}
215226
}
216227

217-
return createLookupPromise(family, hostname, all, hints, verbatim);
228+
return createLookupPromise(family, hostname, all, hints, dnsOrder);
218229
}
219230

220231

lib/internal/dns/utils.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ let defaultResolver;
202202
let dnsOrder;
203203

204204
function initializeDns() {
205+
// TODO@PI: Validate options
205206
const orderFromCLI = getOptionValue('--dns-result-order');
206207
if (!orderFromCLI) {
207208
dnsOrder ??= 'verbatim';
@@ -277,12 +278,8 @@ function emitInvalidHostnameWarning(hostname) {
277278
}
278279
}
279280

280-
function getDefaultVerbatim() {
281-
return dnsOrder !== 'ipv4first';
282-
}
283-
284281
function setDefaultResultOrder(value) {
285-
validateOneOf(value, 'dnsOrder', ['verbatim', 'ipv4first']);
282+
validateOneOf(value, 'dnsOrder', ['verbatim', 'ipv4first', 'ipv6first']);
286283
dnsOrder = value;
287284
}
288285

@@ -351,7 +348,6 @@ module.exports = {
351348
validateTimeout,
352349
validateTries,
353350
emitInvalidHostnameWarning,
354-
getDefaultVerbatim,
355351
getDefaultResultOrder,
356352
setDefaultResultOrder,
357353
errorCodes,

lib/internal/modules/esm/fetch_module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ async function isLocalAddress(hostname) {
253253
) {
254254
hostname = StringPrototypeSlice(hostname, 1, -1);
255255
}
256-
const addr = await dnsLookup(hostname, { verbatim: true });
256+
const addr = await dnsLookup(hostname, { order: 'verbatim' });
257257
const ipv = addr.family === 4 ? 'ipv4' : 'ipv6';
258258
return allowList.check(addr.address, ipv);
259259
} catch {

0 commit comments

Comments
 (0)