Skip to content

Commit 1ece82b

Browse files
committed
stream: consolidate common code from push and unshift helper functions
1 parent 67b1383 commit 1ece82b

1 file changed

Lines changed: 17 additions & 34 deletions

File tree

lib/internal/streams/readable.js

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ Readable.prototype.push = function(chunk, encoding) {
368368
debug('push', chunk);
369369

370370
const state = this._readableState;
371+
if (chunk === null) {
372+
state[kState] &= ~kReading;
373+
onEofChunk(this, state);
374+
return false;
375+
}
376+
371377
return (state[kState] & kObjectMode) === 0 ?
372378
readableAddChunkPushByteMode(this, state, chunk, encoding) :
373379
readableAddChunkPushObjectMode(this, state, chunk, encoding);
@@ -377,20 +383,19 @@ Readable.prototype.push = function(chunk, encoding) {
377383
Readable.prototype.unshift = function(chunk, encoding) {
378384
debug('unshift', chunk);
379385
const state = this._readableState;
386+
if (chunk === null) {
387+
state[kState] &= ~kReading;
388+
onEofChunk(this, state);
389+
return false;
390+
}
391+
380392
return (state[kState] & kObjectMode) === 0 ?
381393
readableAddChunkUnshiftByteMode(this, state, chunk, encoding) :
382-
readableAddChunkUnshiftObjectMode(this, state, chunk);
394+
readableAddChunkUnshiftValue(this, state, chunk);
383395
};
384396

385397

386398
function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
387-
if (chunk === null) {
388-
state[kState] &= ~kReading;
389-
onEofChunk(stream, state);
390-
391-
return false;
392-
}
393-
394399
if (typeof chunk === 'string') {
395400
encoding = encoding || state.defaultEncoding;
396401
if (state.encoding !== encoding) {
@@ -418,21 +423,11 @@ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
418423
return readableAddChunkUnshiftValue(stream, state, chunk);
419424
}
420425

421-
function readableAddChunkUnshiftObjectMode(stream, state, chunk) {
422-
if (chunk === null) {
423-
state[kState] &= ~kReading;
424-
onEofChunk(stream, state);
425-
426-
return false;
427-
}
428-
429-
return readableAddChunkUnshiftValue(stream, state, chunk);
430-
}
431-
432426
function readableAddChunkUnshiftValue(stream, state, chunk) {
433-
if ((state[kState] & kEndEmitted) !== 0)
427+
if ((state[kState] & kEndEmitted) !== 0) {
434428
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
435-
else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
429+
return false;
430+
} else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
436431
return false;
437432
else
438433
addChunk(stream, state, chunk, true);
@@ -441,12 +436,6 @@ function readableAddChunkUnshiftValue(stream, state, chunk) {
441436
}
442437

443438
function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
444-
if (chunk === null) {
445-
state[kState] &= ~kReading;
446-
onEofChunk(stream, state);
447-
return false;
448-
}
449-
450439
if (typeof chunk === 'string') {
451440
encoding = encoding || state.defaultEncoding;
452441
if (state.encoding !== encoding) {
@@ -464,7 +453,7 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
464453
return false;
465454
}
466455

467-
if (!chunk || chunk.length <= 0) {
456+
if (!chunk || chunk.length === 0) {
468457
state[kState] &= ~kReading;
469458
maybeReadMore(stream, state);
470459

@@ -494,12 +483,6 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
494483
}
495484

496485
function readableAddChunkPushObjectMode(stream, state, chunk, encoding) {
497-
if (chunk === null) {
498-
state[kState] &= ~kReading;
499-
onEofChunk(stream, state);
500-
return false;
501-
}
502-
503486
if ((state[kState] & kEnded) !== 0) {
504487
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
505488
return false;

0 commit comments

Comments
 (0)