Skip to content

Commit fe9bb7e

Browse files
cjihrigjasnell
authored andcommitted
net: support passing null to listen()
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: #14205 PR-URL: #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]>
1 parent 24271a7 commit fe9bb7e

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

lib/net.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,11 +1443,12 @@ Server.prototype.listen = function(...args) {
14431443
}
14441444

14451445
// ([port][, host][, backlog][, cb]) where port is omitted,
1446-
// that is, listen() or listen(cb),
1447-
// or (options[, cb]) where options.port is explicitly set as undefined,
1448-
// bind to an arbitrary unused port
1446+
// that is, listen(), listen(null), listen(cb), or listen(null, cb)
1447+
// or (options[, cb]) where options.port is explicitly set as undefined or
1448+
// null, bind to an arbitrary unused port
14491449
if (args.length === 0 || typeof args[0] === 'function' ||
1450-
(typeof options.port === 'undefined' && 'port' in options)) {
1450+
(typeof options.port === 'undefined' && 'port' in options) ||
1451+
options.port === null) {
14511452
options.port = 0;
14521453
}
14531454
// ([port][, host][, backlog][, cb]) where port is specified

test/parallel/test-net-server-listen-options.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const listenOnPort = [
4242
listen('0', common.mustCall(close));
4343
listen(0, common.mustCall(close));
4444
listen(undefined, common.mustCall(close));
45+
listen(null, common.mustCall(close));
4546
// Test invalid ports
4647
assert.throws(() => listen(-1, common.mustNotCall()), portError);
4748
assert.throws(() => listen(NaN, common.mustNotCall()), portError);
@@ -71,8 +72,6 @@ const listenOnPort = [
7172
`expect listen(${util.inspect(options)}) to throw`);
7273
}
7374

74-
shouldFailToListen(null, { port: null });
75-
shouldFailToListen({ port: null });
7675
shouldFailToListen(false, { port: false });
7776
shouldFailToListen({ port: false });
7877
shouldFailToListen(true, { port: true });

0 commit comments

Comments
 (0)