-
Notifications
You must be signed in to change notification settings - Fork 1
/
writable_stream_controller.ts
318 lines (298 loc) · 9.13 KB
/
writable_stream_controller.ts
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
import { SizeAlgorithm, StartAlgorithm } from "./readable_stream.ts";
import {
WritableStream,
AbortAlgorithm,
CloseAlgorithm,
IsWritableStream,
WriteAlgorithm,
WritableStreamUpdateBackpressure,
WritableStreamDealWithRejection,
WritableStreamCloseQueuedOrInFlight,
WritableStreamFinishErroring,
WritableStreamMarkCloseRequestInFlight,
WritableStreamFinishInFlightClose,
WritableStreamFinishInFlightCloseWithError,
WritableStreamMarkFirstWriteRequestInFlight,
WritableStreamFinishInFlightWrite,
WritableStreamStartErroring
} from "./writable_stream.ts";
import { Assert } from "./util.ts";
import {
CreateAlgorithmFromUnderlyingMethod,
DequeueValue,
EnqueueValueWithSize,
InvokeOrNoop,
PeekQueueValue,
ResetQueue
} from "./misc.ts";
export interface WritableStreamController {
error(e);
}
export const ErrorSteps = Symbol("ErrorSteps");
export const AbortSteps = Symbol("AbortSteps");
export function createWritableStreamDefaultController<
T
>(): WritableStreamDefaultController<T> {
const ret = Object.create(WritableStreamDefaultController.prototype);
ret[ErrorSteps] = () => ResetQueue(ret);
ret[AbortSteps] = reason => {
const result = ret.abortAlgorithm(reason);
WritableStreamDefaultControllerClearAlgorithms(ret);
return result;
};
return ret;
}
export class WritableStreamDefaultController<T>
implements WritableStreamController {
abortAlgorithm: AbortAlgorithm;
closeAlgorithm: CloseAlgorithm;
controlledWritableStream: WritableStream;
queue: ("close" | { chunk: T })[];
queueTotalSize: number;
started: boolean;
strategyHWM: number;
strategySizeAlgorithm: SizeAlgorithm;
writeAlgorithm: WriteAlgorithm<T>;
constructor() {
throw new TypeError();
}
error(e) {
if (!IsWritableStreamDefaultController(this)) {
throw new TypeError("this is not WritableStreamDefaultController");
}
const { state } = this.controlledWritableStream;
if (state !== "writable") {
return;
}
WritableStreamDefaultControllerError(this, e);
}
}
export function IsWritableStreamDefaultController<T>(
x
): x is WritableStreamDefaultController<T> {
return typeof x === "object" && x.hasOwnProperty("controlledWritableStream");
}
export function SetUpWritableStreamDefaultController<T>(params: {
stream: WritableStream;
controller: WritableStreamDefaultController<T>;
startAlgorithm: StartAlgorithm;
writeAlgorithm: WriteAlgorithm<T>;
closeAlgorithm: CloseAlgorithm;
abortAlgorithm: AbortAlgorithm;
highWaterMark: number;
sizeAlgorithm: SizeAlgorithm;
}) {
const {
stream,
controller,
startAlgorithm,
writeAlgorithm,
closeAlgorithm,
abortAlgorithm,
highWaterMark,
sizeAlgorithm
} = params;
Assert(IsWritableStream(stream));
Assert(stream.writableStreamController === void 0);
controller.controlledWritableStream = stream;
stream.writableStreamController = controller;
ResetQueue(controller);
controller.started = false;
controller.strategySizeAlgorithm = sizeAlgorithm;
controller.strategyHWM = highWaterMark;
controller.writeAlgorithm = writeAlgorithm;
controller.closeAlgorithm = closeAlgorithm;
controller.abortAlgorithm = abortAlgorithm;
const backpressure = WritableStreamDefaultControllerGetBackpressure(
controller
);
WritableStreamUpdateBackpressure(stream, backpressure);
Promise.resolve(startAlgorithm())
.then(() => {
Assert(stream.state === "writable" || stream.state === "erroring");
controller.started = true;
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
})
.catch(r => {
Assert(stream.state === "writable" || stream.state === "erroring");
controller.started = true;
WritableStreamDealWithRejection(stream, r);
});
}
export function SetUpWritableStreamDefaultControllerFromUnderlyingSink(
stream: WritableStream,
underlyingSink,
highWaterMark: number,
sizeAlgorithm: SizeAlgorithm
) {
Assert(underlyingSink !== void 0);
const controller = createWritableStreamDefaultController();
const startAlgorithm = () =>
InvokeOrNoop(underlyingSink, "start", controller);
const writeAlgorithm = CreateAlgorithmFromUnderlyingMethod(
underlyingSink,
"write",
1,
controller
);
const closeAlgorithm = CreateAlgorithmFromUnderlyingMethod(
underlyingSink,
"close",
0
);
const abortAlgorithm = CreateAlgorithmFromUnderlyingMethod(
underlyingSink,
"abort",
1
);
SetUpWritableStreamDefaultController({
stream,
controller,
startAlgorithm,
writeAlgorithm,
closeAlgorithm,
abortAlgorithm,
highWaterMark,
sizeAlgorithm
});
}
export function WritableStreamDefaultControllerClearAlgorithms<T>(
controller: WritableStreamDefaultController<T>
) {
controller.writeAlgorithm = void 0;
controller.closeAlgorithm = void 0;
controller.abortAlgorithm = void 0;
controller.strategySizeAlgorithm = void 0;
}
export function WritableStreamDefaultControllerClose<T>(
controller: WritableStreamDefaultController<T>
) {
EnqueueValueWithSize(controller, "close", 0);
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
}
export function WritableStreamDefaultControllerGetChunkSize<T>(
controller: WritableStreamDefaultController<T>,
chunk
): number {
try {
return controller.strategySizeAlgorithm(chunk);
} catch (e) {
WritableStreamDefaultControllerErrorIfNeeded(controller, e);
return 1;
}
}
export function WritableStreamDefaultControllerGetDesiredSize<T>(
controller: WritableStreamDefaultController<T>
): number {
return controller.strategyHWM - controller.queueTotalSize;
}
export function WritableStreamDefaultControllerWrite<T>(
controller: WritableStreamDefaultController<T>,
chunk,
chunkSize: number
) {
const writeRecord = { chunk };
try {
EnqueueValueWithSize(controller, writeRecord, chunkSize);
} catch (e) {
WritableStreamDefaultControllerErrorIfNeeded(controller, e);
return;
}
const stream = controller.controlledWritableStream;
if (
!WritableStreamCloseQueuedOrInFlight(stream) &&
stream.state === "writable"
) {
const backpressure = WritableStreamDefaultControllerGetBackpressure(
controller
);
WritableStreamUpdateBackpressure(stream, backpressure);
}
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
}
export function WritableStreamDefaultControllerAdvanceQueueIfNeeded<T>(
controller: WritableStreamDefaultController<T>
) {
const stream = controller.controlledWritableStream;
if (!controller.started) {
return;
}
if (stream.inFlightWriteRequest !== void 0) {
return;
}
const { state } = stream;
if (state === "closed" || state === "errored") {
return;
}
if (state === "erroring") {
WritableStreamFinishErroring(stream);
return;
}
if (controller.queue.length === 0) {
return;
}
const writeRecord = PeekQueueValue(controller);
if (writeRecord === "close") {
WritableStreamDefaultControllerProcessClose(controller);
} else {
WritableStreamDefaultControllerProcessWrite(controller, writeRecord.chunk);
}
}
export function WritableStreamDefaultControllerErrorIfNeeded<T>(
controller: WritableStreamDefaultController<T>,
error
) {
if (controller.controlledWritableStream.state === "writable") {
WritableStreamDefaultControllerError(controller, error);
}
}
export function WritableStreamDefaultControllerProcessClose<T>(
controller: WritableStreamDefaultController<T>
) {
const stream = controller.controlledWritableStream;
WritableStreamMarkCloseRequestInFlight(stream);
DequeueValue(controller);
Assert(controller.queue.length === 0);
const sinkClosePromise = controller.closeAlgorithm();
WritableStreamDefaultControllerClearAlgorithms(controller);
sinkClosePromise
.then(() => {
WritableStreamFinishInFlightClose(stream);
})
.catch(r => {
WritableStreamFinishInFlightCloseWithError(stream, r);
});
}
export function WritableStreamDefaultControllerProcessWrite<T>(
controller: WritableStreamDefaultController<T>,
chunk
) {
const stream = controller.controlledWritableStream;
WritableStreamMarkFirstWriteRequestInFlight(stream);
const sinkWritePromise = controller.writeAlgorithm(chunk);
sinkWritePromise.then(() => {
WritableStreamFinishInFlightWrite(stream);
const { state } = stream;
Assert(state === "writable" || state === "erroring");
DequeueValue(controller);
if (!WritableStreamCloseQueuedOrInFlight(stream) && state === "writable") {
const bp = WritableStreamDefaultControllerGetBackpressure(controller);
WritableStreamUpdateBackpressure(stream, bp);
}
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
});
}
export function WritableStreamDefaultControllerGetBackpressure<T>(
controller: WritableStreamDefaultController<T>
) {
return WritableStreamDefaultControllerGetDesiredSize(controller) <= 0;
}
export function WritableStreamDefaultControllerError<T>(
controller: WritableStreamDefaultController<T>,
error
) {
const stream = controller.controlledWritableStream;
Assert(stream.state === "writable");
WritableStreamDefaultControllerClearAlgorithms(controller);
WritableStreamStartErroring(stream, error);
}