Skip to content
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.from(<ArrayBufferView>) #43862

Closed
LiviaMedeiros opened this issue Jul 16, 2022 · 6 comments
Closed

Buffer.from(<ArrayBufferView>) #43862

LiviaMedeiros opened this issue Jul 16, 2022 · 6 comments
Labels
buffer Issues and PRs related to the buffer subsystem. feature request Issues that request new features to be added to Node.js. never-stale Mark issue so that it is never considered stale

Comments

@LiviaMedeiros
Copy link
Contributor

What is the problem this feature will solve?

Buffer.from() has many different forms, including Buffer.from(arrayBuffer[, byteOffset[, length]]) and Buffer.from(buffer).

However, Buffer.from(abv) does not exist. Depending on exact input, trying to call it with such objects results in false positives, data truncation, data corruption (e.g. double -> unsigned char), thrown TypeErrors and empty buffers.

Creating Buffer from ArrayBufferView's data in userland is extremely error-prone because Buffer.from(arrayBuffer) doesn't copy the data.

What is the feature you are proposing to solve the problem?

Adjust Buffer.from(buffer) to accept not only Buffer and Uint8Array but any ArrayBufferView or at least any TypedArray.

What alternatives have you considered?

Deprecation and removal of Buffer from core into userspace module, with prior porting all core APIs to native Uint8Array/TypedArray/ArrayBufferViews.

@LiviaMedeiros LiviaMedeiros added feature request Issues that request new features to be added to Node.js. buffer Issues and PRs related to the buffer subsystem. labels Jul 16, 2022
@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Jan 13, 2023
@jasnell
Copy link
Member

jasnell commented Jan 13, 2023

This should be kept open. It's still needed

@panva panva added never-stale Mark issue so that it is never considered stale and removed stale labels Jan 13, 2023
@addaleax
Copy link
Member

@jasnell Is this actionable, though? Buffer.from(abv) has well-defined semantics right now (even if those include data truncation – it matches Uint8Array.from() in that regard, afaict).

@jasnell
Copy link
Member

jasnell commented Feb 4, 2023

It doesn't need to be Buffer.from(abv) exactly. It can easily be another variation, Buffer.fromTypedArray or such (I'm horrible with naming so other suggestions are welcome)

@addaleax
Copy link
Member

addaleax commented Feb 4, 2023

Yeah, we could do that. It’s probably worth thinking about the name – it should reflect that it creates the buffer from the underlying memory of a TypedArray rather than its contents – and it’s probably still worth thinking about whether this really makes sense to address in Node.js itself (the fact that writing new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength) is verbose and repetitive isn’t really a Node.js-specific thing).

jasnell added a commit to jasnell/node that referenced this issue Feb 4, 2023
@LiviaMedeiros
Copy link
Contributor Author

Agreed with idea of separate method; its existence by itself would also imply that Buffer.from(abv) does something different.

jasnell added a commit to jasnell/node that referenced this issue Feb 5, 2023
jasnell added a commit to jasnell/node that referenced this issue Feb 5, 2023
jasnell added a commit to jasnell/node that referenced this issue Feb 5, 2023
jasnell added a commit to jasnell/node that referenced this issue Feb 25, 2023
targos pushed a commit that referenced this issue Mar 13, 2023
Fixes: #43862
PR-URL: #46500
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit that referenced this issue Mar 14, 2023
Fixes: #43862
PR-URL: #46500
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
danielleadams pushed a commit that referenced this issue Apr 11, 2023
Fixes: #43862
PR-URL: #46500
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. feature request Issues that request new features to be added to Node.js. never-stale Mark issue so that it is never considered stale
Projects
4 participants