Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: consolidate common code from readable push and unshift helper… #50402

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 17 additions & 34 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ Readable.prototype.push = function(chunk, encoding) {
debug('push', chunk);

const state = this._readableState;
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(this, state);
return false;
}

return (state[kState] & kObjectMode) === 0 ?
readableAddChunkPushByteMode(this, state, chunk, encoding) :
readableAddChunkPushObjectMode(this, state, chunk, encoding);
Expand All @@ -377,20 +383,19 @@ Readable.prototype.push = function(chunk, encoding) {
Readable.prototype.unshift = function(chunk, encoding) {
debug('unshift', chunk);
const state = this._readableState;
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(this, state);
return false;
}

return (state[kState] & kObjectMode) === 0 ?
readableAddChunkUnshiftByteMode(this, state, chunk, encoding) :
readableAddChunkUnshiftObjectMode(this, state, chunk);
readableAddChunkUnshiftValue(this, state, chunk);
};


function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(stream, state);

return false;
}

if (typeof chunk === 'string') {
encoding = encoding || state.defaultEncoding;
if (state.encoding !== encoding) {
Expand Down Expand Up @@ -418,21 +423,11 @@ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
return readableAddChunkUnshiftValue(stream, state, chunk);
}

function readableAddChunkUnshiftObjectMode(stream, state, chunk) {
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(stream, state);

return false;
}

return readableAddChunkUnshiftValue(stream, state, chunk);
}

function readableAddChunkUnshiftValue(stream, state, chunk) {
wa-Nadoo marked this conversation as resolved.
Show resolved Hide resolved
if ((state[kState] & kEndEmitted) !== 0)
if ((state[kState] & kEndEmitted) !== 0) {
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
return false;
} else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
return false;
else
addChunk(stream, state, chunk, true);
Expand All @@ -441,12 +436,6 @@ function readableAddChunkUnshiftValue(stream, state, chunk) {
}

function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(stream, state);
return false;
}

if (typeof chunk === 'string') {
encoding = encoding || state.defaultEncoding;
if (state.encoding !== encoding) {
Expand All @@ -464,7 +453,7 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
return false;
}

if (!chunk || chunk.length <= 0) {
if (!chunk || chunk.length === 0) {
state[kState] &= ~kReading;
maybeReadMore(stream, state);

Expand Down Expand Up @@ -494,12 +483,6 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
}

function readableAddChunkPushObjectMode(stream, state, chunk, encoding) {
if (chunk === null) {
state[kState] &= ~kReading;
onEofChunk(stream, state);
return false;
}

if ((state[kState] & kEnded) !== 0) {
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
return false;
Expand Down