Skip to content

Commit

Permalink
buffer: correctly apply prototype to cloned File / Blob
Browse files Browse the repository at this point in the history
PR-URL: #55138
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
RedYetiDev committed Sep 28, 2024
1 parent e7d2732 commit f805d0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/internal/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}, {
Expand Down

0 comments on commit f805d0b

Please sign in to comment.