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 dns to more modern JavaScript #5762

Closed
wants to merge 11 commits into from
75 changes: 47 additions & 28 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Contributor

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.

} 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];
}
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
};
Expand All @@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change this? the return callback on errors is a commonly used pattern.


if (this.family) {
this.callback(null, addresses[0], this.family);
} else {
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For array instantiation like this new should be used. Also, results.push() below will need to be changed too.

Copy link
Member Author

Choose a reason for hiding this comment

The 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],
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, unary + and Number do the same thing but Number is more explicit and obvious. Not everyone is familiar with things like this so when there is no doubt about performance issues I'd rather refactor to cleaner code.

I'm also not sure about the >>>s to convert to integer numbers. We use them for the options object in this file and I wonder if I shouldn't convert them to Math.floor since it's more obvious and it's not a performance sensitive area.

callback = makeAsync(callback);

var req = new GetNameInfoReqWrap();
Expand All @@ -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);
}
}


Expand All @@ -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;
};
Expand Down Expand Up @@ -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_}`);
}
};

Expand All @@ -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}]`);
}
};

Expand Down