@gjsify/web-streams 0.4.0 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +69 -65
- package/src/index.spec.ts +0 -2043
- package/src/index.ts +0 -113
- package/src/queuing-strategies.ts +0 -67
- package/src/readable-stream.ts +0 -1337
- package/src/register/queuing.ts +0 -10
- package/src/register/readable.ts +0 -16
- package/src/register/text-streams.ts +0 -10
- package/src/register/transform.ts +0 -16
- package/src/register/writable.ts +0 -16
- package/src/register.ts +0 -8
- package/src/test.browser.mts +0 -114
- package/src/test.mts +0 -6
- package/src/text-decoder-stream.ts +0 -183
- package/src/text-encoder-stream.ts +0 -62
- package/src/transform-stream.ts +0 -410
- package/src/util.ts +0 -170
- package/src/writable-stream.ts +0 -773
- package/tsconfig.json +0 -30
- package/tsconfig.tsbuildinfo +0 -1
package/src/transform-stream.ts
DELETED
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
// WHATWG Streams — TransformStream
|
|
2
|
-
// Adapted from refs/node/lib/internal/webstreams/transformstream.js
|
|
3
|
-
// Copyright (c) Node.js contributors. MIT license.
|
|
4
|
-
// Modifications: Removed primordials, transfer, inspect, Node.js error codes
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
kState, kType,
|
|
8
|
-
isBrandCheck,
|
|
9
|
-
createPromiseCallback,
|
|
10
|
-
extractHighWaterMark,
|
|
11
|
-
extractSizeAlgorithm,
|
|
12
|
-
nonOpFlush,
|
|
13
|
-
nonOpCancel,
|
|
14
|
-
} from './util.js';
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
createReadableStream,
|
|
18
|
-
readableStreamDefaultControllerCanCloseOrEnqueue,
|
|
19
|
-
readableStreamDefaultControllerClose,
|
|
20
|
-
readableStreamDefaultControllerEnqueue,
|
|
21
|
-
readableStreamDefaultControllerError,
|
|
22
|
-
readableStreamDefaultControllerGetDesiredSize,
|
|
23
|
-
readableStreamDefaultControllerHasBackpressure,
|
|
24
|
-
} from './readable-stream.js';
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
createWritableStream,
|
|
28
|
-
writableStreamDefaultControllerErrorIfNeeded,
|
|
29
|
-
} from './writable-stream.js';
|
|
30
|
-
|
|
31
|
-
const kSkipThrow = Symbol('kSkipThrow');
|
|
32
|
-
|
|
33
|
-
// ---- TransformStream ----
|
|
34
|
-
|
|
35
|
-
export class TransformStream {
|
|
36
|
-
[kType] = 'TransformStream';
|
|
37
|
-
[kState]: any;
|
|
38
|
-
|
|
39
|
-
constructor(
|
|
40
|
-
transformer: any = {},
|
|
41
|
-
writableStrategy: any = {},
|
|
42
|
-
readableStrategy: any = {},
|
|
43
|
-
) {
|
|
44
|
-
if (transformer != null && typeof transformer !== 'object') {
|
|
45
|
-
throw new TypeError('transformer must be an object');
|
|
46
|
-
}
|
|
47
|
-
if (writableStrategy != null && typeof writableStrategy !== 'object') {
|
|
48
|
-
throw new TypeError('writableStrategy must be an object');
|
|
49
|
-
}
|
|
50
|
-
if (readableStrategy != null && typeof readableStrategy !== 'object') {
|
|
51
|
-
throw new TypeError('readableStrategy must be an object');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const readableType = transformer?.readableType;
|
|
55
|
-
const writableType = transformer?.writableType;
|
|
56
|
-
const start = transformer?.start;
|
|
57
|
-
|
|
58
|
-
if (readableType !== undefined) {
|
|
59
|
-
throw new RangeError(`Invalid readableType: ${readableType}`);
|
|
60
|
-
}
|
|
61
|
-
if (writableType !== undefined) {
|
|
62
|
-
throw new RangeError(`Invalid writableType: ${writableType}`);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const readableHighWaterMark = extractHighWaterMark(readableStrategy?.highWaterMark, 0);
|
|
66
|
-
const readableSize = extractSizeAlgorithm(readableStrategy?.size);
|
|
67
|
-
const writableHighWaterMark = extractHighWaterMark(writableStrategy?.highWaterMark, 1);
|
|
68
|
-
const writableSize = extractSizeAlgorithm(writableStrategy?.size);
|
|
69
|
-
|
|
70
|
-
const startPromise = Promise.withResolvers<void>();
|
|
71
|
-
|
|
72
|
-
initializeTransformStream(
|
|
73
|
-
this,
|
|
74
|
-
startPromise,
|
|
75
|
-
writableHighWaterMark,
|
|
76
|
-
writableSize,
|
|
77
|
-
readableHighWaterMark,
|
|
78
|
-
readableSize,
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
setupTransformStreamDefaultControllerFromTransformer(this, transformer);
|
|
82
|
-
|
|
83
|
-
if (start !== undefined) {
|
|
84
|
-
startPromise.resolve(start.call(transformer, this[kState].controller) as unknown as void);
|
|
85
|
-
} else {
|
|
86
|
-
startPromise.resolve();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
get readable(): any {
|
|
91
|
-
if (!isTransformStream(this)) throw new TypeError('Invalid this');
|
|
92
|
-
return this[kState].readable;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get writable(): any {
|
|
96
|
-
if (!isTransformStream(this)) throw new TypeError('Invalid this');
|
|
97
|
-
return this[kState].writable;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
get [Symbol.toStringTag]() {
|
|
101
|
-
return 'TransformStream';
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// ---- TransformStreamDefaultController ----
|
|
106
|
-
|
|
107
|
-
export class TransformStreamDefaultController {
|
|
108
|
-
[kType] = 'TransformStreamDefaultController';
|
|
109
|
-
[kState]: any;
|
|
110
|
-
|
|
111
|
-
constructor(skipThrowSymbol?: symbol) {
|
|
112
|
-
if (skipThrowSymbol !== kSkipThrow) {
|
|
113
|
-
throw new TypeError('Illegal constructor');
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
get desiredSize(): number | null {
|
|
118
|
-
if (!isTransformStreamDefaultController(this)) throw new TypeError('Invalid this');
|
|
119
|
-
const { stream } = this[kState];
|
|
120
|
-
const { readable } = stream[kState];
|
|
121
|
-
const readableController = readable[kState].controller;
|
|
122
|
-
return readableStreamDefaultControllerGetDesiredSize(readableController);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
enqueue(chunk?: any): void {
|
|
126
|
-
if (!isTransformStreamDefaultController(this)) throw new TypeError('Invalid this');
|
|
127
|
-
transformStreamDefaultControllerEnqueue(this, chunk);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
error(reason?: unknown): void {
|
|
131
|
-
if (!isTransformStreamDefaultController(this)) throw new TypeError('Invalid this');
|
|
132
|
-
transformStreamDefaultControllerError(this, reason);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
terminate(): void {
|
|
136
|
-
if (!isTransformStreamDefaultController(this)) throw new TypeError('Invalid this');
|
|
137
|
-
transformStreamDefaultControllerTerminate(this);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
get [Symbol.toStringTag]() {
|
|
141
|
-
return 'TransformStreamDefaultController';
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// ---- Brand checks ----
|
|
146
|
-
|
|
147
|
-
export const isTransformStream = isBrandCheck('TransformStream');
|
|
148
|
-
export const isTransformStreamDefaultController = isBrandCheck('TransformStreamDefaultController');
|
|
149
|
-
|
|
150
|
-
// ---- Internal functions ----
|
|
151
|
-
|
|
152
|
-
async function defaultTransformAlgorithm(chunk: unknown, controller: any) {
|
|
153
|
-
transformStreamDefaultControllerEnqueue(controller, chunk);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function initializeTransformStream(
|
|
157
|
-
stream: any,
|
|
158
|
-
startPromise: any,
|
|
159
|
-
writableHighWaterMark: number,
|
|
160
|
-
writableSizeAlgorithm: (chunk: any) => number,
|
|
161
|
-
readableHighWaterMark: number,
|
|
162
|
-
readableSizeAlgorithm: (chunk: any) => number,
|
|
163
|
-
): void {
|
|
164
|
-
const startAlgorithm = () => startPromise.promise;
|
|
165
|
-
|
|
166
|
-
const writable = createWritableStream(
|
|
167
|
-
startAlgorithm,
|
|
168
|
-
(chunk: unknown) => transformStreamDefaultSinkWriteAlgorithm(stream, chunk),
|
|
169
|
-
() => transformStreamDefaultSinkCloseAlgorithm(stream),
|
|
170
|
-
(reason: unknown) => transformStreamDefaultSinkAbortAlgorithm(stream, reason),
|
|
171
|
-
writableHighWaterMark,
|
|
172
|
-
writableSizeAlgorithm,
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
const readable = createReadableStream(
|
|
176
|
-
startAlgorithm,
|
|
177
|
-
() => transformStreamDefaultSourcePullAlgorithm(stream),
|
|
178
|
-
(reason: unknown) => transformStreamDefaultSourceCancelAlgorithm(stream, reason),
|
|
179
|
-
readableHighWaterMark,
|
|
180
|
-
readableSizeAlgorithm,
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
stream[kState] = {
|
|
184
|
-
readable,
|
|
185
|
-
writable,
|
|
186
|
-
controller: undefined,
|
|
187
|
-
backpressure: undefined as boolean | undefined,
|
|
188
|
-
backpressureChange: {
|
|
189
|
-
promise: undefined as Promise<void> | undefined,
|
|
190
|
-
resolve: undefined as ((value: void | PromiseLike<void>) => void) | undefined,
|
|
191
|
-
reject: undefined as ((reason?: unknown) => void) | undefined,
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
transformStreamSetBackpressure(stream, true);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function transformStreamError(stream: any, error: unknown): void {
|
|
199
|
-
const { readable } = stream[kState];
|
|
200
|
-
readableStreamDefaultControllerError(readable[kState].controller, error);
|
|
201
|
-
transformStreamErrorWritableAndUnblockWrite(stream, error);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function transformStreamErrorWritableAndUnblockWrite(stream: any, error: unknown): void {
|
|
205
|
-
const { controller, writable } = stream[kState];
|
|
206
|
-
transformStreamDefaultControllerClearAlgorithms(controller);
|
|
207
|
-
writableStreamDefaultControllerErrorIfNeeded(writable[kState].controller, error);
|
|
208
|
-
transformStreamUnblockWrite(stream);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function transformStreamUnblockWrite(stream: any): void {
|
|
212
|
-
if (stream[kState].backpressure) {
|
|
213
|
-
transformStreamSetBackpressure(stream, false);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function transformStreamSetBackpressure(stream: any, backpressure: boolean): void {
|
|
218
|
-
if (stream[kState].backpressureChange.promise !== undefined) {
|
|
219
|
-
stream[kState].backpressureChange.resolve?.();
|
|
220
|
-
}
|
|
221
|
-
stream[kState].backpressureChange = Promise.withResolvers<void>();
|
|
222
|
-
stream[kState].backpressure = backpressure;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function setupTransformStreamDefaultController(
|
|
226
|
-
stream: any,
|
|
227
|
-
controller: any,
|
|
228
|
-
transformAlgorithm: Function,
|
|
229
|
-
flushAlgorithm: Function,
|
|
230
|
-
cancelAlgorithm: Function,
|
|
231
|
-
): void {
|
|
232
|
-
controller[kState] = {
|
|
233
|
-
stream,
|
|
234
|
-
transformAlgorithm,
|
|
235
|
-
flushAlgorithm,
|
|
236
|
-
cancelAlgorithm,
|
|
237
|
-
finishPromise: undefined as Promise<void> | undefined,
|
|
238
|
-
};
|
|
239
|
-
stream[kState].controller = controller;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function setupTransformStreamDefaultControllerFromTransformer(
|
|
243
|
-
stream: any,
|
|
244
|
-
transformer: any,
|
|
245
|
-
): void {
|
|
246
|
-
const controller = new TransformStreamDefaultController(kSkipThrow);
|
|
247
|
-
const transform = transformer?.transform;
|
|
248
|
-
const flush = transformer?.flush;
|
|
249
|
-
const cancel = transformer?.cancel;
|
|
250
|
-
const transformAlgorithm = transform
|
|
251
|
-
? createPromiseCallback('transformer.transform', transform, transformer)
|
|
252
|
-
: defaultTransformAlgorithm;
|
|
253
|
-
const flushAlgorithm = flush
|
|
254
|
-
? createPromiseCallback('transformer.flush', flush, transformer)
|
|
255
|
-
: nonOpFlush;
|
|
256
|
-
const cancelAlgorithm = cancel
|
|
257
|
-
? createPromiseCallback('transformer.cancel', cancel, transformer)
|
|
258
|
-
: nonOpCancel;
|
|
259
|
-
|
|
260
|
-
setupTransformStreamDefaultController(
|
|
261
|
-
stream, controller, transformAlgorithm, flushAlgorithm, cancelAlgorithm,
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function transformStreamDefaultControllerClearAlgorithms(controller: any): void {
|
|
266
|
-
controller[kState].transformAlgorithm = undefined;
|
|
267
|
-
controller[kState].flushAlgorithm = undefined;
|
|
268
|
-
controller[kState].cancelAlgorithm = undefined;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function transformStreamDefaultControllerEnqueue(controller: any, chunk: any): void {
|
|
272
|
-
const { stream } = controller[kState];
|
|
273
|
-
const { readable } = stream[kState];
|
|
274
|
-
const readableController = readable[kState].controller;
|
|
275
|
-
if (!readableStreamDefaultControllerCanCloseOrEnqueue(readableController)) {
|
|
276
|
-
throw new TypeError('Unable to enqueue');
|
|
277
|
-
}
|
|
278
|
-
try {
|
|
279
|
-
readableStreamDefaultControllerEnqueue(readableController, chunk);
|
|
280
|
-
} catch (error: unknown) {
|
|
281
|
-
transformStreamErrorWritableAndUnblockWrite(stream, error);
|
|
282
|
-
throw readable[kState].storedError;
|
|
283
|
-
}
|
|
284
|
-
const backpressure = readableStreamDefaultControllerHasBackpressure(readableController);
|
|
285
|
-
if (backpressure !== stream[kState].backpressure) {
|
|
286
|
-
transformStreamSetBackpressure(stream, true);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function transformStreamDefaultControllerError(controller: any, error: unknown): void {
|
|
291
|
-
transformStreamError(controller[kState].stream, error);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
async function transformStreamDefaultControllerPerformTransform(controller: any, chunk: any): Promise<void> {
|
|
295
|
-
try {
|
|
296
|
-
const transformAlgorithm = controller[kState].transformAlgorithm;
|
|
297
|
-
if (transformAlgorithm === undefined) return;
|
|
298
|
-
return await transformAlgorithm(chunk, controller);
|
|
299
|
-
} catch (error) {
|
|
300
|
-
transformStreamError(controller[kState].stream, error);
|
|
301
|
-
throw error;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
function transformStreamDefaultControllerTerminate(controller: any): void {
|
|
306
|
-
const { stream } = controller[kState];
|
|
307
|
-
const { readable } = stream[kState];
|
|
308
|
-
readableStreamDefaultControllerClose(readable[kState].controller);
|
|
309
|
-
transformStreamErrorWritableAndUnblockWrite(stream, new TypeError('TransformStream has been terminated'));
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function transformStreamDefaultSinkWriteAlgorithm(stream: any, chunk: unknown): Promise<void> {
|
|
313
|
-
const { controller } = stream[kState];
|
|
314
|
-
if (stream[kState].backpressure) {
|
|
315
|
-
const backpressureChange = stream[kState].backpressureChange.promise;
|
|
316
|
-
return backpressureChange.then(() => {
|
|
317
|
-
const { writable } = stream[kState];
|
|
318
|
-
if (writable[kState].state === 'erroring') {
|
|
319
|
-
throw writable[kState].storedError;
|
|
320
|
-
}
|
|
321
|
-
return transformStreamDefaultControllerPerformTransform(controller, chunk);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
return transformStreamDefaultControllerPerformTransform(controller, chunk);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
async function transformStreamDefaultSinkAbortAlgorithm(stream: any, reason: unknown): Promise<void> {
|
|
328
|
-
const { controller, readable } = stream[kState];
|
|
329
|
-
if (controller[kState].finishPromise !== undefined) {
|
|
330
|
-
return controller[kState].finishPromise;
|
|
331
|
-
}
|
|
332
|
-
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
333
|
-
controller[kState].finishPromise = promise;
|
|
334
|
-
const cancelPromise = controller[kState].cancelAlgorithm(reason);
|
|
335
|
-
transformStreamDefaultControllerClearAlgorithms(controller);
|
|
336
|
-
cancelPromise.then(
|
|
337
|
-
() => {
|
|
338
|
-
if (readable[kState].state === 'errored') {
|
|
339
|
-
reject(readable[kState].storedError);
|
|
340
|
-
} else {
|
|
341
|
-
readableStreamDefaultControllerError(readable[kState].controller, reason);
|
|
342
|
-
resolve();
|
|
343
|
-
}
|
|
344
|
-
},
|
|
345
|
-
(error: unknown) => {
|
|
346
|
-
readableStreamDefaultControllerError(readable[kState].controller, error);
|
|
347
|
-
reject(error);
|
|
348
|
-
},
|
|
349
|
-
);
|
|
350
|
-
return controller[kState].finishPromise;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function transformStreamDefaultSinkCloseAlgorithm(stream: any): Promise<void> {
|
|
354
|
-
const { readable, controller } = stream[kState];
|
|
355
|
-
if (controller[kState].finishPromise !== undefined) {
|
|
356
|
-
return controller[kState].finishPromise;
|
|
357
|
-
}
|
|
358
|
-
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
359
|
-
controller[kState].finishPromise = promise;
|
|
360
|
-
const flushPromise = controller[kState].flushAlgorithm(controller);
|
|
361
|
-
transformStreamDefaultControllerClearAlgorithms(controller);
|
|
362
|
-
flushPromise.then(
|
|
363
|
-
() => {
|
|
364
|
-
if (readable[kState].state === 'errored') {
|
|
365
|
-
reject(readable[kState].storedError);
|
|
366
|
-
} else {
|
|
367
|
-
readableStreamDefaultControllerClose(readable[kState].controller);
|
|
368
|
-
resolve();
|
|
369
|
-
}
|
|
370
|
-
},
|
|
371
|
-
(error: unknown) => {
|
|
372
|
-
readableStreamDefaultControllerError(readable[kState].controller, error);
|
|
373
|
-
reject(error);
|
|
374
|
-
},
|
|
375
|
-
);
|
|
376
|
-
return controller[kState].finishPromise;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function transformStreamDefaultSourcePullAlgorithm(stream: any): Promise<void> {
|
|
380
|
-
transformStreamSetBackpressure(stream, false);
|
|
381
|
-
return stream[kState].backpressureChange.promise;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function transformStreamDefaultSourceCancelAlgorithm(stream: any, reason: unknown): Promise<void> {
|
|
385
|
-
const { controller, writable } = stream[kState];
|
|
386
|
-
if (controller[kState].finishPromise !== undefined) {
|
|
387
|
-
return controller[kState].finishPromise;
|
|
388
|
-
}
|
|
389
|
-
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
390
|
-
controller[kState].finishPromise = promise;
|
|
391
|
-
const cancelPromise = controller[kState].cancelAlgorithm(reason);
|
|
392
|
-
transformStreamDefaultControllerClearAlgorithms(controller);
|
|
393
|
-
cancelPromise.then(
|
|
394
|
-
() => {
|
|
395
|
-
if (writable[kState].state === 'errored') {
|
|
396
|
-
reject(writable[kState].storedError);
|
|
397
|
-
} else {
|
|
398
|
-
writableStreamDefaultControllerErrorIfNeeded(writable[kState].controller, reason);
|
|
399
|
-
transformStreamUnblockWrite(stream);
|
|
400
|
-
resolve();
|
|
401
|
-
}
|
|
402
|
-
},
|
|
403
|
-
(error: unknown) => {
|
|
404
|
-
writableStreamDefaultControllerErrorIfNeeded(writable[kState].controller, error);
|
|
405
|
-
transformStreamUnblockWrite(stream);
|
|
406
|
-
reject(error);
|
|
407
|
-
},
|
|
408
|
-
);
|
|
409
|
-
return controller[kState].finishPromise;
|
|
410
|
-
}
|
package/src/util.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
// WHATWG Streams — shared utilities
|
|
2
|
-
// Adapted from refs/node/lib/internal/webstreams/util.js
|
|
3
|
-
// Copyright (c) Node.js contributors. MIT license.
|
|
4
|
-
// Modifications: Removed primordials, internalBinding, Node.js error codes
|
|
5
|
-
|
|
6
|
-
export const kState = Symbol('kState');
|
|
7
|
-
export const kType = Symbol('kType');
|
|
8
|
-
|
|
9
|
-
// ---- Brand checking ----
|
|
10
|
-
|
|
11
|
-
export function isBrandCheck(brand: string) {
|
|
12
|
-
return (value: unknown): boolean => {
|
|
13
|
-
return value != null &&
|
|
14
|
-
typeof value === 'object' &&
|
|
15
|
-
(value as Record<symbol, unknown>)[kState] !== undefined &&
|
|
16
|
-
(value as Record<symbol, unknown>)[kType] === brand;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// ---- Queue management ----
|
|
21
|
-
|
|
22
|
-
export function dequeueValue(controller: any): any {
|
|
23
|
-
const { value, size } = controller[kState].queue.shift();
|
|
24
|
-
controller[kState].queueTotalSize =
|
|
25
|
-
Math.max(0, controller[kState].queueTotalSize - size);
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function resetQueue(controller: any): void {
|
|
30
|
-
controller[kState].queue = [];
|
|
31
|
-
controller[kState].queueTotalSize = 0;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function peekQueueValue(controller: any): any {
|
|
35
|
-
return controller[kState].queue[0].value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function enqueueValueWithSize(controller: any, value: any, size: number): void {
|
|
39
|
-
size = +size;
|
|
40
|
-
if (typeof size !== 'number' || size < 0 || Number.isNaN(size) || size === Infinity) {
|
|
41
|
-
throw new RangeError(`Invalid size: ${size}`);
|
|
42
|
-
}
|
|
43
|
-
controller[kState].queue.push({ value, size });
|
|
44
|
-
controller[kState].queueTotalSize += size;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// ---- Strategy extraction ----
|
|
48
|
-
|
|
49
|
-
export function extractHighWaterMark(value: number | undefined, defaultHWM: number): number {
|
|
50
|
-
if (value === undefined) return defaultHWM;
|
|
51
|
-
value = +value;
|
|
52
|
-
if (typeof value !== 'number' || Number.isNaN(value) || value < 0) {
|
|
53
|
-
throw new RangeError(`Invalid highWaterMark: ${value}`);
|
|
54
|
-
}
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function extractSizeAlgorithm(size: ((chunk: any) => number) | undefined): (chunk: any) => number {
|
|
59
|
-
if (size === undefined) return () => 1;
|
|
60
|
-
if (typeof size !== 'function') {
|
|
61
|
-
throw new TypeError('strategy.size must be a function');
|
|
62
|
-
}
|
|
63
|
-
return size;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// ---- Buffer utilities ----
|
|
67
|
-
|
|
68
|
-
export function cloneAsUint8Array(view: ArrayBufferView): Uint8Array {
|
|
69
|
-
const buffer = view.buffer;
|
|
70
|
-
const byteOffset = view.byteOffset;
|
|
71
|
-
const byteLength = view.byteLength;
|
|
72
|
-
return new Uint8Array(buffer.slice(byteOffset, byteOffset + byteLength));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function ArrayBufferViewGetBuffer(view: ArrayBufferView): ArrayBuffer {
|
|
76
|
-
return view.buffer as ArrayBuffer;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function ArrayBufferViewGetByteLength(view: ArrayBufferView): number {
|
|
80
|
-
return view.byteLength;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function ArrayBufferViewGetByteOffset(view: ArrayBufferView): number {
|
|
84
|
-
return view.byteOffset;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// ---- Promise utilities ----
|
|
88
|
-
|
|
89
|
-
export function setPromiseHandled(promise: Promise<unknown>): void {
|
|
90
|
-
promise.then(() => {}, () => {});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function createPromiseCallback(name: string, fn: Function, thisArg: unknown) {
|
|
94
|
-
if (typeof fn !== 'function') {
|
|
95
|
-
throw new TypeError(`${name} must be a function`);
|
|
96
|
-
}
|
|
97
|
-
// Always return a Promise, even if fn is synchronous
|
|
98
|
-
return async (...args: unknown[]) => fn.call(thisArg, ...args);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// ---- No-op callbacks ----
|
|
102
|
-
|
|
103
|
-
export async function nonOpFlush(): Promise<void> {}
|
|
104
|
-
export function nonOpStart(): void {}
|
|
105
|
-
export async function nonOpPull(): Promise<void> {}
|
|
106
|
-
export async function nonOpCancel(): Promise<void> {}
|
|
107
|
-
export async function nonOpWrite(): Promise<void> {}
|
|
108
|
-
|
|
109
|
-
// ---- Iterator utilities ----
|
|
110
|
-
|
|
111
|
-
const AsyncIteratorPrototype = Object.getPrototypeOf(
|
|
112
|
-
Object.getPrototypeOf(async function* () {}).prototype
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
export const AsyncIterator = {
|
|
116
|
-
__proto__: AsyncIteratorPrototype,
|
|
117
|
-
next: undefined as (() => Promise<IteratorResult<unknown>>) | undefined,
|
|
118
|
-
return: undefined as ((value?: unknown) => Promise<IteratorResult<unknown>>) | undefined,
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export function createAsyncFromSyncIterator(syncIteratorRecord: { iterator: Iterator<unknown>; nextMethod: Function; done: boolean }) {
|
|
122
|
-
const syncIterable = {
|
|
123
|
-
[Symbol.iterator]: () => syncIteratorRecord.iterator,
|
|
124
|
-
};
|
|
125
|
-
const asyncIterator = (async function* () {
|
|
126
|
-
return yield* syncIterable;
|
|
127
|
-
}());
|
|
128
|
-
const nextMethod = asyncIterator.next;
|
|
129
|
-
return { iterator: asyncIterator, nextMethod, done: false };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function getIterator(obj: Record<string | symbol, unknown>, kind: 'sync' | 'async' = 'sync', method?: Function) {
|
|
133
|
-
if (method === undefined) {
|
|
134
|
-
if (kind === 'async') {
|
|
135
|
-
method = obj[Symbol.asyncIterator] as Function | undefined;
|
|
136
|
-
if (method == null) {
|
|
137
|
-
const syncMethod = obj[Symbol.iterator] as Function | undefined;
|
|
138
|
-
if (syncMethod === undefined) {
|
|
139
|
-
throw new TypeError('Object is not iterable');
|
|
140
|
-
}
|
|
141
|
-
const syncIteratorRecord = getIterator(obj, 'sync', syncMethod);
|
|
142
|
-
return createAsyncFromSyncIterator(syncIteratorRecord);
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
method = obj[Symbol.iterator] as Function | undefined;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (method === undefined) {
|
|
149
|
-
throw new TypeError('Object is not iterable');
|
|
150
|
-
}
|
|
151
|
-
const iterator = method.call(obj);
|
|
152
|
-
if (typeof iterator !== 'object' || iterator === null) {
|
|
153
|
-
throw new TypeError('The iterator method must return an object');
|
|
154
|
-
}
|
|
155
|
-
const nextMethod = iterator.next;
|
|
156
|
-
return { iterator, nextMethod, done: false };
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export function iteratorNext(iteratorRecord: { iterator: unknown; nextMethod: Function; done: boolean }, value?: unknown) {
|
|
160
|
-
let result;
|
|
161
|
-
if (value === undefined) {
|
|
162
|
-
result = iteratorRecord.nextMethod.call(iteratorRecord.iterator);
|
|
163
|
-
} else {
|
|
164
|
-
result = iteratorRecord.nextMethod.call(iteratorRecord.iterator, value);
|
|
165
|
-
}
|
|
166
|
-
if (typeof result !== 'object' || result === null) {
|
|
167
|
-
throw new TypeError('The iterator.next() method must return an object');
|
|
168
|
-
}
|
|
169
|
-
return result;
|
|
170
|
-
}
|