Skip to content

Commit

Permalink
net: support passing null to listen()
Browse files Browse the repository at this point in the history
This commit fixes a regression around the handling of null
as the port passed to Server#listen(). With this commit,
null, undefined, and 0 have the same behavior, which was the
case in Node 4.

Fixes: nodejs#14205
PR-URL: nodejs#14221
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
cjihrig committed Sep 22, 2017
1 parent 84063a7 commit 20259f9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1451,11 +1451,12 @@ Server.prototype.listen = function(...args) {
}

// ([port][, host][, backlog][, cb]) where port is omitted,
// that is, listen() or listen(cb),
// or (options[, cb]) where options.port is explicitly set as undefined,
// bind to an arbitrary unused port
// that is, listen(), listen(null), listen(cb), or listen(null, cb)
// or (options[, cb]) where options.port is explicitly set as undefined or
// null, bind to an arbitrary unused port
if (args.length === 0 || typeof args[0] === 'function' ||
(typeof options.port === 'undefined' && 'port' in options)) {
(typeof options.port === 'undefined' && 'port' in options) ||
options.port === null) {
options.port = 0;
}
// ([port][, host][, backlog][, cb]) where port is specified
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-net-server-listen-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const listenOnPort = [
listen('0', common.mustCall(close));
listen(0, common.mustCall(close));
listen(undefined, common.mustCall(close));
listen(null, common.mustCall(close));
// Test invalid ports
assert.throws(() => listen(-1, common.mustNotCall()), portError);
assert.throws(() => listen(NaN, common.mustNotCall()), portError);
Expand Down Expand Up @@ -71,8 +72,6 @@ const listenOnPort = [
`expect listen(${util.inspect(options)}) to throw`);
}

shouldFailToListen(null, { port: null });
shouldFailToListen({ port: null });
shouldFailToListen(false, { port: false });
shouldFailToListen({ port: false });
shouldFailToListen(true, { port: true });
Expand Down

0 comments on commit 20259f9

Please sign in to comment.