From fe71574bea9a35f6b4bb6ccce2cf380660e6489c Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Sat, 21 Sep 2024 12:34:50 -0400 Subject: [PATCH] stream: construct correct prototype to transferred streams --- lib/internal/webstreams/readablestream.js | 23 ++++------- lib/internal/webstreams/transformstream.js | 44 +++++++++------------- lib/internal/webstreams/writablestream.js | 23 ++++------- 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 94c76a83898222..847864465c14db 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -19,6 +19,7 @@ const { PromisePrototypeThen, PromiseReject, PromiseResolve, + ReflectConstruct, SafePromiseAll, Symbol, SymbolAsyncIterator, @@ -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() { + markTransferMode(this, false, true); + this[kType] = 'ReadableStream'; + this[kState] = createReadableStreamState(); + this[kIsClosedPromise] = createDeferredPromise(); + }, [], ReadableStream); } TransferredReadableStream.prototype[kDeserialize] = () => {}; diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index 68b1e7aa83d9fc..f637826a36b17a 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -5,6 +5,7 @@ const { ObjectDefineProperties, ObjectSetPrototypeOf, PromisePrototypeThen, + ReflectConstruct, Symbol, SymbolToStringTag, } = primordials; @@ -255,33 +256,24 @@ ObjectDefineProperties(TransformStream.prototype, { [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] = () => {}; diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index 43f7a5121b1e97..013a2e72d0f7dd 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -11,6 +11,7 @@ const { PromisePrototypeThen, PromiseReject, PromiseResolve, + ReflectConstruct, Symbol, SymbolToStringTag, } = primordials; @@ -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] = () => {};