diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index e7d115e98f12b9..a1845574f93ee0 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -140,6 +140,7 @@ const kChunk = Symbol('kChunk'); const kError = Symbol('kError'); const kPull = Symbol('kPull'); const kRelease = Symbol('kRelease'); +const kSkipThrow = Symbol('kSkipThrow'); let releasedError; let releasingError; @@ -675,8 +676,10 @@ TransferredReadableStream.prototype[kDeserialize] = () => {}; class ReadableStreamBYOBRequest { [kType] = 'ReadableStreamBYOBRequest'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -758,17 +761,14 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, { }); function createReadableStreamBYOBRequest(controller, view) { - return ReflectConstruct( - function() { - this[kType] = 'ReadableStreamBYOBRequest'; - this[kState] = { - controller, - view, - }; - }, - [], - ReadableStreamBYOBRequest, - ); + const stream = new ReadableStreamBYOBRequest(kSkipThrow); + + stream[kState] = { + controller, + view, + }; + + return stream; } class DefaultReadRequest { @@ -1018,9 +1018,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, { class ReadableStreamDefaultController { [kType] = 'ReadableStreamDefaultController'; + [kState] = {}; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -1076,22 +1079,14 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name), }); -function createReadableStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'ReadableStreamDefaultController'; - this[kState] = {}; - }, - [], - ReadableStreamDefaultController, - ); -} - class ReadableByteStreamController { [kType] = 'ReadableByteStreamController'; + [kState] = {}; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -1202,17 +1197,6 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name), }); -function createReadableByteStreamController() { - return ReflectConstruct( - function() { - this[kType] = 'ReadableByteStreamController'; - this[kState] = {}; - }, - [], - ReadableByteStreamController, - ); -} - function createTeeReadableStream(start, pull, cancel) { return ReflectConstruct( function() { @@ -2415,7 +2399,7 @@ function setupReadableStreamDefaultControllerFromSource( source, highWaterMark, sizeAlgorithm) { - const controller = createReadableStreamDefaultController(); + const controller = new ReadableStreamDefaultController(kSkipThrow); const start = source?.start; const pull = source?.pull; const cancel = source?.cancel; @@ -3213,7 +3197,7 @@ function setupReadableByteStreamControllerFromSource( stream, source, highWaterMark) { - const controller = createReadableByteStreamController(); + const controller = new ReadableByteStreamController(kSkipThrow); const start = source?.start; const pull = source?.pull; const cancel = source?.cancel; diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index c5b2aa90ffae5f..7a8f20a6eece38 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -8,6 +8,7 @@ const { PromiseResolve, ReflectConstruct, SymbolToStringTag, + Symbol, } = primordials; const { @@ -65,6 +66,8 @@ const { const assert = require('internal/assert'); +const kSkipThrow = Symbol('kSkipThrow'); + const getNonWritablePropertyDescriptor = (value) => { return { __proto__: null, @@ -268,8 +271,10 @@ TransferredTransformStream.prototype[kDeserialize] = () => {}; class TransformStreamDefaultController { [kType] = 'TransformStreamDefaultController'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } /** @@ -330,15 +335,6 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name), }); -function createTransformStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'TransformStreamDefaultController'; - }, - [], - TransformStreamDefaultController); -} - const isTransformStream = isBrandCheck('TransformStream'); const isTransformStreamDefaultController = @@ -453,7 +449,7 @@ function setupTransformStreamDefaultController( function setupTransformStreamDefaultControllerFromTransformer( stream, transformer) { - const controller = createTransformStreamDefaultController(); + const controller = new TransformStreamDefaultController(kSkipThrow); const transform = transformer?.transform || defaultTransformAlgorithm; const flush = transformer?.flush || nonOpFlush; const transformAlgorithm = diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index 2544c179f53952..46d6ae28772c32 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -81,6 +81,7 @@ const assert = require('internal/assert'); const kAbort = Symbol('kAbort'); const kCloseSentinel = Symbol('kCloseSentinel'); const kError = Symbol('kError'); +const kSkipThrow = Symbol('kSkipThrow'); let releasedError; @@ -522,8 +523,10 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, { class WritableStreamDefaultController { [kType] = 'WritableStreamDefaultController'; - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + if (skipThrowSymbol !== kSkipThrow) { + throw new ERR_ILLEGAL_CONSTRUCTOR(); + } } [kAbort](reason) { @@ -569,14 +572,6 @@ ObjectDefineProperties(WritableStreamDefaultController.prototype, { [SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name), }); -function createWritableStreamDefaultController() { - return ReflectConstruct( - function() { - this[kType] = 'WritableStreamDefaultController'; - }, - [], WritableStreamDefaultController); -} - const isWritableStream = isBrandCheck('WritableStream'); const isWritableStreamDefaultWriter = @@ -1233,7 +1228,7 @@ function setupWritableStreamDefaultControllerFromSink( sink, highWaterMark, sizeAlgorithm) { - const controller = createWritableStreamDefaultController(); + const controller = new WritableStreamDefaultController(kSkipThrow); const start = sink?.start; const write = sink?.write; const close = sink?.close;