Skip to content

Commit 0bd24a6

Browse files
himself65ronag
authored andcommitted
stream: simplify Readable push/unshift logic
PR-URL: #32899 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2496db8 commit 0bd24a6

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

lib/_stream_readable.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,15 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
233233
if (!state.objectMode) {
234234
if (typeof chunk === 'string') {
235235
encoding = encoding || state.defaultEncoding;
236-
if (addToFront && state.encoding && state.encoding !== encoding) {
237-
// When unshifting, if state.encoding is set, we have to save
238-
// the string in the BufferList with the state encoding.
239-
chunk = Buffer.from(chunk, encoding).toString(state.encoding);
240-
} else if (encoding !== state.encoding) {
241-
chunk = Buffer.from(chunk, encoding);
242-
encoding = '';
236+
if (state.encoding !== encoding) {
237+
if (addToFront && state.encoding) {
238+
// When unshifting, if state.encoding is set, we have to save
239+
// the string in the BufferList with the state encoding.
240+
chunk = Buffer.from(chunk, encoding).toString(state.encoding);
241+
} else {
242+
chunk = Buffer.from(chunk, encoding);
243+
encoding = '';
244+
}
243245
}
244246
} else if (chunk instanceof Buffer) {
245247
encoding = '';

0 commit comments

Comments
 (0)