dns: add dns/promises alias

PR-URL: https://github.com/nodejs/node/pull/32953
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
shisama 2020-04-21 04:46:26 +09:00 committed by Ruben Bridgewater
parent 7a094dc505
commit 039cd00a9a
6 changed files with 60 additions and 1 deletions

View File

@ -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

5
lib/dns/promises.js Normal file
View File

@ -0,0 +1,5 @@
'use strict';
const dnsPromises = require('internal/dns/promises');
dnsPromises.setServers = require('dns').setServers;
module.exports = dnsPromises;

View File

@ -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',

View File

@ -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"`
});

View File

@ -0,0 +1,6 @@
'use strict';
require('../common');
const assert = require('assert');
assert.strictEqual(require('dns/promises'), require('dns').promises);

View File

@ -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);
});
}