Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign updoc: add documentation for buffer.byteOffset #21718
Conversation
This comment has been minimized.
This comment has been minimized.
nodejs-github-bot
added
buffer
doc
labels
Jul 9, 2018
vsemozhetbyt
approved these changes
Jul 9, 2018
|
With some nits) |
| @@ -992,6 +992,31 @@ console.log(buffer.buffer === arrayBuffer); | |||
| // Prints: true | |||
| ``` | |||
|
|
|||
| ### buf.byteOffset | |||
|
|
|||
| * {integer} The byteOffset on the underlying `ArrayBuffer` object based on | |||
This comment has been minimized.
This comment has been minimized.
| 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, |
This comment has been minimized.
This comment has been minimized.
| in this case one needs to specify the `byteOffset` correctly: | ||
|
|
||
| ```js | ||
| // create a buffer smaller than `Buffer.poolSize` |
This comment has been minimized.
This comment has been minimized.
| // 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 |
This comment has been minimized.
This comment has been minimized.
TimothyGu
reviewed
Jul 9, 2018
|
Thanks for documenting how The complication here is that we don't document any of the other properties inherited from I'd prefer a caveat be put on the overload of |
lpinca
approved these changes
Jul 9, 2018
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
That is fair. I will document that, but under edit: on second thought, why wouldn't you use |
jasnell
approved these changes
Jul 10, 2018
This comment has been minimized.
This comment has been minimized.
|
I have no strong opinion about adding it or not. But the change itself is LGTM with the comments addressed. |
cjihrig
approved these changes
Jul 16, 2018
trivikr
approved these changes
Jul 16, 2018
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Landed in b70367e |
AndreasMadsen commentedJul 9, 2018
Also document a common issue when casting a Buffer object to a
TypedArray object.
Fixes: #19301
Checklist