-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
doc: Uint8Array support in Buffer functions #19948
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
Closed
+2
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
This should likely be
Buffer|TypedArraywith the descriptionAn existingBufferorTypedArray` instance to copy from.It's worth noting that any
TypedArraycan be used, not justUint8Array. The issue, however, is that as values are copied, each will be coerced to an 8-byte value. For instance,Buffer.from(new Uint32Array([0xffff, 2, 3])will produce aBufferwith values[0xff, 2, 3]. Likewise,Buffer.from(new Float32Array([1.23, 2, 3]))will yield aBufferwith values[1, 2, 3].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.
@jasnell That’s more or less already documented as https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array – we accept anything with a
lengthproperty, basically.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.
Oh sorry, I meant just worth noting in this thread, not in the docs. My mistake, I wasn't clear about that at all. The change here that I'd like to see is just listing
TypedArrayinstead ofUint8Arrayin the bullet point.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.
@jasnell the body language may need some clarification if the signature mentions general Typed Arrays:
For a typed array that isn't a
Uint8Array, does the "data" refer to the typed values or the.bufferdata? e.g.This issue is obviated in the Uint8Array case but is relevant for other typed arrays.
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.
Yeah, exactly: Other kinds of
TypedArrays are treated just like regularArrays. Hence my comment above – I think it would make more sense to documentTypedArrayas part of theBuffer.from(array)heading.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
Buffer.from(new Float32Array([Math.PI]).buffer)variant is already covered in the documentation by theClass Method: Buffer.from(arrayBuffer[, byteOffset[, length]])section. In that specific case, when anArrayBufferinstance is passed, theBufferreturned is a view over the existingArrayBufferand no data is copied or coerced.The
Buffer.from(new Float32Array([Math.PI]))case is covered by theClass Method: Buffer.from(buffer)variant but the doc for that specific variant only needs to be indicate that anyTypedArraycan be passed but that each typed value will be copied and coerced into 8-bit values in the returnedBuffer.The
Class Method: Buffer.from(buffer)andClass Method: Buffer.from(array)variants can be merged into a single section as the behavior for each is generally identical.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.
Local state was messed up, resubmitted PR in #19949 . I agree with @addaleax 's suggestion to clarify the typed array situation elsewhere, but that is a separate discussion
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.
Also note that
Buffer.from(new Uint8Array([1,2,3]))is treated exactly the same asBuffer.from([1,2,3])... that is, the newBuffercontains a copy of the original.while
Buffer.from((new Uint8Array([1,2,3])).buffer)returns a newBufferthat contains a view over the same backingArrayBuffer. No data is copied.