Skip to content

Commit cd7cf05

Browse files
aduh95targos
authored andcommitted
dns: refactor to use more primordials
PR-URL: #36314 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 226a86c commit cd7cf05

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎lib/dns.js‎

Lines changed: 5 additions & 2 deletions
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
constcares=internalBinding('cares_wrap');
@@ -197,7 +199,8 @@ ObjectDefineProperty(lookupService, customPromisifyArgs,
197199

198200
functiononresolve(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(typeofresolver==='function'){
264-
returnresolver.call(this,hostname,callback);
267+
returnReflectApply(resolver,this,[hostname,callback]);
265268
}
266269
thrownewERR_INVALID_OPT_VALUE('rrtype',rrtype);
267270
}

‎lib/internal/dns/promises.js‎

Lines changed: 5 additions & 2 deletions
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
thrownewERR_INVALID_ARG_TYPE('rrtype','string',rrtype);
247250
}
248251

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

252255

‎lib/internal/dns/utils.js‎

Lines changed: 13 additions & 8 deletions
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-
returnthis._handle.getServers().map((val)=>{
53+
returnArrayPrototypeMap(this._handle.getServers(),(val)=>{
4954
if(!val[1]||val[1]===IANA_DNS_PORT)
5055
returnval[0];
5156

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

68-
servers.forEach((serv,index)=>{
73+
ArrayPrototypeForEach(servers,(serv,index)=>{
6974
if(typeofserv!=='string'){
7075
thrownewERR_INVALID_ARG_TYPE(`servers[${index}]`,'string',serv);
7176
}
7277
letipVersion=isIP(serv);
7378

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

77-
constmatch=serv.match(IPv6RE);
82+
constmatch=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-
constaddrSplitMatch=serv.match(addrSplitRE);
96+
constaddrSplitMatch=StringPrototypeMatch(serv,addrSplitRE);
9297

9398
if(addrSplitMatch){
9499
consthostIP=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
consterr=strerror(errorNumber);
114119
thrownewERR_DNS_SET_SERVERS_FAILED(err,servers);
115120
}
@@ -156,8 +161,8 @@ function setDefaultResolver(resolver) {
156161
}
157162

158163
functionbindDefaultResolver(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)