-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
buffer: add from(ArrayBufferView
) support
#43863
buffer: add from(ArrayBufferView
) support
#43863
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests with TypedArray
s that have a byteOffset
that is not 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current behavior appears to be consistent with new Uint8Array(typedArray)
and Uint8Array.from(typedArray)
(except for 64-bit integers, but that should not be difficult to fix).
> Buffer.from(Uint16Array.from([1, 0xff, 0xffff]))
<Buffer 01 ff ff>
> Uint8Array.from(Uint16Array.from([1, 0xff, 0xffff]))
Uint8Array(3) [ 1, 255, 255 ]
The behavior proposed here appears to go directly against the design of TypedArray.from()
and TypedArray
constructors.
> Buffer.from(Uint16Array.from([1, 0xff, 0xffff]))
<Buffer 01 00 ff 00 ff ff>
> Uint8Array.from(Uint16Array.from([1, 0xff, 0xffff]))
Uint8Array(3) [ 1, 255, 255 ]
I don't think we should make breaking changes that further increase the gap between Buffer
and Uint8Array
.
That's a good point. I think, compatibility between
The fact that any If we do care about this compatibility, the documentation requires adjustments because special casing for Independently from that, we can add a separate method like |
@tniessen did #43863 (comment) convinced you to change your mind or are you still blocking it? To be clear, I haven't looked into it myself (I'm simply going over the open
semver-major
|
Thanks @aduh95, I should have replied here earlier.
Yes. This is a breaking change to a stable API that, as far as I am aware, no users have asked for, and which goes directly against the design of the base class
It can certainly be a footgun. But the same thing can be said about platform-dependent byte representations of multi-byte data types, and the behavior proposed here can also easily and explicitly be done in userland. |
Superseded by better alternative w/o breaking changes: #46500. Thanks for the reviews! |
I had completely missed this! My apologies for duplicating the work. |
Fixes: #43862
CITGM with
Buffer.from
throwingSyntaxError
whenisArrayBufferView(value) && !(value instanceof Uint8Array)
: https://ci.nodejs.org/job/citgm-smoker/2970/