You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Documentation for creating a Buffer which shares a TypedArray's memory is misleading #31348
The documentation for Bufferreads, in part (abbreviated for clarity):
It is possible to create a new Buffer that shares the same allocated memory as a TypedArray instance by using the TypedArray object's .buffer property.
This works in this case, but I think it's hazardous advice for arbitrary TypedArrays because the backing ArrayBuffer could extend beyond the bounds of the TypedArray which is providing a view of it. For example:
constarrA=Uint8Array.from([0x63,0x64,0x65,0x66])// 4 elementsconstarrB=newUint8Array(arrA.buffer,1,2)// 2 elementsconsole.log(arrA.buffer===arrB.buffer)// trueconstbuf=Buffer.from(arrB.buffer)console.log(buf)// expected, based on documented advice: <Buffer 64 65>// actual: <Buffer 63 64 65 66>
It might be better to have the docs suggest Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength) as the general solution to this problem.
Activity
targos commentedon Dec 26, 2020
I agree. Would you like to open a pull request?
doc: clarify Buffer.from when using ArrayBuffer
doc: clarify Buffer.from when using ArrayBuffer
doc: clarify Buffer.from when using ArrayBuffer