diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2019-04-11 19:16:34 -0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-06-02 16:56:13 +0200 |
commit | df339bccf24839da473937bd523602cf68b114af (patch) | |
tree | b395a06d7cb07c5d6aec063da970afd873aa6df3 /lib | |
parent | e0c4a9c8eda01e0d4abe71a16f322140cabc4e48 (diff) | |
download | android-node-v8-df339bccf24839da473937bd523602cf68b114af.tar.gz android-node-v8-df339bccf24839da473937bd523602cf68b114af.tar.bz2 android-node-v8-df339bccf24839da473937bd523602cf68b114af.zip |
stream: convert string to Buffer when calling `unshift(<string>)`
`readable.unshift` can take a string as an argument, but that
string wasn't being converted to a Buffer, which caused a
<TypeError: Argument must be a buffer> in some cases. Also if a
string was passed, that string was coerced to utf8 encoding.
A second optional argument `encoding` was added to `unshift` to
fix the encoding issue.
Fixes: https://github.com/nodejs/node/issues/27192
PR-URL: https://github.com/nodejs/node/pull/27194
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_stream_readable.js | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 163a042f08..d6db718875 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -207,13 +207,28 @@ Readable.prototype._destroy = function(err, cb) { // similar to how Writable.write() returns true if you should // write() some more. Readable.prototype.push = function(chunk, encoding) { - const state = this._readableState; - var skipChunkCheck; + return readableAddChunk(this, chunk, encoding, false); +}; + +// Unshift should *always* be something directly out of read() +Readable.prototype.unshift = function(chunk, encoding) { + return readableAddChunk(this, chunk, encoding, true); +}; + +function readableAddChunk(stream, chunk, encoding, addToFront) { + debug('readableAddChunk', chunk); + const state = stream._readableState; + + let skipChunkCheck; if (!state.objectMode) { if (typeof chunk === 'string') { encoding = encoding || state.defaultEncoding; - if (encoding !== state.encoding) { + 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 = ''; } @@ -223,17 +238,6 @@ Readable.prototype.push = function(chunk, encoding) { skipChunkCheck = true; } - return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); -}; - -// Unshift should *always* be something directly out of read() -Readable.prototype.unshift = function(chunk) { - return readableAddChunk(this, chunk, null, true, false); -}; - -function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { - debug('readableAddChunk', chunk); - const state = stream._readableState; if (chunk === null) { state.reading = false; onEofChunk(stream, state); |