Skip to content

Commit

Permalink
dns: support promisified lookup(Service)
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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 authored and Olivier Martin committed May 19, 2017
1 parent ea09dd7 commit 1ba0b9b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`][].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/internet/test-dns-ipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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();
}));
});
28 changes: 28 additions & 0 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1ba0b9b

Please sign in to comment.