Skip to content
Closed
Prev Previous commit
Next Next commit
Use normalizeConnectArgs in listen
This is mostly preparation, `options` will be used later.
  • Loading branch information
jscissr committed Aug 13, 2016
commit 88c44c1ffa1b54ae6741b81f7a0c789753652fda
19 changes: 16 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1326,9 +1326,20 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {


Server.prototype.listen = function() {
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg === 'function') {
this.once('listening', lastArg);
const argsLen = arguments.length;
Copy link
Contributor

@mscdex mscdex Aug 15, 2016

Choose a reason for hiding this comment

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

This line is unnecessary, just use arguments.length in both places below. V8 is smart enough to optimize the

for (...; i < array.length; ...)

case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copied this snippet; it was introduced in d0582ef. If you want to change this, I'd say it should be changed everywhere, and I think that doesn't fit into this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I disagree that it has to be all or nothing for this type of change. I do not see any harm in changing it in this PR.

var args = new Array(argsLen);
for (var i = 0; i < argsLen; i++)
args[i] = arguments[i];
args = normalizeConnectArgs(args);
Copy link
Member

Choose a reason for hiding this comment

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

Can you please rename this to normalizeArgs, and add a quick comment to mention that it is used on both places?


if (args[1]) {
Copy link
Contributor

@mscdex mscdex Aug 16, 2016

Choose a reason for hiding this comment

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

Might change this to args.length > 1 && args[1] as it may deopt?
EDIT: Or maybe we could just have normalizeArgs() always return an array of length 2, with the second element being null if no callback.

this.once('listening', args[1]);
}

var options = args[0];
if (arguments.length === 0 || typeof arguments[0] === 'function') {
Copy link
Member

Choose a reason for hiding this comment

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

why are you using arguments here, if you already have args?

// Bind to a random port.
options.port = 0;
}

var port = toNumber(arguments[0]);
Expand All @@ -1337,6 +1348,8 @@ Server.prototype.listen = function() {
// When the ip is omitted it can be the second argument.
var backlog = toNumber(arguments[1]) || toNumber(arguments[2]);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this going to cause issues with args?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What kind of issues? I've never heard about that. 😕

Copy link
Member

Choose a reason for hiding this comment

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

We have the data on two places, args and arguments. This causes confusion on where things are defaulted, etc, making the code hard to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see now, args is actually overwritten with the result of normalizeArgs, and does not contain this data anymore. That's because the APIs of connect and listen are not equal, they are just similar and listen needs some special handling.

Something that I could do is using an array destructor:

var [options, cb] = normalizeArgs(args);

What do you think? Alternatively we could just leave it as it is, in my opinion.


options = options._handle || options.handle || options;

if (arguments.length === 0 || typeof arguments[0] === 'function') {
// Bind to a random port.
listen(this, null, 0, null, backlog);
Expand Down