Skip to content

DataView doesn't work with Buffer from pool #2888

@skomski

Description

@skomski

I wanted to simplify the various .read{Type} or .write{Type} by using a DataView implementation and
found out that is not possible to use the Buffer's ArrayBuffer if the buffer was sliced from the pool.

const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer;

const buffer = new Buffer(4).fill(0);
new DataView(buffer.buffer).setFloat32(0, 1, true);
console.log(buffer.buffer.byteLength);
console.log(buffer);

const slowBuffer = new SlowBuffer(4).fill(0);
new DataView(slowBuffer.buffer).setFloat32(0, 1, true);
console.log(slowBuffer.buffer.byteLength);
console.log(slowBuffer);

assert.deepEqual(buffer, slowBuffer);

results into

8192
<Buffer 00 00 00 00>
4
<Buffer 00 00 80 3f>

AssertionError: <Buffer 00 00 00 00> deepEqual <Buffer 00 00 80 3f>

The problem is the underlying ArrayBuffer for the new sliced TypedArray does not change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions