-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
bad Buffer conversion behaviour due to Uint8Array inheritance #28725
Comments
|
How about introducing a new API like I saw a lot of ( |
Also, maybe we could emit a warning for this? That's platform independent and if someone does |
It creates a new Buffer that shares ArrayBuffer with another ArrayBufferView. A shortcut for `Buffer.from(view.buffer, view.byteOffset, view.byteLength)` but it requires no temporary var. refs: nodejs#28725
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: nodejs#28725
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: #28725 PR-URL: #48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: #28725 PR-URL: #48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: #28725 PR-URL: #48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: nodejs#28725 PR-URL: nodejs#48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: nodejs#28725 PR-URL: nodejs#48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
The code for Buffer.from() treats non-Buffer and non-Uint8Array Array-likes as Arrays. This creates some confusion when passing various TypedArrays to Buffer.from(). The documentation now reflects the actual behavior. Fixes: nodejs#28725 PR-URL: nodejs#48274 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Harshitha K P <harshitha014@gmail.com>
tl;dr - TypedArrays of lower-order truncate high-order values, which is is confusing (and hard to work around).
Ok so
Buffer.from(new Uint32Array([0x4701c993]))
returns a buffer with a length of1
and a value in index0
of0x93
.This is a bad conversion and seems obviously wrong to me. An explicit 32-bit type was provided, and not just
[0x4701c993]
(for which the docs says interpreted as octets).I don't possibly see how this could be the desired behavior, the resulting values are nothing like the input at all. But, here's the spec: https://www.ecma-international.org/ecma-262/6.0/#sec-touint8
Also, the
Buffer#write<U><Type><Size><endian>()
APIs help little, since they are awkwardly designed when used for such a purpose. (They return bytes written and not the buffer.) Presumably, if I had a whole array of int32s I'd have to write some kind of custom converter, which seems wrong considering we are converting to a runtime standard object from a primitive type.Suggestion: fix this somehow, or, provide a nicer API to do a reasonable conversion.
The text was updated successfully, but these errors were encountered: