From 5a85bc3d32f918b193635ec85f8245d569a01c1e Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 2 Nov 2021 14:24:41 -0700 Subject: [PATCH 1/8] fix: Blob constructor on various ArrayBuffer views Fixes #40705 --- lib/internal/blob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 5fb25e04fe1abe..6055ead01e7633 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -123,7 +123,7 @@ function getSource(source, endings) { // 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); + source = new Uint8Array(source.buffer.slice(0), source.byteOffset); return [source.byteLength, source]; } From cf3abfd7ee16c643b9dd9e1f020f1db4d69feca0 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 2 Nov 2021 14:32:36 -0700 Subject: [PATCH 2/8] fix: only copy necessary bytes --- lib/internal/blob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 6055ead01e7633..da7d2b1635a0e9 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -123,7 +123,7 @@ function getSource(source, endings) { // 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.buffer.slice(0), source.byteOffset); + source = new Uint8Array(source.buffer.slice(source.byteOffset)); return [source.byteLength, source]; } From ccc06b72f3e87aff7e8afa2b50b4e8395079ac49 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 2 Nov 2021 21:57:24 +0000 Subject: [PATCH 3/8] chore: add test case --- test/parallel/test-blob.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 2ac91ac99c9bcc..01025ebc56eb90 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -220,3 +220,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.equal(blob.text(), "PASSPASSPASSPASSPASSPASSPASS"); + assert.equal(blob.type, ""); +})().then(common.mustCall()); From bcab31ed78586211c6f5924102b1f7748912f59e Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 3 Nov 2021 05:03:33 +0000 Subject: [PATCH 4/8] fix: slice buffer from both sides --- lib/internal/blob.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index da7d2b1635a0e9..b1c2a6e155edf6 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -121,10 +121,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.buffer.slice(source.byteOffset)); - 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 { From 95ca1742f274d535aa05c2da51863e874f4d1b96 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 3 Nov 2021 08:57:24 -0700 Subject: [PATCH 5/8] Update test/parallel/test-blob.js --- test/parallel/test-blob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 01025ebc56eb90..22d25da4f45453 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -229,7 +229,7 @@ assert.throws(() => new Blob({}), { new Int16Array([0x4150, 0x5353]), new Uint32Array([0x53534150]), new Int32Array([0x53534150]), - new Float32Array([0xD341500000]) + new Float32Array([0xD341500000]), ]); assert.equal(blob.text(), "PASSPASSPASSPASSPASSPASSPASS"); From 42f3f748f60e10b24c78dc2a70e72c53547e5dd5 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 3 Nov 2021 08:58:24 -0700 Subject: [PATCH 6/8] fix: address lint & test errors --- lib/internal/blob.js | 4 ++-- test/parallel/test-blob.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index b1c2a6e155edf6..462fe3fe6b75b9 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -122,8 +122,8 @@ function getSource(source, endings) { // We copy into a new Uint8Array because the underlying // BackingStores are going to be detached and owned by // the Blob. - const { buffer, byteOffset, byteLength } = source - const slice = buffer.slice(byteOffset, byteOffset + byteLength) + const { buffer, byteOffset, byteLength } = source; + const slice = buffer.slice(byteOffset, byteOffset + byteLength); return [byteLength, new Uint8Array(slice)]; } diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 22d25da4f45453..6f7dac2fe9875f 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -232,6 +232,6 @@ assert.throws(() => new Blob({}), { new Float32Array([0xD341500000]), ]); - assert.equal(blob.text(), "PASSPASSPASSPASSPASSPASSPASS"); - assert.equal(blob.type, ""); + assert.strictEqual(await blob.text(), "PASSPASSPASSPASSPASSPASSPASS"); + assert.strictEqual(blob.type, ''); })().then(common.mustCall()); From 9791e08cd9b8bb454b14840dc0d64bf8d3a0a126 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 3 Nov 2021 14:06:22 -0700 Subject: [PATCH 7/8] fix: lint error at test/parallel/test-blob.js --- test/parallel/test-blob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 6f7dac2fe9875f..1eaa232ba1e9ec 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -232,6 +232,6 @@ assert.throws(() => new Blob({}), { new Float32Array([0xD341500000]), ]); - assert.strictEqual(await blob.text(), "PASSPASSPASSPASSPASSPASSPASS"); + assert.strictEqual(await blob.text(), 'PASSPASSPASSPASSPASSPASSPASS'); assert.strictEqual(blob.type, ''); })().then(common.mustCall()); From df6ad221669fbc915b3e706ad767987011e95c1b Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Mon, 8 Nov 2021 12:55:39 -0800 Subject: [PATCH 8/8] chore: fix test failures for big endian hosts --- test/parallel/test-blob.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 1eaa232ba1e9ec..6a011dd250c21d 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -232,6 +232,6 @@ assert.throws(() => new Blob({}), { new Float32Array([0xD341500000]), ]); - assert.strictEqual(await blob.text(), 'PASSPASSPASSPASSPASSPASSPASS'); + assert.strictEqual(blob.size, 28); assert.strictEqual(blob.type, ''); })().then(common.mustCall());