-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform-stream.js
450 lines (338 loc) · 14.2 KB
/
transform-stream.js
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
'use strict';
const { assert } = require('./utils.js');
const { InvokeOrNoop, PromiseInvokeOrPerformFallback, PromiseInvokeOrNoop, typeIsObject } = require('./helpers.js');
const { ReadableStream, ReadableStreamDefaultControllerClose,
ReadableStreamDefaultControllerEnqueue, ReadableStreamDefaultControllerError,
ReadableStreamDefaultControllerGetDesiredSize } = require('./readable-stream.js');
const { WritableStream, WritableStreamDefaultControllerError } = require('./writable-stream.js');
// Methods on the transform stream controller object
function TransformStreamCloseReadable(transformStream) {
// console.log('TransformStreamCloseReadable()');
if (transformStream._errored === true) {
throw new TypeError('TransformStream is already errored');
}
if (transformStream._readableClosed === true) {
throw new TypeError('Readable side is already closed');
}
TransformStreamCloseReadableInternal(transformStream);
}
function TransformStreamEnqueueToReadable(transformStream, chunk) {
// console.log('TransformStreamEnqueueToReadable()');
if (transformStream._errored === true) {
throw new TypeError('TransformStream is already errored');
}
if (transformStream._readableClosed === true) {
throw new TypeError('Readable side is already closed');
}
// We throttle transformer.transform invocation based on the backpressure of the ReadableStream, but we still
// accept TransformStreamEnqueueToReadable() calls.
const controller = transformStream._readableController;
try {
ReadableStreamDefaultControllerEnqueue(controller, chunk);
} catch (e) {
// This happens when readableStrategy.size() throws.
// The ReadableStream has already errored itself.
transformStream._readableClosed = true;
TransformStreamErrorIfNeeded(transformStream, e);
throw transformStream._storedError;
}
const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);
const maybeBackpressure = desiredSize <= 0;
if (maybeBackpressure === true && transformStream._backpressure === false) {
// This allows pull() again. When desiredSize is 0, it's possible that a pull() will happen immediately (but
// asynchronously) after this because of pending read()s and set _backpressure back to false.
//
// If pull() could be called from inside enqueue(), then this logic would be wrong. This cannot happen
// because there is always a promise pending from start() or pull() when _backpressure is false.
TransformStreamSetBackpressure(transformStream, true);
}
}
function TransformStreamError(transformStream, e) {
if (transformStream._errored === true) {
throw new TypeError('TransformStream is already errored');
}
TransformStreamErrorInternal(transformStream, e);
}
// Abstract operations.
function TransformStreamCloseReadableInternal(transformStream) {
assert(transformStream._errored === false);
assert(transformStream._readableClosed === false);
try {
ReadableStreamDefaultControllerClose(transformStream._readableController);
} catch (e) {
assert(false);
}
transformStream._readableClosed = true;
}
function TransformStreamErrorIfNeeded(transformStream, e) {
if (transformStream._errored === false) {
TransformStreamErrorInternal(transformStream, e);
}
}
function TransformStreamErrorInternal(transformStream, e) {
// console.log('TransformStreamErrorInternal()');
assert(transformStream._errored === false);
transformStream._errored = true;
transformStream._storedError = e;
if (transformStream._writableDone === false) {
WritableStreamDefaultControllerError(transformStream._writableController, e);
}
if (transformStream._readableClosed === false) {
ReadableStreamDefaultControllerError(transformStream._readableController, e);
}
}
// Used for preventing the next write() call on TransformStreamSink until there
// is no longer backpressure.
function TransformStreamReadableReadyPromise(transformStream) {
assert(transformStream._backpressureChangePromise !== undefined,
'_backpressureChangePromise should have been initialized');
if (transformStream._backpressure === false) {
return Promise.resolve();
}
assert(transformStream._backpressure === true, '_backpressure should have been initialized');
return transformStream._backpressureChangePromise;
}
function TransformStreamSetBackpressure(transformStream, backpressure) {
// console.log(`TransformStreamSetBackpressure(${backpressure})`);
// Passes also when called during construction.
assert(transformStream._backpressure !== backpressure,
'TransformStreamSetBackpressure() should be called only when backpressure is changed');
if (transformStream._backpressureChangePromise !== undefined) {
// The fulfillment value is just for a sanity check.
transformStream._backpressureChangePromise_resolve(backpressure);
}
transformStream._backpressureChangePromise = new Promise(resolve => {
transformStream._backpressureChangePromise_resolve = resolve;
});
transformStream._backpressureChangePromise.then(resolution => {
assert(resolution !== backpressure,
'_backpressureChangePromise should be fulfilled only when backpressure is changed');
});
transformStream._backpressure = backpressure;
}
function TransformStreamDefaultTransform(chunk, transformStreamController) {
const transformStream = transformStreamController._controlledTransformStream;
TransformStreamEnqueueToReadable(transformStream, chunk);
return Promise.resolve();
}
function TransformStreamTransform(transformStream, chunk) {
// console.log('TransformStreamTransform()');
assert(transformStream._errored === false);
assert(transformStream._transforming === false);
assert(transformStream._backpressure === false);
transformStream._transforming = true;
const transformer = transformStream._transformer;
const controller = transformStream._transformStreamController;
const transformPromise = PromiseInvokeOrPerformFallback(transformer, 'transform', [chunk, controller],
TransformStreamDefaultTransform, [chunk, controller]);
return transformPromise.then(
() => {
transformStream._transforming = false;
return TransformStreamReadableReadyPromise(transformStream);
},
e => {
TransformStreamErrorIfNeeded(transformStream, e);
return Promise.reject(e);
});
}
function IsTransformStreamDefaultController(x) {
if (!typeIsObject(x)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
return false;
}
return true;
}
function IsTransformStream(x) {
if (!typeIsObject(x)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
return false;
}
return true;
}
class TransformStreamSink {
constructor(transformStream, startPromise) {
this._transformStream = transformStream;
this._startPromise = startPromise;
}
start(c) {
const transformStream = this._transformStream;
transformStream._writableController = c;
return this._startPromise.then(() => TransformStreamReadableReadyPromise(transformStream));
}
write(chunk) {
// console.log('TransformStreamSink.write()');
const transformStream = this._transformStream;
return TransformStreamTransform(transformStream, chunk);
}
abort() {
const transformStream = this._transformStream;
transformStream._writableDone = true;
TransformStreamErrorInternal(transformStream, new TypeError('Writable side aborted'));
}
close() {
// console.log('TransformStreamSink.close()');
const transformStream = this._transformStream;
assert(transformStream._transforming === false);
transformStream._writableDone = true;
const flushPromise = PromiseInvokeOrNoop(transformStream._transformer,
'flush', [transformStream._transformStreamController]);
// Return a promise that is fulfilled with undefined on success.
return flushPromise.then(() => {
if (transformStream._errored === true) {
return Promise.reject(transformStream._storedError);
}
if (transformStream._readableClosed === false) {
TransformStreamCloseReadableInternal(transformStream);
}
return Promise.resolve();
}).catch(r => {
TransformStreamErrorIfNeeded(transformStream, r);
return Promise.reject(transformStream._storedError);
});
}
}
class TransformStreamSource {
constructor(transformStream, startPromise) {
this._transformStream = transformStream;
this._startPromise = startPromise;
}
start(c) {
const transformStream = this._transformStream;
transformStream._readableController = c;
return this._startPromise.then(() => {
// Prevent the first pull() call until there is backpressure.
assert(transformStream._backpressureChangePromise !== undefined,
'_backpressureChangePromise should have been initialized');
if (transformStream._backpressure === true) {
return Promise.resolve();
}
assert(transformStream._backpressure === false, '_backpressure should have been initialized');
return transformStream._backpressureChangePromise;
});
}
pull() {
// console.log('TransformStreamSource.pull()');
const transformStream = this._transformStream;
// Invariant. Enforced by the promises returned by start() and pull().
assert(transformStream._backpressure === true, 'pull() should be never called while _backpressure is false');
assert(transformStream._backpressureChangePromise !== undefined,
'_backpressureChangePromise should have been initialized');
TransformStreamSetBackpressure(transformStream, false);
// Prevent the next pull() call until there is backpressure.
return transformStream._backpressureChangePromise;
}
cancel() {
const transformStream = this._transformStream;
transformStream._readableClosed = true;
TransformStreamErrorInternal(transformStream, new TypeError('Readable side canceled'));
}
}
class TransformStreamDefaultController {
constructor(transformStream) {
if (IsTransformStream(transformStream) === false) {
throw new TypeError('TransformStreamDefaultController can only be ' +
'constructed with a TransformStream instance');
}
if (transformStream._transformStreamController !== undefined) {
throw new TypeError('TransformStreamDefaultController instances can ' +
'only be created by the TransformStream constructor');
}
this._controlledTransformStream = transformStream;
}
get desiredSize() {
if (IsTransformStreamDefaultController(this) === false) {
throw defaultControllerBrandCheckException('desiredSize');
}
const transformStream = this._controlledTransformStream;
const readableController = transformStream._readableController;
return ReadableStreamDefaultControllerGetDesiredSize(readableController);
}
enqueue(chunk) {
if (IsTransformStreamDefaultController(this) === false) {
throw defaultControllerBrandCheckException('enqueue');
}
TransformStreamEnqueueToReadable(this._controlledTransformStream, chunk);
}
close() {
if (IsTransformStreamDefaultController(this) === false) {
throw defaultControllerBrandCheckException('close');
}
TransformStreamCloseReadable(this._controlledTransformStream);
}
error(reason) {
if (IsTransformStreamDefaultController(this) === false) {
throw defaultControllerBrandCheckException('error');
}
TransformStreamError(this._controlledTransformStream, reason);
}
}
class TransformStream {
constructor(transformer = {}) {
this._transformer = transformer;
const { readableStrategy, writableStrategy } = transformer;
this._transforming = false;
this._errored = false;
this._storedError = undefined;
this._writableController = undefined;
this._readableController = undefined;
this._transformStreamController = undefined;
this._writableDone = false;
this._readableClosed = false;
this._backpressure = undefined;
this._backpressureChangePromise = undefined;
this._backpressureChangePromise_resolve = undefined;
this._transformStreamController = new TransformStreamDefaultController(this);
let startPromise_resolve;
const startPromise = new Promise(resolve => {
startPromise_resolve = resolve;
});
const source = new TransformStreamSource(this, startPromise);
this._readable = new ReadableStream(source, readableStrategy);
const sink = new TransformStreamSink(this, startPromise);
this._writable = new WritableStream(sink, writableStrategy);
assert(this._writableController !== undefined);
assert(this._readableController !== undefined);
const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(this._readableController);
// Set _backpressure based on desiredSize. As there is no read() at this point, we can just interpret
// desiredSize being non-positive as backpressure.
TransformStreamSetBackpressure(this, desiredSize <= 0);
const transformStream = this;
const startResult = InvokeOrNoop(transformer, 'start',
[transformStream._transformStreamController]);
startPromise_resolve(startResult);
startPromise.catch(e => {
// The underlyingSink and underlyingSource will error the readable and writable ends on their own.
if (transformStream._errored === false) {
transformStream._errored = true;
transformStream._storedError = e;
}
});
}
get readable() {
if (IsTransformStream(this) === false) {
throw streamBrandCheckException('readable');
}
return this._readable;
}
get writable() {
if (IsTransformStream(this) === false) {
throw streamBrandCheckException('writable');
}
return this._writable;
}
}
module.exports = { TransformStream };
// Helper functions for the TransformStreamDefaultController.
function defaultControllerBrandCheckException(name) {
return new TypeError(
`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
}
// Helper functions for the TransformStream.
function streamBrandCheckException(name) {
return new TypeError(
`TransformStream.prototype.${name} can only be used on a TransformStream`);
}