Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: set stream prototype to closest transferable superclass #55067

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading