-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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 dns to more modern JavaScript #5762
Changes from 3 commits
8d6c22e
f5c4f55
2784847
48fde13
bfe0f40
2d54ae9
85f9b87
457de39
4f124f7
6fe93ed
b3ced41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ function errnoException(err, syscall, hostname) { | |
} | ||
var ex = null; | ||
if (typeof err === 'string') { // c-ares error code. | ||
ex = new Error(syscall + ' ' + err + (hostname ? ' ' + hostname : '')); | ||
ex = new Error(`${syscall} ${err} ${(hostname ? ' ' + hostname : '')}`); | ||
ex.code = err; | ||
ex.errno = err; | ||
ex.syscall = syscall; | ||
|
@@ -62,12 +62,13 @@ function makeAsync(callback) { | |
return function asyncCallback() { | ||
if (asyncCallback.immediately) { | ||
// The API already returned, we can invoke the callback immediately. | ||
callback.apply(null, arguments); | ||
callback(...arguments); | ||
} else { | ||
var args = new Array(arguments.length + 1); | ||
args[0] = callback; | ||
for (var i = 1, a = 0; a < arguments.length; ++i, ++a) | ||
for (var i = 1, a = 0; a < arguments.length; ++i, ++a) { | ||
args[i] = arguments[a]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: not in love with the style changes |
||
process.nextTick.apply(null, args); | ||
} | ||
}; | ||
|
@@ -76,8 +77,10 @@ function makeAsync(callback) { | |
|
||
function onlookup(err, addresses) { | ||
if (err) { | ||
return this.callback(errnoException(err, 'getaddrinfo', this.hostname)); | ||
this.callback(errnoException(err, 'getaddrinfo', this.hostname)); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change this? the |
||
|
||
if (this.family) { | ||
this.callback(null, addresses[0], this.family); | ||
} else { | ||
|
@@ -87,11 +90,13 @@ function onlookup(err, addresses) { | |
|
||
|
||
function onlookupall(err, addresses) { | ||
var results = []; | ||
if (err) { | ||
return this.callback(errnoException(err, 'getaddrinfo', this.hostname)); | ||
this.callback(errnoException(err, 'getaddrinfo', this.hostname)); | ||
return; | ||
} | ||
|
||
const results = Array(addresses.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For array instantiation like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks |
||
|
||
for (var i = 0; i < addresses.length; i++) { | ||
results.push({ | ||
address: addresses[i], | ||
|
@@ -176,25 +181,29 @@ exports.lookup = function lookup(hostname, options, callback) { | |
|
||
|
||
function onlookupservice(err, host, service) { | ||
if (err) | ||
if (err) { | ||
return this.callback(errnoException(err, 'getnameinfo', this.host)); | ||
} | ||
|
||
this.callback(null, host, service); | ||
} | ||
|
||
|
||
// lookupService(address, port, callback) | ||
exports.lookupService = function(host, port, callback) { | ||
if (arguments.length !== 3) | ||
if (arguments.length !== 3) { | ||
throw new Error('Invalid arguments'); | ||
} | ||
|
||
if (cares.isIP(host) === 0) | ||
if (cares.isIP(host) === 0) { | ||
throw new TypeError('"host" argument needs to be a valid IP address'); | ||
} | ||
|
||
if (port == null || !isLegalPort(port)) | ||
if (port == null || !isLegalPort(port)) { | ||
throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`); | ||
} | ||
|
||
port = +port; | ||
port = Number(port); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, unary I'm also not sure about the |
||
callback = makeAsync(callback); | ||
|
||
var req = new GetNameInfoReqWrap(); | ||
|
@@ -204,18 +213,21 @@ exports.lookupService = function(host, port, callback) { | |
req.oncomplete = onlookupservice; | ||
|
||
var err = cares.getnameinfo(req, host, port); | ||
if (err) throw errnoException(err, 'getnameinfo', host); | ||
if (err) { | ||
throw errnoException(err, 'getnameinfo', host); | ||
} | ||
|
||
callback.immediately = true; | ||
return req; | ||
}; | ||
|
||
|
||
function onresolve(err, result) { | ||
if (err) | ||
if (err) { | ||
this.callback(errnoException(err, this.bindingName, this.hostname)); | ||
else | ||
} else { | ||
this.callback(null, result); | ||
} | ||
} | ||
|
||
|
||
|
@@ -236,7 +248,10 @@ function resolver(bindingName) { | |
req.hostname = name; | ||
req.oncomplete = onresolve; | ||
var err = binding(req, name); | ||
if (err) throw errnoException(err, bindingName); | ||
if (err) { | ||
throw errnoException(err, bindingName); | ||
} | ||
|
||
callback.immediately = true; | ||
return req; | ||
}; | ||
|
@@ -272,7 +287,7 @@ exports.resolve = function(hostname, type_, callback_) { | |
if (typeof resolver === 'function') { | ||
return resolver(hostname, callback); | ||
} else { | ||
throw new Error('Unknown type "' + type_ + '"'); | ||
throw new Error(`Unknown type ${type_}`); | ||
} | ||
}; | ||
|
||
|
@@ -292,36 +307,40 @@ exports.setServers = function(servers) { | |
servers.forEach(function(serv) { | ||
var ver = isIp(serv); | ||
|
||
if (ver) | ||
return newSet.push([ver, serv]); | ||
|
||
if (ver) { | ||
newSet.push([ver, serv]); | ||
return; | ||
} | ||
var match = serv.match(/\[(.*)\](:\d+)?/); | ||
|
||
// we have an IPv6 in brackets | ||
if (match) { | ||
ver = isIp(match[1]); | ||
if (ver) | ||
return newSet.push([ver, match[1]]); | ||
if (ver) { | ||
newSet.push([ver, match[1]]); | ||
return; | ||
} | ||
} | ||
|
||
var s = serv.split(/:\d+$/)[0]; | ||
ver = isIp(s); | ||
|
||
if (ver) | ||
return newSet.push([ver, s]); | ||
if (ver) { | ||
newSet.push([ver, s]); | ||
return; | ||
} | ||
|
||
throw new Error('IP address is not properly formatted: ' + serv); | ||
throw new Error(`IP address is not properly formatted: ${serv}`); | ||
}); | ||
|
||
var r = cares.setServers(newSet); | ||
var errorNumber = cares.setServers(newSet); | ||
|
||
if (r) { | ||
if (errorNumber !== 0) { | ||
// reset the servers to the old servers, because ares probably unset them | ||
cares.setServers(orig.join(',')); | ||
|
||
var err = cares.strerror(r); | ||
throw new Error('c-ares failed to set servers: "' + err + | ||
'" [' + servers + ']'); | ||
throw new Error(`c-ares failed to set servers: ${err} [${servers}]`); | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spread is still significantly slower than
apply()
, even in v8 4.9, so we should probably avoid changing it for now.