Skip to content

Commit 9fc5700

Browse files
authored
stream: improve WebStreams performance
PR-URL: #49089 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent ee8b7f1 commit 9fc5700

File tree

3 files changed

+39
-64
lines changed

3 files changed

+39
-64
lines changed

lib/internal/webstreams/readablestream.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const kChunk = Symbol('kChunk');
140140
const kError = Symbol('kError');
141141
const kPull = Symbol('kPull');
142142
const kRelease = Symbol('kRelease');
143+
const kSkipThrow = Symbol('kSkipThrow');
143144

144145
let releasedError;
145146
let releasingError;
@@ -675,8 +676,10 @@ TransferredReadableStream.prototype[kDeserialize] = () => {};
675676
class ReadableStreamBYOBRequest {
676677
[kType] = 'ReadableStreamBYOBRequest';
677678

678-
constructor() {
679-
throw new ERR_ILLEGAL_CONSTRUCTOR();
679+
constructor(skipThrowSymbol = undefined) {
680+
if (skipThrowSymbol !== kSkipThrow) {
681+
throw new ERR_ILLEGAL_CONSTRUCTOR();
682+
}
680683
}
681684

682685
/**
@@ -758,17 +761,14 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, {
758761
});
759762

760763
function createReadableStreamBYOBRequest(controller, view) {
761-
return ReflectConstruct(
762-
function() {
763-
this[kType] = 'ReadableStreamBYOBRequest';
764-
this[kState] = {
765-
controller,
766-
view,
767-
};
768-
},
769-
[],
770-
ReadableStreamBYOBRequest,
771-
);
764+
const stream = new ReadableStreamBYOBRequest(kSkipThrow);
765+
766+
stream[kState] = {
767+
controller,
768+
view,
769+
};
770+
771+
return stream;
772772
}
773773

774774
class DefaultReadRequest {
@@ -1018,9 +1018,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, {
10181018

10191019
class ReadableStreamDefaultController {
10201020
[kType] = 'ReadableStreamDefaultController';
1021+
[kState] = {};
10211022

1022-
constructor() {
1023-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1023+
constructor(skipThrowSymbol = undefined) {
1024+
if (skipThrowSymbol !== kSkipThrow) {
1025+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1026+
}
10241027
}
10251028

10261029
/**
@@ -1076,22 +1079,14 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, {
10761079
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name),
10771080
});
10781081

1079-
function createReadableStreamDefaultController() {
1080-
return ReflectConstruct(
1081-
function() {
1082-
this[kType] = 'ReadableStreamDefaultController';
1083-
this[kState] = {};
1084-
},
1085-
[],
1086-
ReadableStreamDefaultController,
1087-
);
1088-
}
1089-
10901082
class ReadableByteStreamController {
10911083
[kType] = 'ReadableByteStreamController';
1084+
[kState] = {};
10921085

1093-
constructor() {
1094-
throw new ERR_ILLEGAL_CONSTRUCTOR();
1086+
constructor(skipThrowSymbol = undefined) {
1087+
if (skipThrowSymbol !== kSkipThrow) {
1088+
throw new ERR_ILLEGAL_CONSTRUCTOR();
1089+
}
10951090
}
10961091

10971092
/**
@@ -1202,17 +1197,6 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, {
12021197
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name),
12031198
});
12041199

1205-
function createReadableByteStreamController() {
1206-
return ReflectConstruct(
1207-
function() {
1208-
this[kType] = 'ReadableByteStreamController';
1209-
this[kState] = {};
1210-
},
1211-
[],
1212-
ReadableByteStreamController,
1213-
);
1214-
}
1215-
12161200
function createTeeReadableStream(start, pull, cancel) {
12171201
return ReflectConstruct(
12181202
function() {
@@ -2415,7 +2399,7 @@ function setupReadableStreamDefaultControllerFromSource(
24152399
source,
24162400
highWaterMark,
24172401
sizeAlgorithm) {
2418-
const controller = createReadableStreamDefaultController();
2402+
const controller = new ReadableStreamDefaultController(kSkipThrow);
24192403
const start = source?.start;
24202404
const pull = source?.pull;
24212405
const cancel = source?.cancel;
@@ -3213,7 +3197,7 @@ function setupReadableByteStreamControllerFromSource(
32133197
stream,
32143198
source,
32153199
highWaterMark) {
3216-
const controller = createReadableByteStreamController();
3200+
const controller = new ReadableByteStreamController(kSkipThrow);
32173201
const start = source?.start;
32183202
const pull = source?.pull;
32193203
const cancel = source?.cancel;

lib/internal/webstreams/transformstream.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
PromiseResolve,
99
ReflectConstruct,
1010
SymbolToStringTag,
11+
Symbol,
1112
} = primordials;
1213

1314
const {
@@ -65,6 +66,8 @@ const {
6566

6667
const assert = require('internal/assert');
6768

69+
const kSkipThrow = Symbol('kSkipThrow');
70+
6871
const getNonWritablePropertyDescriptor = (value) => {
6972
return {
7073
__proto__: null,
@@ -268,8 +271,10 @@ TransferredTransformStream.prototype[kDeserialize] = () => {};
268271
class TransformStreamDefaultController {
269272
[kType] = 'TransformStreamDefaultController';
270273

271-
constructor() {
272-
throw new ERR_ILLEGAL_CONSTRUCTOR();
274+
constructor(skipThrowSymbol = undefined) {
275+
if (skipThrowSymbol !== kSkipThrow) {
276+
throw new ERR_ILLEGAL_CONSTRUCTOR();
277+
}
273278
}
274279

275280
/**
@@ -330,15 +335,6 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, {
330335
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name),
331336
});
332337

333-
function createTransformStreamDefaultController() {
334-
return ReflectConstruct(
335-
function() {
336-
this[kType] = 'TransformStreamDefaultController';
337-
},
338-
[],
339-
TransformStreamDefaultController);
340-
}
341-
342338
const isTransformStream =
343339
isBrandCheck('TransformStream');
344340
const isTransformStreamDefaultController =
@@ -453,7 +449,7 @@ function setupTransformStreamDefaultController(
453449
function setupTransformStreamDefaultControllerFromTransformer(
454450
stream,
455451
transformer) {
456-
const controller = createTransformStreamDefaultController();
452+
const controller = new TransformStreamDefaultController(kSkipThrow);
457453
const transform = transformer?.transform || defaultTransformAlgorithm;
458454
const flush = transformer?.flush || nonOpFlush;
459455
const transformAlgorithm =

lib/internal/webstreams/writablestream.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const assert = require('internal/assert');
8181
const kAbort = Symbol('kAbort');
8282
const kCloseSentinel = Symbol('kCloseSentinel');
8383
const kError = Symbol('kError');
84+
const kSkipThrow = Symbol('kSkipThrow');
8485

8586
let releasedError;
8687

@@ -522,8 +523,10 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, {
522523
class WritableStreamDefaultController {
523524
[kType] = 'WritableStreamDefaultController';
524525

525-
constructor() {
526-
throw new ERR_ILLEGAL_CONSTRUCTOR();
526+
constructor(skipThrowSymbol = undefined) {
527+
if (skipThrowSymbol !== kSkipThrow) {
528+
throw new ERR_ILLEGAL_CONSTRUCTOR();
529+
}
527530
}
528531

529532
[kAbort](reason) {
@@ -569,14 +572,6 @@ ObjectDefineProperties(WritableStreamDefaultController.prototype, {
569572
[SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name),
570573
});
571574

572-
function createWritableStreamDefaultController() {
573-
return ReflectConstruct(
574-
function() {
575-
this[kType] = 'WritableStreamDefaultController';
576-
},
577-
[], WritableStreamDefaultController);
578-
}
579-
580575
const isWritableStream =
581576
isBrandCheck('WritableStream');
582577
const isWritableStreamDefaultWriter =
@@ -1233,7 +1228,7 @@ function setupWritableStreamDefaultControllerFromSink(
12331228
sink,
12341229
highWaterMark,
12351230
sizeAlgorithm) {
1236-
const controller = createWritableStreamDefaultController();
1231+
const controller = new WritableStreamDefaultController(kSkipThrow);
12371232
const start = sink?.start;
12381233
const write = sink?.write;
12391234
const close = sink?.close;

0 commit comments

Comments
 (0)