Skip to content

Commit

Permalink
dns: accept 'IPv4' and 'IPv6' as valid family values
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Jun 5, 2022
1 parent c7c8d86 commit a6fdc5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
16 changes: 11 additions & 5 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ section if a custom port is used.
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43054
description: For compatibility with `node:net`, when passing an option
object the `family` option can be the string `'IPv4'` or the
string `'IPv6'`.
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the `callback` argument
Expand All @@ -197,9 +202,10 @@ changes:

* `hostname` {string}
* `options` {integer | Object}
* `family` {integer} The record family. Must be `4`, `6`, or `0`. The value
`0` indicates that IPv4 and IPv6 addresses are both returned. **Default:**
`0`.
* `family` {integer|string} The record family. Must be `4`, `6`, or `0`. For
backward compatibility reasons,`'IPv4'` and `'IPv6'` are interpreted as `4`
and `6` respectively. The value `0` indicates that IPv4 and IPv6 addresses
are both returned. **Default:** `0`.
* `hints` {number} One or more [supported `getaddrinfo` flags][]. Multiple
flags may be passed by bitwise `OR`ing their values.
* `all` {boolean} When `true`, the callback returns all resolved addresses in
Expand All @@ -219,8 +225,8 @@ changes:

Resolves a host name (e.g. `'nodejs.org'`) into the first found A (IPv4) or
AAAA (IPv6) record. All `option` properties are optional. If `options` is an
integer, then it must be `4` or `6` – if `options` is not provided, then IPv4
and IPv6 addresses are both returned if found.
integer, then it must be `4` or `6` – if `options` is `0` or not provided, then
IPv4 and IPv6 addresses are both returned if found.

With the `all` option set to `true`, the arguments for `callback` change to
`(err, addresses)`, with `addresses` being an array of objects with the
Expand Down
14 changes: 12 additions & 2 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ function lookup(hostname, options, callback) {
validateHints(hints);
}
if (options?.family != null) {
validateOneOf(options.family, 'options.family', validFamilies, true);
family = options.family;
switch (options.family) {
case 'IPv4':
family = 4;
break;
case 'IPv6':
family = 6;
break;
default:
validateOneOf(options.family, 'options.family', validFamilies, true);
family = options.family;
break;
}
}
if (options?.all != null) {
validateBoolean(options.all, 'options.all');
Expand Down
2 changes: 1 addition & 1 deletion test/internet/test-dns-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dns.lookup(addresses.NOT_FOUND, {

assert.throws(
() => dnsPromises.lookup(addresses.NOT_FOUND, {
family: 'IPv4',
family: 'ipv4',
all: 'all'
}),
{ code: 'ERR_INVALID_ARG_VALUE' }
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-socket-ready-without-cb.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const net = require('net');
const server = net.createServer(common.mustCall(function(conn) {
conn.end();
server.close();
})).listen(0, 'localhost', common.mustCall(function() {
})).listen(0, common.mustCall(function() {
const client = new net.Socket();

client.on('ready', common.mustCall(function() {
Expand Down

0 comments on commit a6fdc5c

Please sign in to comment.