-
Notifications
You must be signed in to change notification settings - Fork 0
/
writable-stream.js
1023 lines (794 loc) · 31.7 KB
/
writable-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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const { InvokeOrNoop, PromiseInvokeOrNoop, ValidateAndNormalizeQueuingStrategy, typeIsObject } =
require('./helpers.js');
const { assert, rethrowAssertionErrorRejection } = require('./utils.js');
const { DequeueValue, EnqueueValueWithSize, PeekQueueValue, ResetQueue } = require('./queue-with-sizes.js');
class WritableStream {
constructor(underlyingSink = {}, { size, highWaterMark = 1 } = {}) {
this._state = 'writable';
// The error that will be reported by new method calls once the state becomes errored. Only set when [[state]] is
// 'erroring' or 'errored'. May be set to an undefined value.
this._storedError = undefined;
this._writer = undefined;
// Initialize to undefined first because the constructor of the controller checks this
// variable to validate the caller.
this._writableStreamController = undefined;
// This queue is placed here instead of the writer class in order to allow for passing a writer to the next data
// producer without waiting for the queued writes to finish.
this._writeRequests = [];
// Write requests are removed from _writeRequests when write() is called on the underlying sink. This prevents
// them from being erroneously rejected on error. If a write() call is in-flight, the request is stored here.
this._inFlightWriteRequest = undefined;
// The promise that was returned from writer.close(). Stored here because it may be fulfilled after the writer
// has been detached.
this._closeRequest = undefined;
// Close request is removed from _closeRequest when close() is called on the underlying sink. This prevents it
// from being erroneously rejected on error. If a close() call is in-flight, the request is stored here.
this._inFlightCloseRequest = undefined;
// The promise that was returned from writer.abort(). This may also be fulfilled after the writer has detached.
this._pendingAbortRequest = undefined;
// The backpressure signal set by the controller.
this._backpressure = false;
const type = underlyingSink.type;
if (type !== undefined) {
throw new RangeError('Invalid type is specified');
}
this._writableStreamController = new WritableStreamDefaultController(this, underlyingSink, size, highWaterMark);
this._writableStreamController.__startSteps();
}
get locked() {
if (IsWritableStream(this) === false) {
throw streamBrandCheckException('locked');
}
return IsWritableStreamLocked(this);
}
abort(reason) {
if (IsWritableStream(this) === false) {
return Promise.reject(streamBrandCheckException('abort'));
}
if (IsWritableStreamLocked(this) === true) {
return Promise.reject(new TypeError('Cannot abort a stream that already has a writer'));
}
return WritableStreamAbort(this, reason);
}
getWriter() {
if (IsWritableStream(this) === false) {
throw streamBrandCheckException('getWriter');
}
return AcquireWritableStreamDefaultWriter(this);
}
}
module.exports = {
AcquireWritableStreamDefaultWriter,
IsWritableStream,
IsWritableStreamLocked,
WritableStream,
WritableStreamAbort,
WritableStreamDefaultControllerError,
WritableStreamDefaultWriterCloseWithErrorPropagation,
WritableStreamDefaultWriterRelease,
WritableStreamDefaultWriterWrite,
WritableStreamCloseQueuedOrInFlight
};
// Abstract operations for the WritableStream.
function AcquireWritableStreamDefaultWriter(stream) {
return new WritableStreamDefaultWriter(stream);
}
function IsWritableStream(x) {
if (!typeIsObject(x)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
return false;
}
return true;
}
function IsWritableStreamLocked(stream) {
assert(IsWritableStream(stream) === true, 'IsWritableStreamLocked should only be used on known writable streams');
if (stream._writer === undefined) {
return false;
}
return true;
}
function WritableStreamAbort(stream, reason) {
const state = stream._state;
if (state === 'closed') {
return Promise.resolve(undefined);
}
if (state === 'errored') {
return Promise.reject(stream._storedError);
}
const error = new TypeError('Requested to abort');
if (stream._pendingAbortRequest !== undefined) {
return Promise.reject(error);
}
assert(state === 'writable' || state === 'erroring', 'state must be writable or erroring');
let wasAlreadyErroring = false;
if (state === 'erroring') {
wasAlreadyErroring = true;
// reason will not be used, so don't keep a reference to it.
reason = undefined;
}
const promise = new Promise((resolve, reject) => {
stream._pendingAbortRequest = {
_resolve: resolve,
_reject: reject,
_reason: reason,
_wasAlreadyErroring: wasAlreadyErroring
};
});
if (wasAlreadyErroring === false) {
WritableStreamStartErroring(stream, error);
}
return promise;
}
// WritableStream API exposed for controllers.
function WritableStreamAddWriteRequest(stream) {
assert(IsWritableStreamLocked(stream) === true);
assert(stream._state === 'writable');
const promise = new Promise((resolve, reject) => {
const writeRequest = {
_resolve: resolve,
_reject: reject
};
stream._writeRequests.push(writeRequest);
});
return promise;
}
function WritableStreamDealWithRejection(stream, error) {
const state = stream._state;
if (state === 'writable') {
WritableStreamStartErroring(stream, error);
return;
}
assert(state === 'erroring');
WritableStreamFinishErroring(stream);
}
function WritableStreamStartErroring(stream, reason) {
assert(stream._storedError === undefined, 'stream._storedError === undefined');
assert(stream._state === 'writable', 'state must be writable');
const controller = stream._writableStreamController;
assert(controller !== undefined, 'controller must not be undefined');
stream._state = 'erroring';
stream._storedError = reason;
const writer = stream._writer;
if (writer !== undefined) {
WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason);
}
if (WritableStreamHasOperationMarkedInFlight(stream) === false && controller._started === true) {
WritableStreamFinishErroring(stream);
}
}
function WritableStreamFinishErroring(stream) {
assert(stream._state === 'erroring', 'stream._state === erroring');
assert(WritableStreamHasOperationMarkedInFlight(stream) === false,
'WritableStreamHasOperationMarkedInFlight(stream) === false');
stream._state = 'errored';
stream._writableStreamController.__errorSteps();
const storedError = stream._storedError;
for (let i = 0; i < stream._writeRequests.length; i++) {
const writeRequest = stream._writeRequests[i];
writeRequest._reject(storedError);
}
stream._writeRequests = [];
if (stream._pendingAbortRequest === undefined) {
WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
return;
}
const abortRequest = stream._pendingAbortRequest;
stream._pendingAbortRequest = undefined;
if (abortRequest._wasAlreadyErroring === true) {
abortRequest._reject(storedError);
WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
return;
}
const promise = stream._writableStreamController.__abortSteps(abortRequest._reason);
promise.then(
() => {
abortRequest._resolve();
WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
},
reason => {
abortRequest._reject(reason);
WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
});
}
function WritableStreamFinishInFlightWrite(stream) {
assert(stream._inFlightWriteRequest !== undefined);
stream._inFlightWriteRequest._resolve(undefined);
stream._inFlightWriteRequest = undefined;
}
function WritableStreamFinishInFlightWriteWithError(stream, error) {
assert(stream._inFlightWriteRequest !== undefined);
stream._inFlightWriteRequest._reject(error);
stream._inFlightWriteRequest = undefined;
assert(stream._state === 'writable' || stream._state === 'erroring');
WritableStreamDealWithRejection(stream, error);
}
function WritableStreamFinishInFlightClose(stream) {
assert(stream._inFlightCloseRequest !== undefined);
stream._inFlightCloseRequest._resolve(undefined);
stream._inFlightCloseRequest = undefined;
const state = stream._state;
assert(state === 'writable' || state === 'erroring');
if (state === 'erroring') {
// The error was too late to do anything, so it is ignored.
stream._storedError = undefined;
if (stream._pendingAbortRequest !== undefined) {
stream._pendingAbortRequest._resolve();
stream._pendingAbortRequest = undefined;
}
}
stream._state = 'closed';
const writer = stream._writer;
if (writer !== undefined) {
defaultWriterClosedPromiseResolve(writer);
}
assert(stream._pendingAbortRequest === undefined, 'stream._pendingAbortRequest === undefined');
assert(stream._storedError === undefined, 'stream._storedError === undefined');
}
function WritableStreamFinishInFlightCloseWithError(stream, error) {
assert(stream._inFlightCloseRequest !== undefined);
stream._inFlightCloseRequest._reject(error);
stream._inFlightCloseRequest = undefined;
assert(stream._state === 'writable' || stream._state === 'erroring');
// Never execute sink abort() after sink close().
if (stream._pendingAbortRequest !== undefined) {
stream._pendingAbortRequest._reject(error);
stream._pendingAbortRequest = undefined;
}
WritableStreamDealWithRejection(stream, error);
}
// TODO(ricea): Fix alphabetical order.
function WritableStreamCloseQueuedOrInFlight(stream) {
if (stream._closeRequest === undefined && stream._inFlightCloseRequest === undefined) {
return false;
}
return true;
}
function WritableStreamHasOperationMarkedInFlight(stream) {
if (stream._inFlightWriteRequest === undefined && stream._inFlightCloseRequest === undefined) {
return false;
}
return true;
}
function WritableStreamMarkCloseRequestInFlight(stream) {
assert(stream._inFlightCloseRequest === undefined);
assert(stream._closeRequest !== undefined);
stream._inFlightCloseRequest = stream._closeRequest;
stream._closeRequest = undefined;
}
function WritableStreamMarkFirstWriteRequestInFlight(stream) {
assert(stream._inFlightWriteRequest === undefined, 'there must be no pending write request');
assert(stream._writeRequests.length !== 0, 'writeRequests must not be empty');
stream._inFlightWriteRequest = stream._writeRequests.shift();
}
function WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream) {
assert(stream._state === 'errored', '_stream_.[[state]] is `"errored"`');
if (stream._closeRequest !== undefined) {
assert(stream._inFlightCloseRequest === undefined);
stream._closeRequest._reject(stream._storedError);
stream._closeRequest = undefined;
}
const writer = stream._writer;
if (writer !== undefined) {
defaultWriterClosedPromiseReject(writer, stream._storedError);
writer._closedPromise.catch(() => {});
}
}
function WritableStreamUpdateBackpressure(stream, backpressure) {
assert(stream._state === 'writable');
assert(WritableStreamCloseQueuedOrInFlight(stream) === false);
const writer = stream._writer;
if (writer !== undefined && backpressure !== stream._backpressure) {
if (backpressure === true) {
defaultWriterReadyPromiseReset(writer);
} else {
assert(backpressure === false);
defaultWriterReadyPromiseResolve(writer);
}
}
stream._backpressure = backpressure;
}
class WritableStreamDefaultWriter {
constructor(stream) {
if (IsWritableStream(stream) === false) {
throw new TypeError('WritableStreamDefaultWriter can only be constructed with a WritableStream instance');
}
if (IsWritableStreamLocked(stream) === true) {
throw new TypeError('This stream has already been locked for exclusive writing by another writer');
}
this._ownerWritableStream = stream;
stream._writer = this;
const state = stream._state;
if (state === 'writable') {
if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._backpressure === true) {
defaultWriterReadyPromiseInitialize(this);
} else {
defaultWriterReadyPromiseInitializeAsResolved(this);
}
defaultWriterClosedPromiseInitialize(this);
} else if (state === 'erroring') {
defaultWriterReadyPromiseInitializeAsRejected(this, stream._storedError);
this._readyPromise.catch(() => {});
defaultWriterClosedPromiseInitialize(this);
} else if (state === 'closed') {
defaultWriterReadyPromiseInitializeAsResolved(this);
defaultWriterClosedPromiseInitializeAsResolved(this);
} else {
assert(state === 'errored', 'state must be errored');
const storedError = stream._storedError;
defaultWriterReadyPromiseInitializeAsRejected(this, storedError);
this._readyPromise.catch(() => {});
defaultWriterClosedPromiseInitializeAsRejected(this, storedError);
this._closedPromise.catch(() => {});
}
}
get closed() {
if (IsWritableStreamDefaultWriter(this) === false) {
return Promise.reject(defaultWriterBrandCheckException('closed'));
}
return this._closedPromise;
}
get desiredSize() {
if (IsWritableStreamDefaultWriter(this) === false) {
throw defaultWriterBrandCheckException('desiredSize');
}
if (this._ownerWritableStream === undefined) {
throw defaultWriterLockException('desiredSize');
}
return WritableStreamDefaultWriterGetDesiredSize(this);
}
get ready() {
if (IsWritableStreamDefaultWriter(this) === false) {
return Promise.reject(defaultWriterBrandCheckException('ready'));
}
return this._readyPromise;
}
abort(reason) {
if (IsWritableStreamDefaultWriter(this) === false) {
return Promise.reject(defaultWriterBrandCheckException('abort'));
}
if (this._ownerWritableStream === undefined) {
return Promise.reject(defaultWriterLockException('abort'));
}
return WritableStreamDefaultWriterAbort(this, reason);
}
close() {
if (IsWritableStreamDefaultWriter(this) === false) {
return Promise.reject(defaultWriterBrandCheckException('close'));
}
const stream = this._ownerWritableStream;
if (stream === undefined) {
return Promise.reject(defaultWriterLockException('close'));
}
if (WritableStreamCloseQueuedOrInFlight(stream) === true) {
return Promise.reject(new TypeError('cannot close an already-closing stream'));
}
return WritableStreamDefaultWriterClose(this);
}
releaseLock() {
if (IsWritableStreamDefaultWriter(this) === false) {
throw defaultWriterBrandCheckException('releaseLock');
}
const stream = this._ownerWritableStream;
if (stream === undefined) {
return;
}
assert(stream._writer !== undefined);
WritableStreamDefaultWriterRelease(this);
}
write(chunk) {
if (IsWritableStreamDefaultWriter(this) === false) {
return Promise.reject(defaultWriterBrandCheckException('write'));
}
if (this._ownerWritableStream === undefined) {
return Promise.reject(defaultWriterLockException('write to'));
}
return WritableStreamDefaultWriterWrite(this, chunk);
}
}
// Abstract operations for the WritableStreamDefaultWriter.
function IsWritableStreamDefaultWriter(x) {
if (!typeIsObject(x)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
return false;
}
return true;
}
// A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
function WritableStreamDefaultWriterAbort(writer, reason) {
const stream = writer._ownerWritableStream;
assert(stream !== undefined);
return WritableStreamAbort(stream, reason);
}
function WritableStreamDefaultWriterClose(writer) {
const stream = writer._ownerWritableStream;
assert(stream !== undefined);
const state = stream._state;
if (state === 'closed' || state === 'errored') {
return Promise.reject(new TypeError(
`The stream (in ${state} state) is not in the writable state and cannot be closed`));
}
assert(state === 'writable' || state === 'erroring');
assert(WritableStreamCloseQueuedOrInFlight(stream) === false);
const promise = new Promise((resolve, reject) => {
const closeRequest = {
_resolve: resolve,
_reject: reject
};
stream._closeRequest = closeRequest;
});
if (stream._backpressure === true && state === 'writable') {
defaultWriterReadyPromiseResolve(writer);
}
WritableStreamDefaultControllerClose(stream._writableStreamController);
return promise;
}
function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {
const stream = writer._ownerWritableStream;
assert(stream !== undefined);
const state = stream._state;
if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {
return Promise.resolve();
}
if (state === 'errored') {
return Promise.reject(stream._storedError);
}
assert(state === 'writable' || state === 'erroring');
return WritableStreamDefaultWriterClose(writer);
}
function WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) {
if (writer._closedPromiseState === 'pending') {
defaultWriterClosedPromiseReject(writer, error);
} else {
defaultWriterClosedPromiseResetToRejected(writer, error);
}
writer._closedPromise.catch(() => {});
}
function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) {
if (writer._readyPromiseState === 'pending') {
defaultWriterReadyPromiseReject(writer, error);
} else {
defaultWriterReadyPromiseResetToRejected(writer, error);
}
writer._readyPromise.catch(() => {});
}
function WritableStreamDefaultWriterGetDesiredSize(writer) {
const stream = writer._ownerWritableStream;
const state = stream._state;
if (state === 'errored' || state === 'erroring') {
return null;
}
if (state === 'closed') {
return 0;
}
return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);
}
function WritableStreamDefaultWriterRelease(writer) {
const stream = writer._ownerWritableStream;
assert(stream !== undefined);
assert(stream._writer === writer);
const releasedError = new TypeError(
'Writer was released and can no longer be used to monitor the stream\'s closedness');
WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError);
// The state transitions to "errored" before the sink abort() method runs, but the writer.closed promise is not
// rejected until afterwards. This means that simply testing state will not work.
WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError);
stream._writer = undefined;
writer._ownerWritableStream = undefined;
}
function WritableStreamDefaultWriterWrite(writer, chunk) {
const stream = writer._ownerWritableStream;
assert(stream !== undefined);
const controller = stream._writableStreamController;
const chunkSize = WritableStreamDefaultControllerGetChunkSize(controller, chunk);
if (stream !== writer._ownerWritableStream) {
return Promise.reject(defaultWriterLockException('write to'));
}
const state = stream._state;
if (state === 'errored') {
return Promise.reject(stream._storedError);
}
if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {
return Promise.reject(new TypeError('The stream is closing or closed and cannot be written to'));
}
if (state === 'erroring') {
return Promise.reject(stream._storedError);
}
assert(state === 'writable');
const promise = WritableStreamAddWriteRequest(stream);
WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);
return promise;
}
class WritableStreamDefaultController {
constructor(stream, underlyingSink, size, highWaterMark) {
if (IsWritableStream(stream) === false) {
throw new TypeError('WritableStreamDefaultController can only be constructed with a WritableStream instance');
}
if (stream._writableStreamController !== undefined) {
throw new TypeError(
'WritableStreamDefaultController instances can only be created by the WritableStream constructor');
}
this._controlledWritableStream = stream;
this._underlyingSink = underlyingSink;
// Need to set the slots so that the assert doesn't fire. In the spec the slots already exist implicitly.
this._queue = undefined;
this._queueTotalSize = undefined;
ResetQueue(this);
this._started = false;
const normalizedStrategy = ValidateAndNormalizeQueuingStrategy(size, highWaterMark);
this._strategySize = normalizedStrategy.size;
this._strategyHWM = normalizedStrategy.highWaterMark;
const backpressure = WritableStreamDefaultControllerGetBackpressure(this);
WritableStreamUpdateBackpressure(stream, backpressure);
}
error(e) {
if (IsWritableStreamDefaultController(this) === false) {
throw new TypeError(
'WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
}
const state = this._controlledWritableStream._state;
if (state !== 'writable') {
// The stream is closed, errored or will be soon. The sink can't do anything useful if it gets an error here, so
// just treat it as a no-op.
return;
}
WritableStreamDefaultControllerError(this, e);
}
__abortSteps(reason) {
return PromiseInvokeOrNoop(this._underlyingSink, 'abort', [reason]);
}
__errorSteps() {
ResetQueue(this);
}
__startSteps() {
const startResult = InvokeOrNoop(this._underlyingSink, 'start', [this]);
const stream = this._controlledWritableStream;
Promise.resolve(startResult).then(
() => {
assert(stream._state === 'writable' || stream._state === 'erroring');
this._started = true;
WritableStreamDefaultControllerAdvanceQueueIfNeeded(this);
},
r => {
assert(stream._state === 'writable' || stream._state === 'erroring');
this._started = true;
WritableStreamDealWithRejection(stream, r);
}
)
.catch(rethrowAssertionErrorRejection);
}
}
// Abstract operations implementing interface required by the WritableStream.
function WritableStreamDefaultControllerClose(controller) {
EnqueueValueWithSize(controller, 'close', 0);
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
}
function WritableStreamDefaultControllerGetChunkSize(controller, chunk) {
const strategySize = controller._strategySize;
if (strategySize === undefined) {
return 1;
}
try {
return strategySize(chunk);
} catch (chunkSizeE) {
WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);
return 1;
}
}
function WritableStreamDefaultControllerGetDesiredSize(controller) {
return controller._strategyHWM - controller._queueTotalSize;
}
function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) {
const writeRecord = { chunk };
try {
EnqueueValueWithSize(controller, writeRecord, chunkSize);
} catch (enqueueE) {
WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);
return;
}
const stream = controller._controlledWritableStream;
if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._state === 'writable') {
const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
WritableStreamUpdateBackpressure(stream, backpressure);
}
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
}
// Abstract operations for the WritableStreamDefaultController.
function IsWritableStreamDefaultController(x) {
if (!typeIsObject(x)) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(x, '_underlyingSink')) {
return false;
}
return true;
}
function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {
const stream = controller._controlledWritableStream;
if (controller._started === false) {
return;
}
if (stream._inFlightWriteRequest !== undefined) {
return;
}
const state = stream._state;
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);
}
}
function WritableStreamDefaultControllerErrorIfNeeded(controller, error) {
if (controller._controlledWritableStream._state === 'writable') {
WritableStreamDefaultControllerError(controller, error);
}
}
function WritableStreamDefaultControllerProcessClose(controller) {
const stream = controller._controlledWritableStream;
WritableStreamMarkCloseRequestInFlight(stream);
DequeueValue(controller);
assert(controller._queue.length === 0, 'queue must be empty once the final write record is dequeued');
const sinkClosePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'close', []);
sinkClosePromise.then(
() => {
WritableStreamFinishInFlightClose(stream);
},
reason => {
WritableStreamFinishInFlightCloseWithError(stream, reason);
}
)
.catch(rethrowAssertionErrorRejection);
}
function WritableStreamDefaultControllerProcessWrite(controller, chunk) {
const stream = controller._controlledWritableStream;
WritableStreamMarkFirstWriteRequestInFlight(stream);
const sinkWritePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'write', [chunk, controller]);
sinkWritePromise.then(
() => {
WritableStreamFinishInFlightWrite(stream);
const state = stream._state;
assert(state === 'writable' || state === 'erroring');
DequeueValue(controller);
if (WritableStreamCloseQueuedOrInFlight(stream) === false && state === 'writable') {
const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
WritableStreamUpdateBackpressure(stream, backpressure);
}
WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
},
reason => {
WritableStreamFinishInFlightWriteWithError(stream, reason);
}
)
.catch(rethrowAssertionErrorRejection);
}
function WritableStreamDefaultControllerGetBackpressure(controller) {
const desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);
return desiredSize <= 0;
}
// A client of WritableStreamDefaultController may use these functions directly to bypass state check.
function WritableStreamDefaultControllerError(controller, error) {
const stream = controller._controlledWritableStream;
assert(stream._state === 'writable');
WritableStreamStartErroring(stream, error);
}
// Helper functions for the WritableStream.
function streamBrandCheckException(name) {
return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
}
// Helper functions for the WritableStreamDefaultWriter.
function defaultWriterBrandCheckException(name) {
return new TypeError(
`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
}
function defaultWriterLockException(name) {
return new TypeError('Cannot ' + name + ' a stream using a released writer');
}
function defaultWriterClosedPromiseInitialize(writer) {
writer._closedPromise = new Promise((resolve, reject) => {
writer._closedPromise_resolve = resolve;
writer._closedPromise_reject = reject;
writer._closedPromiseState = 'pending';
});
}
function defaultWriterClosedPromiseInitializeAsRejected(writer, reason) {
writer._closedPromise = Promise.reject(reason);
writer._closedPromise_resolve = undefined;
writer._closedPromise_reject = undefined;
writer._closedPromiseState = 'rejected';
}
function defaultWriterClosedPromiseInitializeAsResolved(writer) {
writer._closedPromise = Promise.resolve(undefined);
writer._closedPromise_resolve = undefined;
writer._closedPromise_reject = undefined;
writer._closedPromiseState = 'resolved';
}
function defaultWriterClosedPromiseReject(writer, reason) {
assert(writer._closedPromise_resolve !== undefined, 'writer._closedPromise_resolve !== undefined');
assert(writer._closedPromise_reject !== undefined, 'writer._closedPromise_reject !== undefined');
assert(writer._closedPromiseState === 'pending', 'writer._closedPromiseState is pending');
writer._closedPromise_reject(reason);
writer._closedPromise_resolve = undefined;
writer._closedPromise_reject = undefined;
writer._closedPromiseState = 'rejected';
}
function defaultWriterClosedPromiseResetToRejected(writer, reason) {
assert(writer._closedPromise_resolve === undefined, 'writer._closedPromise_resolve === undefined');
assert(writer._closedPromise_reject === undefined, 'writer._closedPromise_reject === undefined');
assert(writer._closedPromiseState !== 'pending', 'writer._closedPromiseState is not pending');
writer._closedPromise = Promise.reject(reason);
writer._closedPromiseState = 'rejected';
}
function defaultWriterClosedPromiseResolve(writer) {
assert(writer._closedPromise_resolve !== undefined, 'writer._closedPromise_resolve !== undefined');
assert(writer._closedPromise_reject !== undefined, 'writer._closedPromise_reject !== undefined');
assert(writer._closedPromiseState === 'pending', 'writer._closedPromiseState is pending');
writer._closedPromise_resolve(undefined);
writer._closedPromise_resolve = undefined;
writer._closedPromise_reject = undefined;
writer._closedPromiseState = 'resolved';
}
function defaultWriterReadyPromiseInitialize(writer) {
writer._readyPromise = new Promise((resolve, reject) => {
writer._readyPromise_resolve = resolve;
writer._readyPromise_reject = reject;
});
writer._readyPromiseState = 'pending';
}
function defaultWriterReadyPromiseInitializeAsRejected(writer, reason) {
writer._readyPromise = Promise.reject(reason);
writer._readyPromise_resolve = undefined;
writer._readyPromise_reject = undefined;
writer._readyPromiseState = 'rejected';
}
function defaultWriterReadyPromiseInitializeAsResolved(writer) {
writer._readyPromise = Promise.resolve(undefined);
writer._readyPromise_resolve = undefined;
writer._readyPromise_reject = undefined;
writer._readyPromiseState = 'fulfilled';
}
function defaultWriterReadyPromiseReject(writer, reason) {
assert(writer._readyPromise_resolve !== undefined, 'writer._readyPromise_resolve !== undefined');
assert(writer._readyPromise_reject !== undefined, 'writer._readyPromise_reject !== undefined');
writer._readyPromise_reject(reason);
writer._readyPromise_resolve = undefined;
writer._readyPromise_reject = undefined;
writer._readyPromiseState = 'rejected';
}
function defaultWriterReadyPromiseReset(writer) {
assert(writer._readyPromise_resolve === undefined, 'writer._readyPromise_resolve === undefined');
assert(writer._readyPromise_reject === undefined, 'writer._readyPromise_reject === undefined');
writer._readyPromise = new Promise((resolve, reject) => {