Skip to content

Commit 6e76752

Browse files
ronagcodebytere
authored andcommitted
stream: simplify push
PR-URL: #31150 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 7a8963b commit 6e76752

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

lib/_stream_readable.js

+33-52
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
218218
debug('readableAddChunk', chunk);
219219
const state = stream._readableState;
220220

221-
let skipChunkCheck;
222-
221+
let err;
223222
if (!state.objectMode) {
224223
if (typeof chunk === 'string') {
225224
encoding = encoding || state.defaultEncoding;
@@ -231,54 +230,47 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
231230
chunk = Buffer.from(chunk, encoding);
232231
encoding = '';
233232
}
234-
skipChunkCheck = true;
233+
} else if (chunk instanceof Buffer) {
234+
encoding = '';
235+
} else if (Stream._isUint8Array(chunk)) {
236+
chunk = Stream._uint8ArrayToBuffer(chunk);
237+
encoding = '';
238+
} else if (chunk != null) {
239+
err = new ERR_INVALID_ARG_TYPE(
240+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
235241
}
236-
} else {
237-
skipChunkCheck = true;
238242
}
239243

240-
if (chunk === null) {
244+
if (err) {
245+
errorOrDestroy(stream, err);
246+
} else if (chunk === null) {
241247
state.reading = false;
242248
onEofChunk(stream, state);
243-
} else {
244-
var er;
245-
if (!skipChunkCheck)
246-
er = chunkInvalid(state, chunk);
247-
if (er) {
248-
errorOrDestroy(stream, er);
249-
} else if (state.objectMode || (chunk && chunk.length > 0)) {
250-
if (typeof chunk !== 'string' &&
251-
!state.objectMode &&
252-
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
253-
!(chunk instanceof Buffer)) {
254-
chunk = Stream._uint8ArrayToBuffer(chunk);
255-
}
256-
257-
if (addToFront) {
258-
if (state.endEmitted)
259-
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
249+
} else if (state.objectMode || (chunk && chunk.length > 0)) {
250+
if (addToFront) {
251+
if (state.endEmitted)
252+
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
253+
else
254+
addChunk(stream, state, chunk, true);
255+
} else if (state.ended) {
256+
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
257+
} else if (state.destroyed) {
258+
return false;
259+
} else {
260+
state.reading = false;
261+
if (state.decoder && !encoding) {
262+
chunk = state.decoder.write(chunk);
263+
if (state.objectMode || chunk.length !== 0)
264+
addChunk(stream, state, chunk, false);
260265
else
261-
addChunk(stream, state, chunk, true);
262-
} else if (state.ended) {
263-
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
264-
} else if (state.destroyed) {
265-
return false;
266+
maybeReadMore(stream, state);
266267
} else {
267-
state.reading = false;
268-
if (state.decoder && !encoding) {
269-
chunk = state.decoder.write(chunk);
270-
if (state.objectMode || chunk.length !== 0)
271-
addChunk(stream, state, chunk, false);
272-
else
273-
maybeReadMore(stream, state);
274-
} else {
275-
addChunk(stream, state, chunk, false);
276-
}
268+
addChunk(stream, state, chunk, false);
277269
}
278-
} else if (!addToFront) {
279-
state.reading = false;
280-
maybeReadMore(stream, state);
281270
}
271+
} else if (!addToFront) {
272+
state.reading = false;
273+
maybeReadMore(stream, state);
282274
}
283275

284276
// We can push more data if we are below the highWaterMark.
@@ -306,17 +298,6 @@ function addChunk(stream, state, chunk, addToFront) {
306298
maybeReadMore(stream, state);
307299
}
308300

309-
function chunkInvalid(state, chunk) {
310-
if (!Stream._isUint8Array(chunk) &&
311-
typeof chunk !== 'string' &&
312-
chunk !== undefined &&
313-
!state.objectMode) {
314-
return new ERR_INVALID_ARG_TYPE(
315-
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
316-
}
317-
}
318-
319-
320301
Readable.prototype.isPaused = function() {
321302
const state = this._readableState;
322303
return state[kPaused] === true || state.flowing === false;

0 commit comments

Comments
 (0)