From 66ef6dc16a6e70119d38da4b0407fc35cf26e8e5 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 --- 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 8d45be73baf316..0c1b259d7341ee 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_ARG_VALUE('rrtype', rrtype); } diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 4240f9397487d8..e0158eef81307e 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 1c50a089d1d89a..27d25c92ad93aa 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); }); }