From f805d0be957ca652d1128bd106a880281ec7982b Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Sat, 28 Sep 2024 18:05:58 -0400 Subject: [PATCH] buffer: correctly apply prototype to cloned `File` / `Blob` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55138 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/internal/blob.js | 1 + lib/internal/file.js | 1 + test/parallel/test-structuredClone-global.js | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 0c38cd02c4a944..bb9a7d043759d0 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -386,6 +386,7 @@ class Blob { } function TransferableBlob(handle, length, type = '') { + ObjectSetPrototypeOf(this, Blob.prototype); markTransferMode(this, true, false); this[kHandle] = handle; this[kType] = type; diff --git a/lib/internal/file.js b/lib/internal/file.js index 65fec6dfd0c627..46dc96fa1bc6aa 100644 --- a/lib/internal/file.js +++ b/lib/internal/file.js @@ -130,6 +130,7 @@ class File extends Blob { function TransferableFile(handle, length, type = '') { FunctionPrototypeApply(TransferableBlob, this, [handle, length, type]); + ObjectSetPrototypeOf(this, File.prototype); } ObjectSetPrototypeOf(TransferableFile.prototype, File.prototype); diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index 50fcf6047828a8..34b6abe32d3fcf 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -30,6 +30,19 @@ for (const StreamClass of [ReadableStream, WritableStream, TransformStream]) { assert.ok(extendedTransfer instanceof StreamClass); } +for (const Transferrable of [File, Blob]) { + const a2 = Transferrable === File ? '' : {}; + const original = new Transferrable([], a2); + const transfer = structuredClone(original); + assert.strictEqual(Object.getPrototypeOf(transfer), Transferrable.prototype); + assert.ok(transfer instanceof Transferrable); + + const extendedOriginal = new (class extends Transferrable {})([], a2); + const extendedTransfer = structuredClone(extendedOriginal); + assert.strictEqual(Object.getPrototypeOf(extendedTransfer), Transferrable.prototype); + assert.ok(extendedTransfer instanceof Transferrable); +} + { // See: https://github.com/nodejs/node/issues/49940 const cloned = structuredClone({}, {