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: construct correct prototype to transferred streams #55047

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 7 additions & 16 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
PromisePrototypeThen,
PromiseReject,
PromiseResolve,
ReflectConstruct,
SafePromiseAll,
Symbol,
SymbolAsyncIterator,
Expand Down Expand Up @@ -638,23 +639,13 @@ ObjectDefineProperties(ReadableStream, {
from: kEnumerableProperty,
});

function InternalTransferredReadableStream() {
markTransferMode(this, false, true);
this[kType] = 'ReadableStream';
this[kState] = createReadableStreamState();

this[kIsClosedPromise] = createDeferredPromise();
}

ObjectSetPrototypeOf(InternalTransferredReadableStream.prototype, ReadableStream.prototype);
ObjectSetPrototypeOf(InternalTransferredReadableStream, ReadableStream);

function TransferredReadableStream() {
const stream = new InternalTransferredReadableStream();

stream.constructor = ReadableStream;

return stream;
return ReflectConstruct(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReflectConstruct is extremely slow.

Copy link
Member Author

@RedYetiDev RedYetiDev Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware, is there another way to make this WPT compliant? We need a way to have the prototype of the transferred stream === the original prototype.

Copy link
Member Author

@RedYetiDev RedYetiDev Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW regarding speed, this has around a -20% on speed, so not good. Is there a reason we can't just return ReadableStream as is?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deno solves this by adding a special overload to their ReadableStream constructor, which essentially skips the regular steps. Maybe we should do something similar?

markTransferMode(this, false, true);
this[kType] = 'ReadableStream';
this[kState] = createReadableStreamState();
this[kIsClosedPromise] = createDeferredPromise();
}, [], ReadableStream);
}

TransferredReadableStream.prototype[kDeserialize] = () => {};
Expand Down
44 changes: 18 additions & 26 deletions lib/internal/webstreams/transformstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
const {
FunctionPrototypeCall,
ObjectDefineProperties,
ObjectSetPrototypeOf,

Check failure on line 6 in lib/internal/webstreams/transformstream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ObjectSetPrototypeOf' is assigned a value but never used
PromisePrototypeThen,
ReflectConstruct,
Symbol,
SymbolToStringTag,
} = primordials;
Expand Down Expand Up @@ -255,33 +256,24 @@
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStream.name),
});

function InternalTransferredTransformStream() {
markTransferMode(this, false, true);
this[kType] = 'TransformStream';
this[kState] = {
__proto__: null,
readable: undefined,
writable: undefined,
backpressure: undefined,
backpressureChange: {
__proto__: null,
promise: undefined,
resolve: undefined,
reject: undefined,
},
controller: undefined,
};
}

ObjectSetPrototypeOf(InternalTransferredTransformStream.prototype, TransformStream.prototype);
ObjectSetPrototypeOf(InternalTransferredTransformStream, TransformStream);

function TransferredTransformStream() {
const stream = new InternalTransferredTransformStream();

stream.constructor = TransformStream;

return stream;
return ReflectConstruct(function() {
markTransferMode(this, false, true);
this[kType] = 'TransformStream';
this[kState] = {
__proto__: null,
readable: undefined,
writable: undefined,
backpressure: undefined,
backpressureChange: {
__proto__: null,
promise: undefined,
resolve: undefined,
reject: undefined,
},
controller: undefined,
};
}, [], TransformStream);
}

TransferredTransformStream.prototype[kDeserialize] = () => {};
Expand Down
23 changes: 7 additions & 16 deletions lib/internal/webstreams/writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
PromisePrototypeThen,
PromiseReject,
PromiseResolve,
ReflectConstruct,
Symbol,
SymbolToStringTag,
} = primordials;
Expand Down Expand Up @@ -299,23 +300,13 @@ ObjectDefineProperties(WritableStream.prototype, {
[SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStream.name),
});

function InternalTransferredWritableStream() {
markTransferMode(this, false, true);
this[kType] = 'WritableStream';
this[kState] = createWritableStreamState();

this[kIsClosedPromise] = createDeferredPromise();
}

ObjectSetPrototypeOf(InternalTransferredWritableStream.prototype, WritableStream.prototype);
ObjectSetPrototypeOf(InternalTransferredWritableStream, WritableStream);

function TransferredWritableStream() {
const stream = new InternalTransferredWritableStream();

stream.constructor = WritableStream;

return stream;
return ReflectConstruct(function() {
markTransferMode(this, false, true);
this[kType] = 'WritableStream';
this[kState] = createWritableStreamState();
this[kIsClosedPromise] = createDeferredPromise();
}, [], WritableStream);
}

TransferredWritableStream.prototype[kDeserialize] = () => {};
Expand Down
Loading