• <integer> Buffer 底层的 ArrayBuffer 对象的 byteOffset

Buffer.from(ArrayBuffer, byteOffset, length) 设置了 byteOffset 或创建一个小于 Buffer.poolSizeBuffer 时,底层的 ArrayBuffer 的偏移量并不是从 0 开始。

当直接使用 buf.buffer 访问底层的 ArrayBuffer 时,ArrayBuffer 的第一个字节可能并不指向 buf 对象。

所有使用 Buffer 创建 TypedArray 时,需要正确地指定 byteOffset

// 创建一个小于 `Buffer.poolSize` 的 `Buffer`。
const nodeBuffer = new Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

// 将 `Buffer` 赋值给一个 `Int8Array`。
new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);
  • <integer> The byteOffset on the underlying ArrayBuffer object based on which this Buffer object is created.

When setting byteOffset in Buffer.from(ArrayBuffer, byteOffset, length) or sometimes when allocating a buffer smaller than Buffer.poolSize the buffer doesn't start from a zero offset on the underlying ArrayBuffer.

This can cause problems when accessing the underlying ArrayBuffer directly 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 a TypedArray object, in this case one needs to specify the byteOffset correctly:

// 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 an Int8 TypedArray remember to use the
// byteOffset.
new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length);