Skip to content

Commit

Permalink
net: support passing undefined to listen()
Browse files Browse the repository at this point in the history
For consistency with 4.x and 8.x.

This commit also contains a forward port of
#14232 to confirm that 4.x and 6.x
behave identically with respect to the port argument.

PR-URL: #14234
Refs: #14205
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
  • Loading branch information
sam-github committed Sep 19, 2017
1 parent 7cde322 commit e925c97
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ Server.prototype.listen = function() {
self.once('listening', lastArg);
}

var port = toNumber(arguments[0]);
var port = typeof arguments[0] === 'undefined' ? 0 : toNumber(arguments[0]);

// The third optional argument is the backlog size.
// When the ip is omitted it can be the second argument.
Expand Down
44 changes: 44 additions & 0 deletions test/parallel/test-net-listen-port-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,47 @@ net.Server().listen({ port: '' + common.PORT }, close);
net.Server().listen({ port: port }, common.fail);
}, /invalid listen argument/i);
});

// Repeat the tests, passing port as an argument, which validates somewhat
// differently.

net.Server().listen(undefined, close);
net.Server().listen('0', close);

// 'nan', skip, treated as a path, not a port
//'+Infinity', skip, treated as a path, not a port
//'-Infinity' skip, treated as a path, not a port

// 4.x treats these as 0, but 6.x treats them as invalid numbers.
[
-1,
123.456,
0x10000,
1 / 0,
-1 / 0,
].forEach(function(port) {
assert.throws(function() {
net.Server().listen(port, common.fail);
}, /"port" argument must be >= 0 and < 65536/i);
});

// null is treated as 0
net.Server().listen(null, close);

// false/true are converted to 0/1, arguably a bug, but fixing would be
// semver-major. Note that true fails when port 1 low can't be listened on by
// unprivileged processes (Linux) but the listen does succeed on some Windows
// versions.
net.Server().listen(false, close);

(function() {
const done = common.mustCall(function(err) {
if (err)
return assert.strictEqual(err.code, 'EACCES');

assert.strictEqual(this.address().port, 1);
this.close();
});

net.Server().listen(true).on('error', done).on('listening', done);
})();

0 comments on commit e925c97

Please sign in to comment.