Skip to content

Commit 55dbada

Browse files
committed
stream: simplify push
1 parent c052113 commit 55dbada

File tree

1 file changed

+34
-53
lines changed

1 file changed

+34
-53
lines changed

lib/_stream_readable.js

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,11 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
275275
debug('readableAddChunk', chunk);
276276
const state = stream._readableState;
277277

278-
let skipChunkCheck;
279-
278+
let err;
280279
if (!state.objectMode) {
281-
if (typeof chunk === 'string') {
280+
if (chunk instanceof Buffer) {
281+
encoding = '';
282+
} else if (typeof chunk === 'string') {
282283
encoding = encoding || state.defaultEncoding;
283284
if (addToFront && state.encoding && state.encoding !== encoding) {
284285
// When unshifting, if state.encoding is set, we have to save
@@ -288,54 +289,45 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
288289
chunk = Buffer.from(chunk, encoding);
289290
encoding = '';
290291
}
291-
skipChunkCheck = true;
292+
} else if (Stream._isUint8Array(chunk)) {
293+
chunk = Stream._uint8ArrayToBuffer(chunk);
294+
encoding = '';
295+
} else if (chunk != null) {
296+
err = new ERR_INVALID_ARG_TYPE(
297+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
292298
}
293-
} else {
294-
skipChunkCheck = true;
295299
}
296300

297-
if (chunk === null) {
301+
if (err) {
302+
errorOrDestroy(stream, err);
303+
} else if (chunk === null) {
298304
state.reading = false;
299305
onEofChunk(stream, state);
300-
} else {
301-
var er;
302-
if (!skipChunkCheck)
303-
er = chunkInvalid(state, chunk);
304-
if (er) {
305-
errorOrDestroy(stream, er);
306-
} else if (state.objectMode || (chunk && chunk.length > 0)) {
307-
if (typeof chunk !== 'string' &&
308-
!state.objectMode &&
309-
// Do not use Object.getPrototypeOf as it is slower since V8 7.3.
310-
!(chunk instanceof Buffer)) {
311-
chunk = Stream._uint8ArrayToBuffer(chunk);
312-
}
313-
314-
if (addToFront) {
315-
if (state.endEmitted)
316-
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
306+
} else if (state.objectMode || (chunk && chunk.length > 0)) {
307+
if (addToFront) {
308+
if (state.endEmitted)
309+
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
310+
else
311+
addChunk(stream, state, chunk, true);
312+
} else if (state.ended) {
313+
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
314+
} else if (state.destroyed) {
315+
return false;
316+
} else {
317+
state.reading = false;
318+
if (state.decoder && !encoding) {
319+
chunk = state.decoder.write(chunk);
320+
if (state.objectMode || chunk.length !== 0)
321+
addChunk(stream, state, chunk, false);
317322
else
318-
addChunk(stream, state, chunk, true);
319-
} else if (state.ended) {
320-
errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
321-
} else if (state.destroyed) {
322-
return false;
323+
maybeReadMore(stream, state);
323324
} else {
324-
state.reading = false;
325-
if (state.decoder && !encoding) {
326-
chunk = state.decoder.write(chunk);
327-
if (state.objectMode || chunk.length !== 0)
328-
addChunk(stream, state, chunk, false);
329-
else
330-
maybeReadMore(stream, state);
331-
} else {
332-
addChunk(stream, state, chunk, false);
333-
}
325+
addChunk(stream, state, chunk, false);
334326
}
335-
} else if (!addToFront) {
336-
state.reading = false;
337-
maybeReadMore(stream, state);
338327
}
328+
} else if (!addToFront) {
329+
state.reading = false;
330+
maybeReadMore(stream, state);
339331
}
340332

341333
// We can push more data if we are below the highWaterMark.
@@ -369,17 +361,6 @@ function addChunk(stream, state, chunk, addToFront) {
369361
maybeReadMore(stream, state);
370362
}
371363

372-
function chunkInvalid(state, chunk) {
373-
if (!Stream._isUint8Array(chunk) &&
374-
typeof chunk !== 'string' &&
375-
chunk !== undefined &&
376-
!state.objectMode) {
377-
return new ERR_INVALID_ARG_TYPE(
378-
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
379-
}
380-
}
381-
382-
383364
Readable.prototype.isPaused = function() {
384365
const state = this._readableState;
385366
return state[kPaused] === true || state.flowing === false;

0 commit comments

Comments
 (0)