@@ -681,20 +681,17 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
681
681
dest . end ( ) ;
682
682
}
683
683
684
- // When the dest drains, it reduces the awaitDrain counter
685
- // on the source. This would be more elegant with a .once()
686
- // handler in flow(), but adding and removing repeatedly is
687
- // too slow.
688
- const ondrain = pipeOnDrain ( src ) ;
689
- dest . on ( 'drain' , ondrain ) ;
684
+ let ondrain ;
690
685
691
686
var cleanedUp = false ;
692
687
function cleanup ( ) {
693
688
debug ( 'cleanup' ) ;
694
689
// Cleanup event handlers once the pipe is broken
695
690
dest . removeListener ( 'close' , onclose ) ;
696
691
dest . removeListener ( 'finish' , onfinish ) ;
697
- dest . removeListener ( 'drain' , ondrain ) ;
692
+ if ( ondrain ) {
693
+ dest . removeListener ( 'drain' , ondrain ) ;
694
+ }
698
695
dest . removeListener ( 'error' , onerror ) ;
699
696
dest . removeListener ( 'unpipe' , onunpipe ) ;
700
697
src . removeListener ( 'end' , onend ) ;
@@ -708,7 +705,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
708
705
// flowing again.
709
706
// So, if this is awaiting a drain, then we just call it now.
710
707
// If we don't know, then assume that we are waiting for one.
711
- if ( state . awaitDrain &&
708
+ if ( ondrain && state . awaitDrain &&
712
709
( ! dest . _writableState || dest . _writableState . needDrain ) )
713
710
ondrain ( ) ;
714
711
}
@@ -729,6 +726,14 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
729
726
debug ( 'false write response, pause' , state . awaitDrain ) ;
730
727
state . awaitDrain ++ ;
731
728
}
729
+ if ( ! ondrain ) {
730
+ // When the dest drains, it reduces the awaitDrain counter
731
+ // on the source. This would be more elegant with a .once()
732
+ // handler in flow(), but adding and removing repeatedly is
733
+ // too slow.
734
+ ondrain = pipeOnDrain ( src ) ;
735
+ dest . on ( 'drain' , ondrain ) ;
736
+ }
732
737
src . pause ( ) ;
733
738
}
734
739
}
0 commit comments