Skip to content

Commit faf6654

Browse files
committed
dns: support promisified lookup(Service)
PR-URL: #12442 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: William Kapke <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
1 parent fe5ca3f commit faf6654

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

doc/api/dns.md

+8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ dns.lookup('example.com', options, (err, addresses) =>
123123
// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
124124
```
125125

126+
If this method is invoked as its [`util.promisify()`][]ed version, and `all`
127+
is not set to `true`, it returns a Promise for an object with `address` and
128+
`family` properties.
129+
126130
### Supported getaddrinfo flags
127131

128132
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) => {
163167
});
164168
```
165169

170+
If this method is invoked as its [`util.promisify()`][]ed version, it returns a
171+
Promise for an object with `hostname` and `service` properties.
172+
166173
## dns.resolve(hostname[, rrtype], callback)
167174
<!-- YAML
168175
added: v0.1.27
@@ -528,3 +535,4 @@ uses. For instance, _they do not use the configuration from `/etc/hosts`_.
528535
[Implementation considerations section]: #dns_implementation_considerations
529536
[supported `getaddrinfo` flags]: #dns_supported_getaddrinfo_flags
530537
[the official libuv documentation]: http://docs.libuv.org/en/latest/threadpool.html
538+
[`util.promisify()`]: util.html#util_util_promisify_original

lib/dns.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const util = require('util');
2626
const cares = process.binding('cares_wrap');
2727
const uv = process.binding('uv');
2828
const internalNet = require('internal/net');
29+
const { customPromisifyArgs } = require('internal/util');
2930

3031
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
3132
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
@@ -189,6 +190,9 @@ function lookup(hostname, options, callback) {
189190
return req;
190191
}
191192

193+
Object.defineProperty(lookup, customPromisifyArgs,
194+
{ value: ['address', 'family'], enumerable: false });
195+
192196

193197
function onlookupservice(err, host, service) {
194198
if (err)
@@ -228,6 +232,9 @@ function lookupService(host, port, callback) {
228232
return req;
229233
}
230234

235+
Object.defineProperty(lookupService, customPromisifyArgs,
236+
{ value: ['hostname', 'service'], enumerable: false });
237+
231238

232239
function onresolve(err, result, ttls) {
233240
if (ttls && this.ttl)

test/internet/test-dns-ipv4.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const common = require('../common');
33
const assert = require('assert');
44
const dns = require('dns');
55
const net = require('net');
6+
const util = require('util');
67
const isIPv4 = net.isIPv4;
78

9+
common.crashOnUnhandledRejection();
10+
811
let running = false;
912
const queue = [];
1013

@@ -184,3 +187,13 @@ TEST(function test_lookupservice_ip_ipv4(done) {
184187

185188
checkWrap(req);
186189
});
190+
191+
TEST(function test_lookupservice_ip_ipv4_promise(done) {
192+
util.promisify(dns.lookupService)('127.0.0.1', 80)
193+
.then(common.mustCall(({hostname, service}) => {
194+
assert.strictEqual(typeof hostname, 'string');
195+
assert(hostname.length > 0);
196+
assert(['http', 'www', '80'].includes(service));
197+
done();
198+
}));
199+
});

test/internet/test-dns.js

+28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const isIPv4 = net.isIPv4;
2828
const isIPv6 = net.isIPv6;
2929
const util = require('util');
3030

31+
common.crashOnUnhandledRejection();
32+
3133
let expected = 0;
3234
let completed = 0;
3335
let running = false;
@@ -428,6 +430,32 @@ TEST(function test_lookup_ip_all(done) {
428430
});
429431

430432

433+
TEST(function test_lookup_ip_all_promise(done) {
434+
const req = util.promisify(dns.lookup)('127.0.0.1', {all: true})
435+
.then(function(ips) {
436+
assert.ok(Array.isArray(ips));
437+
assert.ok(ips.length > 0);
438+
assert.strictEqual(ips[0].address, '127.0.0.1');
439+
assert.strictEqual(ips[0].family, 4);
440+
441+
done();
442+
});
443+
444+
checkWrap(req);
445+
});
446+
447+
448+
TEST(function test_lookup_ip_promise(done) {
449+
util.promisify(dns.lookup)('127.0.0.1')
450+
.then(function({ address, family }) {
451+
assert.strictEqual(address, '127.0.0.1');
452+
assert.strictEqual(family, 4);
453+
454+
done();
455+
});
456+
});
457+
458+
431459
TEST(function test_lookup_null_all(done) {
432460
const req = dns.lookup(null, {all: true}, function(err, ips, family) {
433461
assert.ifError(err);

0 commit comments

Comments
 (0)