Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
buffer: add from(ArrayBufferView) support
  • Loading branch information
LiviaMedeiros committed Jul 16, 2022
commit 356a0f684bc98cbce3051e4a18d8acdd428661c9
12 changes: 8 additions & 4 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1207,10 +1207,14 @@ console.log(buf);

<!-- YAML
added: v5.10.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43863
description: The `buffer` parameter can now be an any `ArrayBufferView`.
-->

* `buffer` {Buffer|Uint8Array} An existing `Buffer` or [`Uint8Array`][] from
which to copy data.
* `buffer` {Buffer|ArrayBufferView} An existing `Buffer`, [`TypedArray`][],
or [`DataView`][] from which to copy data.

Copies the passed `buffer` data onto a new `Buffer` instance.

Expand Down Expand Up @@ -1242,8 +1246,8 @@ console.log(buf2.toString());
// Prints: buffer
```

A `TypeError` will be thrown if `buffer` is not a `Buffer` or another type
appropriate for `Buffer.from()` variants.
A `TypeError` will be thrown if `buffer` is not an `ArrayBufferView`
or another type appropriate for `Buffer.from()` variants.

### Static method: `Buffer.from(object[, offsetOrEncoding[, length]])`

Expand Down
12 changes: 11 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const {
const {
isAnyArrayBuffer,
isArrayBufferView,
isUint8Array
isTypedArray,
isUint8Array,
} = require('internal/util/types');
const {
inspect: utilInspect
Expand Down Expand Up @@ -306,6 +307,15 @@ Buffer.from = function from(value, encodingOrOffset, length) {
if (isAnyArrayBuffer(value))
return fromArrayBuffer(value, encodingOrOffset, length);

if (isArrayBufferView(value) && !isUint8Array(value)) {
if (!isTypedArray(value)) {
// DataView is not a TypedArray
return fromArrayBuffer(value.buffer.slice(value.byteOffset,
value.byteOffset + value.byteLength));
}
return fromArrayBuffer(value.slice().buffer);
}

const valueOf = value.valueOf && value.valueOf();
if (valueOf != null &&
valueOf !== value &&
Expand Down
66 changes: 58 additions & 8 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,72 @@ assert.strictEqual(d.length, 0);
}

// Test creating a Buffer from a Uint32Array
// Note: it is implicitly interpreted as Array of integers modulo 256
{
const ui32 = new Uint32Array(4).fill(42);
const e = Buffer.from(ui32);
for (const [index, value] of e.entries()) {
assert.strictEqual(value, ui32[index]);
}
assert.deepStrictEqual(e, Buffer.from([
42, 0, 0, 0,
42, 0, 0, 0,
42, 0, 0, 0,
42, 0, 0, 0,
]));
}
// Test creating a Buffer from a Uint32Array (old constructor)
// Note: it is implicitly interpreted as Array of integers modulo 256
{
const ui32 = new Uint32Array(4).fill(42);
const e = Buffer(ui32);
for (const [key, value] of e.entries()) {
assert.strictEqual(value, ui32[key]);
}
assert.deepStrictEqual(e, Buffer.from([
42, 0, 0, 0,
42, 0, 0, 0,
42, 0, 0, 0,
42, 0, 0, 0,
]));
}

// Test creating a Buffer from a BigUint64Array
{
const bui64 = new BigUint64Array(4).fill(42n);
const e = Buffer.from(bui64);
assert.deepStrictEqual(e, Buffer.from([
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
]));
}
// Test creating a Buffer from a BigUint64Array (old constructor)
{
const bui64 = new BigUint64Array(4).fill(42n);
const e = Buffer(bui64);
assert.deepStrictEqual(e, Buffer.from([
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
42, 0, 0, 0, 0, 0, 0, 0,
]));
}

// Test creating a Buffer from a Float64Array
{
const f64 = new Float64Array(4).fill(42);
const e = Buffer.from(f64);
assert.deepStrictEqual(e, Buffer.from([
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
]));
}
// Test creating a Buffer from a Float64Array (old constructor)
{
const f64 = new Float64Array(4).fill(42);
const e = Buffer(f64);
assert.deepStrictEqual(e, Buffer.from([
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
0, 0, 0, 0, 0, 0, 69, 64,
]));
}

// Test invalid encoding for Buffer.toString
Expand Down