From 28761de6d46ad6bcdd8d754b9ccfcfe77ba47a2b Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Fri, 10 Dec 2021 15:36:20 -0800 Subject: [PATCH] buffer: fix `Blob` constructor on various `TypedArray`s Fixes: https://github.com/nodejs/node/issues/40705 PR-URL: https://github.com/nodejs/node/pull/40706 Reviewed-By: Robert Nagy Reviewed-By: Antoine du Hamel Reviewed-By: Minwoo Jung Reviewed-By: James M Snell --- lib/internal/blob.js | 8 ++++---- test/parallel/test-blob.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 7edc66a7046e45..b4b3da8d604286 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -114,10 +114,10 @@ function getSource(source, endings) { // We copy into a new Uint8Array because the underlying // BackingStores are going to be detached and owned by - // the Blob. We also don't want to have to worry about - // byte offsets. - source = new Uint8Array(source); - return [source.byteLength, source]; + // the Blob. + const { buffer, byteOffset, byteLength } = source; + const slice = buffer.slice(byteOffset, byteOffset + byteLength); + return [byteLength, new Uint8Array(slice)]; } class Blob { diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 39301e85c9c9b6..53b9ddb0cb3e81 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -229,3 +229,18 @@ assert.throws(() => new Blob({}), { }); }); } + +(async () => { + const blob = new Blob([ + new Uint8Array([0x50, 0x41, 0x53, 0x53]), + new Int8Array([0x50, 0x41, 0x53, 0x53]), + new Uint16Array([0x4150, 0x5353]), + new Int16Array([0x4150, 0x5353]), + new Uint32Array([0x53534150]), + new Int32Array([0x53534150]), + new Float32Array([0xD341500000]), + ]); + + assert.strictEqual(blob.size, 28); + assert.strictEqual(blob.type, ''); +})().then(common.mustCall());