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
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.
The text was updated successfully, but these errors were encountered:
The documentation for
Buffer
reads, in part (abbreviated for clarity):This works in this case, but I think it's hazardous advice for arbitrary
TypedArray
s because the backingArrayBuffer
could extend beyond the bounds of theTypedArray
which is providing a view of it. For example:It might be better to have the docs suggest
Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength)
as the general solution to this problem.The text was updated successfully, but these errors were encountered: