Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: refactor to use more primordials #36314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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