-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Conversation
@@ -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); |
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.
IMHO the template string additions are fine, but I'm not particularly keen on the style changes unless those kinds of changes are going to be enforced by eslint. |
@mscdex can you please be more specific about which style changes? |
@@ -192,10 +195,10 @@ function onlookupservice(err, host, service) { | |||
// lookupService(address, port, callback) | |||
exports.lookupService = function(host, port, callback) { | |||
if (arguments.length !== 3) { | |||
throw new Error('Invalid arguments'); | |||
throw new TypeError(`Expected 3 arguments, got ${arguments.length}`); |
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.
This isn't really a TypeError
per se, since it is more to do with argument count rather than argument type.
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.
Well, the type of lookupService
is a function that takes three arguments and returns its results - if you pass an incorrect number of arguments to it then you're misusing the type.
For example - when you call setTimeout
with 0 arguments in browsers you get a TypeError. When you call Array#reduce
with 1 argument on an empty array you get a TypeError about the missing argument, and so on.
I don't feel very strongly about this - but the language and most APIs I know throw type errors in this case.
@@ -38,6 +37,9 @@ function errnoException(err, syscall, hostname) { | |||
return ex; | |||
} | |||
|
|||
function validIpVersion(isIpResult) { | |||
return isIpResult !== 0; |
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.
IMHO I don't think it's necessary to extract a conditional like this into its own function.
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.
@mscdex I would agree- but then it would certainly need a comment to explain it. I would not think "validIpVersion" from isIpResult !== 0
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.
I'm open to suggestions here - it wasn't clear to me when I read this file what isIp
returns and I had to read the C++ to realize that it returns 4 for ipv4 6 for ipv6 and 0 for "parsing failed".
The goal was to make the fact that 0 is used for "false" here explicit. I don't mind inlining those checked if you'd like but I think that this helps clarify things - I'm open to other approaches too - what do you think?
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.
The documentation for net.isIP()
describes the valid return values (including 0), so it shouldn't be private knowledge.
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.
That's a good point, I still think it helps the code quality but if there is no consensus on that fact I'll do !== 0
since it's less objectionable and more like the old behavior.
@benjamingr For example, the |
@mscdex do we have a clera coding style guide of where to put braces and where not to and where to drop lines and where not to? I was trying to make this file consistent while I'm working on it - I don't mind any using any other coding style or reverting any changes as long as it's consistent. |
@benjamingr What I meant was unless we configure eslint to enforce those kinds of changes everywhere in node core, making those changes just in this one file just adds unnecessary noise. Right now it's just a use whatever style you want when you're adding/changing a particular piece of code for other reasons. |
} | ||
}; | ||
|
||
|
||
exports.getServers = function() { | ||
exports.getServers = () => { | ||
return cares.getServers(); | ||
}; |
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.
exports.getServers = () => cares.getServers();
Are the curlys required by coding convention here? ::shrugs::
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.
I think they're required in multi-line functions. I recall talking about it but I'm not sure what conclusion was reached and I'm fine with either - @thefourtheye ?
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.
I remember the discussion regarding the parens in arrow function parameters, but not sure about the body. I'll have to look. But I feel that having curly braces for even a single expression kinda beats the purpose of arrow functions.
@mscdex the current state is that lots of files are inconsistent, I want to make things more consistent - even if not at the project level at least within the file. If you think it's not worth it I won't put any more time into it - note that it's not a particularly big part of what this PR is about. |
@benjamingr I'd suggest going on a per-rule basis instead of a per-file basis, starting with rules that don't require a huge amount of changes. Making changes without backing them up through a linter rule is going to lead to inconsistency in the long run. As for a style guide, we don't have one but I suggest we may take a few hints from this one, which looks widely accepted. |
Most of the braces changes here refactor for (var i = 1, a = 0; a < arguments.length; ++i, ++a)
args[i] = arguments[a]; Into for (var i = 1, a = 0; a < arguments.length; ++i, ++a) {
args[i] = arguments[a];
} Which I find more readable. If either of you (or anyone else) finds it objectionable I'll revert it but I personally find the former confusing. |
if (family !== 0 && family !== 4 && family !== 6) { | ||
const msg = `Invalid argument: family must be 4 or 6, got {family}`; | ||
throw new TypeError(msg); | ||
} |
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.
Looks like the $
for the template string went missing.
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.
Thanks :)
Why the |
@mscdex wrote:
Don't want to go too far on a tangent here, but if you would go so far as to support lint-enforcement of template literals over string concatenation (or if you would object to such a thing), please take a look at #5778 |
|
||
port = +port; | ||
port = Number(port); |
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.
Why this change?
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.
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.
@Trott there are instances where the error message changed. (Maybe these changes could be in a separate PR?) |
@Trott exporting arrow fns is semver-major |
args[i] = arguments[a]; | ||
} |
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.
nit: not in love with the style changes
Also, changing error messages is also always semver-major |
I'm +0 on this. I'm all for modernizing the code where it makes sense but the style changes just seem unnecessary. |
Would the other collaborators prefer a different PR that modernizes the code base a little ( I don't feel very strongly about the style changes - but I do want to fix the inconsistencies (like I think I'm pretty close to done minus final fixes. Would love to have consensus first. |
My preferred approach (and the one I think I have more-or-less stuck to) is:
In contrast, putting several different stylistic changes in a single file and having that be the unit of PR is likely to be stalled by bike-shedding. There are exceptions though. Changing |
Pull Request check-list
Affected core subsystem(s)
dns
Description of change
The current dns.js file contains some pretty outdated JavaScript practices - this pull request aims to improve the file and bring it up to the standard coding style the rest of the project has.
WIP: meanwhile feedback is appreciated.