-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream.unshift - TypeError: Argument must be a buffer #27192
Comments
@marcosc90 Would you be interested in opening a PR, since it seems that you already have a solution available? |
Yes, I have the solution ready, I can submit the PR in a couple of hours once I finish working. Just to confirm, a new argument must be added to |
@marcosc90 I’m not sure about the new argument – if the expectation is that the data was previously read from the stream, the encoding should match the encoding used for reading data from the stream (i.e. |
Is this the behaviour that you want? I believe if In any case, I can submit the PR which whatever functionality you prefer. I'm working on a few snippets to see if not adding |
Take the following example, using
That will output: Instead of: With the fix I'm proposing, using: So right now every string unshifted, is coerced to |
`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: #27192 PR-URL: #27194 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
The
stream.unshift
documentation states:The issue occurs when the
stream
encoding is not passed, or is not set usingsetEncoding
. When that happensstate.decoder
is not set andfromList
function may usestate.buffer.concat
(whenstate.buffer.length > 1
) , which internally usescopyBuffer
, that requires the source & target to be aBuffer
orUint8Array
.When
stream.unshift
is called with astring
(which the documentation states that is a valid argument), in some cases where the buffer is filled, andstate.buffer.concat
is triggered, an error will be thrown:Here's the script to reproduce the error:
It's the
parseHeader
example from the documentation, but instead of convertingremaining
to aBuffer
, I pass it directly to.unshift
This can be fixed, either by converting the
chunk
toBuffer
similar to how it's done in.push
, and maybe adding anencoding
argument too. Or by changing the documentation to state that astring
can only be passed if thestream
encoding is set.The text was updated successfully, but these errors were encountered: