@@ -359,7 +359,9 @@ Socket.prototype._destroy = function(exception, cb) {
359359
360360 if ( this . server ) {
361361 this . server . _connections -- ;
362- this . server . _emitCloseIfDrained ( ) ;
362+ if ( this . server . _emitCloseIfDrained ) {
363+ this . server . _emitCloseIfDrained ( ) ;
364+ }
363365 }
364366} ;
365367
@@ -820,12 +822,33 @@ function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
820822
821823
822824var createServerHandle = exports . _createServerHandle =
823- function ( address , port , addressType ) {
825+ function ( address , port , addressType , fd ) {
824826 var r = 0 ;
825827 // assign handle in listen, and clean up if bind or listen fails
826828 var handle ;
827829
828- if ( port == - 1 && addressType == - 1 ) {
830+ if ( typeof fd === 'number' && fd >= 0 ) {
831+ var tty_wrap = process . binding ( 'tty_wrap' ) ;
832+ var type = tty_wrap . guessHandleType ( fd ) ;
833+ switch ( type ) {
834+ case 'PIPE' :
835+ debug ( 'listen pipe fd=' + fd ) ;
836+ // create a PipeWrap
837+ handle = createPipe ( ) ;
838+ handle . open ( fd ) ;
839+ handle . readable = true ;
840+ handle . writable = true ;
841+ break ;
842+
843+ default :
844+ // Not a fd we can listen on. This will trigger an error.
845+ debug ( 'listen invalid fd=' + fd + ' type=' + type ) ;
846+ handle = null ;
847+ break ;
848+ }
849+ return handle ;
850+
851+ } else if ( port == - 1 && addressType == - 1 ) {
829852 handle = createPipe ( ) ;
830853 if ( process . platform === 'win32' ) {
831854 var instances = parseInt ( process . env . NODE_PENDING_PIPE_INSTANCES ) ;
@@ -855,14 +878,14 @@ var createServerHandle = exports._createServerHandle =
855878} ;
856879
857880
858- Server . prototype . _listen2 = function ( address , port , addressType , backlog ) {
881+ Server . prototype . _listen2 = function ( address , port , addressType , backlog , fd ) {
859882 var self = this ;
860883 var r = 0 ;
861884
862885 // If there is not yet a handle, we need to create one and bind.
863886 // In the case of a server sent via IPC, we don't need to do this.
864887 if ( ! self . _handle ) {
865- self . _handle = createServerHandle ( address , port , addressType ) ;
888+ self . _handle = createServerHandle ( address , port , addressType , fd ) ;
866889 if ( ! self . _handle ) {
867890 process . nextTick ( function ( ) {
868891 self . emit ( 'error' , errnoException ( errno , 'listen' ) ) ;
@@ -897,16 +920,16 @@ Server.prototype._listen2 = function(address, port, addressType, backlog) {
897920} ;
898921
899922
900- function listen ( self , address , port , addressType , backlog ) {
923+ function listen ( self , address , port , addressType , backlog , fd ) {
901924 if ( ! cluster ) cluster = require ( 'cluster' ) ;
902925
903926 if ( cluster . isWorker ) {
904- cluster . _getServer ( self , address , port , addressType , function ( handle ) {
927+ cluster . _getServer ( self , address , port , addressType , fd , function ( handle ) {
905928 self . _handle = handle ;
906- self . _listen2 ( address , port , addressType , backlog ) ;
929+ self . _listen2 ( address , port , addressType , backlog , fd ) ;
907930 } ) ;
908931 } else {
909- self . _listen2 ( address , port , addressType , backlog ) ;
932+ self . _listen2 ( address , port , addressType , backlog , fd ) ;
910933 }
911934}
912935
@@ -932,10 +955,21 @@ Server.prototype.listen = function() {
932955 // The port can be found with server.address()
933956 listen ( self , null , null , backlog ) ;
934957
935- } else if ( arguments [ 0 ] instanceof TCP ) {
936- self . _handle = arguments [ 0 ] ;
937- listen ( self , null , - 1 , - 1 , backlog ) ;
938-
958+ } else if ( arguments [ 0 ] && typeof arguments [ 0 ] === 'object' ) {
959+ var h = arguments [ 0 ] ;
960+ if ( h . _handle ) {
961+ h = h . _handle ;
962+ } else if ( h . handle ) {
963+ h = h . handle ;
964+ }
965+ if ( h instanceof TCP ) {
966+ self . _handle = h ;
967+ listen ( self , null , - 1 , - 1 , backlog ) ;
968+ } else if ( h . fd && typeof h . fd === 'number' && h . fd >= 0 ) {
969+ listen ( self , null , null , null , backlog , h . fd ) ;
970+ } else {
971+ throw new Error ( 'Invalid listen argument: ' + h ) ;
972+ }
939973 } else if ( isPipeName ( arguments [ 0 ] ) ) {
940974 // UNIX socket or Windows pipe.
941975 var pipeName = self . _pipeName = arguments [ 0 ] ;
0 commit comments