Created
August 12, 2017 00:12
-
-
Save MylesBorins/f5ab7d9a23126c13c36ead9b2fa64f92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From b80aedf4c19b6d74d6ad303f74d157dc9ddd17e0 Mon Sep 17 00:00:00 2001 | |
From: Sam Roberts <[email protected]> | |
Date: Fri, 14 Jul 2017 11:05:00 -0700 | |
Subject: [PATCH] net: support passing undefined to listen() | |
For consistency with 4.x and 8.x. | |
This commit also contains a forward port of | |
https://github.com/nodejs/node/pull/14232 to confirm that 4.x and 6.x | |
behave identically with respect to the port argument. | |
PR-URL: https://github.com/nodejs/node/pull/14234 | |
Refs: https://github.com/nodejs/node/issues/14205 | |
Reviewed-By: Colin Ihrig <[email protected]> | |
Reviewed-By: James M Snell <[email protected]> | |
Reviewed-By: Sam Roberts <[email protected]> | |
--- | |
lib/net.js | 2 +- | |
test/parallel/test-net-listen-port-option.js | 35 ++++++++++++++++++++++++++++ | |
2 files changed, 36 insertions(+), 1 deletion(-) | |
diff --git a/lib/net.js b/lib/net.js | |
index 5e653c61d2..83a93d6bd4 100644 | |
--- a/lib/net.js | |
+++ b/lib/net.js | |
@@ -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. | |
diff --git a/test/parallel/test-net-listen-port-option.js b/test/parallel/test-net-listen-port-option.js | |
index c4851bd533..3902a709bd 100644 | |
--- a/test/parallel/test-net-listen-port-option.js | |
+++ b/test/parallel/test-net-listen-port-option.js | |
@@ -26,3 +26,38 @@ 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 because ports that low can't be listened | |
+// on by unprivileged processes. | |
+net.Server().listen(false, close); | |
+ | |
+net.Server().listen(true).on('error', common.mustCall(function(err) { | |
+ assert.strictEqual(err.code, 'EACCES'); | |
+})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment