From 4cfa7e07161da1d1f164fcff9f288e1d653a872a Mon Sep 17 00:00:00 2001 From: himself65 Date: Fri, 17 Apr 2020 15:04:38 +0800 Subject: [PATCH] stream: simplify Readable push/unshift logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/32899 Reviewed-By: Robert Nagy Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Juan José Arboleda Reviewed-By: James M Snell --- lib/_stream_readable.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index ec9f86d7eacf4a..f39c49867805d3 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -230,13 +230,15 @@ function readableAddChunk(stream, chunk, encoding, addToFront) { if (!state.objectMode) { if (typeof chunk === 'string') { encoding = encoding || state.defaultEncoding; - if (addToFront && state.encoding && state.encoding !== encoding) { - // When unshifting, if state.encoding is set, we have to save - // the string in the BufferList with the state encoding. - chunk = Buffer.from(chunk, encoding).toString(state.encoding); - } else if (encoding !== state.encoding) { - chunk = Buffer.from(chunk, encoding); - encoding = ''; + if (state.encoding !== encoding) { + if (addToFront && state.encoding) { + // When unshifting, if state.encoding is set, we have to save + // the string in the BufferList with the state encoding. + chunk = Buffer.from(chunk, encoding).toString(state.encoding); + } else { + chunk = Buffer.from(chunk, encoding); + encoding = ''; + } } } else if (chunk instanceof Buffer) { encoding = '';