@ -28,9 +28,13 @@ function Stream() {
util . inherits ( Stream , events . EventEmitter ) ;
util . inherits ( Stream , events . EventEmitter ) ;
exports . Stream = Stream ;
exports . Stream = Stream ;
var pipes = [ ] ;
Stream . prototype . pipe = function ( dest , options ) {
Stream . prototype . pipe = function ( dest , options ) {
var source = this ;
var source = this ;
pipes . push ( dest ) ;
function ondata ( chunk ) {
function ondata ( chunk ) {
if ( dest . writable ) {
if ( dest . writable ) {
if ( false === dest . write ( chunk ) ) source . pause ( ) ;
if ( false === dest . write ( chunk ) ) source . pause ( ) ;
@ -52,10 +56,18 @@ Stream.prototype.pipe = function(dest, options) {
if ( ! options || options . end !== false ) {
if ( ! options || options . end !== false ) {
function onend ( ) {
function onend ( ) {
var index = pipes . indexOf ( dest ) ;
pipes . splice ( index , 1 ) ;
if ( pipes . indexOf ( dest ) > - 1 ) {
return ;
}
dest . end ( ) ;
dest . end ( ) ;
}
}
source . on ( 'end' , onend ) ;
source . on ( 'end' , onend ) ;
source . on ( 'close' , onend ) ;
}
}
/ *
/ *
@ -73,34 +85,35 @@ Stream.prototype.pipe = function(dest, options) {
source . emit ( 'resume' ) ;
source . emit ( 'resume' ) ;
} ;
} ;
}
}
var onpause = function ( ) {
var onpause = function ( ) {
source . pause ( ) ;
source . pause ( ) ;
}
}
dest . on ( 'pause' , onpause ) ;
dest . on ( 'pause' , onpause ) ;
var onresume = function ( ) {
var onresume = function ( ) {
if ( source . readable ) source . resume ( ) ;
if ( source . readable ) source . resume ( ) ;
} ;
} ;
dest . on ( 'resume' , onresume ) ;
dest . on ( 'resume' , onresume ) ;
var cleanup = function ( ) {
var cleanup = function ( ) {
source . removeListener ( 'data' , ondata ) ;
source . removeListener ( 'data' , ondata ) ;
dest . removeListener ( 'drain' , ondrain ) ;
dest . removeListener ( 'drain' , ondrain ) ;
source . removeListener ( 'end' , onend ) ;
source . removeListener ( 'end' , onend ) ;
source . removeListener ( 'close' , onend ) ;
dest . removeListener ( 'pause' , onpause ) ;
dest . removeListener ( 'pause' , onpause ) ;
dest . removeListener ( 'resume' , onresume ) ;
dest . removeListener ( 'resume' , onresume ) ;
source . removeListener ( 'end' , cleanup ) ;
source . removeListener ( 'end' , cleanup ) ;
source . removeListener ( 'close' , cleanup ) ;
source . removeListener ( 'close' , cleanup ) ;
dest . removeListener ( 'end' , cleanup ) ;
dest . removeListener ( 'end' , cleanup ) ;
dest . removeListener ( 'close' , cleanup ) ;
dest . removeListener ( 'close' , cleanup ) ;
}
}
source . on ( 'end' , cleanup ) ;
source . on ( 'end' , cleanup ) ;
source . on ( 'close' , cleanup ) ;
source . on ( 'close' , cleanup ) ;