buffer: add from(ArrayBufferView) support#43863
buffer: add from(ArrayBufferView) support#43863LiviaMedeiros wants to merge 3 commits intonodejs:mainfrom
ArrayBufferView) support#43863Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
aduh95
left a comment
There was a problem hiding this comment.
Can you add tests with TypedArrays that have a byteOffset that is not 0?
tniessen
left a comment
There was a problem hiding this comment.
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.fromthrowingSyntaxErrorwhenisArrayBufferView(value) && !(value instanceof Uint8Array): https://ci.nodejs.org/job/citgm-smoker/2970/