node
node copied to clipboard
buffer: make `Buffer.from` throw on `DataView`
Passing DataView
instance or arbitrary object with any ArrayBuffer in its buffer
property and without numeric length
property results in a new empty Buffer
.
This behaviour isn't documented, and is confusing since buffer
property is checked but not used.
CI: https://ci.nodejs.org/job/node-test-pull-request/52194/
This is a breaking change right? I think we should add semver-major label.
CI: https://ci.nodejs.org/job/node-test-pull-request/52207/
CI: https://ci.nodejs.org/job/node-test-pull-request/52211/
CI: https://ci.nodejs.org/job/node-test-pull-request/52252/
As far as I can tell, the existing behavior of
Buffer.from()
is compatible withUint8Array.from()
in this regard, and sinceBuffer
extendsUint8Array
, it seems wrong to intentionally deviate from spec'd behavior.
The specs of both 23.2.2.1 %TypedArray%.from()
and 23.2.5.1 TypedArray
constructor don't mention buffer
property nor DataView
.
Uint8Array.from()
returns empty buffer for arbitrary argument (as long as it's not null
):
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({ buffer: new ArrayBuffer(8) }));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(new DataView(new ArrayBuffer(8))));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({ buffer: null }));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({}));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(123));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(123n));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(true));
Does that mean that Buffer.from()
should do the same?
Does that mean that
Buffer.from()
should do the same?
I don't know. I am not generally in favor of adopting more web APIs where they don't make sense, but intentionally reducing compatibility with web APIs (as in this PR) does not seem helpful to me either.