-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Add information about buffer.byteOffset property to Buffer.from(string) documentation #19301
Comments
@bartosz-m - why don't you use: var secondBuffer = Buffer.from(firstBuffer, 0, firstBuffer.length); rather than var secondBuffer = Buffer.from(firstBuffer.buffer, 0, firstBuffer.length); ? the Buffer.from(buffer) API provides convenient way of creating a second buffer. The API which used receives an ArrayBuffer object that is shared by multiple objects, and the |
thanks for comment. I've refactored my code to not use Buffer at all. But in the future I may once again be in need of Buffer, so adding to documentation information that |
@gireeshpunathil, FYI your alternative can actually be more misleading, since the second and third arguments are ignored by I think a dedicated section on how the Buffer pooling works would be awesome. |
@TimothyGu - thanks, I overlooked the agree that the Buffer pooling can be better documented, especially which APIs use pooling and which do not. Current doc covers |
I would suggest |
@bartosz-m Note that this behaviour is not specific to var initial = new Uint8Array([1, 2, 3, 4, 5, 6]);
var first = initial.subarray(2, 4); // Uint8Array [ 3, 4 ]
var second = new Uint8Array(first.buffer, 0, first.length); // Uint8Array [ 1, 2 ] |
I'm aware that it is not specific to |
@nodejs/documentation @nodejs/buffer It would be great if someone could either open a PR or drop in some suggested text here to address #19301 (comment). |
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>
I'm was trying to make this example work:
it didn't worked and I didn't know why until I found that
offset
property of thefirstBuffer
wasn't0
as I would assume. I modify it to use correct offset and problem was solved, but I spend more than hour because there wasn't any info in official docs.It would be good to mention in documentation that result of
Buffer.from(string)
can haveoffset
different than0
The text was updated successfully, but these errors were encountered: