web-streams-polyfill 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * web-streams-polyfill v3.0.0
2
+ * web-streams-polyfill v3.1.0
3
3
  */
4
4
  /// <reference lib="es2015.symbol" />
5
5
  const SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
@@ -8,7 +8,7 @@ const SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator ==
8
8
 
9
9
  /// <reference lib="dom" />
10
10
  function noop() {
11
- // do nothing
11
+ return undefined;
12
12
  }
13
13
  function getGlobals() {
14
14
  if (typeof self !== 'undefined') {
@@ -27,7 +27,7 @@ const globals = getGlobals();
27
27
  function typeIsObject(x) {
28
28
  return (typeof x === 'object' && x !== null) || typeof x === 'function';
29
29
  }
30
- const rethrowAssertionErrorRejection = noop;
30
+ const rethrowAssertionErrorRejection = noop;
31
31
 
32
32
  const originalPromise = Promise;
33
33
  const originalPromiseThen = Promise.prototype.then;
@@ -242,6 +242,9 @@ function defaultReaderClosedPromiseInitializeAsResolved(reader) {
242
242
  defaultReaderClosedPromiseResolve(reader);
243
243
  }
244
244
  function defaultReaderClosedPromiseReject(reader, reason) {
245
+ if (reader._closedPromise_reject === undefined) {
246
+ return;
247
+ }
245
248
  setPromiseIsHandledToTrue(reader._closedPromise);
246
249
  reader._closedPromise_reject(reason);
247
250
  reader._closedPromise_resolve = undefined;
@@ -251,6 +254,9 @@ function defaultReaderClosedPromiseResetToRejected(reader, reason) {
251
254
  defaultReaderClosedPromiseInitializeAsRejected(reader, reason);
252
255
  }
253
256
  function defaultReaderClosedPromiseResolve(reader) {
257
+ if (reader._closedPromise_resolve === undefined) {
258
+ return;
259
+ }
254
260
  reader._closedPromise_resolve(undefined);
255
261
  reader._closedPromise_resolve = undefined;
256
262
  reader._closedPromise_reject = undefined;
@@ -377,6 +383,11 @@ function ReadableStreamHasDefaultReader(stream) {
377
383
  }
378
384
  return true;
379
385
  }
386
+ /**
387
+ * A default reader vended by a {@link ReadableStream}.
388
+ *
389
+ * @public
390
+ */
380
391
  class ReadableStreamDefaultReader {
381
392
  constructor(stream) {
382
393
  assertRequiredArgument(stream, 1, 'ReadableStreamDefaultReader');
@@ -387,12 +398,19 @@ class ReadableStreamDefaultReader {
387
398
  ReadableStreamReaderGenericInitialize(this, stream);
388
399
  this._readRequests = new SimpleQueue();
389
400
  }
401
+ /**
402
+ * Returns a promise that will be fulfilled when the stream becomes closed,
403
+ * or rejected if the stream ever errors or the reader's lock is released before the stream finishes closing.
404
+ */
390
405
  get closed() {
391
406
  if (!IsReadableStreamDefaultReader(this)) {
392
407
  return promiseRejectedWith(defaultReaderBrandCheckException('closed'));
393
408
  }
394
409
  return this._closedPromise;
395
410
  }
411
+ /**
412
+ * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
413
+ */
396
414
  cancel(reason = undefined) {
397
415
  if (!IsReadableStreamDefaultReader(this)) {
398
416
  return promiseRejectedWith(defaultReaderBrandCheckException('cancel'));
@@ -402,6 +420,11 @@ class ReadableStreamDefaultReader {
402
420
  }
403
421
  return ReadableStreamReaderGenericCancel(this, reason);
404
422
  }
423
+ /**
424
+ * Returns a promise that allows access to the next chunk from the stream's internal queue, if available.
425
+ *
426
+ * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
427
+ */
405
428
  read() {
406
429
  if (!IsReadableStreamDefaultReader(this)) {
407
430
  return promiseRejectedWith(defaultReaderBrandCheckException('read'));
@@ -423,6 +446,15 @@ class ReadableStreamDefaultReader {
423
446
  ReadableStreamDefaultReaderRead(this, readRequest);
424
447
  return promise;
425
448
  }
449
+ /**
450
+ * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
451
+ * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
452
+ * from now on; otherwise, the reader will appear closed.
453
+ *
454
+ * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
455
+ * the reader's {@link ReadableStreamDefaultReader.read | read()} method has not yet been settled. Attempting to
456
+ * do so will throw a `TypeError` and leave the reader locked to the stream.
457
+ */
426
458
  releaseLock() {
427
459
  if (!IsReadableStreamDefaultReader(this)) {
428
460
  throw defaultReaderBrandCheckException('releaseLock');
@@ -456,7 +488,7 @@ function IsReadableStreamDefaultReader(x) {
456
488
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
457
489
  return false;
458
490
  }
459
- return true;
491
+ return x instanceof ReadableStreamDefaultReader;
460
492
  }
461
493
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
462
494
  const stream = reader._ownerReadableStream;
@@ -588,7 +620,14 @@ function IsReadableStreamAsyncIterator(x) {
588
620
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
589
621
  return false;
590
622
  }
591
- return true;
623
+ try {
624
+ // noinspection SuspiciousTypeOfGuard
625
+ return x._asyncIteratorImpl instanceof
626
+ ReadableStreamAsyncIteratorImpl;
627
+ }
628
+ catch (_a) {
629
+ return false;
630
+ }
592
631
  }
593
632
  // Helper functions for the ReadableStream.
594
633
  function streamAsyncIteratorBrandCheckException(name) {
@@ -602,15 +641,35 @@ const NumberIsNaN = Number.isNaN || function (x) {
602
641
  return x !== x;
603
642
  };
604
643
 
605
- function IsFiniteNonNegativeNumber(v) {
606
- if (!IsNonNegativeNumber(v)) {
607
- return false;
608
- }
609
- if (v === Infinity) {
610
- return false;
644
+ function CreateArrayFromList(elements) {
645
+ // We use arrays to represent lists, so this is basically a no-op.
646
+ // Do a slice though just in case we happen to depend on the unique-ness.
647
+ return elements.slice();
648
+ }
649
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
650
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
651
+ }
652
+ // Not implemented correctly
653
+ function TransferArrayBuffer(O) {
654
+ return O;
655
+ }
656
+ // Not implemented correctly
657
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
658
+ function IsDetachedBuffer(O) {
659
+ return false;
660
+ }
661
+ function ArrayBufferSlice(buffer, begin, end) {
662
+ // ArrayBuffer.prototype.slice is not available on IE10
663
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
664
+ if (buffer.slice) {
665
+ return buffer.slice(begin, end);
611
666
  }
612
- return true;
667
+ const length = end - begin;
668
+ const slice = new ArrayBuffer(length);
669
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
670
+ return slice;
613
671
  }
672
+
614
673
  function IsNonNegativeNumber(v) {
615
674
  if (typeof v !== 'number') {
616
675
  return false;
@@ -623,6 +682,10 @@ function IsNonNegativeNumber(v) {
623
682
  }
624
683
  return true;
625
684
  }
685
+ function CloneAsUint8Array(O) {
686
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
687
+ return new Uint8Array(buffer);
688
+ }
626
689
 
627
690
  function DequeueValue(container) {
628
691
  const pair = container._queue.shift();
@@ -633,8 +696,7 @@ function DequeueValue(container) {
633
696
  return pair.value;
634
697
  }
635
698
  function EnqueueValueWithSize(container, value, size) {
636
- size = Number(size);
637
- if (!IsFiniteNonNegativeNumber(size)) {
699
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
638
700
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
639
701
  }
640
702
  container._queue.push({ value, size });
@@ -649,27 +711,18 @@ function ResetQueue(container) {
649
711
  container._queueTotalSize = 0;
650
712
  }
651
713
 
652
- function CreateArrayFromList(elements) {
653
- // We use arrays to represent lists, so this is basically a no-op.
654
- // Do a slice though just in case we happen to depend on the unique-ness.
655
- return elements.slice();
656
- }
657
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
658
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
659
- }
660
- // Not implemented correctly
661
- function TransferArrayBuffer(O) {
662
- return O;
663
- }
664
- // Not implemented correctly
665
- function IsDetachedBuffer(O) {
666
- return false;
667
- }
668
-
714
+ /**
715
+ * A pull-into request in a {@link ReadableByteStreamController}.
716
+ *
717
+ * @public
718
+ */
669
719
  class ReadableStreamBYOBRequest {
670
720
  constructor() {
671
721
  throw new TypeError('Illegal constructor');
672
722
  }
723
+ /**
724
+ * Returns the view for writing in to, or `null` if the BYOB request has already been responded to.
725
+ */
673
726
  get view() {
674
727
  if (!IsReadableStreamBYOBRequest(this)) {
675
728
  throw byobRequestBrandCheckException('view');
@@ -696,15 +749,10 @@ class ReadableStreamBYOBRequest {
696
749
  if (!ArrayBuffer.isView(view)) {
697
750
  throw new TypeError('You can only respond with array buffer views');
698
751
  }
699
- if (view.byteLength === 0) {
700
- throw new TypeError('chunk must have non-zero byteLength');
701
- }
702
- if (view.buffer.byteLength === 0) {
703
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
704
- }
705
752
  if (this._associatedReadableByteStreamController === undefined) {
706
753
  throw new TypeError('This BYOB request has been invalidated');
707
754
  }
755
+ if (IsDetachedBuffer(view.buffer)) ;
708
756
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
709
757
  }
710
758
  }
@@ -719,29 +767,38 @@ if (typeof SymbolPolyfill.toStringTag === 'symbol') {
719
767
  configurable: true
720
768
  });
721
769
  }
770
+ /**
771
+ * Allows control of a {@link ReadableStream | readable byte stream}'s state and internal queue.
772
+ *
773
+ * @public
774
+ */
722
775
  class ReadableByteStreamController {
723
776
  constructor() {
724
777
  throw new TypeError('Illegal constructor');
725
778
  }
779
+ /**
780
+ * Returns the current BYOB pull request, or `null` if there isn't one.
781
+ */
726
782
  get byobRequest() {
727
783
  if (!IsReadableByteStreamController(this)) {
728
784
  throw byteStreamControllerBrandCheckException('byobRequest');
729
785
  }
730
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
731
- const firstDescriptor = this._pendingPullIntos.peek();
732
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
733
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
734
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
735
- this._byobRequest = byobRequest;
736
- }
737
- return this._byobRequest;
786
+ return ReadableByteStreamControllerGetBYOBRequest(this);
738
787
  }
788
+ /**
789
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
790
+ * over-full. An underlying byte source ought to use this information to determine when and how to apply backpressure.
791
+ */
739
792
  get desiredSize() {
740
793
  if (!IsReadableByteStreamController(this)) {
741
794
  throw byteStreamControllerBrandCheckException('desiredSize');
742
795
  }
743
796
  return ReadableByteStreamControllerGetDesiredSize(this);
744
797
  }
798
+ /**
799
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
800
+ * the stream, but once those are read, the stream will become closed.
801
+ */
745
802
  close() {
746
803
  if (!IsReadableByteStreamController(this)) {
747
804
  throw byteStreamControllerBrandCheckException('close');
@@ -778,6 +835,9 @@ class ReadableByteStreamController {
778
835
  }
779
836
  ReadableByteStreamControllerEnqueue(this, chunk);
780
837
  }
838
+ /**
839
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
840
+ */
781
841
  error(e = undefined) {
782
842
  if (!IsReadableByteStreamController(this)) {
783
843
  throw byteStreamControllerBrandCheckException('error');
@@ -786,10 +846,7 @@ class ReadableByteStreamController {
786
846
  }
787
847
  /** @internal */
788
848
  [CancelSteps](reason) {
789
- if (this._pendingPullIntos.length > 0) {
790
- const firstDescriptor = this._pendingPullIntos.peek();
791
- firstDescriptor.bytesFilled = 0;
792
- }
849
+ ReadableByteStreamControllerClearPendingPullIntos(this);
793
850
  ResetQueue(this);
794
851
  const result = this._cancelAlgorithm(reason);
795
852
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -818,6 +875,7 @@ class ReadableByteStreamController {
818
875
  }
819
876
  const pullIntoDescriptor = {
820
877
  buffer,
878
+ bufferByteLength: autoAllocateChunkSize,
821
879
  byteOffset: 0,
822
880
  byteLength: autoAllocateChunkSize,
823
881
  bytesFilled: 0,
@@ -852,7 +910,7 @@ function IsReadableByteStreamController(x) {
852
910
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
853
911
  return false;
854
912
  }
855
- return true;
913
+ return x instanceof ReadableByteStreamController;
856
914
  }
857
915
  function IsReadableStreamBYOBRequest(x) {
858
916
  if (!typeIsObject(x)) {
@@ -861,7 +919,7 @@ function IsReadableStreamBYOBRequest(x) {
861
919
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
862
920
  return false;
863
921
  }
864
- return true;
922
+ return x instanceof ReadableStreamBYOBRequest;
865
923
  }
866
924
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
867
925
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -943,7 +1001,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
943
1001
  return ready;
944
1002
  }
945
1003
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
946
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
947
1004
  pullIntoDescriptor.bytesFilled += size;
948
1005
  }
949
1006
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -982,9 +1039,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
982
1039
  elementSize = view.constructor.BYTES_PER_ELEMENT;
983
1040
  }
984
1041
  const ctor = view.constructor;
1042
+ // try {
985
1043
  const buffer = TransferArrayBuffer(view.buffer);
1044
+ // } catch (e) {
1045
+ // readIntoRequest._errorSteps(e);
1046
+ // return;
1047
+ // }
986
1048
  const pullIntoDescriptor = {
987
1049
  buffer,
1050
+ bufferByteLength: buffer.byteLength,
988
1051
  byteOffset: view.byteOffset,
989
1052
  byteLength: view.byteLength,
990
1053
  bytesFilled: 0,
@@ -1024,7 +1087,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1024
1087
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1025
1088
  }
1026
1089
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1027
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1028
1090
  const stream = controller._controlledReadableByteStream;
1029
1091
  if (ReadableStreamHasBYOBReader(stream)) {
1030
1092
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1034,34 +1096,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1034
1096
  }
1035
1097
  }
1036
1098
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1037
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1038
- throw new RangeError('bytesWritten out of range');
1039
- }
1040
1099
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1041
1100
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1042
- // TODO: Figure out whether we should detach the buffer or not here.
1043
1101
  return;
1044
1102
  }
1045
1103
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1046
1104
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1047
1105
  if (remainderSize > 0) {
1048
1106
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1049
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1107
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1050
1108
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1051
1109
  }
1052
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1053
1110
  pullIntoDescriptor.bytesFilled -= remainderSize;
1054
1111
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1055
1112
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1056
1113
  }
1057
1114
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1058
1115
  const firstDescriptor = controller._pendingPullIntos.peek();
1059
- const stream = controller._controlledReadableByteStream;
1060
- if (stream._state === 'closed') {
1061
- if (bytesWritten !== 0) {
1062
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1063
- }
1064
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1116
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1117
+ const state = controller._controlledReadableByteStream._state;
1118
+ if (state === 'closed') {
1119
+ ReadableByteStreamControllerRespondInClosedState(controller);
1065
1120
  }
1066
1121
  else {
1067
1122
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1070,7 +1125,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1070
1125
  }
1071
1126
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1072
1127
  const descriptor = controller._pendingPullIntos.shift();
1073
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1074
1128
  return descriptor;
1075
1129
  }
1076
1130
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1130,6 +1184,12 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1130
1184
  const byteOffset = chunk.byteOffset;
1131
1185
  const byteLength = chunk.byteLength;
1132
1186
  const transferredBuffer = TransferArrayBuffer(buffer);
1187
+ if (controller._pendingPullIntos.length > 0) {
1188
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1189
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1190
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1191
+ }
1192
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1133
1193
  if (ReadableStreamHasDefaultReader(stream)) {
1134
1194
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1135
1195
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1159,9 +1219,18 @@ function ReadableByteStreamControllerError(controller, e) {
1159
1219
  ReadableByteStreamControllerClearAlgorithms(controller);
1160
1220
  ReadableStreamError(stream, e);
1161
1221
  }
1222
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1223
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1224
+ const firstDescriptor = controller._pendingPullIntos.peek();
1225
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1226
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1227
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1228
+ controller._byobRequest = byobRequest;
1229
+ }
1230
+ return controller._byobRequest;
1231
+ }
1162
1232
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1163
- const stream = controller._controlledReadableByteStream;
1164
- const state = stream._state;
1233
+ const state = controller._controlledReadableByteStream._state;
1165
1234
  if (state === 'errored') {
1166
1235
  return null;
1167
1236
  }
@@ -1171,21 +1240,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1171
1240
  return controller._strategyHWM - controller._queueTotalSize;
1172
1241
  }
1173
1242
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1174
- bytesWritten = Number(bytesWritten);
1175
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1176
- throw new RangeError('bytesWritten must be a finite');
1243
+ const firstDescriptor = controller._pendingPullIntos.peek();
1244
+ const state = controller._controlledReadableByteStream._state;
1245
+ if (state === 'closed') {
1246
+ if (bytesWritten !== 0) {
1247
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1248
+ }
1249
+ }
1250
+ else {
1251
+ if (bytesWritten === 0) {
1252
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1253
+ }
1254
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1255
+ throw new RangeError('bytesWritten out of range');
1256
+ }
1177
1257
  }
1258
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1178
1259
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1179
1260
  }
1180
1261
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1181
1262
  const firstDescriptor = controller._pendingPullIntos.peek();
1263
+ const state = controller._controlledReadableByteStream._state;
1264
+ if (state === 'closed') {
1265
+ if (view.byteLength !== 0) {
1266
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1267
+ }
1268
+ }
1269
+ else {
1270
+ if (view.byteLength === 0) {
1271
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1272
+ }
1273
+ }
1182
1274
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1183
1275
  throw new RangeError('The region specified by view does not match byobRequest');
1184
1276
  }
1185
- if (firstDescriptor.byteLength !== view.byteLength) {
1277
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1186
1278
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1187
1279
  }
1188
- firstDescriptor.buffer = view.buffer;
1280
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1281
+ throw new RangeError('The region specified by view is larger than byobRequest');
1282
+ }
1283
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1189
1284
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1190
1285
  }
1191
1286
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1227,6 +1322,9 @@ function SetUpReadableByteStreamControllerFromUnderlyingSource(stream, underlyin
1227
1322
  cancelAlgorithm = reason => underlyingByteSource.cancel(reason);
1228
1323
  }
1229
1324
  const autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
1325
+ if (autoAllocateChunkSize === 0) {
1326
+ throw new TypeError('autoAllocateChunkSize must be greater than 0');
1327
+ }
1230
1328
  SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
1231
1329
  }
1232
1330
  function SetUpReadableStreamBYOBRequest(request, controller, view) {
@@ -1273,6 +1371,11 @@ function ReadableStreamHasBYOBReader(stream) {
1273
1371
  }
1274
1372
  return true;
1275
1373
  }
1374
+ /**
1375
+ * A BYOB reader vended by a {@link ReadableStream}.
1376
+ *
1377
+ * @public
1378
+ */
1276
1379
  class ReadableStreamBYOBReader {
1277
1380
  constructor(stream) {
1278
1381
  assertRequiredArgument(stream, 1, 'ReadableStreamBYOBReader');
@@ -1287,12 +1390,19 @@ class ReadableStreamBYOBReader {
1287
1390
  ReadableStreamReaderGenericInitialize(this, stream);
1288
1391
  this._readIntoRequests = new SimpleQueue();
1289
1392
  }
1393
+ /**
1394
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
1395
+ * the reader's lock is released before the stream finishes closing.
1396
+ */
1290
1397
  get closed() {
1291
1398
  if (!IsReadableStreamBYOBReader(this)) {
1292
1399
  return promiseRejectedWith(byobReaderBrandCheckException('closed'));
1293
1400
  }
1294
1401
  return this._closedPromise;
1295
1402
  }
1403
+ /**
1404
+ * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
1405
+ */
1296
1406
  cancel(reason = undefined) {
1297
1407
  if (!IsReadableStreamBYOBReader(this)) {
1298
1408
  return promiseRejectedWith(byobReaderBrandCheckException('cancel'));
@@ -1302,6 +1412,11 @@ class ReadableStreamBYOBReader {
1302
1412
  }
1303
1413
  return ReadableStreamReaderGenericCancel(this, reason);
1304
1414
  }
1415
+ /**
1416
+ * Attempts to reads bytes into view, and returns a promise resolved with the result.
1417
+ *
1418
+ * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
1419
+ */
1305
1420
  read(view) {
1306
1421
  if (!IsReadableStreamBYOBReader(this)) {
1307
1422
  return promiseRejectedWith(byobReaderBrandCheckException('read'));
@@ -1315,6 +1430,7 @@ class ReadableStreamBYOBReader {
1315
1430
  if (view.buffer.byteLength === 0) {
1316
1431
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1317
1432
  }
1433
+ if (IsDetachedBuffer(view.buffer)) ;
1318
1434
  if (this._ownerReadableStream === undefined) {
1319
1435
  return promiseRejectedWith(readerLockException('read from'));
1320
1436
  }
@@ -1332,6 +1448,15 @@ class ReadableStreamBYOBReader {
1332
1448
  ReadableStreamBYOBReaderRead(this, view, readIntoRequest);
1333
1449
  return promise;
1334
1450
  }
1451
+ /**
1452
+ * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
1453
+ * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
1454
+ * from now on; otherwise, the reader will appear closed.
1455
+ *
1456
+ * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
1457
+ * the reader's {@link ReadableStreamBYOBReader.read | read()} method has not yet been settled. Attempting to
1458
+ * do so will throw a `TypeError` and leave the reader locked to the stream.
1459
+ */
1335
1460
  releaseLock() {
1336
1461
  if (!IsReadableStreamBYOBReader(this)) {
1337
1462
  throw byobReaderBrandCheckException('releaseLock');
@@ -1365,7 +1490,7 @@ function IsReadableStreamBYOBReader(x) {
1365
1490
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1366
1491
  return false;
1367
1492
  }
1368
- return true;
1493
+ return x instanceof ReadableStreamBYOBReader;
1369
1494
  }
1370
1495
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1371
1496
  const stream = reader._ownerReadableStream;
@@ -1460,6 +1585,36 @@ function assertWritableStream(x, context) {
1460
1585
  }
1461
1586
  }
1462
1587
 
1588
+ function isAbortSignal(value) {
1589
+ if (typeof value !== 'object' || value === null) {
1590
+ return false;
1591
+ }
1592
+ try {
1593
+ return typeof value.aborted === 'boolean';
1594
+ }
1595
+ catch (_a) {
1596
+ // AbortSignal.prototype.aborted throws if its brand check fails
1597
+ return false;
1598
+ }
1599
+ }
1600
+ const supportsAbortController = typeof AbortController === 'function';
1601
+ /**
1602
+ * Construct a new AbortController, if supported by the platform.
1603
+ *
1604
+ * @internal
1605
+ */
1606
+ function createAbortController() {
1607
+ if (supportsAbortController) {
1608
+ return new AbortController();
1609
+ }
1610
+ return undefined;
1611
+ }
1612
+
1613
+ /**
1614
+ * A writable stream represents a destination for data, into which you can write.
1615
+ *
1616
+ * @public
1617
+ */
1463
1618
  class WritableStream {
1464
1619
  constructor(rawUnderlyingSink = {}, rawStrategy = {}) {
1465
1620
  if (rawUnderlyingSink === undefined) {
@@ -1479,24 +1634,44 @@ class WritableStream {
1479
1634
  const highWaterMark = ExtractHighWaterMark(strategy, 1);
1480
1635
  SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, highWaterMark, sizeAlgorithm);
1481
1636
  }
1637
+ /**
1638
+ * Returns whether or not the writable stream is locked to a writer.
1639
+ */
1482
1640
  get locked() {
1483
1641
  if (!IsWritableStream(this)) {
1484
- throw streamBrandCheckException('locked');
1642
+ throw streamBrandCheckException$2('locked');
1485
1643
  }
1486
1644
  return IsWritableStreamLocked(this);
1487
1645
  }
1646
+ /**
1647
+ * Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be
1648
+ * immediately moved to an errored state, with any queued-up writes discarded. This will also execute any abort
1649
+ * mechanism of the underlying sink.
1650
+ *
1651
+ * The returned promise will fulfill if the stream shuts down successfully, or reject if the underlying sink signaled
1652
+ * that there was an error doing so. Additionally, it will reject with a `TypeError` (without attempting to cancel
1653
+ * the stream) if the stream is currently locked.
1654
+ */
1488
1655
  abort(reason = undefined) {
1489
1656
  if (!IsWritableStream(this)) {
1490
- return promiseRejectedWith(streamBrandCheckException('abort'));
1657
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1491
1658
  }
1492
1659
  if (IsWritableStreamLocked(this)) {
1493
1660
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
1494
1661
  }
1495
1662
  return WritableStreamAbort(this, reason);
1496
1663
  }
1664
+ /**
1665
+ * Closes the stream. The underlying sink will finish processing any previously-written chunks, before invoking its
1666
+ * close behavior. During this time any further attempts to write will fail (without erroring the stream).
1667
+ *
1668
+ * The method returns a promise that will fulfill if all remaining chunks are successfully written and the stream
1669
+ * successfully closes, or rejects if an error is encountered during this process. Additionally, it will reject with
1670
+ * a `TypeError` (without attempting to cancel the stream) if the stream is currently locked.
1671
+ */
1497
1672
  close() {
1498
1673
  if (!IsWritableStream(this)) {
1499
- return promiseRejectedWith(streamBrandCheckException('close'));
1674
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1500
1675
  }
1501
1676
  if (IsWritableStreamLocked(this)) {
1502
1677
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1506,9 +1681,17 @@ class WritableStream {
1506
1681
  }
1507
1682
  return WritableStreamClose(this);
1508
1683
  }
1684
+ /**
1685
+ * Creates a {@link WritableStreamDefaultWriter | writer} and locks the stream to the new writer. While the stream
1686
+ * is locked, no other writer can be acquired until this one is released.
1687
+ *
1688
+ * This functionality is especially useful for creating abstractions that desire the ability to write to a stream
1689
+ * without interruption or interleaving. By getting a writer for the stream, you can ensure nobody else can write at
1690
+ * the same time, which would cause the resulting written data to be unpredictable and probably useless.
1691
+ */
1509
1692
  getWriter() {
1510
1693
  if (!IsWritableStream(this)) {
1511
- throw streamBrandCheckException('getWriter');
1694
+ throw streamBrandCheckException$2('getWriter');
1512
1695
  }
1513
1696
  return AcquireWritableStreamDefaultWriter(this);
1514
1697
  }
@@ -1570,7 +1753,7 @@ function IsWritableStream(x) {
1570
1753
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1571
1754
  return false;
1572
1755
  }
1573
- return true;
1756
+ return x instanceof WritableStream;
1574
1757
  }
1575
1758
  function IsWritableStreamLocked(stream) {
1576
1759
  if (stream._writer === undefined) {
@@ -1579,6 +1762,15 @@ function IsWritableStreamLocked(stream) {
1579
1762
  return true;
1580
1763
  }
1581
1764
  function WritableStreamAbort(stream, reason) {
1765
+ var _a;
1766
+ if (stream._state === 'closed' || stream._state === 'errored') {
1767
+ return promiseResolvedWith(undefined);
1768
+ }
1769
+ stream._writableStreamController._abortReason = reason;
1770
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1771
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1772
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1773
+ // Widen the type again by casting to WritableStreamState.
1582
1774
  const state = stream._state;
1583
1775
  if (state === 'closed' || state === 'errored') {
1584
1776
  return promiseResolvedWith(undefined);
@@ -1764,6 +1956,11 @@ function WritableStreamUpdateBackpressure(stream, backpressure) {
1764
1956
  }
1765
1957
  stream._backpressure = backpressure;
1766
1958
  }
1959
+ /**
1960
+ * A default writer vended by a {@link WritableStream}.
1961
+ *
1962
+ * @public
1963
+ */
1767
1964
  class WritableStreamDefaultWriter {
1768
1965
  constructor(stream) {
1769
1966
  assertRequiredArgument(stream, 1, 'WritableStreamDefaultWriter');
@@ -1797,12 +1994,24 @@ class WritableStreamDefaultWriter {
1797
1994
  defaultWriterClosedPromiseInitializeAsRejected(this, storedError);
1798
1995
  }
1799
1996
  }
1997
+ /**
1998
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
1999
+ * the writer’s lock is released before the stream finishes closing.
2000
+ */
1800
2001
  get closed() {
1801
2002
  if (!IsWritableStreamDefaultWriter(this)) {
1802
2003
  return promiseRejectedWith(defaultWriterBrandCheckException('closed'));
1803
2004
  }
1804
2005
  return this._closedPromise;
1805
2006
  }
2007
+ /**
2008
+ * Returns the desired size to fill the stream’s internal queue. It can be negative, if the queue is over-full.
2009
+ * A producer can use this information to determine the right amount of data to write.
2010
+ *
2011
+ * It will be `null` if the stream cannot be successfully written to (due to either being errored, or having an abort
2012
+ * queued up). It will return zero if the stream is closed. And the getter will throw an exception if invoked when
2013
+ * the writer’s lock is released.
2014
+ */
1806
2015
  get desiredSize() {
1807
2016
  if (!IsWritableStreamDefaultWriter(this)) {
1808
2017
  throw defaultWriterBrandCheckException('desiredSize');
@@ -1812,12 +2021,23 @@ class WritableStreamDefaultWriter {
1812
2021
  }
1813
2022
  return WritableStreamDefaultWriterGetDesiredSize(this);
1814
2023
  }
2024
+ /**
2025
+ * Returns a promise that will be fulfilled when the desired size to fill the stream’s internal queue transitions
2026
+ * from non-positive to positive, signaling that it is no longer applying backpressure. Once the desired size dips
2027
+ * back to zero or below, the getter will return a new promise that stays pending until the next transition.
2028
+ *
2029
+ * If the stream becomes errored or aborted, or the writer’s lock is released, the returned promise will become
2030
+ * rejected.
2031
+ */
1815
2032
  get ready() {
1816
2033
  if (!IsWritableStreamDefaultWriter(this)) {
1817
2034
  return promiseRejectedWith(defaultWriterBrandCheckException('ready'));
1818
2035
  }
1819
2036
  return this._readyPromise;
1820
2037
  }
2038
+ /**
2039
+ * If the reader is active, behaves the same as {@link WritableStream.abort | stream.abort(reason)}.
2040
+ */
1821
2041
  abort(reason = undefined) {
1822
2042
  if (!IsWritableStreamDefaultWriter(this)) {
1823
2043
  return promiseRejectedWith(defaultWriterBrandCheckException('abort'));
@@ -1827,6 +2047,9 @@ class WritableStreamDefaultWriter {
1827
2047
  }
1828
2048
  return WritableStreamDefaultWriterAbort(this, reason);
1829
2049
  }
2050
+ /**
2051
+ * If the reader is active, behaves the same as {@link WritableStream.close | stream.close()}.
2052
+ */
1830
2053
  close() {
1831
2054
  if (!IsWritableStreamDefaultWriter(this)) {
1832
2055
  return promiseRejectedWith(defaultWriterBrandCheckException('close'));
@@ -1840,6 +2063,16 @@ class WritableStreamDefaultWriter {
1840
2063
  }
1841
2064
  return WritableStreamDefaultWriterClose(this);
1842
2065
  }
2066
+ /**
2067
+ * Releases the writer’s lock on the corresponding stream. After the lock is released, the writer is no longer active.
2068
+ * If the associated stream is errored when the lock is released, the writer will appear errored in the same way from
2069
+ * now on; otherwise, the writer will appear closed.
2070
+ *
2071
+ * Note that the lock can still be released even if some ongoing writes have not yet finished (i.e. even if the
2072
+ * promises returned from previous calls to {@link WritableStreamDefaultWriter.write | write()} have not yet settled).
2073
+ * It’s not necessary to hold the lock on the writer for the duration of the write; the lock instead simply prevents
2074
+ * other producers from writing in an interleaved manner.
2075
+ */
1843
2076
  releaseLock() {
1844
2077
  if (!IsWritableStreamDefaultWriter(this)) {
1845
2078
  throw defaultWriterBrandCheckException('releaseLock');
@@ -1883,7 +2116,7 @@ function IsWritableStreamDefaultWriter(x) {
1883
2116
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
1884
2117
  return false;
1885
2118
  }
1886
- return true;
2119
+ return x instanceof WritableStreamDefaultWriter;
1887
2120
  }
1888
2121
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
1889
2122
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -1964,13 +2197,49 @@ function WritableStreamDefaultWriterWrite(writer, chunk) {
1964
2197
  return promise;
1965
2198
  }
1966
2199
  const closeSentinel = {};
2200
+ /**
2201
+ * Allows control of a {@link WritableStream | writable stream}'s state and internal queue.
2202
+ *
2203
+ * @public
2204
+ */
1967
2205
  class WritableStreamDefaultController {
1968
2206
  constructor() {
1969
2207
  throw new TypeError('Illegal constructor');
1970
2208
  }
2209
+ /**
2210
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2211
+ */
2212
+ get abortReason() {
2213
+ if (!IsWritableStreamDefaultController(this)) {
2214
+ throw defaultControllerBrandCheckException$2('abortReason');
2215
+ }
2216
+ return this._abortReason;
2217
+ }
2218
+ /**
2219
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2220
+ */
2221
+ get signal() {
2222
+ if (!IsWritableStreamDefaultController(this)) {
2223
+ throw defaultControllerBrandCheckException$2('signal');
2224
+ }
2225
+ if (this._abortController === undefined) {
2226
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2227
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2228
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2229
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2230
+ }
2231
+ return this._abortController.signal;
2232
+ }
2233
+ /**
2234
+ * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2235
+ *
2236
+ * This method is rarely used, since usually it suffices to return a rejected promise from one of the underlying
2237
+ * sink's methods. However, it can be useful for suddenly shutting down a stream in response to an event outside the
2238
+ * normal lifecycle of interactions with the underlying sink.
2239
+ */
1971
2240
  error(e = undefined) {
1972
2241
  if (!IsWritableStreamDefaultController(this)) {
1973
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2242
+ throw defaultControllerBrandCheckException$2('error');
1974
2243
  }
1975
2244
  const state = this._controlledWritableStream._state;
1976
2245
  if (state !== 'writable') {
@@ -2008,7 +2277,7 @@ function IsWritableStreamDefaultController(x) {
2008
2277
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2009
2278
  return false;
2010
2279
  }
2011
- return true;
2280
+ return x instanceof WritableStreamDefaultController;
2012
2281
  }
2013
2282
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2014
2283
  controller._controlledWritableStream = stream;
@@ -2017,6 +2286,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2017
2286
  controller._queue = undefined;
2018
2287
  controller._queueTotalSize = undefined;
2019
2288
  ResetQueue(controller);
2289
+ controller._abortReason = undefined;
2290
+ controller._abortController = createAbortController();
2020
2291
  controller._started = false;
2021
2292
  controller._strategySizeAlgorithm = sizeAlgorithm;
2022
2293
  controller._strategyHWM = highWaterMark;
@@ -2166,9 +2437,13 @@ function WritableStreamDefaultControllerError(controller, error) {
2166
2437
  WritableStreamStartErroring(stream, error);
2167
2438
  }
2168
2439
  // Helper functions for the WritableStream.
2169
- function streamBrandCheckException(name) {
2440
+ function streamBrandCheckException$2(name) {
2170
2441
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2171
2442
  }
2443
+ // Helper functions for the WritableStreamDefaultController.
2444
+ function defaultControllerBrandCheckException$2(name) {
2445
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2446
+ }
2172
2447
  // Helper functions for the WritableStreamDefaultWriter.
2173
2448
  function defaultWriterBrandCheckException(name) {
2174
2449
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2192,6 +2467,9 @@ function defaultWriterClosedPromiseInitializeAsResolved(writer) {
2192
2467
  defaultWriterClosedPromiseResolve(writer);
2193
2468
  }
2194
2469
  function defaultWriterClosedPromiseReject(writer, reason) {
2470
+ if (writer._closedPromise_reject === undefined) {
2471
+ return;
2472
+ }
2195
2473
  setPromiseIsHandledToTrue(writer._closedPromise);
2196
2474
  writer._closedPromise_reject(reason);
2197
2475
  writer._closedPromise_resolve = undefined;
@@ -2202,6 +2480,9 @@ function defaultWriterClosedPromiseResetToRejected(writer, reason) {
2202
2480
  defaultWriterClosedPromiseInitializeAsRejected(writer, reason);
2203
2481
  }
2204
2482
  function defaultWriterClosedPromiseResolve(writer) {
2483
+ if (writer._closedPromise_resolve === undefined) {
2484
+ return;
2485
+ }
2205
2486
  writer._closedPromise_resolve(undefined);
2206
2487
  writer._closedPromise_resolve = undefined;
2207
2488
  writer._closedPromise_reject = undefined;
@@ -2223,6 +2504,9 @@ function defaultWriterReadyPromiseInitializeAsResolved(writer) {
2223
2504
  defaultWriterReadyPromiseResolve(writer);
2224
2505
  }
2225
2506
  function defaultWriterReadyPromiseReject(writer, reason) {
2507
+ if (writer._readyPromise_reject === undefined) {
2508
+ return;
2509
+ }
2226
2510
  setPromiseIsHandledToTrue(writer._readyPromise);
2227
2511
  writer._readyPromise_reject(reason);
2228
2512
  writer._readyPromise_resolve = undefined;
@@ -2236,25 +2520,15 @@ function defaultWriterReadyPromiseResetToRejected(writer, reason) {
2236
2520
  defaultWriterReadyPromiseInitializeAsRejected(writer, reason);
2237
2521
  }
2238
2522
  function defaultWriterReadyPromiseResolve(writer) {
2523
+ if (writer._readyPromise_resolve === undefined) {
2524
+ return;
2525
+ }
2239
2526
  writer._readyPromise_resolve(undefined);
2240
2527
  writer._readyPromise_resolve = undefined;
2241
2528
  writer._readyPromise_reject = undefined;
2242
2529
  writer._readyPromiseState = 'fulfilled';
2243
2530
  }
2244
2531
 
2245
- function isAbortSignal(value) {
2246
- if (typeof value !== 'object' || value === null) {
2247
- return false;
2248
- }
2249
- try {
2250
- return typeof value.aborted === 'boolean';
2251
- }
2252
- catch (_a) {
2253
- // AbortSignal.prototype.aborted throws if its brand check fails
2254
- return false;
2255
- }
2256
- }
2257
-
2258
2532
  /// <reference lib="dom" />
2259
2533
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2260
2534
 
@@ -2272,6 +2546,7 @@ function isDOMExceptionConstructor(ctor) {
2272
2546
  }
2273
2547
  }
2274
2548
  function createDOMExceptionPolyfill() {
2549
+ // eslint-disable-next-line no-shadow
2275
2550
  const ctor = function DOMException(message, name) {
2276
2551
  this.message = message || '';
2277
2552
  this.name = name || 'Error';
@@ -2283,6 +2558,7 @@ function createDOMExceptionPolyfill() {
2283
2558
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2284
2559
  return ctor;
2285
2560
  }
2561
+ // eslint-disable-next-line no-redeclare
2286
2562
  const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2287
2563
 
2288
2564
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2460,19 +2736,32 @@ function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventC
2460
2736
  });
2461
2737
  }
2462
2738
 
2739
+ /**
2740
+ * Allows control of a {@link ReadableStream | readable stream}'s state and internal queue.
2741
+ *
2742
+ * @public
2743
+ */
2463
2744
  class ReadableStreamDefaultController {
2464
2745
  constructor() {
2465
2746
  throw new TypeError('Illegal constructor');
2466
2747
  }
2748
+ /**
2749
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
2750
+ * over-full. An underlying source ought to use this information to determine when and how to apply backpressure.
2751
+ */
2467
2752
  get desiredSize() {
2468
2753
  if (!IsReadableStreamDefaultController(this)) {
2469
- throw defaultControllerBrandCheckException('desiredSize');
2754
+ throw defaultControllerBrandCheckException$1('desiredSize');
2470
2755
  }
2471
2756
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2472
2757
  }
2758
+ /**
2759
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
2760
+ * the stream, but once those are read, the stream will become closed.
2761
+ */
2473
2762
  close() {
2474
2763
  if (!IsReadableStreamDefaultController(this)) {
2475
- throw defaultControllerBrandCheckException('close');
2764
+ throw defaultControllerBrandCheckException$1('close');
2476
2765
  }
2477
2766
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2478
2767
  throw new TypeError('The stream is not in a state that permits close');
@@ -2481,16 +2770,19 @@ class ReadableStreamDefaultController {
2481
2770
  }
2482
2771
  enqueue(chunk = undefined) {
2483
2772
  if (!IsReadableStreamDefaultController(this)) {
2484
- throw defaultControllerBrandCheckException('enqueue');
2773
+ throw defaultControllerBrandCheckException$1('enqueue');
2485
2774
  }
2486
2775
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2487
2776
  throw new TypeError('The stream is not in a state that permits enqueue');
2488
2777
  }
2489
2778
  return ReadableStreamDefaultControllerEnqueue(this, chunk);
2490
2779
  }
2780
+ /**
2781
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
2782
+ */
2491
2783
  error(e = undefined) {
2492
2784
  if (!IsReadableStreamDefaultController(this)) {
2493
- throw defaultControllerBrandCheckException('error');
2785
+ throw defaultControllerBrandCheckException$1('error');
2494
2786
  }
2495
2787
  ReadableStreamDefaultControllerError(this, e);
2496
2788
  }
@@ -2541,7 +2833,7 @@ function IsReadableStreamDefaultController(x) {
2541
2833
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2542
2834
  return false;
2543
2835
  }
2544
- return true;
2836
+ return x instanceof ReadableStreamDefaultController;
2545
2837
  }
2546
2838
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2547
2839
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2635,8 +2927,7 @@ function ReadableStreamDefaultControllerError(controller, e) {
2635
2927
  ReadableStreamError(stream, e);
2636
2928
  }
2637
2929
  function ReadableStreamDefaultControllerGetDesiredSize(controller) {
2638
- const stream = controller._controlledReadableStream;
2639
- const state = stream._state;
2930
+ const state = controller._controlledReadableStream._state;
2640
2931
  if (state === 'errored') {
2641
2932
  return null;
2642
2933
  }
@@ -2698,11 +2989,17 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underl
2698
2989
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2699
2990
  }
2700
2991
  // Helper functions for the ReadableStreamDefaultController.
2701
- function defaultControllerBrandCheckException(name) {
2992
+ function defaultControllerBrandCheckException$1(name) {
2702
2993
  return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
2703
2994
  }
2704
2995
 
2705
2996
  function ReadableStreamTee(stream, cloneForBranch2) {
2997
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
2998
+ return ReadableByteStreamTee(stream);
2999
+ }
3000
+ return ReadableStreamDefaultTee(stream);
3001
+ }
3002
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2706
3003
  const reader = AcquireReadableStreamDefaultReader(stream);
2707
3004
  let reading = false;
2708
3005
  let canceled1 = false;
@@ -2721,24 +3018,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2721
3018
  }
2722
3019
  reading = true;
2723
3020
  const readRequest = {
2724
- _chunkSteps: value => {
3021
+ _chunkSteps: chunk => {
2725
3022
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2726
3023
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2727
3024
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2728
3025
  queueMicrotask(() => {
2729
3026
  reading = false;
2730
- const value1 = value;
2731
- const value2 = value;
3027
+ const chunk1 = chunk;
3028
+ const chunk2 = chunk;
2732
3029
  // There is no way to access the cloning code right now in the reference implementation.
2733
3030
  // If we add one then we'll need an implementation for serializable objects.
2734
3031
  // if (!canceled2 && cloneForBranch2) {
2735
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3032
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2736
3033
  // }
2737
3034
  if (!canceled1) {
2738
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3035
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2739
3036
  }
2740
3037
  if (!canceled2) {
2741
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3038
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2742
3039
  }
2743
3040
  });
2744
3041
  },
@@ -2750,6 +3047,9 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2750
3047
  if (!canceled2) {
2751
3048
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2752
3049
  }
3050
+ if (!canceled1 || !canceled2) {
3051
+ resolveCancelPromise(undefined);
3052
+ }
2753
3053
  },
2754
3054
  _errorSteps: () => {
2755
3055
  reading = false;
@@ -2786,9 +3086,217 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2786
3086
  uponRejection(reader._closedPromise, (r) => {
2787
3087
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
2788
3088
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3089
+ if (!canceled1 || !canceled2) {
3090
+ resolveCancelPromise(undefined);
3091
+ }
2789
3092
  });
2790
3093
  return [branch1, branch2];
2791
3094
  }
3095
+ function ReadableByteStreamTee(stream) {
3096
+ let reader = AcquireReadableStreamDefaultReader(stream);
3097
+ let reading = false;
3098
+ let canceled1 = false;
3099
+ let canceled2 = false;
3100
+ let reason1;
3101
+ let reason2;
3102
+ let branch1;
3103
+ let branch2;
3104
+ let resolveCancelPromise;
3105
+ const cancelPromise = newPromise(resolve => {
3106
+ resolveCancelPromise = resolve;
3107
+ });
3108
+ function forwardReaderError(thisReader) {
3109
+ uponRejection(thisReader._closedPromise, r => {
3110
+ if (thisReader !== reader) {
3111
+ return;
3112
+ }
3113
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3114
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3115
+ if (!canceled1 || !canceled2) {
3116
+ resolveCancelPromise(undefined);
3117
+ }
3118
+ });
3119
+ }
3120
+ function pullWithDefaultReader() {
3121
+ if (IsReadableStreamBYOBReader(reader)) {
3122
+ ReadableStreamReaderGenericRelease(reader);
3123
+ reader = AcquireReadableStreamDefaultReader(stream);
3124
+ forwardReaderError(reader);
3125
+ }
3126
+ const readRequest = {
3127
+ _chunkSteps: chunk => {
3128
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3129
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3130
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3131
+ queueMicrotask(() => {
3132
+ reading = false;
3133
+ const chunk1 = chunk;
3134
+ let chunk2 = chunk;
3135
+ if (!canceled1 && !canceled2) {
3136
+ try {
3137
+ chunk2 = CloneAsUint8Array(chunk);
3138
+ }
3139
+ catch (cloneE) {
3140
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3141
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3142
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3143
+ return;
3144
+ }
3145
+ }
3146
+ if (!canceled1) {
3147
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3148
+ }
3149
+ if (!canceled2) {
3150
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3151
+ }
3152
+ });
3153
+ },
3154
+ _closeSteps: () => {
3155
+ reading = false;
3156
+ if (!canceled1) {
3157
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3158
+ }
3159
+ if (!canceled2) {
3160
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3161
+ }
3162
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3163
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3164
+ }
3165
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3166
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3167
+ }
3168
+ if (!canceled1 || !canceled2) {
3169
+ resolveCancelPromise(undefined);
3170
+ }
3171
+ },
3172
+ _errorSteps: () => {
3173
+ reading = false;
3174
+ }
3175
+ };
3176
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3177
+ }
3178
+ function pullWithBYOBReader(view, forBranch2) {
3179
+ if (IsReadableStreamDefaultReader(reader)) {
3180
+ ReadableStreamReaderGenericRelease(reader);
3181
+ reader = AcquireReadableStreamBYOBReader(stream);
3182
+ forwardReaderError(reader);
3183
+ }
3184
+ const byobBranch = forBranch2 ? branch2 : branch1;
3185
+ const otherBranch = forBranch2 ? branch1 : branch2;
3186
+ const readIntoRequest = {
3187
+ _chunkSteps: chunk => {
3188
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3189
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3190
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3191
+ queueMicrotask(() => {
3192
+ reading = false;
3193
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3194
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3195
+ if (!otherCanceled) {
3196
+ let clonedChunk;
3197
+ try {
3198
+ clonedChunk = CloneAsUint8Array(chunk);
3199
+ }
3200
+ catch (cloneE) {
3201
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3202
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3203
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3204
+ return;
3205
+ }
3206
+ if (!byobCanceled) {
3207
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3208
+ }
3209
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3210
+ }
3211
+ else if (!byobCanceled) {
3212
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3213
+ }
3214
+ });
3215
+ },
3216
+ _closeSteps: chunk => {
3217
+ reading = false;
3218
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3219
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3220
+ if (!byobCanceled) {
3221
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3222
+ }
3223
+ if (!otherCanceled) {
3224
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3225
+ }
3226
+ if (chunk !== undefined) {
3227
+ if (!byobCanceled) {
3228
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3229
+ }
3230
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3231
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3232
+ }
3233
+ }
3234
+ if (!byobCanceled || !otherCanceled) {
3235
+ resolveCancelPromise(undefined);
3236
+ }
3237
+ },
3238
+ _errorSteps: () => {
3239
+ reading = false;
3240
+ }
3241
+ };
3242
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3243
+ }
3244
+ function pull1Algorithm() {
3245
+ if (reading) {
3246
+ return promiseResolvedWith(undefined);
3247
+ }
3248
+ reading = true;
3249
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3250
+ if (byobRequest === null) {
3251
+ pullWithDefaultReader();
3252
+ }
3253
+ else {
3254
+ pullWithBYOBReader(byobRequest._view, false);
3255
+ }
3256
+ return promiseResolvedWith(undefined);
3257
+ }
3258
+ function pull2Algorithm() {
3259
+ if (reading) {
3260
+ return promiseResolvedWith(undefined);
3261
+ }
3262
+ reading = true;
3263
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3264
+ if (byobRequest === null) {
3265
+ pullWithDefaultReader();
3266
+ }
3267
+ else {
3268
+ pullWithBYOBReader(byobRequest._view, true);
3269
+ }
3270
+ return promiseResolvedWith(undefined);
3271
+ }
3272
+ function cancel1Algorithm(reason) {
3273
+ canceled1 = true;
3274
+ reason1 = reason;
3275
+ if (canceled2) {
3276
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3277
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3278
+ resolveCancelPromise(cancelResult);
3279
+ }
3280
+ return cancelPromise;
3281
+ }
3282
+ function cancel2Algorithm(reason) {
3283
+ canceled2 = true;
3284
+ reason2 = reason;
3285
+ if (canceled1) {
3286
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3287
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3288
+ resolveCancelPromise(cancelResult);
3289
+ }
3290
+ return cancelPromise;
3291
+ }
3292
+ function startAlgorithm() {
3293
+ return;
3294
+ }
3295
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3296
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3297
+ forwardReaderError(reader);
3298
+ return [branch1, branch2];
3299
+ }
2792
3300
 
2793
3301
  function convertUnderlyingDefaultOrByteSource(source, context) {
2794
3302
  assertDictionary(source, context);
@@ -2888,6 +3396,11 @@ function convertReadableWritablePair(pair, context) {
2888
3396
  return { readable, writable };
2889
3397
  }
2890
3398
 
3399
+ /**
3400
+ * A readable stream represents a source of data, from which you can read.
3401
+ *
3402
+ * @public
3403
+ */
2891
3404
  class ReadableStream {
2892
3405
  constructor(rawUnderlyingSource = {}, rawStrategy = {}) {
2893
3406
  if (rawUnderlyingSource === undefined) {
@@ -2912,12 +3425,21 @@ class ReadableStream {
2912
3425
  SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, highWaterMark, sizeAlgorithm);
2913
3426
  }
2914
3427
  }
3428
+ /**
3429
+ * Whether or not the readable stream is locked to a {@link ReadableStreamDefaultReader | reader}.
3430
+ */
2915
3431
  get locked() {
2916
3432
  if (!IsReadableStream(this)) {
2917
3433
  throw streamBrandCheckException$1('locked');
2918
3434
  }
2919
3435
  return IsReadableStreamLocked(this);
2920
3436
  }
3437
+ /**
3438
+ * Cancels the stream, signaling a loss of interest in the stream by a consumer.
3439
+ *
3440
+ * The supplied `reason` argument will be given to the underlying source's {@link UnderlyingSource.cancel | cancel()}
3441
+ * method, which might or might not use it.
3442
+ */
2921
3443
  cancel(reason = undefined) {
2922
3444
  if (!IsReadableStream(this)) {
2923
3445
  return promiseRejectedWith(streamBrandCheckException$1('cancel'));
@@ -2979,6 +3501,17 @@ class ReadableStream {
2979
3501
  }
2980
3502
  return ReadableStreamPipeTo(this, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
2981
3503
  }
3504
+ /**
3505
+ * Tees this readable stream, returning a two-element array containing the two resulting branches as
3506
+ * new {@link ReadableStream} instances.
3507
+ *
3508
+ * Teeing a stream will lock it, preventing any other consumer from acquiring a reader.
3509
+ * To cancel the stream, cancel both of the resulting branches; a composite cancellation reason will then be
3510
+ * propagated to the stream's underlying source.
3511
+ *
3512
+ * Note that the chunks seen in each branch will be the same object. If the chunks are not immutable,
3513
+ * this could allow interference between the two branches.
3514
+ */
2982
3515
  tee() {
2983
3516
  if (!IsReadableStream(this)) {
2984
3517
  throw streamBrandCheckException$1('tee');
@@ -3025,6 +3558,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3025
3558
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3026
3559
  return stream;
3027
3560
  }
3561
+ // Throws if and only if startAlgorithm throws.
3562
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3563
+ const stream = Object.create(ReadableStream.prototype);
3564
+ InitializeReadableStream(stream);
3565
+ const controller = Object.create(ReadableByteStreamController.prototype);
3566
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3567
+ return stream;
3568
+ }
3028
3569
  function InitializeReadableStream(stream) {
3029
3570
  stream._state = 'readable';
3030
3571
  stream._reader = undefined;
@@ -3038,7 +3579,7 @@ function IsReadableStream(x) {
3038
3579
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3039
3580
  return false;
3040
3581
  }
3041
- return true;
3582
+ return x instanceof ReadableStream;
3042
3583
  }
3043
3584
  function IsReadableStreamLocked(stream) {
3044
3585
  if (stream._reader === undefined) {
@@ -3056,6 +3597,13 @@ function ReadableStreamCancel(stream, reason) {
3056
3597
  return promiseRejectedWith(stream._storedError);
3057
3598
  }
3058
3599
  ReadableStreamClose(stream);
3600
+ const reader = stream._reader;
3601
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3602
+ reader._readIntoRequests.forEach(readIntoRequest => {
3603
+ readIntoRequest._closeSteps(undefined);
3604
+ });
3605
+ reader._readIntoRequests = new SimpleQueue();
3606
+ }
3059
3607
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3060
3608
  return transformPromiseWith(sourceCancelPromise, noop);
3061
3609
  }
@@ -3065,13 +3613,13 @@ function ReadableStreamClose(stream) {
3065
3613
  if (reader === undefined) {
3066
3614
  return;
3067
3615
  }
3616
+ defaultReaderClosedPromiseResolve(reader);
3068
3617
  if (IsReadableStreamDefaultReader(reader)) {
3069
3618
  reader._readRequests.forEach(readRequest => {
3070
3619
  readRequest._closeSteps();
3071
3620
  });
3072
3621
  reader._readRequests = new SimpleQueue();
3073
3622
  }
3074
- defaultReaderClosedPromiseResolve(reader);
3075
3623
  }
3076
3624
  function ReadableStreamError(stream, e) {
3077
3625
  stream._state = 'errored';
@@ -3080,6 +3628,7 @@ function ReadableStreamError(stream, e) {
3080
3628
  if (reader === undefined) {
3081
3629
  return;
3082
3630
  }
3631
+ defaultReaderClosedPromiseReject(reader, e);
3083
3632
  if (IsReadableStreamDefaultReader(reader)) {
3084
3633
  reader._readRequests.forEach(readRequest => {
3085
3634
  readRequest._errorSteps(e);
@@ -3092,7 +3641,6 @@ function ReadableStreamError(stream, e) {
3092
3641
  });
3093
3642
  reader._readIntoRequests = new SimpleQueue();
3094
3643
  }
3095
- defaultReaderClosedPromiseReject(reader, e);
3096
3644
  }
3097
3645
  // Helper functions for the ReadableStream.
3098
3646
  function streamBrandCheckException$1(name) {
@@ -3108,21 +3656,37 @@ function convertQueuingStrategyInit(init, context) {
3108
3656
  };
3109
3657
  }
3110
3658
 
3111
- const byteLengthSizeFunction = function size(chunk) {
3659
+ // The size function must not have a prototype property nor be a constructor
3660
+ const byteLengthSizeFunction = (chunk) => {
3112
3661
  return chunk.byteLength;
3113
3662
  };
3663
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3664
+ value: 'size',
3665
+ configurable: true
3666
+ });
3667
+ /**
3668
+ * A queuing strategy that counts the number of bytes in each chunk.
3669
+ *
3670
+ * @public
3671
+ */
3114
3672
  class ByteLengthQueuingStrategy {
3115
3673
  constructor(options) {
3116
3674
  assertRequiredArgument(options, 1, 'ByteLengthQueuingStrategy');
3117
3675
  options = convertQueuingStrategyInit(options, 'First parameter');
3118
3676
  this._byteLengthQueuingStrategyHighWaterMark = options.highWaterMark;
3119
3677
  }
3678
+ /**
3679
+ * Returns the high water mark provided to the constructor.
3680
+ */
3120
3681
  get highWaterMark() {
3121
3682
  if (!IsByteLengthQueuingStrategy(this)) {
3122
3683
  throw byteLengthBrandCheckException('highWaterMark');
3123
3684
  }
3124
3685
  return this._byteLengthQueuingStrategyHighWaterMark;
3125
3686
  }
3687
+ /**
3688
+ * Measures the size of `chunk` by returning the value of its `byteLength` property.
3689
+ */
3126
3690
  get size() {
3127
3691
  if (!IsByteLengthQueuingStrategy(this)) {
3128
3692
  throw byteLengthBrandCheckException('size');
@@ -3151,24 +3715,41 @@ function IsByteLengthQueuingStrategy(x) {
3151
3715
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3152
3716
  return false;
3153
3717
  }
3154
- return true;
3718
+ return x instanceof ByteLengthQueuingStrategy;
3155
3719
  }
3156
3720
 
3157
- const countSizeFunction = function size() {
3721
+ // The size function must not have a prototype property nor be a constructor
3722
+ const countSizeFunction = () => {
3158
3723
  return 1;
3159
3724
  };
3725
+ Object.defineProperty(countSizeFunction, 'name', {
3726
+ value: 'size',
3727
+ configurable: true
3728
+ });
3729
+ /**
3730
+ * A queuing strategy that counts the number of chunks.
3731
+ *
3732
+ * @public
3733
+ */
3160
3734
  class CountQueuingStrategy {
3161
3735
  constructor(options) {
3162
3736
  assertRequiredArgument(options, 1, 'CountQueuingStrategy');
3163
3737
  options = convertQueuingStrategyInit(options, 'First parameter');
3164
3738
  this._countQueuingStrategyHighWaterMark = options.highWaterMark;
3165
3739
  }
3740
+ /**
3741
+ * Returns the high water mark provided to the constructor.
3742
+ */
3166
3743
  get highWaterMark() {
3167
3744
  if (!IsCountQueuingStrategy(this)) {
3168
3745
  throw countBrandCheckException('highWaterMark');
3169
3746
  }
3170
3747
  return this._countQueuingStrategyHighWaterMark;
3171
3748
  }
3749
+ /**
3750
+ * Measures the size of `chunk` by always returning 1.
3751
+ * This ensures that the total queue size is a count of the number of chunks in the queue.
3752
+ */
3172
3753
  get size() {
3173
3754
  if (!IsCountQueuingStrategy(this)) {
3174
3755
  throw countBrandCheckException('size');
@@ -3197,7 +3778,7 @@ function IsCountQueuingStrategy(x) {
3197
3778
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3198
3779
  return false;
3199
3780
  }
3200
- return true;
3781
+ return x instanceof CountQueuingStrategy;
3201
3782
  }
3202
3783
 
3203
3784
  function convertTransformer(original, context) {
@@ -3235,6 +3816,14 @@ function convertTransformerTransformCallback(fn, original, context) {
3235
3816
  }
3236
3817
 
3237
3818
  // Class TransformStream
3819
+ /**
3820
+ * A transform stream consists of a pair of streams: a {@link WritableStream | writable stream},
3821
+ * known as its writable side, and a {@link ReadableStream | readable stream}, known as its readable side.
3822
+ * In a manner specific to the transform stream in question, writes to the writable side result in new data being
3823
+ * made available for reading from the readable side.
3824
+ *
3825
+ * @public
3826
+ */
3238
3827
  class TransformStream {
3239
3828
  constructor(rawTransformer = {}, rawWritableStrategy = {}, rawReadableStrategy = {}) {
3240
3829
  if (rawTransformer === undefined) {
@@ -3266,15 +3855,21 @@ class TransformStream {
3266
3855
  startPromise_resolve(undefined);
3267
3856
  }
3268
3857
  }
3858
+ /**
3859
+ * The readable side of the transform stream.
3860
+ */
3269
3861
  get readable() {
3270
3862
  if (!IsTransformStream(this)) {
3271
- throw streamBrandCheckException$2('readable');
3863
+ throw streamBrandCheckException('readable');
3272
3864
  }
3273
3865
  return this._readable;
3274
3866
  }
3867
+ /**
3868
+ * The writable side of the transform stream.
3869
+ */
3275
3870
  get writable() {
3276
3871
  if (!IsTransformStream(this)) {
3277
- throw streamBrandCheckException$2('writable');
3872
+ throw streamBrandCheckException('writable');
3278
3873
  }
3279
3874
  return this._writable;
3280
3875
  }
@@ -3325,7 +3920,7 @@ function IsTransformStream(x) {
3325
3920
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3326
3921
  return false;
3327
3922
  }
3328
- return true;
3923
+ return x instanceof TransformStream;
3329
3924
  }
3330
3925
  // This is a no-op if both sides are already errored.
3331
3926
  function TransformStreamError(stream, e) {
@@ -3353,32 +3948,48 @@ function TransformStreamSetBackpressure(stream, backpressure) {
3353
3948
  stream._backpressure = backpressure;
3354
3949
  }
3355
3950
  // Class TransformStreamDefaultController
3951
+ /**
3952
+ * Allows control of the {@link ReadableStream} and {@link WritableStream} of the associated {@link TransformStream}.
3953
+ *
3954
+ * @public
3955
+ */
3356
3956
  class TransformStreamDefaultController {
3357
3957
  constructor() {
3358
3958
  throw new TypeError('Illegal constructor');
3359
3959
  }
3960
+ /**
3961
+ * Returns the desired size to fill the readable side’s internal queue. It can be negative, if the queue is over-full.
3962
+ */
3360
3963
  get desiredSize() {
3361
3964
  if (!IsTransformStreamDefaultController(this)) {
3362
- throw defaultControllerBrandCheckException$1('desiredSize');
3965
+ throw defaultControllerBrandCheckException('desiredSize');
3363
3966
  }
3364
3967
  const readableController = this._controlledTransformStream._readable._readableStreamController;
3365
3968
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
3366
3969
  }
3367
3970
  enqueue(chunk = undefined) {
3368
3971
  if (!IsTransformStreamDefaultController(this)) {
3369
- throw defaultControllerBrandCheckException$1('enqueue');
3972
+ throw defaultControllerBrandCheckException('enqueue');
3370
3973
  }
3371
3974
  TransformStreamDefaultControllerEnqueue(this, chunk);
3372
3975
  }
3976
+ /**
3977
+ * Errors both the readable side and the writable side of the controlled transform stream, making all future
3978
+ * interactions with it fail with the given error `e`. Any chunks queued for transformation will be discarded.
3979
+ */
3373
3980
  error(reason = undefined) {
3374
3981
  if (!IsTransformStreamDefaultController(this)) {
3375
- throw defaultControllerBrandCheckException$1('error');
3982
+ throw defaultControllerBrandCheckException('error');
3376
3983
  }
3377
3984
  TransformStreamDefaultControllerError(this, reason);
3378
3985
  }
3986
+ /**
3987
+ * Closes the readable side and errors the writable side of the controlled transform stream. This is useful when the
3988
+ * transformer only needs to consume a portion of the chunks written to the writable side.
3989
+ */
3379
3990
  terminate() {
3380
3991
  if (!IsTransformStreamDefaultController(this)) {
3381
- throw defaultControllerBrandCheckException$1('terminate');
3992
+ throw defaultControllerBrandCheckException('terminate');
3382
3993
  }
3383
3994
  TransformStreamDefaultControllerTerminate(this);
3384
3995
  }
@@ -3403,7 +4014,7 @@ function IsTransformStreamDefaultController(x) {
3403
4014
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3404
4015
  return false;
3405
4016
  }
3406
- return true;
4017
+ return x instanceof TransformStreamDefaultController;
3407
4018
  }
3408
4019
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3409
4020
  controller._controlledTransformStream = stream;
@@ -3520,11 +4131,11 @@ function TransformStreamDefaultSourcePullAlgorithm(stream) {
3520
4131
  return stream._backpressureChangePromise;
3521
4132
  }
3522
4133
  // Helper functions for the TransformStreamDefaultController.
3523
- function defaultControllerBrandCheckException$1(name) {
4134
+ function defaultControllerBrandCheckException(name) {
3524
4135
  return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
3525
4136
  }
3526
4137
  // Helper functions for the TransformStream.
3527
- function streamBrandCheckException$2(name) {
4138
+ function streamBrandCheckException(name) {
3528
4139
  return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
3529
4140
  }
3530
4141