@@ -267,7 +267,6 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
267
267
function addChunk ( stream , state , chunk , addToFront ) {
268
268
if ( state . flowing && state . length === 0 && ! state . sync ) {
269
269
stream . emit ( 'data' , chunk ) ;
270
- stream . read ( 0 ) ;
271
270
} else {
272
271
// update the buffer info.
273
272
state . length += state . objectMode ? 1 : chunk . length ;
@@ -496,7 +495,11 @@ function onEofChunk(stream, state) {
496
495
state . ended = true ;
497
496
498
497
// 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
+ }
500
503
}
501
504
502
505
// Don't emit readable right away in sync mode, because this can trigger
@@ -508,16 +511,15 @@ function emitReadable(stream) {
508
511
if ( ! state . emittedReadable ) {
509
512
debug ( 'emitReadable' , state . flowing ) ;
510
513
state . emittedReadable = true ;
511
- if ( state . sync )
512
- process . nextTick ( emitReadable_ , stream ) ;
513
- else
514
- emitReadable_ ( stream ) ;
514
+ process . nextTick ( emitReadable_ , stream ) ;
515
515
}
516
516
}
517
517
518
518
function emitReadable_ ( stream ) {
519
+ var state = stream . _readableState ;
519
520
debug ( 'emit readable' ) ;
520
521
stream . emit ( 'readable' ) ;
522
+ state . needReadable = ! state . flowing && ! state . ended ;
521
523
flow ( stream ) ;
522
524
}
523
525
@@ -537,7 +539,7 @@ function maybeReadMore(stream, state) {
537
539
538
540
function maybeReadMore_ ( stream , state ) {
539
541
var len = state . length ;
540
- while ( ! state . reading && ! state . flowing && ! state . ended &&
542
+ while ( ! state . reading && ! state . ended &&
541
543
state . length < state . highWaterMark ) {
542
544
debug ( 'maybeReadMore read 0' ) ;
543
545
stream . read ( 0 ) ;
@@ -644,6 +646,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
644
646
debug ( 'ondata' ) ;
645
647
increasedAwaitDrain = false ;
646
648
var ret = dest . write ( chunk ) ;
649
+ debug ( 'dest.write' , ret ) ;
647
650
if ( false === ret && ! increasedAwaitDrain ) {
648
651
// If the user unpiped during `dest.write()`, it is possible
649
652
// to get stuck in a permanently paused state if that write
@@ -824,8 +827,8 @@ function resume(stream, state) {
824
827
}
825
828
826
829
function resume_ ( stream , state ) {
830
+ debug ( 'resume' , state . reading ) ;
827
831
if ( ! state . reading ) {
828
- debug ( 'resume read 0' ) ;
829
832
stream . read ( 0 ) ;
830
833
}
831
834
@@ -1087,13 +1090,16 @@ function copyFromBuffer(n, list) {
1087
1090
function endReadable ( stream ) {
1088
1091
var state = stream . _readableState ;
1089
1092
1093
+ debug ( 'endReadable' , state . endEmitted ) ;
1090
1094
if ( ! state . endEmitted ) {
1091
1095
state . ended = true ;
1092
1096
process . nextTick ( endReadableNT , state , stream ) ;
1093
1097
}
1094
1098
}
1095
1099
1096
1100
function endReadableNT ( state , stream ) {
1101
+ debug ( 'endReadableNT' , state . endEmitted , state . length ) ;
1102
+
1097
1103
// Check that we didn't get one last unshift.
1098
1104
if ( ! state . endEmitted && state . length === 0 ) {
1099
1105
state . endEmitted = true ;
0 commit comments