summaryrefslogtreecommitdiff
path: root/cli/js/web/streams/transform-internals.ts
blob: 9c17db8f6c18ea37603637429b692977dfaa8a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// TODO reenable this code when we enable writableStreams and transport types
// // Forked from https://github.com/stardazed/sd-streams/tree/8928cf04b035fd02fb1340b7eb541c76be37e546
// // Copyright (c) 2018-Present by Arthur Langereis - @zenmumbler MIT

// /**
//  * streams/transform-internals - internal types and functions for transform streams
//  * Part of Stardazed
//  * (c) 2018-Present by Arthur Langereis - @zenmumbler
//  * https://github.com/stardazed/sd-streams
//  */

// /* eslint-disable @typescript-eslint/no-explicit-any */
// // TODO reenable this lint here

// import * as rs from "./readable-internals.ts";
// import * as ws from "./writable-internals.ts";
// import * as shared from "./shared-internals.ts";

// import { createReadableStream } from "./readable-stream.ts";
// import { createWritableStream } from "./writable-stream.ts";

// import { QueuingStrategy, QueuingStrategySizeCallback } from "../dom_types.d.ts";

// export const state_ = Symbol("transformState_");
// export const backpressure_ = Symbol("backpressure_");
// export const backpressureChangePromise_ = Symbol("backpressureChangePromise_");
// export const readable_ = Symbol("readable_");
// export const transformStreamController_ = Symbol("transformStreamController_");
// export const writable_ = Symbol("writable_");

// export const controlledTransformStream_ = Symbol("controlledTransformStream_");
// export const flushAlgorithm_ = Symbol("flushAlgorithm_");
// export const transformAlgorithm_ = Symbol("transformAlgorithm_");

// // ----

// export type TransformFunction<InputType, OutputType> = (
//   chunk: InputType,
//   controller: TransformStreamDefaultController<InputType, OutputType>
// ) => void | PromiseLike<void>;
// export type TransformAlgorithm<InputType> = (chunk: InputType) => Promise<void>;
// export type FlushFunction<InputType, OutputType> = (
//   controller: TransformStreamDefaultController<InputType, OutputType>
// ) => void | PromiseLike<void>;
// export type FlushAlgorithm = () => Promise<void>;

// // ----

// export interface TransformStreamDefaultController<InputType, OutputType> {
//   readonly desiredSize: number | null;
//   enqueue(chunk: OutputType): void;
//   error(reason: shared.ErrorResult): void;
//   terminate(): void;

//   [controlledTransformStream_]: TransformStream<InputType, OutputType>; // The TransformStream instance controlled; also used for the IsTransformStreamDefaultController brand check
//   [flushAlgorithm_]: FlushAlgorithm; // A promise - returning algorithm which communicates a requested close to the transformer
//   [transformAlgorithm_]: TransformAlgorithm<InputType>; // A promise - returning algorithm, taking one argument(the chunk to transform), which requests the transformer perform its transformation
// }

// export interface Transformer<InputType, OutputType> {
//   start?(
//     controller: TransformStreamDefaultController<InputType, OutputType>
//   ): void | PromiseLike<void>;
//   transform?: TransformFunction<InputType, OutputType>;
//   flush?: FlushFunction<InputType, OutputType>;

//   readableType?: undefined; // for future spec changes
//   writableType?: undefined; // for future spec changes
// }

// export declare class TransformStream<InputType, OutputType> {
//   constructor(
//     transformer: Transformer<InputType, OutputType>,
//     writableStrategy: QueuingStrategy<InputType>,
//     readableStrategy: QueuingStrategy<OutputType>
//   );

//   readonly readable: rs.SDReadableStream<OutputType>;
//   readonly writable: ws.WritableStream<InputType>;

//   [backpressure_]: boolean | undefined; // Whether there was backpressure on [[readable]] the last time it was observed
//   [backpressureChangePromise_]: shared.ControlledPromise<void> | undefined; // A promise which is fulfilled and replaced every time the value of[[backpressure]] changes
//   [readable_]: rs.SDReadableStream<OutputType>; // The ReadableStream instance controlled by this object
//   [transformStreamController_]: TransformStreamDefaultController<
//     InputType,
//     OutputType
//   >; // A TransformStreamDefaultController created with the ability to control[[readable]] and[[writable]]; also used for the IsTransformStream brand check
//   [writable_]: ws.WritableStream<InputType>; // The WritableStream instance controlled by this object
// }

// // ---- TransformStream

// export function isTransformStream(
//   value: unknown
// ): value is TransformStream<any, any> {
//   if (typeof value !== "object" || value === null) {
//     return false;
//   }
//   return transformStreamController_ in value;
// }

// export function initializeTransformStream<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   startPromise: Promise<void>,
//   writableHighWaterMark: number,
//   writableSizeAlgorithm: QueuingStrategySizeCallback<InputType>,
//   readableHighWaterMark: number,
//   readableSizeAlgorithm: QueuingStrategySizeCallback<OutputType>
// ): void {
//   const startAlgorithm = function(): Promise<void> {
//     return startPromise;
//   };
//   const writeAlgorithm = function(chunk: InputType): Promise<void> {
//     return transformStreamDefaultSinkWriteAlgorithm(stream, chunk);
//   };
//   const abortAlgorithm = function(reason: shared.ErrorResult): Promise<void> {
//     return transformStreamDefaultSinkAbortAlgorithm(stream, reason);
//   };
//   const closeAlgorithm = function(): Promise<void> {
//     return transformStreamDefaultSinkCloseAlgorithm(stream);
//   };
//   stream[writable_] = createWritableStream<InputType>(
//     startAlgorithm,
//     writeAlgorithm,
//     closeAlgorithm,
//     abortAlgorithm,
//     writableHighWaterMark,
//     writableSizeAlgorithm
//   );

//   const pullAlgorithm = function(): Promise<void> {
//     return transformStreamDefaultSourcePullAlgorithm(stream);
//   };
//   const cancelAlgorithm = function(
//     reason: shared.ErrorResult
//   ): Promise<undefined> {
//     transformStreamErrorWritableAndUnblockWrite(stream, reason);
//     return Promise.resolve(undefined);
//   };
//   stream[readable_] = createReadableStream(
//     startAlgorithm,
//     pullAlgorithm,
//     cancelAlgorithm,
//     readableHighWaterMark,
//     readableSizeAlgorithm
//   );

//   stream[backpressure_] = undefined;
//   stream[backpressureChangePromise_] = undefined;
//   transformStreamSetBackpressure(stream, true);
//   stream[transformStreamController_] = undefined!; // initialize slot for brand-check
// }

// export function transformStreamError<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   error: shared.ErrorResult
// ): void {
//   rs.readableStreamDefaultControllerError(
//     stream[readable_][
//       rs.readableStreamController_
//     ] as rs.SDReadableStreamDefaultController<OutputType>,
//     error
//   );
//   transformStreamErrorWritableAndUnblockWrite(stream, error);
// }

// export function transformStreamErrorWritableAndUnblockWrite<
//   InputType,
//   OutputType
// >(
//   stream: TransformStream<InputType, OutputType>,
//   error: shared.ErrorResult
// ): void {
//   transformStreamDefaultControllerClearAlgorithms(
//     stream[transformStreamController_]
//   );
//   ws.writableStreamDefaultControllerErrorIfNeeded(
//     stream[writable_][ws.writableStreamController_]!,
//     error
//   );
//   if (stream[backpressure_]) {
//     transformStreamSetBackpressure(stream, false);
//   }
// }

// export function transformStreamSetBackpressure<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   backpressure: boolean
// ): void {
//   // Assert: stream.[[backpressure]] is not backpressure.
//   if (stream[backpressure_] !== undefined) {
//     stream[backpressureChangePromise_]!.resolve(undefined);
//   }
//   stream[backpressureChangePromise_] = shared.createControlledPromise<void>();
//   stream[backpressure_] = backpressure;
// }

// // ---- TransformStreamDefaultController

// export function isTransformStreamDefaultController(
//   value: unknown
// ): value is TransformStreamDefaultController<any, any> {
//   if (typeof value !== "object" || value === null) {
//     return false;
//   }
//   return controlledTransformStream_ in value;
// }

// export function setUpTransformStreamDefaultController<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   controller: TransformStreamDefaultController<InputType, OutputType>,
//   transformAlgorithm: TransformAlgorithm<InputType>,
//   flushAlgorithm: FlushAlgorithm
// ): void {
//   // Assert: ! IsTransformStream(stream) is true.
//   // Assert: stream.[[transformStreamController]] is undefined.
//   controller[controlledTransformStream_] = stream;
//   stream[transformStreamController_] = controller;
//   controller[transformAlgorithm_] = transformAlgorithm;
//   controller[flushAlgorithm_] = flushAlgorithm;
// }

// export function transformStreamDefaultControllerClearAlgorithms<
//   InputType,
//   OutputType
// >(controller: TransformStreamDefaultController<InputType, OutputType>): void {
//   // Use ! assertions to override type check here, this way we don't
//   // have to perform type checks/assertions everywhere else.
//   controller[transformAlgorithm_] = undefined!;
//   controller[flushAlgorithm_] = undefined!;
// }

// export function transformStreamDefaultControllerEnqueue<InputType, OutputType>(
//   controller: TransformStreamDefaultController<InputType, OutputType>,
//   chunk: OutputType
// ): void {
//   const stream = controller[controlledTransformStream_];
//   const readableController = stream[readable_][
//     rs.readableStreamController_
//   ] as rs.SDReadableStreamDefaultController<OutputType>;
//   if (
//     !rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController)
//   ) {
//     throw new TypeError();
//   }
//   try {
//     rs.readableStreamDefaultControllerEnqueue(readableController, chunk);
//   } catch (error) {
//     transformStreamErrorWritableAndUnblockWrite(stream, error);
//     throw stream[readable_][shared.storedError_];
//   }
//   const backpressure = rs.readableStreamDefaultControllerHasBackpressure(
//     readableController
//   );
//   if (backpressure !== stream[backpressure_]) {
//     // Assert: backpressure is true.
//     transformStreamSetBackpressure(stream, true);
//   }
// }

// export function transformStreamDefaultControllerError<InputType, OutputType>(
//   controller: TransformStreamDefaultController<InputType, OutputType>,
//   error: shared.ErrorResult
// ): void {
//   transformStreamError(controller[controlledTransformStream_], error);
// }

// export function transformStreamDefaultControllerPerformTransform<
//   InputType,
//   OutputType
// >(
//   controller: TransformStreamDefaultController<InputType, OutputType>,
//   chunk: InputType
// ): Promise<void> {
//   const transformPromise = controller[transformAlgorithm_](chunk);
//   return transformPromise.catch(error => {
//     transformStreamError(controller[controlledTransformStream_], error);
//     throw error;
//   });
// }

// export function transformStreamDefaultControllerTerminate<
//   InputType,
//   OutputType
// >(controller: TransformStreamDefaultController<InputType, OutputType>): void {
//   const stream = controller[controlledTransformStream_];
//   const readableController = stream[readable_][
//     rs.readableStreamController_
//   ] as rs.SDReadableStreamDefaultController<OutputType>;
//   if (rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController)) {
//     rs.readableStreamDefaultControllerClose(readableController);
//   }
//   const error = new TypeError("The transform stream has been terminated");
//   transformStreamErrorWritableAndUnblockWrite(stream, error);
// }

// // ---- Transform Sinks

// export function transformStreamDefaultSinkWriteAlgorithm<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   chunk: InputType
// ): Promise<void> {
//   // Assert: stream.[[writable]].[[state]] is "writable".
//   const controller = stream[transformStreamController_];
//   if (stream[backpressure_]) {
//     const backpressureChangePromise = stream[backpressureChangePromise_]!;
//     // Assert: backpressureChangePromise is not undefined.
//     return backpressureChangePromise.promise.then(_ => {
//       const writable = stream[writable_];
//       const state = writable[shared.state_];
//       if (state === "erroring") {
//         throw writable[shared.storedError_];
//       }
//       // Assert: state is "writable".
//       return transformStreamDefaultControllerPerformTransform(
//         controller,
//         chunk
//       );
//     });
//   }
//   return transformStreamDefaultControllerPerformTransform(controller, chunk);
// }

// export function transformStreamDefaultSinkAbortAlgorithm<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>,
//   reason: shared.ErrorResult
// ): Promise<void> {
//   transformStreamError(stream, reason);
//   return Promise.resolve(undefined);
// }

// export function transformStreamDefaultSinkCloseAlgorithm<InputType, OutputType>(
//   stream: TransformStream<InputType, OutputType>
// ): Promise<void> {
//   const readable = stream[readable_];
//   const controller = stream[transformStreamController_];
//   const flushPromise = controller[flushAlgorithm_]();
//   transformStreamDefaultControllerClearAlgorithms(controller);

//   return flushPromise.then(
//     _ => {
//       if (readable[shared.state_] === "errored") {
//         throw readable[shared.storedError_];
//       }
//       const readableController = readable[
//         rs.readableStreamController_
//       ] as rs.SDReadableStreamDefaultController<OutputType>;
//       if (
//         rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController)
//       ) {
//         rs.readableStreamDefaultControllerClose(readableController);
//       }
//     },
//     error => {
//       transformStreamError(stream, error);
//       throw readable[shared.storedError_];
//     }
//   );
// }

// // ---- Transform Sources

// export function transformStreamDefaultSourcePullAlgorithm<
//   InputType,
//   OutputType
// >(stream: TransformStream<InputType, OutputType>): Promise<void> {
//   // Assert: stream.[[backpressure]] is true.
//   // Assert: stream.[[backpressureChangePromise]] is not undefined.
//   transformStreamSetBackpressure(stream, false);
//   return stream[backpressureChangePromise_]!.promise;
// }