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

doc: Uint8Array support in Buffer functions #19949

Closed
wants to merge 1 commit into from
Closed

doc: Uint8Array support in Buffer functions #19949

wants to merge 1 commit into from

Conversation

SheetJSDev
Copy link
Contributor

Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Checklist

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. doc Issues and PRs related to the documentations. labels Apr 11, 2018
@vsemozhetbyt vsemozhetbyt added the fast-track PRs that do not need to wait for 48 hours to land. label Apr 11, 2018
@@ -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.
Copy link
Member

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 BufferorTypedArray` to copy data from.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@addaleax addaleax removed the fast-track PRs that do not need to wait for 48 hours to land. label Apr 11, 2018
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt left a 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)

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing period: from -> from.

Copy link
Member

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

@@ -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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

@Trott Trott left a 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!

@SheetJSDev
Copy link
Contributor Author

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(array) text, since the array case applies the same sort of coercion:

> 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.

@jasnell
Copy link
Member

jasnell commented Apr 12, 2018

I'm good with that!

@Trott
Copy link
Member

Trott commented Apr 12, 2018

@nodejs/documentation What should we do about the type? I don't think adding {array-like} passes the smell test for me. We don't do that anywhere else in our docs. But what should we do in this case?

@@ -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)`]
Copy link
Member

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.

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 12, 2018

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.

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

@Trott Trott left a 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.

@SheetJSDev
Copy link
Contributor Author

@Trott feel free to tinker with the commit.

On the issue of "array-like", Buffer.from knows about array-like objects: https://github.com/nodejs/node/blob/master/lib/buffer.js#L234

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":

$ grep "Array-like Object" -RiIl lib
lib/buffer.js

@Trott
Copy link
Member

Trott commented Apr 12, 2018

@SheetJSDev I'm OK with the term array-like in prose. What gives me pause is it's use as a type. The types we use are finite: {any}, {string}, {object}, etc. We don't use {array-like} in the listings of types for arguments anywhere else, so this would be a first. And that seems potentially problematic to me. But maybe that's just me. I suspect it's also the only place we'd be using an adjective for a type rather than a noun. It seems awkward to me, but I can live with it if it's the best descriptor we can come up with that accurately communicates what's going on.

@vsemozhetbyt
Copy link
Contributor

@Trott
Copy link
Member

Trott commented Apr 14, 2018

Copy link
Member

@devsnek devsnek left a 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
@Trott
Copy link
Member

Trott commented Apr 20, 2018

@Trott
Copy link
Member

Trott commented Apr 20, 2018

Landed in 2652ab3.

Thanks for the contribution! 🎉

@Trott Trott closed this Apr 20, 2018
Trott pushed a commit to Trott/io.js that referenced this pull request Apr 20, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: nodejs#14118

PR-URL: nodejs#19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
jasnell pushed a commit that referenced this pull request Apr 20, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
@ChALkeR
Copy link
Member

ChALkeR commented Jun 29, 2018

@nodejs/lts @MylesBorins It might be worth to land this on v8.x LTS.
The behavior is explicitly there since beca324 (but it also worked before that).

@MylesBorins
Copy link
Contributor

@ChALkeR the commit does not land cleanly, could you open a backport?

@ChALkeR
Copy link
Member

ChALkeR commented Jun 29, 2018

@MylesBorins #21590 — will that do?

MylesBorins pushed a commit that referenced this pull request Jun 29, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Backport-PR-URL: #21590
PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
MylesBorins pushed a commit that referenced this pull request Jul 9, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Backport-PR-URL: #21590
PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Jul 9, 2018
MylesBorins pushed a commit that referenced this pull request Jul 10, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Backport-PR-URL: #21590
PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
rvagg pushed a commit that referenced this pull request Aug 16, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Backport-PR-URL: #21590
PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
rvagg pushed a commit that referenced this pull request Aug 16, 2018
Buffer.from / new Buffer accept Uint8Array

Fixes: #14118

Backport-PR-URL: #21590
PR-URL: #19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this pull request May 5, 2024
Buffer.from / new Buffer accept Uint8Array

Fixes: nodejs/node#14118

PR-URL: nodejs/node#19949
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
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. doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants