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.
package/dist/polyfill.mjs CHANGED
@@ -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
  var SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
@@ -8,7 +8,7 @@ var 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 @@ var globals = getGlobals();
27
27
  function typeIsObject(x) {
28
28
  return (typeof x === 'object' && x !== null) || typeof x === 'function';
29
29
  }
30
- var rethrowAssertionErrorRejection = noop;
30
+ var rethrowAssertionErrorRejection = noop;
31
31
 
32
32
  var originalPromise = Promise;
33
33
  var originalPromiseThen = Promise.prototype.then;
@@ -247,6 +247,9 @@ function defaultReaderClosedPromiseInitializeAsResolved(reader) {
247
247
  defaultReaderClosedPromiseResolve(reader);
248
248
  }
249
249
  function defaultReaderClosedPromiseReject(reader, reason) {
250
+ if (reader._closedPromise_reject === undefined) {
251
+ return;
252
+ }
250
253
  setPromiseIsHandledToTrue(reader._closedPromise);
251
254
  reader._closedPromise_reject(reason);
252
255
  reader._closedPromise_resolve = undefined;
@@ -256,6 +259,9 @@ function defaultReaderClosedPromiseResetToRejected(reader, reason) {
256
259
  defaultReaderClosedPromiseInitializeAsRejected(reader, reason);
257
260
  }
258
261
  function defaultReaderClosedPromiseResolve(reader) {
262
+ if (reader._closedPromise_resolve === undefined) {
263
+ return;
264
+ }
259
265
  reader._closedPromise_resolve(undefined);
260
266
  reader._closedPromise_resolve = undefined;
261
267
  reader._closedPromise_reject = undefined;
@@ -382,6 +388,11 @@ function ReadableStreamHasDefaultReader(stream) {
382
388
  }
383
389
  return true;
384
390
  }
391
+ /**
392
+ * A default reader vended by a {@link ReadableStream}.
393
+ *
394
+ * @public
395
+ */
385
396
  var ReadableStreamDefaultReader = /** @class */ (function () {
386
397
  function ReadableStreamDefaultReader(stream) {
387
398
  assertRequiredArgument(stream, 1, 'ReadableStreamDefaultReader');
@@ -393,6 +404,10 @@ var ReadableStreamDefaultReader = /** @class */ (function () {
393
404
  this._readRequests = new SimpleQueue();
394
405
  }
395
406
  Object.defineProperty(ReadableStreamDefaultReader.prototype, "closed", {
407
+ /**
408
+ * Returns a promise that will be fulfilled when the stream becomes closed,
409
+ * or rejected if the stream ever errors or the reader's lock is released before the stream finishes closing.
410
+ */
396
411
  get: function () {
397
412
  if (!IsReadableStreamDefaultReader(this)) {
398
413
  return promiseRejectedWith(defaultReaderBrandCheckException('closed'));
@@ -402,6 +417,9 @@ var ReadableStreamDefaultReader = /** @class */ (function () {
402
417
  enumerable: false,
403
418
  configurable: true
404
419
  });
420
+ /**
421
+ * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
422
+ */
405
423
  ReadableStreamDefaultReader.prototype.cancel = function (reason) {
406
424
  if (reason === void 0) { reason = undefined; }
407
425
  if (!IsReadableStreamDefaultReader(this)) {
@@ -412,6 +430,11 @@ var ReadableStreamDefaultReader = /** @class */ (function () {
412
430
  }
413
431
  return ReadableStreamReaderGenericCancel(this, reason);
414
432
  };
433
+ /**
434
+ * Returns a promise that allows access to the next chunk from the stream's internal queue, if available.
435
+ *
436
+ * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
437
+ */
415
438
  ReadableStreamDefaultReader.prototype.read = function () {
416
439
  if (!IsReadableStreamDefaultReader(this)) {
417
440
  return promiseRejectedWith(defaultReaderBrandCheckException('read'));
@@ -433,6 +456,15 @@ var ReadableStreamDefaultReader = /** @class */ (function () {
433
456
  ReadableStreamDefaultReaderRead(this, readRequest);
434
457
  return promise;
435
458
  };
459
+ /**
460
+ * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
461
+ * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
462
+ * from now on; otherwise, the reader will appear closed.
463
+ *
464
+ * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
465
+ * the reader's {@link ReadableStreamDefaultReader.read | read()} method has not yet been settled. Attempting to
466
+ * do so will throw a `TypeError` and leave the reader locked to the stream.
467
+ */
436
468
  ReadableStreamDefaultReader.prototype.releaseLock = function () {
437
469
  if (!IsReadableStreamDefaultReader(this)) {
438
470
  throw defaultReaderBrandCheckException('releaseLock');
@@ -467,7 +499,7 @@ function IsReadableStreamDefaultReader(x) {
467
499
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
468
500
  return false;
469
501
  }
470
- return true;
502
+ return x instanceof ReadableStreamDefaultReader;
471
503
  }
472
504
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
473
505
  var stream = reader._ownerReadableStream;
@@ -615,7 +647,14 @@ function IsReadableStreamAsyncIterator(x) {
615
647
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
616
648
  return false;
617
649
  }
618
- return true;
650
+ try {
651
+ // noinspection SuspiciousTypeOfGuard
652
+ return x._asyncIteratorImpl instanceof
653
+ ReadableStreamAsyncIteratorImpl;
654
+ }
655
+ catch (_a) {
656
+ return false;
657
+ }
619
658
  }
620
659
  // Helper functions for the ReadableStream.
621
660
  function streamAsyncIteratorBrandCheckException(name) {
@@ -629,15 +668,35 @@ var NumberIsNaN = Number.isNaN || function (x) {
629
668
  return x !== x;
630
669
  };
631
670
 
632
- function IsFiniteNonNegativeNumber(v) {
633
- if (!IsNonNegativeNumber(v)) {
634
- return false;
635
- }
636
- if (v === Infinity) {
637
- return false;
671
+ function CreateArrayFromList(elements) {
672
+ // We use arrays to represent lists, so this is basically a no-op.
673
+ // Do a slice though just in case we happen to depend on the unique-ness.
674
+ return elements.slice();
675
+ }
676
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
677
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
678
+ }
679
+ // Not implemented correctly
680
+ function TransferArrayBuffer(O) {
681
+ return O;
682
+ }
683
+ // Not implemented correctly
684
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
685
+ function IsDetachedBuffer(O) {
686
+ return false;
687
+ }
688
+ function ArrayBufferSlice(buffer, begin, end) {
689
+ // ArrayBuffer.prototype.slice is not available on IE10
690
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
691
+ if (buffer.slice) {
692
+ return buffer.slice(begin, end);
638
693
  }
639
- return true;
694
+ var length = end - begin;
695
+ var slice = new ArrayBuffer(length);
696
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
697
+ return slice;
640
698
  }
699
+
641
700
  function IsNonNegativeNumber(v) {
642
701
  if (typeof v !== 'number') {
643
702
  return false;
@@ -650,6 +709,10 @@ function IsNonNegativeNumber(v) {
650
709
  }
651
710
  return true;
652
711
  }
712
+ function CloneAsUint8Array(O) {
713
+ var buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
714
+ return new Uint8Array(buffer);
715
+ }
653
716
 
654
717
  function DequeueValue(container) {
655
718
  var pair = container._queue.shift();
@@ -660,8 +723,7 @@ function DequeueValue(container) {
660
723
  return pair.value;
661
724
  }
662
725
  function EnqueueValueWithSize(container, value, size) {
663
- size = Number(size);
664
- if (!IsFiniteNonNegativeNumber(size)) {
726
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
665
727
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
666
728
  }
667
729
  container._queue.push({ value: value, size: size });
@@ -676,28 +738,19 @@ function ResetQueue(container) {
676
738
  container._queueTotalSize = 0;
677
739
  }
678
740
 
679
- function CreateArrayFromList(elements) {
680
- // We use arrays to represent lists, so this is basically a no-op.
681
- // Do a slice though just in case we happen to depend on the unique-ness.
682
- return elements.slice();
683
- }
684
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
685
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
686
- }
687
- // Not implemented correctly
688
- function TransferArrayBuffer(O) {
689
- return O;
690
- }
691
- // Not implemented correctly
692
- function IsDetachedBuffer(O) {
693
- return false;
694
- }
695
-
741
+ /**
742
+ * A pull-into request in a {@link ReadableByteStreamController}.
743
+ *
744
+ * @public
745
+ */
696
746
  var ReadableStreamBYOBRequest = /** @class */ (function () {
697
747
  function ReadableStreamBYOBRequest() {
698
748
  throw new TypeError('Illegal constructor');
699
749
  }
700
750
  Object.defineProperty(ReadableStreamBYOBRequest.prototype, "view", {
751
+ /**
752
+ * Returns the view for writing in to, or `null` if the BYOB request has already been responded to.
753
+ */
701
754
  get: function () {
702
755
  if (!IsReadableStreamBYOBRequest(this)) {
703
756
  throw byobRequestBrandCheckException('view');
@@ -727,15 +780,10 @@ var ReadableStreamBYOBRequest = /** @class */ (function () {
727
780
  if (!ArrayBuffer.isView(view)) {
728
781
  throw new TypeError('You can only respond with array buffer views');
729
782
  }
730
- if (view.byteLength === 0) {
731
- throw new TypeError('chunk must have non-zero byteLength');
732
- }
733
- if (view.buffer.byteLength === 0) {
734
- throw new TypeError("chunk's buffer must have non-zero byteLength");
735
- }
736
783
  if (this._associatedReadableByteStreamController === undefined) {
737
784
  throw new TypeError('This BYOB request has been invalidated');
738
785
  }
786
+ if (IsDetachedBuffer(view.buffer)) ;
739
787
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
740
788
  };
741
789
  return ReadableStreamBYOBRequest;
@@ -751,28 +799,33 @@ if (typeof SymbolPolyfill.toStringTag === 'symbol') {
751
799
  configurable: true
752
800
  });
753
801
  }
802
+ /**
803
+ * Allows control of a {@link ReadableStream | readable byte stream}'s state and internal queue.
804
+ *
805
+ * @public
806
+ */
754
807
  var ReadableByteStreamController = /** @class */ (function () {
755
808
  function ReadableByteStreamController() {
756
809
  throw new TypeError('Illegal constructor');
757
810
  }
758
811
  Object.defineProperty(ReadableByteStreamController.prototype, "byobRequest", {
812
+ /**
813
+ * Returns the current BYOB pull request, or `null` if there isn't one.
814
+ */
759
815
  get: function () {
760
816
  if (!IsReadableByteStreamController(this)) {
761
817
  throw byteStreamControllerBrandCheckException('byobRequest');
762
818
  }
763
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
764
- var firstDescriptor = this._pendingPullIntos.peek();
765
- var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
766
- var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
767
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
768
- this._byobRequest = byobRequest;
769
- }
770
- return this._byobRequest;
819
+ return ReadableByteStreamControllerGetBYOBRequest(this);
771
820
  },
772
821
  enumerable: false,
773
822
  configurable: true
774
823
  });
775
824
  Object.defineProperty(ReadableByteStreamController.prototype, "desiredSize", {
825
+ /**
826
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
827
+ * over-full. An underlying byte source ought to use this information to determine when and how to apply backpressure.
828
+ */
776
829
  get: function () {
777
830
  if (!IsReadableByteStreamController(this)) {
778
831
  throw byteStreamControllerBrandCheckException('desiredSize');
@@ -782,6 +835,10 @@ var ReadableByteStreamController = /** @class */ (function () {
782
835
  enumerable: false,
783
836
  configurable: true
784
837
  });
838
+ /**
839
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
840
+ * the stream, but once those are read, the stream will become closed.
841
+ */
785
842
  ReadableByteStreamController.prototype.close = function () {
786
843
  if (!IsReadableByteStreamController(this)) {
787
844
  throw byteStreamControllerBrandCheckException('close');
@@ -818,6 +875,9 @@ var ReadableByteStreamController = /** @class */ (function () {
818
875
  }
819
876
  ReadableByteStreamControllerEnqueue(this, chunk);
820
877
  };
878
+ /**
879
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
880
+ */
821
881
  ReadableByteStreamController.prototype.error = function (e) {
822
882
  if (e === void 0) { e = undefined; }
823
883
  if (!IsReadableByteStreamController(this)) {
@@ -827,10 +887,7 @@ var ReadableByteStreamController = /** @class */ (function () {
827
887
  };
828
888
  /** @internal */
829
889
  ReadableByteStreamController.prototype[CancelSteps] = function (reason) {
830
- if (this._pendingPullIntos.length > 0) {
831
- var firstDescriptor = this._pendingPullIntos.peek();
832
- firstDescriptor.bytesFilled = 0;
833
- }
890
+ ReadableByteStreamControllerClearPendingPullIntos(this);
834
891
  ResetQueue(this);
835
892
  var result = this._cancelAlgorithm(reason);
836
893
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -859,6 +916,7 @@ var ReadableByteStreamController = /** @class */ (function () {
859
916
  }
860
917
  var pullIntoDescriptor = {
861
918
  buffer: buffer,
919
+ bufferByteLength: autoAllocateChunkSize,
862
920
  byteOffset: 0,
863
921
  byteLength: autoAllocateChunkSize,
864
922
  bytesFilled: 0,
@@ -894,7 +952,7 @@ function IsReadableByteStreamController(x) {
894
952
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
895
953
  return false;
896
954
  }
897
- return true;
955
+ return x instanceof ReadableByteStreamController;
898
956
  }
899
957
  function IsReadableStreamBYOBRequest(x) {
900
958
  if (!typeIsObject(x)) {
@@ -903,7 +961,7 @@ function IsReadableStreamBYOBRequest(x) {
903
961
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
904
962
  return false;
905
963
  }
906
- return true;
964
+ return x instanceof ReadableStreamBYOBRequest;
907
965
  }
908
966
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
909
967
  var shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -985,7 +1043,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
985
1043
  return ready;
986
1044
  }
987
1045
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
988
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
989
1046
  pullIntoDescriptor.bytesFilled += size;
990
1047
  }
991
1048
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1024,9 +1081,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1024
1081
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1025
1082
  }
1026
1083
  var ctor = view.constructor;
1084
+ // try {
1027
1085
  var buffer = TransferArrayBuffer(view.buffer);
1086
+ // } catch (e) {
1087
+ // readIntoRequest._errorSteps(e);
1088
+ // return;
1089
+ // }
1028
1090
  var pullIntoDescriptor = {
1029
1091
  buffer: buffer,
1092
+ bufferByteLength: buffer.byteLength,
1030
1093
  byteOffset: view.byteOffset,
1031
1094
  byteLength: view.byteLength,
1032
1095
  bytesFilled: 0,
@@ -1066,7 +1129,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1066
1129
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1067
1130
  }
1068
1131
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1069
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1070
1132
  var stream = controller._controlledReadableByteStream;
1071
1133
  if (ReadableStreamHasBYOBReader(stream)) {
1072
1134
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1076,34 +1138,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1076
1138
  }
1077
1139
  }
1078
1140
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1079
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1080
- throw new RangeError('bytesWritten out of range');
1081
- }
1082
1141
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1083
1142
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1084
- // TODO: Figure out whether we should detach the buffer or not here.
1085
1143
  return;
1086
1144
  }
1087
1145
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1088
1146
  var remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1089
1147
  if (remainderSize > 0) {
1090
1148
  var end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1091
- var remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1149
+ var remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1092
1150
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1093
1151
  }
1094
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1095
1152
  pullIntoDescriptor.bytesFilled -= remainderSize;
1096
1153
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1097
1154
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1098
1155
  }
1099
1156
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1100
1157
  var firstDescriptor = controller._pendingPullIntos.peek();
1101
- var stream = controller._controlledReadableByteStream;
1102
- if (stream._state === 'closed') {
1103
- if (bytesWritten !== 0) {
1104
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1105
- }
1106
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1158
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1159
+ var state = controller._controlledReadableByteStream._state;
1160
+ if (state === 'closed') {
1161
+ ReadableByteStreamControllerRespondInClosedState(controller);
1107
1162
  }
1108
1163
  else {
1109
1164
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1112,7 +1167,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1112
1167
  }
1113
1168
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1114
1169
  var descriptor = controller._pendingPullIntos.shift();
1115
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1116
1170
  return descriptor;
1117
1171
  }
1118
1172
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1172,6 +1226,12 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1172
1226
  var byteOffset = chunk.byteOffset;
1173
1227
  var byteLength = chunk.byteLength;
1174
1228
  var transferredBuffer = TransferArrayBuffer(buffer);
1229
+ if (controller._pendingPullIntos.length > 0) {
1230
+ var firstPendingPullInto = controller._pendingPullIntos.peek();
1231
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1232
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1233
+ }
1234
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1175
1235
  if (ReadableStreamHasDefaultReader(stream)) {
1176
1236
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1177
1237
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1201,9 +1261,18 @@ function ReadableByteStreamControllerError(controller, e) {
1201
1261
  ReadableByteStreamControllerClearAlgorithms(controller);
1202
1262
  ReadableStreamError(stream, e);
1203
1263
  }
1264
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1265
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1266
+ var firstDescriptor = controller._pendingPullIntos.peek();
1267
+ var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1268
+ var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1269
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1270
+ controller._byobRequest = byobRequest;
1271
+ }
1272
+ return controller._byobRequest;
1273
+ }
1204
1274
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1205
- var stream = controller._controlledReadableByteStream;
1206
- var state = stream._state;
1275
+ var state = controller._controlledReadableByteStream._state;
1207
1276
  if (state === 'errored') {
1208
1277
  return null;
1209
1278
  }
@@ -1213,21 +1282,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1213
1282
  return controller._strategyHWM - controller._queueTotalSize;
1214
1283
  }
1215
1284
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1216
- bytesWritten = Number(bytesWritten);
1217
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1218
- throw new RangeError('bytesWritten must be a finite');
1285
+ var firstDescriptor = controller._pendingPullIntos.peek();
1286
+ var state = controller._controlledReadableByteStream._state;
1287
+ if (state === 'closed') {
1288
+ if (bytesWritten !== 0) {
1289
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1290
+ }
1291
+ }
1292
+ else {
1293
+ if (bytesWritten === 0) {
1294
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1295
+ }
1296
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1297
+ throw new RangeError('bytesWritten out of range');
1298
+ }
1219
1299
  }
1300
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1220
1301
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1221
1302
  }
1222
1303
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1223
1304
  var firstDescriptor = controller._pendingPullIntos.peek();
1305
+ var state = controller._controlledReadableByteStream._state;
1306
+ if (state === 'closed') {
1307
+ if (view.byteLength !== 0) {
1308
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1309
+ }
1310
+ }
1311
+ else {
1312
+ if (view.byteLength === 0) {
1313
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1314
+ }
1315
+ }
1224
1316
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1225
1317
  throw new RangeError('The region specified by view does not match byobRequest');
1226
1318
  }
1227
- if (firstDescriptor.byteLength !== view.byteLength) {
1319
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1228
1320
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1229
1321
  }
1230
- firstDescriptor.buffer = view.buffer;
1322
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1323
+ throw new RangeError('The region specified by view is larger than byobRequest');
1324
+ }
1325
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1231
1326
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1232
1327
  }
1233
1328
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1269,6 +1364,9 @@ function SetUpReadableByteStreamControllerFromUnderlyingSource(stream, underlyin
1269
1364
  cancelAlgorithm = function (reason) { return underlyingByteSource.cancel(reason); };
1270
1365
  }
1271
1366
  var autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
1367
+ if (autoAllocateChunkSize === 0) {
1368
+ throw new TypeError('autoAllocateChunkSize must be greater than 0');
1369
+ }
1272
1370
  SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
1273
1371
  }
1274
1372
  function SetUpReadableStreamBYOBRequest(request, controller, view) {
@@ -1315,6 +1413,11 @@ function ReadableStreamHasBYOBReader(stream) {
1315
1413
  }
1316
1414
  return true;
1317
1415
  }
1416
+ /**
1417
+ * A BYOB reader vended by a {@link ReadableStream}.
1418
+ *
1419
+ * @public
1420
+ */
1318
1421
  var ReadableStreamBYOBReader = /** @class */ (function () {
1319
1422
  function ReadableStreamBYOBReader(stream) {
1320
1423
  assertRequiredArgument(stream, 1, 'ReadableStreamBYOBReader');
@@ -1330,6 +1433,10 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1330
1433
  this._readIntoRequests = new SimpleQueue();
1331
1434
  }
1332
1435
  Object.defineProperty(ReadableStreamBYOBReader.prototype, "closed", {
1436
+ /**
1437
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
1438
+ * the reader's lock is released before the stream finishes closing.
1439
+ */
1333
1440
  get: function () {
1334
1441
  if (!IsReadableStreamBYOBReader(this)) {
1335
1442
  return promiseRejectedWith(byobReaderBrandCheckException('closed'));
@@ -1339,6 +1446,9 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1339
1446
  enumerable: false,
1340
1447
  configurable: true
1341
1448
  });
1449
+ /**
1450
+ * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
1451
+ */
1342
1452
  ReadableStreamBYOBReader.prototype.cancel = function (reason) {
1343
1453
  if (reason === void 0) { reason = undefined; }
1344
1454
  if (!IsReadableStreamBYOBReader(this)) {
@@ -1349,6 +1459,11 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1349
1459
  }
1350
1460
  return ReadableStreamReaderGenericCancel(this, reason);
1351
1461
  };
1462
+ /**
1463
+ * Attempts to reads bytes into view, and returns a promise resolved with the result.
1464
+ *
1465
+ * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
1466
+ */
1352
1467
  ReadableStreamBYOBReader.prototype.read = function (view) {
1353
1468
  if (!IsReadableStreamBYOBReader(this)) {
1354
1469
  return promiseRejectedWith(byobReaderBrandCheckException('read'));
@@ -1362,6 +1477,7 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1362
1477
  if (view.buffer.byteLength === 0) {
1363
1478
  return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
1364
1479
  }
1480
+ if (IsDetachedBuffer(view.buffer)) ;
1365
1481
  if (this._ownerReadableStream === undefined) {
1366
1482
  return promiseRejectedWith(readerLockException('read from'));
1367
1483
  }
@@ -1379,6 +1495,15 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1379
1495
  ReadableStreamBYOBReaderRead(this, view, readIntoRequest);
1380
1496
  return promise;
1381
1497
  };
1498
+ /**
1499
+ * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
1500
+ * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
1501
+ * from now on; otherwise, the reader will appear closed.
1502
+ *
1503
+ * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
1504
+ * the reader's {@link ReadableStreamBYOBReader.read | read()} method has not yet been settled. Attempting to
1505
+ * do so will throw a `TypeError` and leave the reader locked to the stream.
1506
+ */
1382
1507
  ReadableStreamBYOBReader.prototype.releaseLock = function () {
1383
1508
  if (!IsReadableStreamBYOBReader(this)) {
1384
1509
  throw byobReaderBrandCheckException('releaseLock');
@@ -1413,7 +1538,7 @@ function IsReadableStreamBYOBReader(x) {
1413
1538
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1414
1539
  return false;
1415
1540
  }
1416
- return true;
1541
+ return x instanceof ReadableStreamBYOBReader;
1417
1542
  }
1418
1543
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1419
1544
  var stream = reader._ownerReadableStream;
@@ -1508,6 +1633,36 @@ function assertWritableStream(x, context) {
1508
1633
  }
1509
1634
  }
1510
1635
 
1636
+ function isAbortSignal(value) {
1637
+ if (typeof value !== 'object' || value === null) {
1638
+ return false;
1639
+ }
1640
+ try {
1641
+ return typeof value.aborted === 'boolean';
1642
+ }
1643
+ catch (_a) {
1644
+ // AbortSignal.prototype.aborted throws if its brand check fails
1645
+ return false;
1646
+ }
1647
+ }
1648
+ var supportsAbortController = typeof AbortController === 'function';
1649
+ /**
1650
+ * Construct a new AbortController, if supported by the platform.
1651
+ *
1652
+ * @internal
1653
+ */
1654
+ function createAbortController() {
1655
+ if (supportsAbortController) {
1656
+ return new AbortController();
1657
+ }
1658
+ return undefined;
1659
+ }
1660
+
1661
+ /**
1662
+ * A writable stream represents a destination for data, into which you can write.
1663
+ *
1664
+ * @public
1665
+ */
1511
1666
  var WritableStream = /** @class */ (function () {
1512
1667
  function WritableStream(rawUnderlyingSink, rawStrategy) {
1513
1668
  if (rawUnderlyingSink === void 0) { rawUnderlyingSink = {}; }
@@ -1530,28 +1685,48 @@ var WritableStream = /** @class */ (function () {
1530
1685
  SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, highWaterMark, sizeAlgorithm);
1531
1686
  }
1532
1687
  Object.defineProperty(WritableStream.prototype, "locked", {
1688
+ /**
1689
+ * Returns whether or not the writable stream is locked to a writer.
1690
+ */
1533
1691
  get: function () {
1534
1692
  if (!IsWritableStream(this)) {
1535
- throw streamBrandCheckException('locked');
1693
+ throw streamBrandCheckException$2('locked');
1536
1694
  }
1537
1695
  return IsWritableStreamLocked(this);
1538
1696
  },
1539
1697
  enumerable: false,
1540
1698
  configurable: true
1541
1699
  });
1700
+ /**
1701
+ * Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be
1702
+ * immediately moved to an errored state, with any queued-up writes discarded. This will also execute any abort
1703
+ * mechanism of the underlying sink.
1704
+ *
1705
+ * The returned promise will fulfill if the stream shuts down successfully, or reject if the underlying sink signaled
1706
+ * that there was an error doing so. Additionally, it will reject with a `TypeError` (without attempting to cancel
1707
+ * the stream) if the stream is currently locked.
1708
+ */
1542
1709
  WritableStream.prototype.abort = function (reason) {
1543
1710
  if (reason === void 0) { reason = undefined; }
1544
1711
  if (!IsWritableStream(this)) {
1545
- return promiseRejectedWith(streamBrandCheckException('abort'));
1712
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1546
1713
  }
1547
1714
  if (IsWritableStreamLocked(this)) {
1548
1715
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
1549
1716
  }
1550
1717
  return WritableStreamAbort(this, reason);
1551
1718
  };
1719
+ /**
1720
+ * Closes the stream. The underlying sink will finish processing any previously-written chunks, before invoking its
1721
+ * close behavior. During this time any further attempts to write will fail (without erroring the stream).
1722
+ *
1723
+ * The method returns a promise that will fulfill if all remaining chunks are successfully written and the stream
1724
+ * successfully closes, or rejects if an error is encountered during this process. Additionally, it will reject with
1725
+ * a `TypeError` (without attempting to cancel the stream) if the stream is currently locked.
1726
+ */
1552
1727
  WritableStream.prototype.close = function () {
1553
1728
  if (!IsWritableStream(this)) {
1554
- return promiseRejectedWith(streamBrandCheckException('close'));
1729
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1555
1730
  }
1556
1731
  if (IsWritableStreamLocked(this)) {
1557
1732
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1561,9 +1736,17 @@ var WritableStream = /** @class */ (function () {
1561
1736
  }
1562
1737
  return WritableStreamClose(this);
1563
1738
  };
1739
+ /**
1740
+ * Creates a {@link WritableStreamDefaultWriter | writer} and locks the stream to the new writer. While the stream
1741
+ * is locked, no other writer can be acquired until this one is released.
1742
+ *
1743
+ * This functionality is especially useful for creating abstractions that desire the ability to write to a stream
1744
+ * without interruption or interleaving. By getting a writer for the stream, you can ensure nobody else can write at
1745
+ * the same time, which would cause the resulting written data to be unpredictable and probably useless.
1746
+ */
1564
1747
  WritableStream.prototype.getWriter = function () {
1565
1748
  if (!IsWritableStream(this)) {
1566
- throw streamBrandCheckException('getWriter');
1749
+ throw streamBrandCheckException$2('getWriter');
1567
1750
  }
1568
1751
  return AcquireWritableStreamDefaultWriter(this);
1569
1752
  };
@@ -1628,7 +1811,7 @@ function IsWritableStream(x) {
1628
1811
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1629
1812
  return false;
1630
1813
  }
1631
- return true;
1814
+ return x instanceof WritableStream;
1632
1815
  }
1633
1816
  function IsWritableStreamLocked(stream) {
1634
1817
  if (stream._writer === undefined) {
@@ -1637,6 +1820,15 @@ function IsWritableStreamLocked(stream) {
1637
1820
  return true;
1638
1821
  }
1639
1822
  function WritableStreamAbort(stream, reason) {
1823
+ var _a;
1824
+ if (stream._state === 'closed' || stream._state === 'errored') {
1825
+ return promiseResolvedWith(undefined);
1826
+ }
1827
+ stream._writableStreamController._abortReason = reason;
1828
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1829
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1830
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1831
+ // Widen the type again by casting to WritableStreamState.
1640
1832
  var state = stream._state;
1641
1833
  if (state === 'closed' || state === 'errored') {
1642
1834
  return promiseResolvedWith(undefined);
@@ -1822,6 +2014,11 @@ function WritableStreamUpdateBackpressure(stream, backpressure) {
1822
2014
  }
1823
2015
  stream._backpressure = backpressure;
1824
2016
  }
2017
+ /**
2018
+ * A default writer vended by a {@link WritableStream}.
2019
+ *
2020
+ * @public
2021
+ */
1825
2022
  var WritableStreamDefaultWriter = /** @class */ (function () {
1826
2023
  function WritableStreamDefaultWriter(stream) {
1827
2024
  assertRequiredArgument(stream, 1, 'WritableStreamDefaultWriter');
@@ -1856,6 +2053,10 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1856
2053
  }
1857
2054
  }
1858
2055
  Object.defineProperty(WritableStreamDefaultWriter.prototype, "closed", {
2056
+ /**
2057
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
2058
+ * the writer’s lock is released before the stream finishes closing.
2059
+ */
1859
2060
  get: function () {
1860
2061
  if (!IsWritableStreamDefaultWriter(this)) {
1861
2062
  return promiseRejectedWith(defaultWriterBrandCheckException('closed'));
@@ -1866,6 +2067,14 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1866
2067
  configurable: true
1867
2068
  });
1868
2069
  Object.defineProperty(WritableStreamDefaultWriter.prototype, "desiredSize", {
2070
+ /**
2071
+ * Returns the desired size to fill the stream’s internal queue. It can be negative, if the queue is over-full.
2072
+ * A producer can use this information to determine the right amount of data to write.
2073
+ *
2074
+ * It will be `null` if the stream cannot be successfully written to (due to either being errored, or having an abort
2075
+ * queued up). It will return zero if the stream is closed. And the getter will throw an exception if invoked when
2076
+ * the writer’s lock is released.
2077
+ */
1869
2078
  get: function () {
1870
2079
  if (!IsWritableStreamDefaultWriter(this)) {
1871
2080
  throw defaultWriterBrandCheckException('desiredSize');
@@ -1879,6 +2088,14 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1879
2088
  configurable: true
1880
2089
  });
1881
2090
  Object.defineProperty(WritableStreamDefaultWriter.prototype, "ready", {
2091
+ /**
2092
+ * Returns a promise that will be fulfilled when the desired size to fill the stream’s internal queue transitions
2093
+ * from non-positive to positive, signaling that it is no longer applying backpressure. Once the desired size dips
2094
+ * back to zero or below, the getter will return a new promise that stays pending until the next transition.
2095
+ *
2096
+ * If the stream becomes errored or aborted, or the writer’s lock is released, the returned promise will become
2097
+ * rejected.
2098
+ */
1882
2099
  get: function () {
1883
2100
  if (!IsWritableStreamDefaultWriter(this)) {
1884
2101
  return promiseRejectedWith(defaultWriterBrandCheckException('ready'));
@@ -1888,6 +2105,9 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1888
2105
  enumerable: false,
1889
2106
  configurable: true
1890
2107
  });
2108
+ /**
2109
+ * If the reader is active, behaves the same as {@link WritableStream.abort | stream.abort(reason)}.
2110
+ */
1891
2111
  WritableStreamDefaultWriter.prototype.abort = function (reason) {
1892
2112
  if (reason === void 0) { reason = undefined; }
1893
2113
  if (!IsWritableStreamDefaultWriter(this)) {
@@ -1898,6 +2118,9 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1898
2118
  }
1899
2119
  return WritableStreamDefaultWriterAbort(this, reason);
1900
2120
  };
2121
+ /**
2122
+ * If the reader is active, behaves the same as {@link WritableStream.close | stream.close()}.
2123
+ */
1901
2124
  WritableStreamDefaultWriter.prototype.close = function () {
1902
2125
  if (!IsWritableStreamDefaultWriter(this)) {
1903
2126
  return promiseRejectedWith(defaultWriterBrandCheckException('close'));
@@ -1911,6 +2134,16 @@ var WritableStreamDefaultWriter = /** @class */ (function () {
1911
2134
  }
1912
2135
  return WritableStreamDefaultWriterClose(this);
1913
2136
  };
2137
+ /**
2138
+ * Releases the writer’s lock on the corresponding stream. After the lock is released, the writer is no longer active.
2139
+ * If the associated stream is errored when the lock is released, the writer will appear errored in the same way from
2140
+ * now on; otherwise, the writer will appear closed.
2141
+ *
2142
+ * Note that the lock can still be released even if some ongoing writes have not yet finished (i.e. even if the
2143
+ * promises returned from previous calls to {@link WritableStreamDefaultWriter.write | write()} have not yet settled).
2144
+ * It’s not necessary to hold the lock on the writer for the duration of the write; the lock instead simply prevents
2145
+ * other producers from writing in an interleaved manner.
2146
+ */
1914
2147
  WritableStreamDefaultWriter.prototype.releaseLock = function () {
1915
2148
  if (!IsWritableStreamDefaultWriter(this)) {
1916
2149
  throw defaultWriterBrandCheckException('releaseLock');
@@ -1956,7 +2189,7 @@ function IsWritableStreamDefaultWriter(x) {
1956
2189
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
1957
2190
  return false;
1958
2191
  }
1959
- return true;
2192
+ return x instanceof WritableStreamDefaultWriter;
1960
2193
  }
1961
2194
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
1962
2195
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2037,14 +2270,58 @@ function WritableStreamDefaultWriterWrite(writer, chunk) {
2037
2270
  return promise;
2038
2271
  }
2039
2272
  var closeSentinel = {};
2273
+ /**
2274
+ * Allows control of a {@link WritableStream | writable stream}'s state and internal queue.
2275
+ *
2276
+ * @public
2277
+ */
2040
2278
  var WritableStreamDefaultController = /** @class */ (function () {
2041
2279
  function WritableStreamDefaultController() {
2042
2280
  throw new TypeError('Illegal constructor');
2043
2281
  }
2282
+ Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
2283
+ /**
2284
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2285
+ */
2286
+ get: function () {
2287
+ if (!IsWritableStreamDefaultController(this)) {
2288
+ throw defaultControllerBrandCheckException$2('abortReason');
2289
+ }
2290
+ return this._abortReason;
2291
+ },
2292
+ enumerable: false,
2293
+ configurable: true
2294
+ });
2295
+ Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
2296
+ /**
2297
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2298
+ */
2299
+ get: function () {
2300
+ if (!IsWritableStreamDefaultController(this)) {
2301
+ throw defaultControllerBrandCheckException$2('signal');
2302
+ }
2303
+ if (this._abortController === undefined) {
2304
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2305
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2306
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2307
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2308
+ }
2309
+ return this._abortController.signal;
2310
+ },
2311
+ enumerable: false,
2312
+ configurable: true
2313
+ });
2314
+ /**
2315
+ * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2316
+ *
2317
+ * This method is rarely used, since usually it suffices to return a rejected promise from one of the underlying
2318
+ * sink's methods. However, it can be useful for suddenly shutting down a stream in response to an event outside the
2319
+ * normal lifecycle of interactions with the underlying sink.
2320
+ */
2044
2321
  WritableStreamDefaultController.prototype.error = function (e) {
2045
2322
  if (e === void 0) { e = undefined; }
2046
2323
  if (!IsWritableStreamDefaultController(this)) {
2047
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2324
+ throw defaultControllerBrandCheckException$2('error');
2048
2325
  }
2049
2326
  var state = this._controlledWritableStream._state;
2050
2327
  if (state !== 'writable') {
@@ -2083,7 +2360,7 @@ function IsWritableStreamDefaultController(x) {
2083
2360
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2084
2361
  return false;
2085
2362
  }
2086
- return true;
2363
+ return x instanceof WritableStreamDefaultController;
2087
2364
  }
2088
2365
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2089
2366
  controller._controlledWritableStream = stream;
@@ -2092,6 +2369,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2092
2369
  controller._queue = undefined;
2093
2370
  controller._queueTotalSize = undefined;
2094
2371
  ResetQueue(controller);
2372
+ controller._abortReason = undefined;
2373
+ controller._abortController = createAbortController();
2095
2374
  controller._started = false;
2096
2375
  controller._strategySizeAlgorithm = sizeAlgorithm;
2097
2376
  controller._strategyHWM = highWaterMark;
@@ -2241,9 +2520,13 @@ function WritableStreamDefaultControllerError(controller, error) {
2241
2520
  WritableStreamStartErroring(stream, error);
2242
2521
  }
2243
2522
  // Helper functions for the WritableStream.
2244
- function streamBrandCheckException(name) {
2523
+ function streamBrandCheckException$2(name) {
2245
2524
  return new TypeError("WritableStream.prototype." + name + " can only be used on a WritableStream");
2246
2525
  }
2526
+ // Helper functions for the WritableStreamDefaultController.
2527
+ function defaultControllerBrandCheckException$2(name) {
2528
+ return new TypeError("WritableStreamDefaultController.prototype." + name + " can only be used on a WritableStreamDefaultController");
2529
+ }
2247
2530
  // Helper functions for the WritableStreamDefaultWriter.
2248
2531
  function defaultWriterBrandCheckException(name) {
2249
2532
  return new TypeError("WritableStreamDefaultWriter.prototype." + name + " can only be used on a WritableStreamDefaultWriter");
@@ -2267,6 +2550,9 @@ function defaultWriterClosedPromiseInitializeAsResolved(writer) {
2267
2550
  defaultWriterClosedPromiseResolve(writer);
2268
2551
  }
2269
2552
  function defaultWriterClosedPromiseReject(writer, reason) {
2553
+ if (writer._closedPromise_reject === undefined) {
2554
+ return;
2555
+ }
2270
2556
  setPromiseIsHandledToTrue(writer._closedPromise);
2271
2557
  writer._closedPromise_reject(reason);
2272
2558
  writer._closedPromise_resolve = undefined;
@@ -2277,6 +2563,9 @@ function defaultWriterClosedPromiseResetToRejected(writer, reason) {
2277
2563
  defaultWriterClosedPromiseInitializeAsRejected(writer, reason);
2278
2564
  }
2279
2565
  function defaultWriterClosedPromiseResolve(writer) {
2566
+ if (writer._closedPromise_resolve === undefined) {
2567
+ return;
2568
+ }
2280
2569
  writer._closedPromise_resolve(undefined);
2281
2570
  writer._closedPromise_resolve = undefined;
2282
2571
  writer._closedPromise_reject = undefined;
@@ -2298,6 +2587,9 @@ function defaultWriterReadyPromiseInitializeAsResolved(writer) {
2298
2587
  defaultWriterReadyPromiseResolve(writer);
2299
2588
  }
2300
2589
  function defaultWriterReadyPromiseReject(writer, reason) {
2590
+ if (writer._readyPromise_reject === undefined) {
2591
+ return;
2592
+ }
2301
2593
  setPromiseIsHandledToTrue(writer._readyPromise);
2302
2594
  writer._readyPromise_reject(reason);
2303
2595
  writer._readyPromise_resolve = undefined;
@@ -2311,25 +2603,15 @@ function defaultWriterReadyPromiseResetToRejected(writer, reason) {
2311
2603
  defaultWriterReadyPromiseInitializeAsRejected(writer, reason);
2312
2604
  }
2313
2605
  function defaultWriterReadyPromiseResolve(writer) {
2606
+ if (writer._readyPromise_resolve === undefined) {
2607
+ return;
2608
+ }
2314
2609
  writer._readyPromise_resolve(undefined);
2315
2610
  writer._readyPromise_resolve = undefined;
2316
2611
  writer._readyPromise_reject = undefined;
2317
2612
  writer._readyPromiseState = 'fulfilled';
2318
2613
  }
2319
2614
 
2320
- function isAbortSignal(value) {
2321
- if (typeof value !== 'object' || value === null) {
2322
- return false;
2323
- }
2324
- try {
2325
- return typeof value.aborted === 'boolean';
2326
- }
2327
- catch (_a) {
2328
- // AbortSignal.prototype.aborted throws if its brand check fails
2329
- return false;
2330
- }
2331
- }
2332
-
2333
2615
  /// <reference lib="dom" />
2334
2616
  var NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2335
2617
 
@@ -2347,6 +2629,7 @@ function isDOMExceptionConstructor(ctor) {
2347
2629
  }
2348
2630
  }
2349
2631
  function createDOMExceptionPolyfill() {
2632
+ // eslint-disable-next-line no-shadow
2350
2633
  var ctor = function DOMException(message, name) {
2351
2634
  this.message = message || '';
2352
2635
  this.name = name || 'Error';
@@ -2358,6 +2641,7 @@ function createDOMExceptionPolyfill() {
2358
2641
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2359
2642
  return ctor;
2360
2643
  }
2644
+ // eslint-disable-next-line no-redeclare
2361
2645
  var DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2362
2646
 
2363
2647
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2535,23 +2819,36 @@ function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventC
2535
2819
  });
2536
2820
  }
2537
2821
 
2822
+ /**
2823
+ * Allows control of a {@link ReadableStream | readable stream}'s state and internal queue.
2824
+ *
2825
+ * @public
2826
+ */
2538
2827
  var ReadableStreamDefaultController = /** @class */ (function () {
2539
2828
  function ReadableStreamDefaultController() {
2540
2829
  throw new TypeError('Illegal constructor');
2541
2830
  }
2542
2831
  Object.defineProperty(ReadableStreamDefaultController.prototype, "desiredSize", {
2832
+ /**
2833
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
2834
+ * over-full. An underlying source ought to use this information to determine when and how to apply backpressure.
2835
+ */
2543
2836
  get: function () {
2544
2837
  if (!IsReadableStreamDefaultController(this)) {
2545
- throw defaultControllerBrandCheckException('desiredSize');
2838
+ throw defaultControllerBrandCheckException$1('desiredSize');
2546
2839
  }
2547
2840
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2548
2841
  },
2549
2842
  enumerable: false,
2550
2843
  configurable: true
2551
2844
  });
2845
+ /**
2846
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
2847
+ * the stream, but once those are read, the stream will become closed.
2848
+ */
2552
2849
  ReadableStreamDefaultController.prototype.close = function () {
2553
2850
  if (!IsReadableStreamDefaultController(this)) {
2554
- throw defaultControllerBrandCheckException('close');
2851
+ throw defaultControllerBrandCheckException$1('close');
2555
2852
  }
2556
2853
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2557
2854
  throw new TypeError('The stream is not in a state that permits close');
@@ -2561,17 +2858,20 @@ var ReadableStreamDefaultController = /** @class */ (function () {
2561
2858
  ReadableStreamDefaultController.prototype.enqueue = function (chunk) {
2562
2859
  if (chunk === void 0) { chunk = undefined; }
2563
2860
  if (!IsReadableStreamDefaultController(this)) {
2564
- throw defaultControllerBrandCheckException('enqueue');
2861
+ throw defaultControllerBrandCheckException$1('enqueue');
2565
2862
  }
2566
2863
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2567
2864
  throw new TypeError('The stream is not in a state that permits enqueue');
2568
2865
  }
2569
2866
  return ReadableStreamDefaultControllerEnqueue(this, chunk);
2570
2867
  };
2868
+ /**
2869
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
2870
+ */
2571
2871
  ReadableStreamDefaultController.prototype.error = function (e) {
2572
2872
  if (e === void 0) { e = undefined; }
2573
2873
  if (!IsReadableStreamDefaultController(this)) {
2574
- throw defaultControllerBrandCheckException('error');
2874
+ throw defaultControllerBrandCheckException$1('error');
2575
2875
  }
2576
2876
  ReadableStreamDefaultControllerError(this, e);
2577
2877
  };
@@ -2623,7 +2923,7 @@ function IsReadableStreamDefaultController(x) {
2623
2923
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2624
2924
  return false;
2625
2925
  }
2626
- return true;
2926
+ return x instanceof ReadableStreamDefaultController;
2627
2927
  }
2628
2928
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2629
2929
  var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2717,8 +3017,7 @@ function ReadableStreamDefaultControllerError(controller, e) {
2717
3017
  ReadableStreamError(stream, e);
2718
3018
  }
2719
3019
  function ReadableStreamDefaultControllerGetDesiredSize(controller) {
2720
- var stream = controller._controlledReadableStream;
2721
- var state = stream._state;
3020
+ var state = controller._controlledReadableStream._state;
2722
3021
  if (state === 'errored') {
2723
3022
  return null;
2724
3023
  }
@@ -2780,11 +3079,17 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underl
2780
3079
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2781
3080
  }
2782
3081
  // Helper functions for the ReadableStreamDefaultController.
2783
- function defaultControllerBrandCheckException(name) {
3082
+ function defaultControllerBrandCheckException$1(name) {
2784
3083
  return new TypeError("ReadableStreamDefaultController.prototype." + name + " can only be used on a ReadableStreamDefaultController");
2785
3084
  }
2786
3085
 
2787
3086
  function ReadableStreamTee(stream, cloneForBranch2) {
3087
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3088
+ return ReadableByteStreamTee(stream);
3089
+ }
3090
+ return ReadableStreamDefaultTee(stream);
3091
+ }
3092
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2788
3093
  var reader = AcquireReadableStreamDefaultReader(stream);
2789
3094
  var reading = false;
2790
3095
  var canceled1 = false;
@@ -2803,24 +3108,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2803
3108
  }
2804
3109
  reading = true;
2805
3110
  var readRequest = {
2806
- _chunkSteps: function (value) {
3111
+ _chunkSteps: function (chunk) {
2807
3112
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2808
3113
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2809
3114
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2810
3115
  queueMicrotask(function () {
2811
3116
  reading = false;
2812
- var value1 = value;
2813
- var value2 = value;
3117
+ var chunk1 = chunk;
3118
+ var chunk2 = chunk;
2814
3119
  // There is no way to access the cloning code right now in the reference implementation.
2815
3120
  // If we add one then we'll need an implementation for serializable objects.
2816
3121
  // if (!canceled2 && cloneForBranch2) {
2817
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3122
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2818
3123
  // }
2819
3124
  if (!canceled1) {
2820
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3125
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2821
3126
  }
2822
3127
  if (!canceled2) {
2823
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3128
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2824
3129
  }
2825
3130
  });
2826
3131
  },
@@ -2832,6 +3137,9 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2832
3137
  if (!canceled2) {
2833
3138
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2834
3139
  }
3140
+ if (!canceled1 || !canceled2) {
3141
+ resolveCancelPromise(undefined);
3142
+ }
2835
3143
  },
2836
3144
  _errorSteps: function () {
2837
3145
  reading = false;
@@ -2868,9 +3176,217 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2868
3176
  uponRejection(reader._closedPromise, function (r) {
2869
3177
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
2870
3178
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3179
+ if (!canceled1 || !canceled2) {
3180
+ resolveCancelPromise(undefined);
3181
+ }
2871
3182
  });
2872
3183
  return [branch1, branch2];
2873
3184
  }
3185
+ function ReadableByteStreamTee(stream) {
3186
+ var reader = AcquireReadableStreamDefaultReader(stream);
3187
+ var reading = false;
3188
+ var canceled1 = false;
3189
+ var canceled2 = false;
3190
+ var reason1;
3191
+ var reason2;
3192
+ var branch1;
3193
+ var branch2;
3194
+ var resolveCancelPromise;
3195
+ var cancelPromise = newPromise(function (resolve) {
3196
+ resolveCancelPromise = resolve;
3197
+ });
3198
+ function forwardReaderError(thisReader) {
3199
+ uponRejection(thisReader._closedPromise, function (r) {
3200
+ if (thisReader !== reader) {
3201
+ return;
3202
+ }
3203
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3204
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3205
+ if (!canceled1 || !canceled2) {
3206
+ resolveCancelPromise(undefined);
3207
+ }
3208
+ });
3209
+ }
3210
+ function pullWithDefaultReader() {
3211
+ if (IsReadableStreamBYOBReader(reader)) {
3212
+ ReadableStreamReaderGenericRelease(reader);
3213
+ reader = AcquireReadableStreamDefaultReader(stream);
3214
+ forwardReaderError(reader);
3215
+ }
3216
+ var readRequest = {
3217
+ _chunkSteps: function (chunk) {
3218
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3219
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3220
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3221
+ queueMicrotask(function () {
3222
+ reading = false;
3223
+ var chunk1 = chunk;
3224
+ var chunk2 = chunk;
3225
+ if (!canceled1 && !canceled2) {
3226
+ try {
3227
+ chunk2 = CloneAsUint8Array(chunk);
3228
+ }
3229
+ catch (cloneE) {
3230
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3231
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3232
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3233
+ return;
3234
+ }
3235
+ }
3236
+ if (!canceled1) {
3237
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3238
+ }
3239
+ if (!canceled2) {
3240
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3241
+ }
3242
+ });
3243
+ },
3244
+ _closeSteps: function () {
3245
+ reading = false;
3246
+ if (!canceled1) {
3247
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3248
+ }
3249
+ if (!canceled2) {
3250
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3251
+ }
3252
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3253
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3254
+ }
3255
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3256
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3257
+ }
3258
+ if (!canceled1 || !canceled2) {
3259
+ resolveCancelPromise(undefined);
3260
+ }
3261
+ },
3262
+ _errorSteps: function () {
3263
+ reading = false;
3264
+ }
3265
+ };
3266
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3267
+ }
3268
+ function pullWithBYOBReader(view, forBranch2) {
3269
+ if (IsReadableStreamDefaultReader(reader)) {
3270
+ ReadableStreamReaderGenericRelease(reader);
3271
+ reader = AcquireReadableStreamBYOBReader(stream);
3272
+ forwardReaderError(reader);
3273
+ }
3274
+ var byobBranch = forBranch2 ? branch2 : branch1;
3275
+ var otherBranch = forBranch2 ? branch1 : branch2;
3276
+ var readIntoRequest = {
3277
+ _chunkSteps: function (chunk) {
3278
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3279
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3280
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3281
+ queueMicrotask(function () {
3282
+ reading = false;
3283
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3284
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3285
+ if (!otherCanceled) {
3286
+ var clonedChunk = void 0;
3287
+ try {
3288
+ clonedChunk = CloneAsUint8Array(chunk);
3289
+ }
3290
+ catch (cloneE) {
3291
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3292
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3293
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3294
+ return;
3295
+ }
3296
+ if (!byobCanceled) {
3297
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3298
+ }
3299
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3300
+ }
3301
+ else if (!byobCanceled) {
3302
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3303
+ }
3304
+ });
3305
+ },
3306
+ _closeSteps: function (chunk) {
3307
+ reading = false;
3308
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3309
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3310
+ if (!byobCanceled) {
3311
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3312
+ }
3313
+ if (!otherCanceled) {
3314
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3315
+ }
3316
+ if (chunk !== undefined) {
3317
+ if (!byobCanceled) {
3318
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3319
+ }
3320
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3321
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3322
+ }
3323
+ }
3324
+ if (!byobCanceled || !otherCanceled) {
3325
+ resolveCancelPromise(undefined);
3326
+ }
3327
+ },
3328
+ _errorSteps: function () {
3329
+ reading = false;
3330
+ }
3331
+ };
3332
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3333
+ }
3334
+ function pull1Algorithm() {
3335
+ if (reading) {
3336
+ return promiseResolvedWith(undefined);
3337
+ }
3338
+ reading = true;
3339
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3340
+ if (byobRequest === null) {
3341
+ pullWithDefaultReader();
3342
+ }
3343
+ else {
3344
+ pullWithBYOBReader(byobRequest._view, false);
3345
+ }
3346
+ return promiseResolvedWith(undefined);
3347
+ }
3348
+ function pull2Algorithm() {
3349
+ if (reading) {
3350
+ return promiseResolvedWith(undefined);
3351
+ }
3352
+ reading = true;
3353
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3354
+ if (byobRequest === null) {
3355
+ pullWithDefaultReader();
3356
+ }
3357
+ else {
3358
+ pullWithBYOBReader(byobRequest._view, true);
3359
+ }
3360
+ return promiseResolvedWith(undefined);
3361
+ }
3362
+ function cancel1Algorithm(reason) {
3363
+ canceled1 = true;
3364
+ reason1 = reason;
3365
+ if (canceled2) {
3366
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3367
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3368
+ resolveCancelPromise(cancelResult);
3369
+ }
3370
+ return cancelPromise;
3371
+ }
3372
+ function cancel2Algorithm(reason) {
3373
+ canceled2 = true;
3374
+ reason2 = reason;
3375
+ if (canceled1) {
3376
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3377
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3378
+ resolveCancelPromise(cancelResult);
3379
+ }
3380
+ return cancelPromise;
3381
+ }
3382
+ function startAlgorithm() {
3383
+ return;
3384
+ }
3385
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3386
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3387
+ forwardReaderError(reader);
3388
+ return [branch1, branch2];
3389
+ }
2874
3390
 
2875
3391
  function convertUnderlyingDefaultOrByteSource(source, context) {
2876
3392
  assertDictionary(source, context);
@@ -2970,6 +3486,11 @@ function convertReadableWritablePair(pair, context) {
2970
3486
  return { readable: readable, writable: writable };
2971
3487
  }
2972
3488
 
3489
+ /**
3490
+ * A readable stream represents a source of data, from which you can read.
3491
+ *
3492
+ * @public
3493
+ */
2973
3494
  var ReadableStream = /** @class */ (function () {
2974
3495
  function ReadableStream(rawUnderlyingSource, rawStrategy) {
2975
3496
  if (rawUnderlyingSource === void 0) { rawUnderlyingSource = {}; }
@@ -2997,6 +3518,9 @@ var ReadableStream = /** @class */ (function () {
2997
3518
  }
2998
3519
  }
2999
3520
  Object.defineProperty(ReadableStream.prototype, "locked", {
3521
+ /**
3522
+ * Whether or not the readable stream is locked to a {@link ReadableStreamDefaultReader | reader}.
3523
+ */
3000
3524
  get: function () {
3001
3525
  if (!IsReadableStream(this)) {
3002
3526
  throw streamBrandCheckException$1('locked');
@@ -3006,6 +3530,12 @@ var ReadableStream = /** @class */ (function () {
3006
3530
  enumerable: false,
3007
3531
  configurable: true
3008
3532
  });
3533
+ /**
3534
+ * Cancels the stream, signaling a loss of interest in the stream by a consumer.
3535
+ *
3536
+ * The supplied `reason` argument will be given to the underlying source's {@link UnderlyingSource.cancel | cancel()}
3537
+ * method, which might or might not use it.
3538
+ */
3009
3539
  ReadableStream.prototype.cancel = function (reason) {
3010
3540
  if (reason === void 0) { reason = undefined; }
3011
3541
  if (!IsReadableStream(this)) {
@@ -3071,6 +3601,17 @@ var ReadableStream = /** @class */ (function () {
3071
3601
  }
3072
3602
  return ReadableStreamPipeTo(this, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
3073
3603
  };
3604
+ /**
3605
+ * Tees this readable stream, returning a two-element array containing the two resulting branches as
3606
+ * new {@link ReadableStream} instances.
3607
+ *
3608
+ * Teeing a stream will lock it, preventing any other consumer from acquiring a reader.
3609
+ * To cancel the stream, cancel both of the resulting branches; a composite cancellation reason will then be
3610
+ * propagated to the stream's underlying source.
3611
+ *
3612
+ * Note that the chunks seen in each branch will be the same object. If the chunks are not immutable,
3613
+ * this could allow interference between the two branches.
3614
+ */
3074
3615
  ReadableStream.prototype.tee = function () {
3075
3616
  if (!IsReadableStream(this)) {
3076
3617
  throw streamBrandCheckException$1('tee');
@@ -3121,6 +3662,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3121
3662
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3122
3663
  return stream;
3123
3664
  }
3665
+ // Throws if and only if startAlgorithm throws.
3666
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3667
+ var stream = Object.create(ReadableStream.prototype);
3668
+ InitializeReadableStream(stream);
3669
+ var controller = Object.create(ReadableByteStreamController.prototype);
3670
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3671
+ return stream;
3672
+ }
3124
3673
  function InitializeReadableStream(stream) {
3125
3674
  stream._state = 'readable';
3126
3675
  stream._reader = undefined;
@@ -3134,7 +3683,7 @@ function IsReadableStream(x) {
3134
3683
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3135
3684
  return false;
3136
3685
  }
3137
- return true;
3686
+ return x instanceof ReadableStream;
3138
3687
  }
3139
3688
  function IsReadableStreamLocked(stream) {
3140
3689
  if (stream._reader === undefined) {
@@ -3152,6 +3701,13 @@ function ReadableStreamCancel(stream, reason) {
3152
3701
  return promiseRejectedWith(stream._storedError);
3153
3702
  }
3154
3703
  ReadableStreamClose(stream);
3704
+ var reader = stream._reader;
3705
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3706
+ reader._readIntoRequests.forEach(function (readIntoRequest) {
3707
+ readIntoRequest._closeSteps(undefined);
3708
+ });
3709
+ reader._readIntoRequests = new SimpleQueue();
3710
+ }
3155
3711
  var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3156
3712
  return transformPromiseWith(sourceCancelPromise, noop);
3157
3713
  }
@@ -3161,13 +3717,13 @@ function ReadableStreamClose(stream) {
3161
3717
  if (reader === undefined) {
3162
3718
  return;
3163
3719
  }
3720
+ defaultReaderClosedPromiseResolve(reader);
3164
3721
  if (IsReadableStreamDefaultReader(reader)) {
3165
3722
  reader._readRequests.forEach(function (readRequest) {
3166
3723
  readRequest._closeSteps();
3167
3724
  });
3168
3725
  reader._readRequests = new SimpleQueue();
3169
3726
  }
3170
- defaultReaderClosedPromiseResolve(reader);
3171
3727
  }
3172
3728
  function ReadableStreamError(stream, e) {
3173
3729
  stream._state = 'errored';
@@ -3176,6 +3732,7 @@ function ReadableStreamError(stream, e) {
3176
3732
  if (reader === undefined) {
3177
3733
  return;
3178
3734
  }
3735
+ defaultReaderClosedPromiseReject(reader, e);
3179
3736
  if (IsReadableStreamDefaultReader(reader)) {
3180
3737
  reader._readRequests.forEach(function (readRequest) {
3181
3738
  readRequest._errorSteps(e);
@@ -3188,7 +3745,6 @@ function ReadableStreamError(stream, e) {
3188
3745
  });
3189
3746
  reader._readIntoRequests = new SimpleQueue();
3190
3747
  }
3191
- defaultReaderClosedPromiseReject(reader, e);
3192
3748
  }
3193
3749
  // Helper functions for the ReadableStream.
3194
3750
  function streamBrandCheckException$1(name) {
@@ -3204,9 +3760,19 @@ function convertQueuingStrategyInit(init, context) {
3204
3760
  };
3205
3761
  }
3206
3762
 
3207
- var byteLengthSizeFunction = function size(chunk) {
3763
+ // The size function must not have a prototype property nor be a constructor
3764
+ var byteLengthSizeFunction = function (chunk) {
3208
3765
  return chunk.byteLength;
3209
3766
  };
3767
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3768
+ value: 'size',
3769
+ configurable: true
3770
+ });
3771
+ /**
3772
+ * A queuing strategy that counts the number of bytes in each chunk.
3773
+ *
3774
+ * @public
3775
+ */
3210
3776
  var ByteLengthQueuingStrategy = /** @class */ (function () {
3211
3777
  function ByteLengthQueuingStrategy(options) {
3212
3778
  assertRequiredArgument(options, 1, 'ByteLengthQueuingStrategy');
@@ -3214,6 +3780,9 @@ var ByteLengthQueuingStrategy = /** @class */ (function () {
3214
3780
  this._byteLengthQueuingStrategyHighWaterMark = options.highWaterMark;
3215
3781
  }
3216
3782
  Object.defineProperty(ByteLengthQueuingStrategy.prototype, "highWaterMark", {
3783
+ /**
3784
+ * Returns the high water mark provided to the constructor.
3785
+ */
3217
3786
  get: function () {
3218
3787
  if (!IsByteLengthQueuingStrategy(this)) {
3219
3788
  throw byteLengthBrandCheckException('highWaterMark');
@@ -3224,6 +3793,9 @@ var ByteLengthQueuingStrategy = /** @class */ (function () {
3224
3793
  configurable: true
3225
3794
  });
3226
3795
  Object.defineProperty(ByteLengthQueuingStrategy.prototype, "size", {
3796
+ /**
3797
+ * Measures the size of `chunk` by returning the value of its `byteLength` property.
3798
+ */
3227
3799
  get: function () {
3228
3800
  if (!IsByteLengthQueuingStrategy(this)) {
3229
3801
  throw byteLengthBrandCheckException('size');
@@ -3256,12 +3828,22 @@ function IsByteLengthQueuingStrategy(x) {
3256
3828
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3257
3829
  return false;
3258
3830
  }
3259
- return true;
3831
+ return x instanceof ByteLengthQueuingStrategy;
3260
3832
  }
3261
3833
 
3262
- var countSizeFunction = function size() {
3834
+ // The size function must not have a prototype property nor be a constructor
3835
+ var countSizeFunction = function () {
3263
3836
  return 1;
3264
3837
  };
3838
+ Object.defineProperty(countSizeFunction, 'name', {
3839
+ value: 'size',
3840
+ configurable: true
3841
+ });
3842
+ /**
3843
+ * A queuing strategy that counts the number of chunks.
3844
+ *
3845
+ * @public
3846
+ */
3265
3847
  var CountQueuingStrategy = /** @class */ (function () {
3266
3848
  function CountQueuingStrategy(options) {
3267
3849
  assertRequiredArgument(options, 1, 'CountQueuingStrategy');
@@ -3269,6 +3851,9 @@ var CountQueuingStrategy = /** @class */ (function () {
3269
3851
  this._countQueuingStrategyHighWaterMark = options.highWaterMark;
3270
3852
  }
3271
3853
  Object.defineProperty(CountQueuingStrategy.prototype, "highWaterMark", {
3854
+ /**
3855
+ * Returns the high water mark provided to the constructor.
3856
+ */
3272
3857
  get: function () {
3273
3858
  if (!IsCountQueuingStrategy(this)) {
3274
3859
  throw countBrandCheckException('highWaterMark');
@@ -3279,6 +3864,10 @@ var CountQueuingStrategy = /** @class */ (function () {
3279
3864
  configurable: true
3280
3865
  });
3281
3866
  Object.defineProperty(CountQueuingStrategy.prototype, "size", {
3867
+ /**
3868
+ * Measures the size of `chunk` by always returning 1.
3869
+ * This ensures that the total queue size is a count of the number of chunks in the queue.
3870
+ */
3282
3871
  get: function () {
3283
3872
  if (!IsCountQueuingStrategy(this)) {
3284
3873
  throw countBrandCheckException('size');
@@ -3311,7 +3900,7 @@ function IsCountQueuingStrategy(x) {
3311
3900
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3312
3901
  return false;
3313
3902
  }
3314
- return true;
3903
+ return x instanceof CountQueuingStrategy;
3315
3904
  }
3316
3905
 
3317
3906
  function convertTransformer(original, context) {
@@ -3349,6 +3938,14 @@ function convertTransformerTransformCallback(fn, original, context) {
3349
3938
  }
3350
3939
 
3351
3940
  // Class TransformStream
3941
+ /**
3942
+ * A transform stream consists of a pair of streams: a {@link WritableStream | writable stream},
3943
+ * known as its writable side, and a {@link ReadableStream | readable stream}, known as its readable side.
3944
+ * In a manner specific to the transform stream in question, writes to the writable side result in new data being
3945
+ * made available for reading from the readable side.
3946
+ *
3947
+ * @public
3948
+ */
3352
3949
  var TransformStream = /** @class */ (function () {
3353
3950
  function TransformStream(rawTransformer, rawWritableStrategy, rawReadableStrategy) {
3354
3951
  if (rawTransformer === void 0) { rawTransformer = {}; }
@@ -3384,9 +3981,12 @@ var TransformStream = /** @class */ (function () {
3384
3981
  }
3385
3982
  }
3386
3983
  Object.defineProperty(TransformStream.prototype, "readable", {
3984
+ /**
3985
+ * The readable side of the transform stream.
3986
+ */
3387
3987
  get: function () {
3388
3988
  if (!IsTransformStream(this)) {
3389
- throw streamBrandCheckException$2('readable');
3989
+ throw streamBrandCheckException('readable');
3390
3990
  }
3391
3991
  return this._readable;
3392
3992
  },
@@ -3394,9 +3994,12 @@ var TransformStream = /** @class */ (function () {
3394
3994
  configurable: true
3395
3995
  });
3396
3996
  Object.defineProperty(TransformStream.prototype, "writable", {
3997
+ /**
3998
+ * The writable side of the transform stream.
3999
+ */
3397
4000
  get: function () {
3398
4001
  if (!IsTransformStream(this)) {
3399
- throw streamBrandCheckException$2('writable');
4002
+ throw streamBrandCheckException('writable');
3400
4003
  }
3401
4004
  return this._writable;
3402
4005
  },
@@ -3451,7 +4054,7 @@ function IsTransformStream(x) {
3451
4054
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3452
4055
  return false;
3453
4056
  }
3454
- return true;
4057
+ return x instanceof TransformStream;
3455
4058
  }
3456
4059
  // This is a no-op if both sides are already errored.
3457
4060
  function TransformStreamError(stream, e) {
@@ -3479,14 +4082,22 @@ function TransformStreamSetBackpressure(stream, backpressure) {
3479
4082
  stream._backpressure = backpressure;
3480
4083
  }
3481
4084
  // Class TransformStreamDefaultController
4085
+ /**
4086
+ * Allows control of the {@link ReadableStream} and {@link WritableStream} of the associated {@link TransformStream}.
4087
+ *
4088
+ * @public
4089
+ */
3482
4090
  var TransformStreamDefaultController = /** @class */ (function () {
3483
4091
  function TransformStreamDefaultController() {
3484
4092
  throw new TypeError('Illegal constructor');
3485
4093
  }
3486
4094
  Object.defineProperty(TransformStreamDefaultController.prototype, "desiredSize", {
4095
+ /**
4096
+ * Returns the desired size to fill the readable side’s internal queue. It can be negative, if the queue is over-full.
4097
+ */
3487
4098
  get: function () {
3488
4099
  if (!IsTransformStreamDefaultController(this)) {
3489
- throw defaultControllerBrandCheckException$1('desiredSize');
4100
+ throw defaultControllerBrandCheckException('desiredSize');
3490
4101
  }
3491
4102
  var readableController = this._controlledTransformStream._readable._readableStreamController;
3492
4103
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
@@ -3497,20 +4108,28 @@ var TransformStreamDefaultController = /** @class */ (function () {
3497
4108
  TransformStreamDefaultController.prototype.enqueue = function (chunk) {
3498
4109
  if (chunk === void 0) { chunk = undefined; }
3499
4110
  if (!IsTransformStreamDefaultController(this)) {
3500
- throw defaultControllerBrandCheckException$1('enqueue');
4111
+ throw defaultControllerBrandCheckException('enqueue');
3501
4112
  }
3502
4113
  TransformStreamDefaultControllerEnqueue(this, chunk);
3503
4114
  };
4115
+ /**
4116
+ * Errors both the readable side and the writable side of the controlled transform stream, making all future
4117
+ * interactions with it fail with the given error `e`. Any chunks queued for transformation will be discarded.
4118
+ */
3504
4119
  TransformStreamDefaultController.prototype.error = function (reason) {
3505
4120
  if (reason === void 0) { reason = undefined; }
3506
4121
  if (!IsTransformStreamDefaultController(this)) {
3507
- throw defaultControllerBrandCheckException$1('error');
4122
+ throw defaultControllerBrandCheckException('error');
3508
4123
  }
3509
4124
  TransformStreamDefaultControllerError(this, reason);
3510
4125
  };
4126
+ /**
4127
+ * Closes the readable side and errors the writable side of the controlled transform stream. This is useful when the
4128
+ * transformer only needs to consume a portion of the chunks written to the writable side.
4129
+ */
3511
4130
  TransformStreamDefaultController.prototype.terminate = function () {
3512
4131
  if (!IsTransformStreamDefaultController(this)) {
3513
- throw defaultControllerBrandCheckException$1('terminate');
4132
+ throw defaultControllerBrandCheckException('terminate');
3514
4133
  }
3515
4134
  TransformStreamDefaultControllerTerminate(this);
3516
4135
  };
@@ -3536,7 +4155,7 @@ function IsTransformStreamDefaultController(x) {
3536
4155
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3537
4156
  return false;
3538
4157
  }
3539
- return true;
4158
+ return x instanceof TransformStreamDefaultController;
3540
4159
  }
3541
4160
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3542
4161
  controller._controlledTransformStream = stream;
@@ -3653,11 +4272,11 @@ function TransformStreamDefaultSourcePullAlgorithm(stream) {
3653
4272
  return stream._backpressureChangePromise;
3654
4273
  }
3655
4274
  // Helper functions for the TransformStreamDefaultController.
3656
- function defaultControllerBrandCheckException$1(name) {
4275
+ function defaultControllerBrandCheckException(name) {
3657
4276
  return new TypeError("TransformStreamDefaultController.prototype." + name + " can only be used on a TransformStreamDefaultController");
3658
4277
  }
3659
4278
  // Helper functions for the TransformStream.
3660
- function streamBrandCheckException$2(name) {
4279
+ function streamBrandCheckException(name) {
3661
4280
  return new TypeError("TransformStream.prototype." + name + " can only be used on a TransformStream");
3662
4281
  }
3663
4282