From cf6061e81ec898e07c35215cb82f3ec0d6c6cc2a Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Fri, 19 Jan 2018 16:06:03 +0100 Subject: [PATCH] Node.js also supports Buffer creation from {buffer: ArrayBuffer} --- index.js | 2 +- test/node/test-buffer-alloc.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 0ee40c1..bc48ecc 100644 --- a/index.js +++ b/index.js @@ -104,7 +104,7 @@ function from (value, encodingOrOffset, length) { throw new TypeError('"value" argument must not be a number') } - if (isArrayBuffer(value)) { + if (isArrayBuffer(value) || (value && isArrayBuffer(value.buffer))) { return fromArrayBuffer(value, encodingOrOffset, length) } diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index 8b3e52b..f8137bd 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -999,9 +999,8 @@ assert.doesNotThrow(() => Buffer.from(new ArrayBuffer())); // Test that ArrayBuffer from a different context is detected correctly var arrayBuf = vm.runInNewContext('new ArrayBuffer()'); assert.doesNotThrow(() => Buffer.from(arrayBuf)); -/* assert.doesNotThrow(() => Buffer.from({ buffer: arrayBuf })); - +/* assert.throws(() => Buffer.alloc({ valueOf: () => 1 }), /"size" argument must be of type number/); assert.throws(() => Buffer.alloc({ valueOf: () => -1 }), -- 2.34.1