Skip to content

Commit

Permalink
dns: add dns/promises alias
Browse files Browse the repository at this point in the history
PR-URL: #32953
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
shisama authored and BridgeAR committed May 24, 2020
1 parent 7a094dc commit 039cd00
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ earlier ones time out or result in some other error.

The `dns.promises` API provides an alternative set of asynchronous DNS methods
that return `Promise` objects rather than using callbacks. The API is accessible
via `require('dns').promises`.
via `require('dns').promises` or `require('dns/promises')`.

### Class: `dnsPromises.Resolver`
<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions lib/dns/promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const dnsPromises = require('internal/dns/promises');
dnsPromises.setServers = require('dns').setServers;
module.exports = dnsPromises;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'lib/cluster.js',
'lib/dgram.js',
'lib/dns.js',
'lib/dns/promises.js',
'lib/domain.js',
'lib/events.js',
'lib/fs.js',
Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-dns-promises.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Flags: --expose-internals
import '../common/index.mjs';
import assert from 'assert';
import { lookupService } from 'dns/promises';

const invalidAddress = 'fasdfdsaf';

assert.throws(() => {
lookupService(invalidAddress, 0);
}, {
code: 'ERR_INVALID_OPT_VALUE',
name: 'TypeError',
message: `The value "${invalidAddress}" is invalid for option "address"`
});
6 changes: 6 additions & 0 deletions test/parallel/test-dns-promises-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

require('../common');
const assert = require('assert');

assert.strictEqual(require('dns/promises'), require('dns').promises);
33 changes: 33 additions & 0 deletions test/parallel/test-dns-setservers-type-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,36 @@ const promiseResolver = new dns.promises.Resolver();
);
});
}

// This test for 'dns/promises'
{
const {
setServers
} = require('dns/promises');

// This should not throw any error.
(async () => {
setServers([ '127.0.0.1' ]);
})();

[
[null],
[undefined],
[Number(addresses.DNS4_SERVER)],
[
{
address: addresses.DNS4_SERVER
}
]
].forEach((val) => {
const errObj = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "servers[0]" argument must be of type string.' +
common.invalidArgTypeHelper(val[0])
};
assert.throws(() => {
setServers(val);
}, errObj);
});
}

0 comments on commit 039cd00

Please sign in to comment.