summaryrefslogtreecommitdiff
path: root/cli/js/streams/transform-stream.ts
blob: 090f781358cbd0ef950f66b9961fcc00a06bfacb (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
// 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-stream - TransformStream class implementation
//  * 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 ts from "./transform-internals.ts";
// import * as shared from "./shared-internals.ts";
// import { TransformStreamDefaultController } from "./transform-stream-default-controller.ts";
// import { QueuingStrategy } from "../dom_types.ts";

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

//   constructor(
//     transformer: ts.Transformer<InputType, OutputType> = {},
//     writableStrategy: QueuingStrategy<InputType> = {},
//     readableStrategy: QueuingStrategy<OutputType> = {}
//   ) {
//     const writableSizeFunction = writableStrategy.size;
//     const writableHighWaterMark = writableStrategy.highWaterMark;
//     const readableSizeFunction = readableStrategy.size;
//     const readableHighWaterMark = readableStrategy.highWaterMark;

//     const writableType = transformer.writableType;
//     if (writableType !== undefined) {
//       throw new RangeError(
//         "The transformer's `writableType` field must be undefined"
//       );
//     }
//     const writableSizeAlgorithm = shared.makeSizeAlgorithmFromSizeFunction(
//       writableSizeFunction
//     );
//     const writableHWM = shared.validateAndNormalizeHighWaterMark(
//       writableHighWaterMark === undefined ? 1 : writableHighWaterMark
//     );

//     const readableType = transformer.readableType;
//     if (readableType !== undefined) {
//       throw new RangeError(
//         "The transformer's `readableType` field must be undefined"
//       );
//     }
//     const readableSizeAlgorithm = shared.makeSizeAlgorithmFromSizeFunction(
//       readableSizeFunction
//     );
//     const readableHWM = shared.validateAndNormalizeHighWaterMark(
//       readableHighWaterMark === undefined ? 0 : readableHighWaterMark
//     );

//     const startPromise = shared.createControlledPromise<void>();
//     ts.initializeTransformStream(
//       this,
//       startPromise.promise,
//       writableHWM,
//       writableSizeAlgorithm,
//       readableHWM,
//       readableSizeAlgorithm
//     );
//     setUpTransformStreamDefaultControllerFromTransformer(this, transformer);

//     const startResult = shared.invokeOrNoop(transformer, "start", [
//       this[ts.transformStreamController_]
//     ]);
//     startPromise.resolve(startResult);
//   }

//   get readable(): rs.SDReadableStream<OutputType> {
//     if (!ts.isTransformStream(this)) {
//       throw new TypeError();
//     }
//     return this[ts.readable_];
//   }

//   get writable(): ws.WritableStream<InputType> {
//     if (!ts.isTransformStream(this)) {
//       throw new TypeError();
//     }
//     return this[ts.writable_];
//   }
// }

// function setUpTransformStreamDefaultControllerFromTransformer<
//   InputType,
//   OutputType
// >(
//   stream: TransformStream<InputType, OutputType>,
//   transformer: ts.Transformer<InputType, OutputType>
// ): void {
//   const controller = Object.create(
//     TransformStreamDefaultController.prototype
//   ) as TransformStreamDefaultController<InputType, OutputType>;
//   let transformAlgorithm: ts.TransformAlgorithm<InputType>;

//   const transformMethod = transformer.transform;
//   if (transformMethod !== undefined) {
//     if (typeof transformMethod !== "function") {
//       throw new TypeError(
//         "`transform` field of the transformer must be a function"
//       );
//     }
//     transformAlgorithm = (chunk: InputType): Promise<any> =>
//       shared.promiseCall(transformMethod, transformer, [chunk, controller]);
//   } else {
//     // use identity transform
//     transformAlgorithm = function(chunk: InputType): Promise<void> {
//       try {
//         // OutputType and InputType are the same here
//         ts.transformStreamDefaultControllerEnqueue(
//           controller,
//           (chunk as unknown) as OutputType
//         );
//       } catch (error) {
//         return Promise.reject(error);
//       }
//       return Promise.resolve(undefined);
//     };
//   }
//   const flushAlgorithm = shared.createAlgorithmFromUnderlyingMethod(
//     transformer,
//     "flush",
//     [controller]
//   );
//   ts.setUpTransformStreamDefaultController(
//     stream,
//     controller,
//     transformAlgorithm,
//     flushAlgorithm
//   );
// }