From cd7cf0547a844e0997cfefc7741b1f89b6f8d7f6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 20 Nov 2020 10:26:23 +0100 Subject: [PATCH] dns: refactor to use more primordials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36314 Reviewed-By: Michaƫl Zasso Reviewed-By: Rich Trott --- lib/dns.js | 7 +++++-- lib/internal/dns/promises.js | 7 +++++-- lib/internal/dns/utils.js | 21 +++++++++++++-------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index 86b540cf08750a..b34627224d23c9 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -22,9 +22,11 @@ 'use strict'; const { + ArrayPrototypeMap, ObjectCreate, ObjectDefineProperties, ObjectDefineProperty, + ReflectApply, } = primordials; const cares = internalBinding('cares_wrap'); @@ -197,7 +199,8 @@ ObjectDefineProperty(lookupService, customPromisifyArgs, function onresolve(err, result, ttls) { if (ttls && this.ttl) - result = result.map((address, index) => ({ address, ttl: ttls[index] })); + result = ArrayPrototypeMap( + result, (address, index) => ({ address, ttl: ttls[index] })); if (err) this.callback(dnsException(err, this.bindingName, this.hostname)); @@ -261,7 +264,7 @@ function resolve(hostname, rrtype, callback) { } if (typeof resolver === 'function') { - return resolver.call(this, hostname, callback); + return ReflectApply(resolver, this, [hostname, callback]); } throw new ERR_INVALID_OPT_VALUE('rrtype', rrtype); } diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 42ba76dda4f0ac..267083270ccbd1 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -1,9 +1,11 @@ 'use strict'; const { + ArrayPrototypeMap, ObjectCreate, ObjectDefineProperty, Promise, + ReflectApply, } = primordials; const { @@ -169,7 +171,8 @@ function onresolve(err, result, ttls) { } if (ttls && this.ttl) - result = result.map((address, index) => ({ address, ttl: ttls[index] })); + result = ArrayPrototypeMap( + result, (address, index) => ({ address, ttl: ttls[index] })); this.resolve(result); } @@ -246,7 +249,7 @@ Resolver.prototype.resolve = function resolve(hostname, rrtype) { throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype); } - return resolver.call(this, hostname); + return ReflectApply(resolver, this, [hostname]); }; diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index afaad42ec4290e..cef6032e09579e 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -2,8 +2,13 @@ const { ArrayIsArray, + ArrayPrototypeForEach, + ArrayPrototypeJoin, + ArrayPrototypeMap, ArrayPrototypePush, + FunctionPrototypeBind, NumberParseInt, + StringPrototypeMatch, StringPrototypeReplace, } = primordials; @@ -45,7 +50,7 @@ class Resolver { } getServers() { - return this._handle.getServers().map((val) => { + return ArrayPrototypeMap(this._handle.getServers(), (val) => { if (!val[1] || val[1] === IANA_DNS_PORT) return val[0]; @@ -65,16 +70,16 @@ class Resolver { const orig = this._handle.getServers(); const newSet = []; - servers.forEach((serv, index) => { + ArrayPrototypeForEach(servers, (serv, index) => { if (typeof serv !== 'string') { throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv); } let ipVersion = isIP(serv); if (ipVersion !== 0) - return newSet.push([ipVersion, serv, IANA_DNS_PORT]); + return ArrayPrototypePush(newSet, [ipVersion, serv, IANA_DNS_PORT]); - const match = serv.match(IPv6RE); + const match = StringPrototypeMatch(serv, IPv6RE); // Check for an IPv6 in brackets. if (match) { @@ -88,7 +93,7 @@ class Resolver { } // addr::port - const addrSplitMatch = serv.match(addrSplitRE); + const addrSplitMatch = StringPrototypeMatch(serv, addrSplitRE); if (addrSplitMatch) { const hostIP = addrSplitMatch[1]; @@ -109,7 +114,7 @@ class Resolver { if (errorNumber !== 0) { // Reset the servers to the old servers, because ares probably unset them. - this._handle.setServers(orig.join(',')); + this._handle.setServers(ArrayPrototypeJoin(orig, ',')); const err = strerror(errorNumber); throw new ERR_DNS_SET_SERVERS_FAILED(err, servers); } @@ -156,8 +161,8 @@ function setDefaultResolver(resolver) { } function bindDefaultResolver(target, source) { - resolverKeys.forEach((key) => { - target[key] = source[key].bind(defaultResolver); + ArrayPrototypeForEach(resolverKeys, (key) => { + target[key] = FunctionPrototypeBind(source[key], defaultResolver); }); }