-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: refactor Server.prototype.listen
This PR simplifies Server.prototype.listen, removing some redundancy and inconsistency. Because listen and connect have a similar function signature, normalizeConnectArgs can be reused for listen. listenAfterLookup renamed to lookupAndListen for consistency with lookupAndConnect, and moved out of Server.prototype.listen. PR-URL: #4039 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Glen Keane <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
- Loading branch information
Showing
3 changed files
with
98 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,45 @@ | ||
'use strict'; | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var net = require('net'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
function close() { this.close(); } | ||
net.Server().listen({ port: undefined }, close); | ||
net.Server().listen({ port: '' + common.PORT }, close); | ||
|
||
[ 'nan', | ||
-1, | ||
123.456, | ||
0x10000, | ||
1 / 0, | ||
-1 / 0, | ||
'+Infinity', | ||
'-Infinity' | ||
].forEach(function(port) { | ||
assert.throws(function() { | ||
net.Server().listen({ port: port }, common.fail); | ||
}, /"port" argument must be >= 0 and < 65536/i); | ||
}); | ||
// From lib/net.js | ||
function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; } | ||
|
||
function isPipeName(s) { | ||
return typeof s === 'string' && toNumber(s) === false; | ||
} | ||
|
||
const listenVariants = [ | ||
(port, cb) => net.Server().listen({port}, cb), | ||
(port, cb) => net.Server().listen(port, cb) | ||
]; | ||
|
||
listenVariants.forEach((listenVariant, i) => { | ||
listenVariant(undefined, common.mustCall(close)); | ||
listenVariant('0', common.mustCall(close)); | ||
|
||
[ | ||
'nan', | ||
-1, | ||
123.456, | ||
0x10000, | ||
1 / 0, | ||
-1 / 0, | ||
'+Infinity', | ||
'-Infinity' | ||
].forEach((port) => { | ||
if (i === 1 && isPipeName(port)) { | ||
// skip this, because listen(port) can also be listen(path) | ||
return; | ||
} | ||
assert.throws(() => listenVariant(port, common.fail), | ||
/"port" argument must be >= 0 and < 65536/i); | ||
}); | ||
|
||
[null, true, false].forEach(function(port) { | ||
assert.throws(function() { | ||
net.Server().listen({ port: port }, common.fail); | ||
}, /invalid listen argument/i); | ||
[null, true, false].forEach((port) => | ||
assert.throws(() => listenVariant(port, common.fail), | ||
/invalid listen argument/i)); | ||
}); |
fd6af98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
niubility。