-
Notifications
You must be signed in to change notification settings - Fork 30k
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
doc: Uint8Array support in Buffer functions #19949
Conversation
doc/api/buffer.md
Outdated
@@ -402,7 +402,7 @@ changes: | |||
|
|||
> Stability: 0 - Deprecated: Use [`Buffer.from(buffer)`] instead. | |||
|
|||
* `buffer` {Buffer} An existing `Buffer` to copy data from. | |||
* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`] to copy data from. |
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.
I would make this {Buffer|TypedArray}
and An existing
Bufferor
TypedArray` to copy data from.
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.
Still not a fan of that… as you noted in the other PR, this has semantics which match Buffer.from(array)
, which we document as a different overload, and similarly has a completely different performance profile from Uint8Array
.
I’d be okay with it if we were to merge those two headings together, but that seems out of scope for this PR.
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.
@addaleax The problem with citing Buffer.from(array)
is the last line:
A TypeError will be thrown if array is not an Array.
Typed arrays aren't Arrays :/
If it is agreed that Buffer.from
should accept general typed arrays, the behavior should be documented. It might make sense to just introduce a new section Buffer.from(typedarray)
. In any case, I think that's outside the scope of this PR.
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.
A TypeError will be thrown if array is not an Array.
Well that’s just a plain lie in the docs. 😄 Even Buffer.from({length: 4})
works perfectly – really all that matters is that .length
is not undefined
. If you prefer it, that can also be taken care of at a later point/by somebody else.
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.
Probably better to do it later with fresh eyes. The other Buffer functions should also be revisited.
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.
@SheetJSDev Thank you!
Mentioned nits can be fixed by a lander if it is not convenient for you)
doc/api/buffer.md
Outdated
@@ -842,7 +842,7 @@ A `TypeError` will be thrown if `arrayBuffer` is not an [`ArrayBuffer`] or a | |||
added: v5.10.0 | |||
--> | |||
|
|||
* `buffer` {Buffer} An existing `Buffer` to copy data from. | |||
* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`] to copy data from |
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.
Missing period: from
-> from.
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.
to coy data from
-> from which to copy data
doc/api/buffer.md
Outdated
@@ -402,7 +402,7 @@ changes: | |||
|
|||
> Stability: 0 - Deprecated: Use [`Buffer.from(buffer)`] instead. | |||
|
|||
* `buffer` {Buffer} An existing `Buffer` to copy data from. | |||
* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`] to copy data from. |
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 doc has many violations of 'wrap after 80 characters' rule and we make an exception for it now, but maybe it is worth to not add more. So this and next changed line can be wrapped.
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.
@vsemozhetbyt after facing issues earlier today with github not refreshing PRs, it's probably better for a lander to amend the commit.
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.
to copy data from
-> from which to copy data
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.
Hi, @SheetJSDev! Welcome and thanks for the PR! Documentation PRs can attract a lot of comments. Don't get discouraged!
Updated commit, incorporating @Trott 's recommendation as well as reworking lines that violated the 80-character limit. @jasnell @addaleax on second look, the best way to resolve the general Typed Array case is to modify the > Buffer.from([Math.PI])
<Buffer 03>
> Buffer.from(new Float32Array([Math.PI]))
<Buffer 03> One possible definition: ### Class Method: Buffer.from(array)
* `array` {array-like}
Allocates a new `Buffer` of length `array.length`. Values from `array` are
converted to unsigned 8-bit integers.
<example here> Given that general TypedArrays are array-like, it would be covered in this case. |
I'm good with that! |
@nodejs/documentation What should we do about the type? I don't think adding |
doc/api/buffer.md
Outdated
@@ -81,7 +80,7 @@ to one of these new APIs.* | |||
|
|||
* [`Buffer.from(array)`] returns a new `Buffer` that *contains a copy* of the | |||
provided octets. | |||
* [`Buffer.from(arrayBuffer[, byteOffset [, length]])`][`Buffer.from(arrayBuffer)`] | |||
* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`] |
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 whitespace change on this line makes this formatted differently from all our other docs. This change appears to be just to keep things to wrapping at 80 characters. I would undo it and not worry about the 80 character wrapping on this line in this PR. It's good to take care of lines that you are already editing for other reasons, and we often encourage doing so for nearby lines too. But to add 80-char wrapping to the entire doc with this otherwise narrow change is probably not the way to go. For one thing, it will make backporting more difficult.
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.
Was not this a typo space in this exact case? Space before comma seems uncommon. There were some in this doc, but not everywhere.
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.
@vsemozhetbyt Yes, you're right. Not sure about shortening arrayBuffer
to arrayBuf
but then again, we already use buf
elsewhere for a Buffer
instance, so why not? :-D
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.
I'm convinced the whitespace was in error. If you search https://nodejs.org/api/all.html for [,
, you will find 9 instances, corresponding to this function and to socket.send(msg, [offset, length,] port [, address] [, callback])
. I think the socket.send
case has whitespace because both address
and callback
are optional, and it would be very confusing to read without the whitespace.
The shortened arrayBuf
occurs in the link marker, not in the actual body text, so it won't affect the HTML output. IMHO it's an innocuous change.
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.
Yup, I'm on board with this now. Sorry for the noise.
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.
Left some micro-nits but looks good. Really close, I think.
@Trott feel free to tinker with the commit. On the issue of "array-like", You can elicit that error with the following corner case: > Buffer.from(({valueOf:()=>{}}))
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object Searching in the source tree reveals that it is the only error message that uses the phrase "Array-like Object":
|
@SheetJSDev I'm OK with the term |
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.
nice to see you around friend :D
Buffer.from / new Buffer accept Uint8Array Fixes: #14118
Landed in 2652ab3. Thanks for the contribution! 🎉 |
Buffer.from / new Buffer accept Uint8Array Fixes: nodejs#14118 PR-URL: nodejs#19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
@nodejs/lts @MylesBorins It might be worth to land this on v8.x LTS. |
@ChALkeR the commit does not land cleanly, could you open a backport? |
@MylesBorins #21590 — will that do? |
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 Backport-PR-URL: #21590 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 Backport-PR-URL: #21590 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 Backport-PR-URL: #21590 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 Backport-PR-URL: #21590 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: #14118 Backport-PR-URL: #21590 PR-URL: #19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array Fixes: nodejs/node#14118 PR-URL: nodejs/node#19949 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Buffer.from / new Buffer accept Uint8Array
Fixes: #14118
Checklist