@@ -30,6 +30,7 @@ const EE = require('events');
3030const Stream = require ( 'stream' ) ;
3131const { Buffer } = require ( 'buffer' ) ;
3232
33+ const internalUtil = require ( 'internal/util' ) ;
3334const debug = require ( 'internal/util/debuglog' ) . debuglog ( 'stream' ) ;
3435const BufferList = require ( 'internal/streams/buffer_list' ) ;
3536const destroyImpl = require ( 'internal/streams/destroy' ) ;
@@ -97,8 +98,7 @@ function ReadableState(options, stream, isDuplex) {
9798 // array.shift()
9899 this . buffer = new BufferList ( ) ;
99100 this . length = 0 ;
100- this . pipes = null ;
101- this . pipesCount = 0 ;
101+ this . pipes = [ ] ;
102102 this . flowing = null ;
103103 this . ended = false ;
104104 this . endEmitted = false ;
@@ -148,6 +148,13 @@ function ReadableState(options, stream, isDuplex) {
148148 }
149149}
150150
151+ Object . defineProperty ( ReadableState . prototype , 'pipesCount' , {
152+ get : internalUtil . deprecate ( function ( ) {
153+ return this . pipes . length ;
154+ } , '_readableState.pipesCount is deprecated. ' +
155+ 'Use _readableState.pipes.length instead.' , 'DEP0133' ) ,
156+ } ) ;
157+
151158function Readable ( options ) {
152159 if ( ! ( this instanceof Readable ) )
153160 return new Readable ( options ) ;
@@ -635,19 +642,8 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
635642 const src = this ;
636643 const state = this . _readableState ;
637644
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 ) ;
645+ state . pipes . push ( dest ) ;
646+ debug ( 'pipe count=%d opts=%j' , state . pipes . length , pipeOpts ) ;
651647
652648 const doEnd = ( ! pipeOpts || pipeOpts . end !== false ) &&
653649 dest !== process . stdout &&
@@ -717,9 +713,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
717713 // to get stuck in a permanently paused state if that write
718714 // also returned false.
719715 // => 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 ) {
716+ if ( state . pipes . length > 0 && state . pipes . includes ( dest ) && ! cleanedUp ) {
723717 debug ( 'false write response, pause' , state . awaitDrain ) ;
724718 state . awaitDrain ++ ;
725719 }
@@ -789,38 +783,16 @@ Readable.prototype.unpipe = function(dest) {
789783 const unpipeInfo = { hasUnpiped : false } ;
790784
791785 // If we're not piping anywhere, then do nothing.
792- if ( state . pipesCount === 0 )
793- return this ;
794-
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 ) ;
786+ if ( state . pipes . length === 0 )
810787 return this ;
811- }
812-
813- // Slow case with multiple pipe destinations.
814788
815789 if ( ! dest ) {
816790 // remove all.
817791 var dests = state . pipes ;
818- var len = state . pipesCount ;
819- state . pipes = null ;
820- state . pipesCount = 0 ;
792+ state . pipes = [ ] ;
821793 state . flowing = false ;
822794
823- for ( var i = 0 ; i < len ; i ++ )
795+ for ( var i = 0 ; i < dests . length ; i ++ )
824796 dests [ i ] . emit ( 'unpipe' , this , { hasUnpiped : false } ) ;
825797 return this ;
826798 }
@@ -831,9 +803,8 @@ Readable.prototype.unpipe = function(dest) {
831803 return this ;
832804
833805 state . pipes . splice ( index , 1 ) ;
834- state . pipesCount -= 1 ;
835- if ( state . pipesCount === 1 )
836- state . pipes = state . pipes [ 0 ] ;
806+ if ( state . pipes . length === 0 )
807+ state . flowing = false ;
837808
838809 dest . emit ( 'unpipe' , this , unpipeInfo ) ;
839810
0 commit comments