From df339bccf24839da473937bd523602cf68b114af Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Thu, 11 Apr 2019 19:16:34 -0300 Subject: [PATCH] stream: convert string to Buffer when calling `unshift()` `readable.unshift` can take a string as an argument, but that string wasn't being converted to a Buffer, which caused a 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 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Rich Trott --- doc/api/errors.md | 2 +- doc/api/stream.md | 4 +- lib/_stream_readable.js | 32 +-- test/parallel/test-stream-readable-unshift.js | 187 ++++++++++++++++++ 4 files changed, 209 insertions(+), 16 deletions(-) create mode 100644 test/parallel/test-stream-readable-unshift.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 9420e07b16c..ebbe215b607 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2349,7 +2349,7 @@ such as `process.stdout.on('data')`. [`sign.sign()`]: crypto.html#crypto_sign_sign_privatekey_outputencoding [`stream.pipe()`]: stream.html#stream_readable_pipe_destination_options [`stream.push()`]: stream.html#stream_readable_push_chunk_encoding -[`stream.unshift()`]: stream.html#stream_readable_unshift_chunk +[`stream.unshift()`]: stream.html#stream_readable_unshift_chunk_encoding [`stream.write()`]: stream.html#stream_writable_write_chunk_encoding_callback [`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal [`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback diff --git a/doc/api/stream.md b/doc/api/stream.md index b78fa7fde46..46f4226039c 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1195,7 +1195,7 @@ setTimeout(() => { }, 1000); ``` -##### readable.unshift(chunk) +##### readable.unshift(chunk[, encoding])