-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: add documentation for buffer.byteOffset #21718
The head ref may contain hidden characters: "doc\u2013buffer-byteOffset"
Conversation
Also document a common issue when casting a Buffer object to a TypedArray object. Fixes: #19301
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.
With some nits)
doc/api/buffer.md
Outdated
@@ -992,6 +992,31 @@ console.log(buffer.buffer === arrayBuffer); | |||
// Prints: true | |||
``` | |||
|
|||
### buf.byteOffset | |||
|
|||
* {integer} The byteOffset on the underlying `ArrayBuffer` object based on |
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.
byteOffset
-> `byteOffset`
or byte offset
?
doc/api/buffer.md
Outdated
using `buf.buffer`, as the first bytes in this `ArrayBuffer` may be unrelated | ||
to the `buf` object itself. | ||
|
||
A common issue is when casting a `Buffer` object to an `TypedArray` object, |
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.
an `TypedArray`
-> a `TypedArray`
.
doc/api/buffer.md
Outdated
in this case one needs to specify the `byteOffset` correctly: | ||
|
||
```js | ||
// create a buffer smaller than `Buffer.poolSize` |
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.
- create -> Create?
- A period in the end?
doc/api/buffer.md
Outdated
// create a buffer smaller than `Buffer.poolSize` | ||
const nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
|
||
// When casting the node.js Buffer to a Int8 TypedArray remember to use the |
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.
- node.js -> Node.js
- a Int8 -> an Int8
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.
Thanks for documenting how byteOffset
is used, it's definitely something many people miss.
The complication here is that we don't document any of the other properties inherited from Uint8Array
or %TypedArray%
(except of
, for which we supply our own implementation). Case in point is the byteLength
property, which provides a uniform way to check the actual byte size of the array for any ArrayBuffer
views and is encouraged for use in certain circumstances, even though it is equivalent to buf.length
.
I'd prefer a caveat be put on the overload of Buffer.from
that takes an ArrayBuffer
, reminding people to supply an appropriate byteOffset
and byteLength
. Additionally I'd prefer the use of byteLength
over buf.length
as it is useful for making sure the code works for not just Buffer
but any other typed array types.
I don't think that is a complication. I also don't know the in what "certain circumstances" it is "encouraged" so I'm not really suited to document this. Feel free to open a PR yourself.
I would consider that an anti-pattern, since
I honestly think that adds more confusion than value. The reader would wonder why |
In core at least we try to make all functions that take
There seems to be a miscommunication here. My understanding was that the former is a reference to > aBuf = Buffer.alloc(10)
<Buffer 00 00 00 00 00 00 00 00 00 00>
> bBuf = Buffer.from(aBuf)
<Buffer 00 00 00 00 00 00 00 00 00 00>
> cBuf = Buffer.from(aBuf.buffer, aBuf.byteOffset, aBuf.byteLength)
<Buffer 00 00 00 00 00 00 00 00 00 00>
> aBuf[0] = 1
1
> aBuf
<Buffer 01 00 00 00 00 00 00 00 00 00>
> bBuf
<Buffer 00 00 00 00 00 00 00 00 00 00>
> cBuf
<Buffer 01 00 00 00 00 00 00 00 00 00> My concern (which seems to be the same that drove you to open the PR) is that I've seen many people who use
Fair point. |
That is fair. I will document that, but under edit: on second thought, why wouldn't you use |
I have no strong opinion about adding it or not. But the change itself is LGTM with the comments addressed. |
Landed in b70367e |
Also document a common issue when casting a Buffer object to a TypedArray object. Fixes: #19301 PR-URL: #21718 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Also document a common issue when casting a Buffer object to a TypedArray object. Fixes: #19301 PR-URL: #21718 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Also document a common issue when casting a Buffer object to a
TypedArray object.
Fixes: #19301
Checklist