-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: refactor Server.prototype.listen #4039
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d5452a6
Move listenAfterLookup out of listen
jscissr 88c44c1
Use normalizeConnectArgs in listen
jscissr df8d8c8
Replace h with options
jscissr 959582c
Only deal with options object
jscissr d1c6f7b
Fix linter errors
jscissr 708e1b4
Test the new behavior
jscissr 150723d
Use argument.length directly
jscissr 5ab4028
Rename normalizeConnectArgs to normalizeArgs
jscissr 0bffc03
Use string interpolation
jscissr db798ea
Style: Use brackets around if
jscissr e772fa3
Test: Just use '0' for the port
jscissr 3fb1435
Optimize argument handling
jscissr 4a65449
Update normalizeArgs comment
jscissr 3848eae
Or expression instead of `if`
jscissr 4bb5770
length checking before array access
jscissr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,15 +64,16 @@ exports.connect = exports.createConnection = function() { | |
var args = new Array(argsLen); | ||
for (var i = 0; i < argsLen; i++) | ||
args[i] = arguments[i]; | ||
args = normalizeConnectArgs(args); | ||
args = normalizeArgs(args); | ||
debug('createConnection', args); | ||
var s = new Socket(args[0]); | ||
return Socket.prototype.connect.apply(s, args); | ||
}; | ||
|
||
// Returns an array [options] or [options, cb] | ||
// Returns an array [options, cb], where cb can be null. | ||
// It is the same as the argument of Socket.prototype.connect(). | ||
function normalizeConnectArgs(args) { | ||
// This is also used by Server.prototype.listen(). | ||
function normalizeArgs(args) { | ||
var options = {}; | ||
|
||
if (args[0] !== null && typeof args[0] === 'object') { | ||
|
@@ -90,9 +91,11 @@ function normalizeConnectArgs(args) { | |
} | ||
|
||
var cb = args[args.length - 1]; | ||
return typeof cb === 'function' ? [options, cb] : [options]; | ||
if (typeof cb !== 'function') | ||
cb = null; | ||
return [options, cb]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment for |
||
} | ||
exports._normalizeConnectArgs = normalizeConnectArgs; | ||
exports._normalizeArgs = normalizeArgs; | ||
|
||
|
||
// called when creating new Socket, or when re-using a closed Socket | ||
|
@@ -889,7 +892,7 @@ Socket.prototype.connect = function(options, cb) { | |
var args = new Array(argsLen); | ||
for (var i = 0; i < argsLen; i++) | ||
args[i] = arguments[i]; | ||
args = normalizeConnectArgs(args); | ||
args = normalizeArgs(args); | ||
return Socket.prototype.connect.apply(this, args); | ||
} | ||
|
||
|
@@ -1326,84 +1329,69 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) { | |
|
||
|
||
Server.prototype.listen = function() { | ||
var self = this; | ||
var args = new Array(arguments.length); | ||
for (var i = 0; i < arguments.length; i++) | ||
args[i] = arguments[i]; | ||
var [options, cb] = normalizeArgs(args); | ||
|
||
var lastArg = arguments[arguments.length - 1]; | ||
if (typeof lastArg === 'function') { | ||
self.once('listening', lastArg); | ||
if (typeof cb === 'function') { | ||
this.once('listening', cb); | ||
} | ||
|
||
var port = toNumber(arguments[0]); | ||
if (args.length === 0 || typeof args[0] === 'function') { | ||
// Bind to a random port. | ||
options.port = 0; | ||
} | ||
|
||
// The third optional argument is the backlog size. | ||
// When the ip is omitted it can be the second argument. | ||
var backlog = toNumber(arguments[1]) || toNumber(arguments[2]); | ||
var backlog = toNumber(args.length > 1 && args[1]) || toNumber(args.length > 2 && args[2]); | ||
|
||
if (arguments.length === 0 || typeof arguments[0] === 'function') { | ||
// Bind to a random port. | ||
listen(self, null, 0, null, backlog); | ||
} else if (arguments[0] !== null && typeof arguments[0] === 'object') { | ||
var h = arguments[0]; | ||
h = h._handle || h.handle || h; | ||
|
||
if (h instanceof TCP) { | ||
self._handle = h; | ||
listen(self, null, -1, -1, backlog); | ||
} else if (typeof h.fd === 'number' && h.fd >= 0) { | ||
listen(self, null, null, null, backlog, h.fd); | ||
} else { | ||
// The first argument is a configuration object | ||
if (h.backlog) | ||
backlog = h.backlog; | ||
|
||
if (typeof h.port === 'number' || typeof h.port === 'string' || | ||
(typeof h.port === 'undefined' && 'port' in h)) { | ||
// Undefined is interpreted as zero (random port) for consistency | ||
// with net.connect(). | ||
assertPort(h.port); | ||
if (h.host) | ||
listenAfterLookup(h.port | 0, h.host, backlog, h.exclusive); | ||
else | ||
listen(self, null, h.port | 0, 4, backlog, undefined, h.exclusive); | ||
} else if (h.path && isPipeName(h.path)) { | ||
const pipeName = self._pipeName = h.path; | ||
listen(self, pipeName, -1, -1, backlog, undefined, h.exclusive); | ||
} else { | ||
throw new Error('Invalid listen argument: ' + h); | ||
} | ||
} | ||
} else if (isPipeName(arguments[0])) { | ||
// UNIX socket or Windows pipe. | ||
const pipeName = self._pipeName = arguments[0]; | ||
listen(self, pipeName, -1, -1, backlog); | ||
|
||
} else if (arguments[1] === undefined || | ||
typeof arguments[1] === 'function' || | ||
typeof arguments[1] === 'number') { | ||
// The first argument is the port, no IP given. | ||
assertPort(port); | ||
listen(self, null, port, 4, backlog); | ||
options = options._handle || options.handle || options; | ||
|
||
if (options instanceof TCP) { | ||
this._handle = options; | ||
listen(this, null, -1, -1, backlog); | ||
} else if (typeof options.fd === 'number' && options.fd >= 0) { | ||
listen(this, null, null, null, backlog, options.fd); | ||
} else { | ||
// The first argument is the port, the second an IP. | ||
assertPort(port); | ||
listenAfterLookup(port, arguments[1], backlog); | ||
} | ||
|
||
function listenAfterLookup(port, address, backlog, exclusive) { | ||
require('dns').lookup(address, function(err, ip, addressType) { | ||
if (err) { | ||
self.emit('error', err); | ||
backlog = options.backlog || backlog; | ||
|
||
if (typeof options.port === 'number' || typeof options.port === 'string' || | ||
(typeof options.port === 'undefined' && 'port' in options)) { | ||
// Undefined is interpreted as zero (random port) for consistency | ||
// with net.connect(). | ||
assertPort(options.port); | ||
if (options.host) { | ||
lookupAndListen(this, options.port | 0, options.host, backlog, | ||
options.exclusive); | ||
} else { | ||
addressType = ip ? addressType : 4; | ||
listen(self, ip, port, addressType, backlog, undefined, exclusive); | ||
listen(this, null, options.port | 0, 4, backlog, undefined, | ||
options.exclusive); | ||
} | ||
}); | ||
} else if (options.path && isPipeName(options.path)) { | ||
// UNIX socket or Windows pipe. | ||
const pipeName = this._pipeName = options.path; | ||
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive); | ||
} else { | ||
throw new Error('Invalid listen argument: ' + options); | ||
} | ||
} | ||
|
||
return self; | ||
return this; | ||
}; | ||
|
||
function lookupAndListen(self, port, address, backlog, exclusive) { | ||
require('dns').lookup(address, function(err, ip, addressType) { | ||
if (err) { | ||
self.emit('error', err); | ||
} else { | ||
addressType = ip ? addressType : 4; | ||
listen(self, ip, port, addressType, backlog, undefined, exclusive); | ||
} | ||
}); | ||
} | ||
|
||
Object.defineProperty(Server.prototype, 'listening', { | ||
get: function() { | ||
return !!this._handle; | ||
|
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)); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should probably say that it is used by
listen()
andconnect()
. Just saying "also" doesn't mean much when the "connect" part is removed.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.
@cjihrig I can fix the comment when I land. Is LGTM apart from this?