-
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
net: handle normalized args in normalizeArgs() #13069
Conversation
lib/net.js
Outdated
@@ -125,6 +125,10 @@ function normalizeArgs(args) { | |||
const arg0 = args[0]; | |||
var options = {}; | |||
if (typeof arg0 === 'object' && arg0 !== null) { | |||
// Handle the case where the arguments were already normalized. | |||
if (Array.isArray(arg0)) |
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.
Using instanceof Array
should be faster.
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.
Actually, in my experiences Array.isArray
is faster because of the difficult-to-optimize nature of the instanceof
operator under ES2015+…
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.
Do you have a benchmark that shows this on master?
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 curious, wouldn't Array.isArray
always be a safer choice? Or because we know how arg0
is created in this specific case we can safely use instanceof Array
?
Just an idea, put a symbol on it, to distinguish from arbitrary |
lib/net.js
Outdated
@@ -125,6 +125,10 @@ function normalizeArgs(args) { | |||
const arg0 = args[0]; | |||
var options = {}; | |||
if (typeof arg0 === 'object' && arg0 !== null) { |
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.
If this is doing the same thing as the if (arguments.length === 1 && Array.isArray(arguments[0]))
block in Socket.prototype.connect
, maybe remove the similar logic there?
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.
@joyeecheung it is related, but not the same thing.
LGTM! |
I'm open to it if others are. |
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.
LGTM
Out of curiosity, when is |
@watson for example in othiym23/async-listener#110. |
Actually, the more I think about it, the more I like the idea of just dropping the |
@cjihrig you mean the code I added? Weird, I thought we tested async-listener against that patch since that was the whole reason for the patch 😅 |
@cjihrig it was kind of an ugly hack. And allowing |
I do indeed mean that code :-D We tested it, but @vdeturckheim came to me with this script, which was still failing.
Yea, removing the |
Ah, ok got it now thanks for the explanation 😄 Yeah, the symbol trick might be ok |
New commit (3564d322fd891dccc97e80c90f522412bad54da5) has the symbol approach. |
lib/internal/net.js
Outdated
@@ -10,5 +10,6 @@ function isLegalPort(port) { | |||
} | |||
|
|||
module.exports = { | |||
isLegalPort | |||
isLegalPort, | |||
kNormalizedConnectSymbol: Symbol('normalizedConnect') |
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.
It's also used by listen()
. Perhaps we should just call it normalizedArgsSymbol
with a 'normalizedArgs'
symbol value? Keeping it generic may also help if we ever need/want it for other normalization functions, like the ones in child_process
or something.
@mscdex renamed the symbol as you suggested. |
LGTM 👍 |
New CI: https://ci.nodejs.org/job/node-test-pull-request/8173/ EDIT: Some platforms fail with EADDRNOTAVAIL. Others fail with ECONNREFUSED. Updated the test to handle both. CI: https://ci.nodejs.org/job/node-test-pull-request/8174/ |
This commit attaches a Symbol to the result of net._normalizeArgs(). This prevents normal arrays from being passed to the internal Socket.prototype.connect() bypass logic. PR-URL: nodejs#13069 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Awesome! Thank you @cjihrig !! |
This commit attaches a Symbol to the result of net._normalizeArgs(). This prevents normal arrays from being passed to the internal Socket.prototype.connect() bypass logic. PR-URL: nodejs#13069 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Should this land on v6.x? If so it will need a manual backport |
I'm 👎 to backport. This would need a lot of other changes. |
This commit allows
normalizeArgs()
to be called with its own previous output. Prior to this change, multiple calls tonormalizeArgs()
would create a nested structure that produced invalid arguments.cc: @vdeturckheim @joyeecheung @mcollina @jasnell (from othiym23/async-listener#110)
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
net