Permalink
Browse files

dns: support promisified `lookup(Service)`

PR-URL: #12442
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: William Kapke <william.kapke@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
  • Loading branch information...
addaleax committed Apr 16, 2017
1 parent fe5ca3f commit faf6654ff75e0f275afddfd980387235c3ddf103
Showing with 56 additions and 0 deletions.
  1. +8 −0 doc/api/dns.md
  2. +7 −0 lib/dns.js
  3. +13 −0 test/internet/test-dns-ipv4.js
  4. +28 −0 test/internet/test-dns.js
View
@@ -123,6 +123,10 @@ dns.lookup('example.com', options, (err, addresses) =>
// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
```
If this method is invoked as its [`util.promisify()`][]ed version, and `all`
is not set to `true`, it returns a Promise for an object with `address` and
`family` properties.
### Supported getaddrinfo flags
The following flags can be passed as hints to [`dns.lookup()`][].
@@ -163,6 +167,9 @@ dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
});
```
If this method is invoked as its [`util.promisify()`][]ed version, it returns a
Promise for an object with `hostname` and `service` properties.
## dns.resolve(hostname[, rrtype], callback)
<!-- YAML
added: v0.1.27
@@ -528,3 +535,4 @@ uses. For instance, _they do not use the configuration from `/etc/hosts`_.
[Implementation considerations section]: #dns_implementation_considerations
[supported `getaddrinfo` flags]: #dns_supported_getaddrinfo_flags
[the official libuv documentation]: http://docs.libuv.org/en/latest/threadpool.html
[`util.promisify()`]: util.html#util_util_promisify_original
View
@@ -26,6 +26,7 @@ const util = require('util');
const cares = process.binding('cares_wrap');
const uv = process.binding('uv');
const internalNet = require('internal/net');
const { customPromisifyArgs } = require('internal/util');
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
@@ -189,6 +190,9 @@ function lookup(hostname, options, callback) {
return req;
}
Object.defineProperty(lookup, customPromisifyArgs,
{ value: ['address', 'family'], enumerable: false });
function onlookupservice(err, host, service) {
if (err)
@@ -228,6 +232,9 @@ function lookupService(host, port, callback) {
return req;
}
Object.defineProperty(lookupService, customPromisifyArgs,
{ value: ['hostname', 'service'], enumerable: false });
function onresolve(err, result, ttls) {
if (ttls && this.ttl)
@@ -3,8 +3,11 @@ const common = require('../common');
const assert = require('assert');
const dns = require('dns');
const net = require('net');
const util = require('util');
const isIPv4 = net.isIPv4;
common.crashOnUnhandledRejection();
let running = false;
const queue = [];
@@ -184,3 +187,13 @@ TEST(function test_lookupservice_ip_ipv4(done) {
checkWrap(req);
});
TEST(function test_lookupservice_ip_ipv4_promise(done) {
util.promisify(dns.lookupService)('127.0.0.1', 80)
.then(common.mustCall(({hostname, service}) => {
assert.strictEqual(typeof hostname, 'string');
assert(hostname.length > 0);
assert(['http', 'www', '80'].includes(service));
done();
}));
});
View
@@ -28,6 +28,8 @@ const isIPv4 = net.isIPv4;
const isIPv6 = net.isIPv6;
const util = require('util');
common.crashOnUnhandledRejection();
let expected = 0;
let completed = 0;
let running = false;
@@ -428,6 +430,32 @@ TEST(function test_lookup_ip_all(done) {
});
TEST(function test_lookup_ip_all_promise(done) {
const req = util.promisify(dns.lookup)('127.0.0.1', {all: true})
.then(function(ips) {
assert.ok(Array.isArray(ips));
assert.ok(ips.length > 0);
assert.strictEqual(ips[0].address, '127.0.0.1');
assert.strictEqual(ips[0].family, 4);
done();
});
checkWrap(req);
});
TEST(function test_lookup_ip_promise(done) {
util.promisify(dns.lookup)('127.0.0.1')
.then(function({ address, family }) {
assert.strictEqual(address, '127.0.0.1');
assert.strictEqual(family, 4);
done();
});
});
TEST(function test_lookup_null_all(done) {
const req = dns.lookup(null, {all: true}, function(err, ips, family) {
assert.ifError(err);

0 comments on commit faf6654

Please sign in to comment.