Skip to content

Commit deaafd5

Browse files
aduh95targos
authored andcommitted
dns: refactor to use more primordials
PR-URL: #36314 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 040b39f commit deaafd5

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

lib/dns.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
'use strict';
2323

2424
const {
25+
ArrayPrototypeMap,
2526
ObjectCreate,
2627
ObjectDefineProperties,
2728
ObjectDefineProperty,
29+
ReflectApply,
2830
} = primordials;
2931

3032
const cares = internalBinding('cares_wrap');
@@ -197,7 +199,8 @@ ObjectDefineProperty(lookupService, customPromisifyArgs,
197199

198200
function onresolve(err, result, ttls) {
199201
if (ttls && this.ttl)
200-
result = result.map((address, index) => ({ address, ttl: ttls[index] }));
202+
result = ArrayPrototypeMap(
203+
result, (address, index) => ({ address, ttl: ttls[index] }));
201204

202205
if (err)
203206
this.callback(dnsException(err, this.bindingName, this.hostname));
@@ -261,7 +264,7 @@ function resolve(hostname, rrtype, callback) {
261264
}
262265

263266
if (typeof resolver === 'function') {
264-
return resolver.call(this, hostname, callback);
267+
return ReflectApply(resolver, this, [hostname, callback]);
265268
}
266269
throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype);
267270
}

lib/internal/dns/promises.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypeMap,
45
ObjectCreate,
56
ObjectDefineProperty,
67
Promise,
8+
ReflectApply,
79
} = primordials;
810

911
const {
@@ -169,7 +171,8 @@ function onresolve(err, result, ttls) {
169171
}
170172

171173
if (ttls && this.ttl)
172-
result = result.map((address, index) => ({ address, ttl: ttls[index] }));
174+
result = ArrayPrototypeMap(
175+
result, (address, index) => ({ address, ttl: ttls[index] }));
173176

174177
this.resolve(result);
175178
}
@@ -246,7 +249,7 @@ Resolver.prototype.resolve = function resolve(hostname, rrtype) {
246249
throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype);
247250
}
248251

249-
return resolver.call(this, hostname);
252+
return ReflectApply(resolver, this, [hostname]);
250253
};
251254

252255

lib/internal/dns/utils.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypeForEach,
6+
ArrayPrototypeJoin,
7+
ArrayPrototypeMap,
58
ArrayPrototypePush,
9+
FunctionPrototypeBind,
610
NumberParseInt,
11+
StringPrototypeMatch,
712
StringPrototypeReplace,
813
} = primordials;
914

@@ -45,7 +50,7 @@ class Resolver {
4550
}
4651

4752
getServers() {
48-
return this._handle.getServers().map((val) => {
53+
return ArrayPrototypeMap(this._handle.getServers(), (val) => {
4954
if (!val[1] || val[1] === IANA_DNS_PORT)
5055
return val[0];
5156

@@ -65,16 +70,16 @@ class Resolver {
6570
const orig = this._handle.getServers();
6671
const newSet = [];
6772

68-
servers.forEach((serv, index) => {
73+
ArrayPrototypeForEach(servers, (serv, index) => {
6974
if (typeof serv !== 'string') {
7075
throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv);
7176
}
7277
let ipVersion = isIP(serv);
7378

7479
if (ipVersion !== 0)
75-
return newSet.push([ipVersion, serv, IANA_DNS_PORT]);
80+
return ArrayPrototypePush(newSet, [ipVersion, serv, IANA_DNS_PORT]);
7681

77-
const match = serv.match(IPv6RE);
82+
const match = StringPrototypeMatch(serv, IPv6RE);
7883

7984
// Check for an IPv6 in brackets.
8085
if (match) {
@@ -88,7 +93,7 @@ class Resolver {
8893
}
8994

9095
// addr::port
91-
const addrSplitMatch = serv.match(addrSplitRE);
96+
const addrSplitMatch = StringPrototypeMatch(serv, addrSplitRE);
9297

9398
if (addrSplitMatch) {
9499
const hostIP = addrSplitMatch[1];
@@ -109,7 +114,7 @@ class Resolver {
109114

110115
if (errorNumber !== 0) {
111116
// Reset the servers to the old servers, because ares probably unset them.
112-
this._handle.setServers(orig.join(','));
117+
this._handle.setServers(ArrayPrototypeJoin(orig, ','));
113118
const err = strerror(errorNumber);
114119
throw new ERR_DNS_SET_SERVERS_FAILED(err, servers);
115120
}
@@ -156,8 +161,8 @@ function setDefaultResolver(resolver) {
156161
}
157162

158163
function bindDefaultResolver(target, source) {
159-
resolverKeys.forEach((key) => {
160-
target[key] = source[key].bind(defaultResolver);
164+
ArrayPrototypeForEach(resolverKeys, (key) => {
165+
target[key] = FunctionPrototypeBind(source[key], defaultResolver);
161166
});
162167
}
163168

0 commit comments

Comments
 (0)