From fe9bb7e51e9798d65fb31e8133ca17dcf31ad62e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 13 Jul 2017 11:10:54 -0400 Subject: [PATCH] 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 Reviewed-By: Evan Lucas Reviewed-By: Sam Roberts Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/net.js | 9 +++++---- test/parallel/test-net-server-listen-options.js | 3 +-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/net.js b/lib/net.js index c4054cf8b61071..bb094103778fcf 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1443,11 +1443,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 diff --git a/test/parallel/test-net-server-listen-options.js b/test/parallel/test-net-server-listen-options.js index d2e70215dc1efb..9f44a5bd3b7a2a 100644 --- a/test/parallel/test-net-server-listen-options.js +++ b/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(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); @@ -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 });