Browse Source

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: https://github.com/nodejs/node/issues/14205
PR-URL: https://github.com/nodejs/node/pull/14221
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
canary-base
cjihrig 8 years ago
parent
commit
20259f9092
No known key found for this signature in database GPG Key ID: 7434390BDBE9B9C5
  1. 9
      lib/net.js
  2. 3
      test/parallel/test-net-server-listen-options.js

9
lib/net.js

@ -1451,11 +1451,12 @@ Server.prototype.listen = function(...args) {
} }
// ([port][, host][, backlog][, cb]) where port is omitted, // ([port][, host][, backlog][, cb]) where port is omitted,
// that is, listen() or listen(cb), // that is, listen(), listen(null), listen(cb), or listen(null, cb)
// or (options[, cb]) where options.port is explicitly set as undefined, // or (options[, cb]) where options.port is explicitly set as undefined or
// bind to an arbitrary unused port // null, bind to an arbitrary unused port
if (args.length === 0 || typeof args[0] === 'function' || 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; options.port = 0;
} }
// ([port][, host][, backlog][, cb]) where port is specified // ([port][, host][, backlog][, cb]) where port is specified

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

@ -42,6 +42,7 @@ const listenOnPort = [
listen('0', common.mustCall(close)); listen('0', common.mustCall(close));
listen(0, common.mustCall(close)); listen(0, common.mustCall(close));
listen(undefined, common.mustCall(close)); listen(undefined, common.mustCall(close));
listen(null, common.mustCall(close));
// Test invalid ports // Test invalid ports
assert.throws(() => listen(-1, common.mustNotCall()), portError); assert.throws(() => listen(-1, common.mustNotCall()), portError);
assert.throws(() => listen(NaN, common.mustNotCall()), portError); assert.throws(() => listen(NaN, common.mustNotCall()), portError);
@ -71,8 +72,6 @@ const listenOnPort = [
`expect listen(${util.inspect(options)}) to throw`); `expect listen(${util.inspect(options)}) to throw`);
} }
shouldFailToListen(null, { port: null });
shouldFailToListen({ port: null });
shouldFailToListen(false, { port: false }); shouldFailToListen(false, { port: false });
shouldFailToListen({ port: false }); shouldFailToListen({ port: false });
shouldFailToListen(true, { port: true }); shouldFailToListen(true, { port: true });

Loading…
Cancel
Save