@@ -367,6 +367,18 @@ Readable.prototype.push = function(chunk, encoding) {
367
367
debug ( 'push' , chunk ) ;
368
368
369
369
const state = this . _readableState ;
370
+ if ( ( state [ kState ] & kEnded ) !== 0 ) {
371
+ errorOrDestroy ( this , new ERR_STREAM_PUSH_AFTER_EOF ( ) ) ;
372
+ return false ;
373
+ } else if ( ( state [ kState ] & ( kDestroyed | kErrored ) ) !== 0 )
374
+ return false ;
375
+
376
+ state [ kState ] &= ~ kReading ;
377
+ if ( chunk === null ) {
378
+ onEofChunk ( this , state ) ;
379
+ return false ;
380
+ }
381
+
370
382
return ( state [ kState ] & kObjectMode ) === 0 ?
371
383
readableAddChunkPushByteMode ( this , state , chunk , encoding ) :
372
384
readableAddChunkPushObjectMode ( this , state , chunk , encoding ) ;
@@ -376,20 +388,25 @@ Readable.prototype.push = function(chunk, encoding) {
376
388
Readable . prototype . unshift = function ( chunk , encoding ) {
377
389
debug ( 'unshift' , chunk ) ;
378
390
const state = this . _readableState ;
391
+ if ( ( state [ kState ] & kEndEmitted ) !== 0 ) {
392
+ errorOrDestroy ( this , new ERR_STREAM_UNSHIFT_AFTER_END_EVENT ( ) ) ;
393
+ return false ;
394
+ } else if ( ( state [ kState ] & ( kDestroyed | kErrored ) ) !== 0 )
395
+ return false ;
396
+
397
+ if ( chunk === null ) {
398
+ state [ kState ] &= ~ kReading ;
399
+ onEofChunk ( this , state ) ;
400
+ return false ;
401
+ }
402
+
379
403
return ( state [ kState ] & kObjectMode ) === 0 ?
380
404
readableAddChunkUnshiftByteMode ( this , state , chunk , encoding ) :
381
405
readableAddChunkUnshiftObjectMode ( this , state , chunk ) ;
382
406
} ;
383
407
384
408
385
409
function readableAddChunkUnshiftByteMode ( stream , state , chunk , encoding ) {
386
- if ( chunk === null ) {
387
- state [ kState ] &= ~ kReading ;
388
- onEofChunk ( stream , state ) ;
389
-
390
- return false ;
391
- }
392
-
393
410
if ( typeof chunk === 'string' ) {
394
411
encoding = encoding || state . defaultEncoding ;
395
412
if ( state . encoding !== encoding ) {
@@ -418,34 +435,16 @@ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
418
435
}
419
436
420
437
function readableAddChunkUnshiftObjectMode ( stream , state , chunk ) {
421
- if ( chunk === null ) {
422
- state [ kState ] &= ~ kReading ;
423
- onEofChunk ( stream , state ) ;
424
-
425
- return false ;
426
- }
427
-
428
438
return readableAddChunkUnshiftValue ( stream , state , chunk ) ;
429
439
}
430
440
431
441
function readableAddChunkUnshiftValue ( stream , state , chunk ) {
432
- if ( ( state [ kState ] & kEndEmitted ) !== 0 )
433
- errorOrDestroy ( stream , new ERR_STREAM_UNSHIFT_AFTER_END_EVENT ( ) ) ;
434
- else if ( ( state [ kState ] & ( kDestroyed | kErrored ) ) !== 0 )
435
- return false ;
436
- else
437
- addChunk ( stream , state , chunk , true ) ;
442
+ addChunk ( stream , state , chunk , true ) ;
438
443
439
444
return canPushMore ( state ) ;
440
445
}
441
446
442
447
function readableAddChunkPushByteMode ( stream , state , chunk , encoding ) {
443
- if ( chunk === null ) {
444
- state [ kState ] &= ~ kReading ;
445
- onEofChunk ( stream , state ) ;
446
- return false ;
447
- }
448
-
449
448
if ( typeof chunk === 'string' ) {
450
449
encoding = encoding || state . defaultEncoding ;
451
450
if ( state . encoding !== encoding ) {
@@ -464,22 +463,11 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
464
463
}
465
464
466
465
if ( ! chunk || chunk . length <= 0 ) {
467
- state [ kState ] &= ~ kReading ;
468
466
maybeReadMore ( stream , state ) ;
469
467
470
468
return canPushMore ( state ) ;
471
469
}
472
470
473
- if ( ( state [ kState ] & kEnded ) !== 0 ) {
474
- errorOrDestroy ( stream , new ERR_STREAM_PUSH_AFTER_EOF ( ) ) ;
475
- return false ;
476
- }
477
-
478
- if ( ( state [ kState ] & ( kDestroyed | kErrored ) ) !== 0 ) {
479
- return false ;
480
- }
481
-
482
- state [ kState ] &= ~ kReading ;
483
471
if ( ( state [ kState ] & kDecoder ) !== 0 && ! encoding ) {
484
472
chunk = state [ kDecoderValue ] . write ( chunk ) ;
485
473
if ( chunk . length === 0 ) {
@@ -493,23 +481,6 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
493
481
}
494
482
495
483
function readableAddChunkPushObjectMode ( stream , state , chunk , encoding ) {
496
- if ( chunk === null ) {
497
- state [ kState ] &= ~ kReading ;
498
- onEofChunk ( stream , state ) ;
499
- return false ;
500
- }
501
-
502
- if ( ( state [ kState ] & kEnded ) !== 0 ) {
503
- errorOrDestroy ( stream , new ERR_STREAM_PUSH_AFTER_EOF ( ) ) ;
504
- return false ;
505
- }
506
-
507
- if ( ( state [ kState ] & ( kDestroyed | kErrored ) ) !== 0 ) {
508
- return false ;
509
- }
510
-
511
- state [ kState ] &= ~ kReading ;
512
-
513
484
if ( ( state [ kState ] & kDecoder ) !== 0 && ! encoding ) {
514
485
chunk = state [ kDecoderValue ] . write ( chunk ) ;
515
486
}
0 commit comments