Skip to content

Commit

Permalink
stream: set stream prototype to closest transferable superclass
Browse files Browse the repository at this point in the history
PR-URL: #55067
Fixes: #54603
Refs: #50107
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jazelly authored and targos committed Oct 4, 2024
1 parent 3304bf3 commit bf69ae1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ ObjectDefineProperties(ReadableStream, {
});

function InternalTransferredReadableStream() {
ObjectSetPrototypeOf(this, ReadableStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'ReadableStream';
this[kState] = createReadableStreamState();
Expand Down Expand Up @@ -1226,6 +1227,7 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, {
});

function InternalReadableStream(start, pull, cancel, highWaterMark, size) {
ObjectSetPrototypeOf(this, ReadableStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'ReadableStream';
this[kState] = createReadableStreamState();
Expand Down Expand Up @@ -1253,6 +1255,7 @@ function createReadableStream(start, pull, cancel, highWaterMark = 1, size = ()
}

function InternalReadableByteStream(start, pull, cancel) {
ObjectSetPrototypeOf(this, ReadableStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'ReadableStream';
this[kState] = createReadableStreamState();
Expand Down
1 change: 1 addition & 0 deletions lib/internal/webstreams/transformstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ ObjectDefineProperties(TransformStream.prototype, {
});

function InternalTransferredTransformStream() {
ObjectSetPrototypeOf(this, TransformStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'TransformStream';
this[kState] = {
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/webstreams/writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ ObjectDefineProperties(WritableStream.prototype, {
});

function InternalTransferredWritableStream() {
ObjectSetPrototypeOf(this, WritableStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'WritableStream';
this[kState] = createWritableStreamState();
Expand Down Expand Up @@ -516,6 +517,7 @@ ObjectDefineProperties(WritableStreamDefaultController.prototype, {
});

function InternalWritableStream(start, write, close, abort, highWaterMark, size) {
ObjectSetPrototypeOf(this, WritableStream.prototype);
markTransferMode(this, false, true);
this[kType] = 'WritableStream';
this[kState] = createWritableStreamState();
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ assert.strictEqual(structuredClone(undefined, null), undefined);
assert.strictEqual(structuredClone(undefined, { transfer: null }), undefined);
assert.strictEqual(structuredClone(undefined, { }), undefined);

// Transferables or its subclasses should be received with its closest transferable superclass
for (const StreamClass of [ReadableStream, WritableStream, TransformStream]) {
const original = new StreamClass();
const transfer = structuredClone(original, { transfer: [original] });
assert.strictEqual(Object.getPrototypeOf(transfer), StreamClass.prototype);
assert.ok(transfer instanceof StreamClass);

const extended = class extends StreamClass {};
const extendedOriginal = new extended();
const extendedTransfer = structuredClone(extendedOriginal, { transfer: [extendedOriginal] });
assert.strictEqual(Object.getPrototypeOf(extendedTransfer), StreamClass.prototype);
assert.ok(extendedTransfer instanceof StreamClass);
}

{
// See: https://github.com/nodejs/node/issues/49940
const cloned = structuredClone({}, {
Expand Down

0 comments on commit bf69ae1

Please sign in to comment.