@@ -267,7 +267,6 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
267267function addChunk ( stream , state , chunk , addToFront ) {
268268 if ( state . flowing && state . length === 0 && ! state . sync ) {
269269 stream . emit ( 'data' , chunk ) ;
270- stream . read ( 0 ) ;
271270 } else {
272271 // update the buffer info.
273272 state . length += state . objectMode ? 1 : chunk . length ;
@@ -496,7 +495,11 @@ function onEofChunk(stream, state) {
496495 state . ended = true ;
497496
498497 // emit 'readable' now to make sure it gets picked up.
499- emitReadable ( stream ) ;
498+ state . needReadable = false ;
499+ if ( ! state . emittedReadable ) {
500+ state . emittedReadable = true ;
501+ emitReadable_ ( stream ) ;
502+ }
500503}
501504
502505// Don't emit readable right away in sync mode, because this can trigger
@@ -508,16 +511,15 @@ function emitReadable(stream) {
508511 if ( ! state . emittedReadable ) {
509512 debug ( 'emitReadable' , state . flowing ) ;
510513 state . emittedReadable = true ;
511- if ( state . sync )
512- process . nextTick ( emitReadable_ , stream ) ;
513- else
514- emitReadable_ ( stream ) ;
514+ process . nextTick ( emitReadable_ , stream ) ;
515515 }
516516}
517517
518518function emitReadable_ ( stream ) {
519+ var state = stream . _readableState ;
519520 debug ( 'emit readable' ) ;
520521 stream . emit ( 'readable' ) ;
522+ state . needReadable = ! state . flowing && ! state . ended ;
521523 flow ( stream ) ;
522524}
523525
@@ -537,7 +539,7 @@ function maybeReadMore(stream, state) {
537539
538540function maybeReadMore_ ( stream , state ) {
539541 var len = state . length ;
540- while ( ! state . reading && ! state . flowing && ! state . ended &&
542+ while ( ! state . reading && ! state . ended &&
541543 state . length < state . highWaterMark ) {
542544 debug ( 'maybeReadMore read 0' ) ;
543545 stream . read ( 0 ) ;
@@ -644,6 +646,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
644646 debug ( 'ondata' ) ;
645647 increasedAwaitDrain = false ;
646648 var ret = dest . write ( chunk ) ;
649+ debug ( 'dest.write' , ret ) ;
647650 if ( false === ret && ! increasedAwaitDrain ) {
648651 // If the user unpiped during `dest.write()`, it is possible
649652 // to get stuck in a permanently paused state if that write
@@ -824,8 +827,8 @@ function resume(stream, state) {
824827}
825828
826829function resume_ ( stream , state ) {
830+ debug ( 'resume' , state . reading ) ;
827831 if ( ! state . reading ) {
828- debug ( 'resume read 0' ) ;
829832 stream . read ( 0 ) ;
830833 }
831834
@@ -1087,13 +1090,16 @@ function copyFromBuffer(n, list) {
10871090function endReadable ( stream ) {
10881091 var state = stream . _readableState ;
10891092
1093+ debug ( 'endReadable' , state . endEmitted ) ;
10901094 if ( ! state . endEmitted ) {
10911095 state . ended = true ;
10921096 process . nextTick ( endReadableNT , state , stream ) ;
10931097 }
10941098}
10951099
10961100function endReadableNT ( state , stream ) {
1101+ debug ( 'endReadableNT' , state . endEmitted , state . length ) ;
1102+
10971103 // Check that we didn't get one last unshift.
10981104 if ( ! state . endEmitted && state . length === 0 ) {
10991105 state . endEmitted = true ;
0 commit comments