From a47ee80114b6e118da06acc0d390329c9b057ca0 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 9420e07b16c871..ebbe215b6075f1 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 b78fa7fde46cad..46f4226039c2df 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1195,7 +1195,7 @@ setTimeout(() => { }, 1000); ``` -##### readable.unshift(chunk) +##### readable.unshift(chunk[, encoding])