@@ -97,8 +97,7 @@ function ReadableState(options, stream, isDuplex) {
9797 // array.shift()
9898 this . buffer = new BufferList ( ) ;
9999 this . length = 0 ;
100- this . pipes = null ;
101- this . pipesCount = 0 ;
100+ this . pipes = [ ] ;
102101 this . flowing = null ;
103102 this . ended = false ;
104103 this . endEmitted = false ;
@@ -148,6 +147,13 @@ function ReadableState(options, stream, isDuplex) {
148147 }
149148}
150149
150+ // Legacy getter for `pipesCount`
151+ Object . defineProperty ( ReadableState . prototype , 'pipesCount' , {
152+ get ( ) {
153+ return this . pipes . length ;
154+ }
155+ } ) ;
156+
151157function Readable ( options ) {
152158 if ( ! ( this instanceof Readable ) )
153159 return new Readable ( options ) ;
@@ -635,19 +641,8 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
635641 const src = this ;
636642 const state = this . _readableState ;
637643
638- switch ( state . pipesCount ) {
639- case 0 :
640- state . pipes = dest ;
641- break ;
642- case 1 :
643- state . pipes = [ state . pipes , dest ] ;
644- break ;
645- default :
646- state . pipes . push ( dest ) ;
647- break ;
648- }
649- state . pipesCount += 1 ;
650- debug ( 'pipe count=%d opts=%j' , state . pipesCount , pipeOpts ) ;
644+ state . pipes . push ( dest ) ;
645+ debug ( 'pipe count=%d opts=%j' , state . pipes . length , pipeOpts ) ;
651646
652647 const doEnd = ( ! pipeOpts || pipeOpts . end !== false ) &&
653648 dest !== process . stdout &&
@@ -717,9 +712,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
717712 // to get stuck in a permanently paused state if that write
718713 // also returned false.
719714 // => Check whether `dest` is still a piping destination.
720- if ( ( ( state . pipesCount === 1 && state . pipes === dest ) ||
721- ( state . pipesCount > 1 && state . pipes . includes ( dest ) ) ) &&
722- ! cleanedUp ) {
715+ if ( state . pipes . length > 0 && state . pipes . includes ( dest ) && ! cleanedUp ) {
723716 debug ( 'false write response, pause' , state . awaitDrain ) ;
724717 state . awaitDrain ++ ;
725718 }
@@ -789,38 +782,16 @@ Readable.prototype.unpipe = function(dest) {
789782 const unpipeInfo = { hasUnpiped : false } ;
790783
791784 // If we're not piping anywhere, then do nothing.
792- if ( state . pipesCount === 0 )
785+ if ( state . pipes . length === 0 )
793786 return this ;
794787
795- // Just one destination. most common case.
796- if ( state . pipesCount === 1 ) {
797- // Passed in one, but it's not the right one.
798- if ( dest && dest !== state . pipes )
799- return this ;
800-
801- if ( ! dest )
802- dest = state . pipes ;
803-
804- // got a match.
805- state . pipes = null ;
806- state . pipesCount = 0 ;
807- state . flowing = false ;
808- if ( dest )
809- dest . emit ( 'unpipe' , this , unpipeInfo ) ;
810- return this ;
811- }
812-
813- // Slow case with multiple pipe destinations.
814-
815788 if ( ! dest ) {
816789 // remove all.
817790 var dests = state . pipes ;
818- var len = state . pipesCount ;
819- state . pipes = null ;
820- state . pipesCount = 0 ;
791+ state . pipes = [ ] ;
821792 state . flowing = false ;
822793
823- for ( var i = 0 ; i < len ; i ++ )
794+ for ( var i = 0 ; i < dests . length ; i ++ )
824795 dests [ i ] . emit ( 'unpipe' , this , { hasUnpiped : false } ) ;
825796 return this ;
826797 }
@@ -831,9 +802,8 @@ Readable.prototype.unpipe = function(dest) {
831802 return this ;
832803
833804 state . pipes . splice ( index , 1 ) ;
834- state . pipesCount -= 1 ;
835- if ( state . pipesCount === 1 )
836- state . pipes = state . pipes [ 0 ] ;
805+ if ( state . pipes . length === 0 )
806+ state . flowing = false ;
837807
838808 dest . emit ( 'unpipe' , this , unpipeInfo ) ;
839809
0 commit comments