Skip to content

Commit

Permalink
dns: support promisified lookup(Service)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Apr 18, 2017
1 parent 234deb5 commit d4ddf2a
Show file tree
Hide file tree
Showing 4 changed files with 52 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 @@ -521,3 +528,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
11 changes: 11 additions & 0 deletions test/internet/test-dns-ipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const common = require('../common');
const assert = require('assert');
const dns = require('dns');
const net = require('net');
const util = require('util');
const isIPv4 = net.isIPv4;

let running = false;
Expand Down Expand Up @@ -184,3 +185,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();
}));
});
26 changes: 26 additions & 0 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,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 d4ddf2a

Please sign in to comment.