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 May 10, 2022
1 parent ec1c616 commit 8895869
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ section if a custom port is used.
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000
description: For compatibility with `node:net`, when passing an option
object the `family` option can be a string.
- 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,7 +201,8 @@ changes:

* `hostname` {string}
* `options` {integer | Object}
* `family` {integer} The record family. Must be `4`, `6`, or `0`. The value
* `family` {integer|string} The record family. Must be `4`, `6`, or `0`.
`'IPv4'` is interpreted as `4`, and `'IPv6'` is interpreted as `6`. The value
`0` indicates that IPv4 and IPv6 addresses are both returned. **Default:**
`0`.
* `hints` {number} One or more [supported `getaddrinfo` flags][]. Multiple
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

0 comments on commit 8895869

Please sign in to comment.