Skip to content

Commit 94549be

Browse files
ronagBridgeAR
authored andcommitted
stream: simplify push
PR-URL: #31150 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent c3b702f commit 94549be

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

lib/_stream_readable.js

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
222222
debug('readableAddChunk', chunk);
223223
const state = stream._readableState;
224224

225-
let skipChunkCheck;
226-
225+
let err;
227226
if (!state.objectMode) {
228227
if (typeof chunk === 'string') {
229228
encoding = encoding || state.defaultEncoding;
@@ -235,54 +234,47 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
235234
chunk = Buffer.from(chunk, encoding);
236235
encoding = '';
237236
}
238-
skipChunkCheck = true;
237+
} else if (chunk instanceof Buffer) {
238+
encoding = '';
239+
} else if (Stream._isUint8Array(chunk)) {
240+
chunk = Stream._uint8ArrayToBuffer(chunk);
241+
encoding = '';
242+
} else if (chunk != null) {
243+
err = new ERR_INVALID_ARG_TYPE(
244+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
239245
}
240-
} else {
241-
skipChunkCheck = true;
242246
}
243247

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

288280
// We can push more data if we are below the highWaterMark.
@@ -316,17 +308,6 @@ function addChunk(stream, state, chunk, addToFront) {
316308
maybeReadMore(stream, state);
317309
}
318310

319-
function chunkInvalid(state, chunk) {
320-
if (!Stream._isUint8Array(chunk) &&
321-
typeof chunk !== 'string' &&
322-
chunk !== undefined &&
323-
!state.objectMode) {
324-
return new ERR_INVALID_ARG_TYPE(
325-
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
326-
}
327-
}
328-
329-
330311
Readable.prototype.isPaused = function() {
331312
const state = this._readableState;
332313
return state[kPaused] === true || state.flowing === false;

0 commit comments

Comments
 (0)