@ -475,6 +475,7 @@ function initStream (self) {
if ( ! self . writable ) self . destroy ( ) ;
// Note: 'close' not emitted until nextTick.
if ( ! self . allowHalfOpen ) self . end ( ) ;
if ( self . _ events && self . _ events [ 'end' ] ) self . emit ( 'end' ) ;
if ( self . onend ) self . onend ( ) ;
} else if ( bytesRead > 0 ) {
@ -519,16 +520,29 @@ function initStream (self) {
self . writable = false ;
}
function Stream ( fd , type ) {
if ( ! ( this instanceof Stream ) ) return new Stream ( fd , type ) ;
// Deprecated API: Stream(fd, type)
// New API: Stream({ fd: 10, type: "unix", allowHalfOpen: true })
function Stream ( options ) {
if ( ! ( this instanceof Stream ) ) return new Stream ( arguments [ 0 ] , arguments [ 1 ] ) ;
stream . Stream . call ( this ) ;
this . fd = null ;
this . type = null ;
this . secure = false ;
this . allowHalfOpen = false ;
if ( parseInt ( fd , 10 ) >= 0 ) {
this . open ( fd , type ) ;
if ( typeof options == "object" ) {
this . fd = options . fd !== undefined ? parseInt ( options . fd , 10 ) : null ;
this . type = options . type || null ;
this . secure = options . secure || false ;
this . allowHalfOpen = options . allowHalfOpen || false ;
} else if ( typeof options == "number" ) {
this . fd = arguments [ 0 ] ;
this . type = arguments [ 1 ] ;
}
if ( parseInt ( this . fd , 10 ) >= 0 ) {
this . open ( this . fd , this . type ) ;
} else {
setImplmentationMethods ( this ) ;
}
@ -1030,8 +1044,8 @@ Stream.prototype._shutdown = function () {
Stream . prototype . end = function ( data , encoding ) {
if ( this . writable ) {
if ( data ) this . write ( data , encoding ) ;
if ( this . _ writeQueueLast ( ) !== END_OF_FILE ) {
if ( data ) this . write ( data , encoding ) ;
this . _ writeQueue . push ( END_OF_FILE ) ;
if ( ! this . _ connecting ) {
this . flush ( ) ;
@ -1041,13 +1055,22 @@ Stream.prototype.end = function (data, encoding) {
} ;
function Server ( listener ) {
if ( ! ( this instanceof Server ) ) return new Server ( listener ) ;
function Server ( /* [ options, ] listener */ ) {
if ( ! ( this instanceof Server ) ) return new Server ( arguments [ 0 ] , arguments [ 1 ] ) ;
events . EventEmitter . call ( this ) ;
var self = this ;
if ( listener ) {
self . addListener ( 'connection' , listener ) ;
var options = { } ;
if ( typeof arguments [ 0 ] == "object" ) {
options = arguments [ 0 ] ;
}
// listener: find the last argument that is a function
for ( var l = arguments . length - 1 ; l >= 0 ; l -- ) {
if ( typeof arguments [ l ] == "function" ) {
self . addListener ( 'connection' , arguments [ l ] ) ;
}
if ( arguments [ l ] !== undefined ) break ;
}
self . connections = 0 ;
@ -1055,6 +1078,8 @@ function Server (listener) {
self . paused = false ;
self . pauseTimeout = 1000 ;
self . allowHalfOpen = options . allowHalfOpen || false ;
function pause ( ) {
// We've hit the maximum file limit. What to do?
// Let's try again in 1 second.
@ -1094,7 +1119,9 @@ function Server (listener) {
self . connections ++ ;
var s = new Stream ( peerInfo . fd , self . type ) ;
var s = new Stream ( { fd : peerInfo . fd ,
type : self . type ,
allowHalfOpen : self . allowHalfOpen } ) ;
s . remoteAddress = peerInfo . address ;
s . remotePort = peerInfo . port ;
s . type = self . type ;
@ -1118,8 +1145,8 @@ util.inherits(Server, events.EventEmitter);
exports . Server = Server ;
exports . createServer = function ( listener ) {
return new Server ( listener ) ;
exports . createServer = function ( ) {
return new Server ( arguments [ 0 ] , arguments [ 1 ] ) ;
} ;