Skip to content

Commit

Permalink
dns: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36314
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Dec 21, 2020
1 parent 040b39f commit deaafd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
7 changes: 5 additions & 2 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
'use strict';

const {
ArrayPrototypeMap,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
ReflectApply,
} = primordials;

const cares = internalBinding('cares_wrap');
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const {
ArrayPrototypeMap,
ObjectCreate,
ObjectDefineProperty,
Promise,
ReflectApply,
} = primordials;

const {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]);
};


Expand Down
21 changes: 13 additions & 8 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

const {
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,
FunctionPrototypeBind,
NumberParseInt,
StringPrototypeMatch,
StringPrototypeReplace,
} = primordials;

Expand Down Expand Up @@ -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];

Expand All @@ -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) {
Expand All @@ -88,7 +93,7 @@ class Resolver {
}

// addr::port
const addrSplitMatch = serv.match(addrSplitRE);
const addrSplitMatch = StringPrototypeMatch(serv, addrSplitRE);

if (addrSplitMatch) {
const hostIP = addrSplitMatch[1];
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
});
}

Expand Down

0 comments on commit deaafd5

Please sign in to comment.