@@ -63,15 +63,16 @@ exports.connect = exports.createConnection = function() {
63
63
var args = new Array ( arguments . length ) ;
64
64
for ( var i = 0 ; i < arguments . length ; i ++ )
65
65
args [ i ] = arguments [ i ] ;
66
- args = normalizeConnectArgs ( args ) ;
66
+ args = normalizeArgs ( args ) ;
67
67
debug ( 'createConnection' , args ) ;
68
68
var s = new Socket ( args [ 0 ] ) ;
69
69
return Socket . prototype . connect . apply ( s , args ) ;
70
70
} ;
71
71
72
- // Returns an array [options] or [options , cb]
72
+ // Returns an array [options, cb], where cb can be null.
73
73
// It is the same as the argument of Socket.prototype.connect().
74
- function normalizeConnectArgs ( args ) {
74
+ // This is used by Server.prototype.listen() and Socket.prototype.connect().
75
+ function normalizeArgs ( args ) {
75
76
var options = { } ;
76
77
77
78
if ( args . length === 0 ) {
@@ -91,9 +92,11 @@ function normalizeConnectArgs(args) {
91
92
}
92
93
93
94
var cb = args [ args . length - 1 ] ;
94
- return typeof cb === 'function' ? [ options , cb ] : [ options ] ;
95
+ if ( typeof cb !== 'function' )
96
+ cb = null ;
97
+ return [ options , cb ] ;
95
98
}
96
- exports . _normalizeConnectArgs = normalizeConnectArgs ;
99
+ exports . _normalizeArgs = normalizeArgs ;
97
100
98
101
99
102
// called when creating new Socket, or when re-using a closed Socket
@@ -888,7 +891,7 @@ Socket.prototype.connect = function(options, cb) {
888
891
var args = new Array ( arguments . length ) ;
889
892
for ( var i = 0 ; i < arguments . length ; i ++ )
890
893
args [ i ] = arguments [ i ] ;
891
- args = normalizeConnectArgs ( args ) ;
894
+ args = normalizeArgs ( args ) ;
892
895
return Socket . prototype . connect . apply ( this , args ) ;
893
896
}
894
897
@@ -1325,84 +1328,70 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
1325
1328
1326
1329
1327
1330
Server . prototype . listen = function ( ) {
1328
- var self = this ;
1331
+ var args = new Array ( arguments . length ) ;
1332
+ for ( var i = 0 ; i < arguments . length ; i ++ )
1333
+ args [ i ] = arguments [ i ] ;
1334
+ var [ options , cb ] = normalizeArgs ( args ) ;
1329
1335
1330
- var lastArg = arguments [ arguments . length - 1 ] ;
1331
- if ( typeof lastArg === 'function' ) {
1332
- self . once ( 'listening' , lastArg ) ;
1336
+ if ( typeof cb === 'function' ) {
1337
+ this . once ( 'listening' , cb ) ;
1333
1338
}
1334
1339
1335
- var port = toNumber ( arguments [ 0 ] ) ;
1340
+ if ( args . length === 0 || typeof args [ 0 ] === 'function' ) {
1341
+ // Bind to a random port.
1342
+ options . port = 0 ;
1343
+ }
1336
1344
1337
1345
// The third optional argument is the backlog size.
1338
1346
// When the ip is omitted it can be the second argument.
1339
- var backlog = toNumber ( arguments [ 1 ] ) || toNumber ( arguments [ 2 ] ) ;
1347
+ var backlog = toNumber ( args . length > 1 && args [ 1 ] ) ||
1348
+ toNumber ( args . length > 2 && args [ 2 ] ) ;
1340
1349
1341
- if ( arguments . length === 0 || typeof arguments [ 0 ] === 'function' ) {
1342
- // Bind to a random port.
1343
- listen ( self , null , 0 , null , backlog ) ;
1344
- } else if ( arguments [ 0 ] !== null && typeof arguments [ 0 ] === 'object' ) {
1345
- var h = arguments [ 0 ] ;
1346
- h = h . _handle || h . handle || h ;
1347
-
1348
- if ( h instanceof TCP ) {
1349
- self . _handle = h ;
1350
- listen ( self , null , - 1 , - 1 , backlog ) ;
1351
- } else if ( typeof h . fd === 'number' && h . fd >= 0 ) {
1352
- listen ( self , null , null , null , backlog , h . fd ) ;
1353
- } else {
1354
- // The first argument is a configuration object
1355
- if ( h . backlog )
1356
- backlog = h . backlog ;
1357
-
1358
- if ( typeof h . port === 'number' || typeof h . port === 'string' ||
1359
- ( typeof h . port === 'undefined' && 'port' in h ) ) {
1360
- // Undefined is interpreted as zero (random port) for consistency
1361
- // with net.connect().
1362
- assertPort ( h . port ) ;
1363
- if ( h . host )
1364
- listenAfterLookup ( h . port | 0 , h . host , backlog , h . exclusive ) ;
1365
- else
1366
- listen ( self , null , h . port | 0 , 4 , backlog , undefined , h . exclusive ) ;
1367
- } else if ( h . path && isPipeName ( h . path ) ) {
1368
- const pipeName = self . _pipeName = h . path ;
1369
- listen ( self , pipeName , - 1 , - 1 , backlog , undefined , h . exclusive ) ;
1370
- } else {
1371
- throw new Error ( 'Invalid listen argument: ' + h ) ;
1372
- }
1373
- }
1374
- } else if ( isPipeName ( arguments [ 0 ] ) ) {
1375
- // UNIX socket or Windows pipe.
1376
- const pipeName = self . _pipeName = arguments [ 0 ] ;
1377
- listen ( self , pipeName , - 1 , - 1 , backlog ) ;
1378
-
1379
- } else if ( arguments [ 1 ] === undefined ||
1380
- typeof arguments [ 1 ] === 'function' ||
1381
- typeof arguments [ 1 ] === 'number' ) {
1382
- // The first argument is the port, no IP given.
1383
- assertPort ( port ) ;
1384
- listen ( self , null , port , 4 , backlog ) ;
1350
+ options = options . _handle || options . handle || options ;
1385
1351
1352
+ if ( options instanceof TCP ) {
1353
+ this . _handle = options ;
1354
+ listen ( this , null , - 1 , - 1 , backlog ) ;
1355
+ } else if ( typeof options . fd === 'number' && options . fd >= 0 ) {
1356
+ listen ( this , null , null , null , backlog , options . fd ) ;
1386
1357
} else {
1387
- // The first argument is the port, the second an IP.
1388
- assertPort ( port ) ;
1389
- listenAfterLookup ( port , arguments [ 1 ] , backlog ) ;
1390
- }
1391
-
1392
- function listenAfterLookup ( port , address , backlog , exclusive ) {
1393
- require ( 'dns' ) . lookup ( address , function ( err , ip , addressType ) {
1394
- if ( err ) {
1395
- self . emit ( 'error' , err ) ;
1358
+ backlog = options . backlog || backlog ;
1359
+
1360
+ if ( typeof options . port === 'number' || typeof options . port === 'string' ||
1361
+ ( typeof options . port === 'undefined' && 'port' in options ) ) {
1362
+ // Undefined is interpreted as zero (random port) for consistency
1363
+ // with net.connect().
1364
+ assertPort ( options . port ) ;
1365
+ if ( options . host ) {
1366
+ lookupAndListen ( this , options . port | 0 , options . host , backlog ,
1367
+ options . exclusive ) ;
1396
1368
} else {
1397
- addressType = ip ? addressType : 4 ;
1398
- listen ( self , ip , port , addressType , backlog , undefined , exclusive ) ;
1369
+ listen ( this , null , options . port | 0 , 4 , backlog , undefined ,
1370
+ options . exclusive ) ;
1399
1371
}
1400
- } ) ;
1372
+ } else if ( options . path && isPipeName ( options . path ) ) {
1373
+ // UNIX socket or Windows pipe.
1374
+ const pipeName = this . _pipeName = options . path ;
1375
+ listen ( this , pipeName , - 1 , - 1 , backlog , undefined , options . exclusive ) ;
1376
+ } else {
1377
+ throw new Error ( 'Invalid listen argument: ' + options ) ;
1378
+ }
1401
1379
}
1402
1380
1403
- return self ;
1381
+ return this ;
1404
1382
} ;
1405
1383
1384
+ function lookupAndListen ( self , port , address , backlog , exclusive ) {
1385
+ require ( 'dns' ) . lookup ( address , function ( err , ip , addressType ) {
1386
+ if ( err ) {
1387
+ self . emit ( 'error' , err ) ;
1388
+ } else {
1389
+ addressType = ip ? addressType : 4 ;
1390
+ listen ( self , ip , port , addressType , backlog , undefined , exclusive ) ;
1391
+ }
1392
+ } ) ;
1393
+ }
1394
+
1406
1395
Object . defineProperty ( Server . prototype , 'listening' , {
1407
1396
get : function ( ) {
1408
1397
return ! ! this . _handle ;
0 commit comments