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;
@@ -599,7 +631,14 @@ function IsReadableStreamAsyncIterator(x) {
599
631
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
600
632
  return false;
601
633
  }
602
- return true;
634
+ try {
635
+ // noinspection SuspiciousTypeOfGuard
636
+ return x._asyncIteratorImpl instanceof
637
+ ReadableStreamAsyncIteratorImpl;
638
+ }
639
+ catch (_a) {
640
+ return false;
641
+ }
603
642
  }
604
643
  // Helper functions for the ReadableStream.
605
644
  function streamAsyncIteratorBrandCheckException(name) {
@@ -613,15 +652,35 @@ const NumberIsNaN = Number.isNaN || function (x) {
613
652
  return x !== x;
614
653
  };
615
654
 
616
- function IsFiniteNonNegativeNumber(v) {
617
- if (!IsNonNegativeNumber(v)) {
618
- return false;
619
- }
620
- if (v === Infinity) {
621
- return false;
655
+ function CreateArrayFromList(elements) {
656
+ // We use arrays to represent lists, so this is basically a no-op.
657
+ // Do a slice though just in case we happen to depend on the unique-ness.
658
+ return elements.slice();
659
+ }
660
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
661
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
662
+ }
663
+ // Not implemented correctly
664
+ function TransferArrayBuffer(O) {
665
+ return O;
666
+ }
667
+ // Not implemented correctly
668
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
669
+ function IsDetachedBuffer(O) {
670
+ return false;
671
+ }
672
+ function ArrayBufferSlice(buffer, begin, end) {
673
+ // ArrayBuffer.prototype.slice is not available on IE10
674
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
675
+ if (buffer.slice) {
676
+ return buffer.slice(begin, end);
622
677
  }
623
- return true;
678
+ const length = end - begin;
679
+ const slice = new ArrayBuffer(length);
680
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
681
+ return slice;
624
682
  }
683
+
625
684
  function IsNonNegativeNumber(v) {
626
685
  if (typeof v !== 'number') {
627
686
  return false;
@@ -634,6 +693,10 @@ function IsNonNegativeNumber(v) {
634
693
  }
635
694
  return true;
636
695
  }
696
+ function CloneAsUint8Array(O) {
697
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
698
+ return new Uint8Array(buffer);
699
+ }
637
700
 
638
701
  function DequeueValue(container) {
639
702
  const pair = container._queue.shift();
@@ -644,8 +707,7 @@ function DequeueValue(container) {
644
707
  return pair.value;
645
708
  }
646
709
  function EnqueueValueWithSize(container, value, size) {
647
- size = Number(size);
648
- if (!IsFiniteNonNegativeNumber(size)) {
710
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
649
711
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
650
712
  }
651
713
  container._queue.push({ value, size });
@@ -660,27 +722,18 @@ function ResetQueue(container) {
660
722
  container._queueTotalSize = 0;
661
723
  }
662
724
 
663
- function CreateArrayFromList(elements) {
664
- // We use arrays to represent lists, so this is basically a no-op.
665
- // Do a slice though just in case we happen to depend on the unique-ness.
666
- return elements.slice();
667
- }
668
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
669
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
670
- }
671
- // Not implemented correctly
672
- function TransferArrayBuffer(O) {
673
- return O;
674
- }
675
- // Not implemented correctly
676
- function IsDetachedBuffer(O) {
677
- return false;
678
- }
679
-
725
+ /**
726
+ * A pull-into request in a {@link ReadableByteStreamController}.
727
+ *
728
+ * @public
729
+ */
680
730
  class ReadableStreamBYOBRequest {
681
731
  constructor() {
682
732
  throw new TypeError('Illegal constructor');
683
733
  }
734
+ /**
735
+ * Returns the view for writing in to, or `null` if the BYOB request has already been responded to.
736
+ */
684
737
  get view() {
685
738
  if (!IsReadableStreamBYOBRequest(this)) {
686
739
  throw byobRequestBrandCheckException('view');
@@ -707,15 +760,10 @@ class ReadableStreamBYOBRequest {
707
760
  if (!ArrayBuffer.isView(view)) {
708
761
  throw new TypeError('You can only respond with array buffer views');
709
762
  }
710
- if (view.byteLength === 0) {
711
- throw new TypeError('chunk must have non-zero byteLength');
712
- }
713
- if (view.buffer.byteLength === 0) {
714
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
715
- }
716
763
  if (this._associatedReadableByteStreamController === undefined) {
717
764
  throw new TypeError('This BYOB request has been invalidated');
718
765
  }
766
+ if (IsDetachedBuffer(view.buffer)) ;
719
767
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
720
768
  }
721
769
  }
@@ -730,29 +778,38 @@ if (typeof SymbolPolyfill.toStringTag === 'symbol') {
730
778
  configurable: true
731
779
  });
732
780
  }
781
+ /**
782
+ * Allows control of a {@link ReadableStream | readable byte stream}'s state and internal queue.
783
+ *
784
+ * @public
785
+ */
733
786
  class ReadableByteStreamController {
734
787
  constructor() {
735
788
  throw new TypeError('Illegal constructor');
736
789
  }
790
+ /**
791
+ * Returns the current BYOB pull request, or `null` if there isn't one.
792
+ */
737
793
  get byobRequest() {
738
794
  if (!IsReadableByteStreamController(this)) {
739
795
  throw byteStreamControllerBrandCheckException('byobRequest');
740
796
  }
741
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
742
- const firstDescriptor = this._pendingPullIntos.peek();
743
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
744
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
745
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
746
- this._byobRequest = byobRequest;
747
- }
748
- return this._byobRequest;
797
+ return ReadableByteStreamControllerGetBYOBRequest(this);
749
798
  }
799
+ /**
800
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
801
+ * over-full. An underlying byte source ought to use this information to determine when and how to apply backpressure.
802
+ */
750
803
  get desiredSize() {
751
804
  if (!IsReadableByteStreamController(this)) {
752
805
  throw byteStreamControllerBrandCheckException('desiredSize');
753
806
  }
754
807
  return ReadableByteStreamControllerGetDesiredSize(this);
755
808
  }
809
+ /**
810
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
811
+ * the stream, but once those are read, the stream will become closed.
812
+ */
756
813
  close() {
757
814
  if (!IsReadableByteStreamController(this)) {
758
815
  throw byteStreamControllerBrandCheckException('close');
@@ -789,6 +846,9 @@ class ReadableByteStreamController {
789
846
  }
790
847
  ReadableByteStreamControllerEnqueue(this, chunk);
791
848
  }
849
+ /**
850
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
851
+ */
792
852
  error(e = undefined) {
793
853
  if (!IsReadableByteStreamController(this)) {
794
854
  throw byteStreamControllerBrandCheckException('error');
@@ -797,10 +857,7 @@ class ReadableByteStreamController {
797
857
  }
798
858
  /** @internal */
799
859
  [CancelSteps](reason) {
800
- if (this._pendingPullIntos.length > 0) {
801
- const firstDescriptor = this._pendingPullIntos.peek();
802
- firstDescriptor.bytesFilled = 0;
803
- }
860
+ ReadableByteStreamControllerClearPendingPullIntos(this);
804
861
  ResetQueue(this);
805
862
  const result = this._cancelAlgorithm(reason);
806
863
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -829,6 +886,7 @@ class ReadableByteStreamController {
829
886
  }
830
887
  const pullIntoDescriptor = {
831
888
  buffer,
889
+ bufferByteLength: autoAllocateChunkSize,
832
890
  byteOffset: 0,
833
891
  byteLength: autoAllocateChunkSize,
834
892
  bytesFilled: 0,
@@ -863,7 +921,7 @@ function IsReadableByteStreamController(x) {
863
921
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
864
922
  return false;
865
923
  }
866
- return true;
924
+ return x instanceof ReadableByteStreamController;
867
925
  }
868
926
  function IsReadableStreamBYOBRequest(x) {
869
927
  if (!typeIsObject(x)) {
@@ -872,7 +930,7 @@ function IsReadableStreamBYOBRequest(x) {
872
930
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
873
931
  return false;
874
932
  }
875
- return true;
933
+ return x instanceof ReadableStreamBYOBRequest;
876
934
  }
877
935
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
878
936
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -954,7 +1012,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
954
1012
  return ready;
955
1013
  }
956
1014
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
957
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
958
1015
  pullIntoDescriptor.bytesFilled += size;
959
1016
  }
960
1017
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -993,9 +1050,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
993
1050
  elementSize = view.constructor.BYTES_PER_ELEMENT;
994
1051
  }
995
1052
  const ctor = view.constructor;
1053
+ // try {
996
1054
  const buffer = TransferArrayBuffer(view.buffer);
1055
+ // } catch (e) {
1056
+ // readIntoRequest._errorSteps(e);
1057
+ // return;
1058
+ // }
997
1059
  const pullIntoDescriptor = {
998
1060
  buffer,
1061
+ bufferByteLength: buffer.byteLength,
999
1062
  byteOffset: view.byteOffset,
1000
1063
  byteLength: view.byteLength,
1001
1064
  bytesFilled: 0,
@@ -1035,7 +1098,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1035
1098
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1036
1099
  }
1037
1100
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1038
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1039
1101
  const stream = controller._controlledReadableByteStream;
1040
1102
  if (ReadableStreamHasBYOBReader(stream)) {
1041
1103
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1045,34 +1107,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1045
1107
  }
1046
1108
  }
1047
1109
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1048
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1049
- throw new RangeError('bytesWritten out of range');
1050
- }
1051
1110
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1052
1111
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1053
- // TODO: Figure out whether we should detach the buffer or not here.
1054
1112
  return;
1055
1113
  }
1056
1114
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1057
1115
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1058
1116
  if (remainderSize > 0) {
1059
1117
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1060
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1118
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1061
1119
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1062
1120
  }
1063
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1064
1121
  pullIntoDescriptor.bytesFilled -= remainderSize;
1065
1122
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1066
1123
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1067
1124
  }
1068
1125
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1069
1126
  const firstDescriptor = controller._pendingPullIntos.peek();
1070
- const stream = controller._controlledReadableByteStream;
1071
- if (stream._state === 'closed') {
1072
- if (bytesWritten !== 0) {
1073
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1074
- }
1075
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1127
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1128
+ const state = controller._controlledReadableByteStream._state;
1129
+ if (state === 'closed') {
1130
+ ReadableByteStreamControllerRespondInClosedState(controller);
1076
1131
  }
1077
1132
  else {
1078
1133
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1081,7 +1136,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1081
1136
  }
1082
1137
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1083
1138
  const descriptor = controller._pendingPullIntos.shift();
1084
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1085
1139
  return descriptor;
1086
1140
  }
1087
1141
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1141,6 +1195,12 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1141
1195
  const byteOffset = chunk.byteOffset;
1142
1196
  const byteLength = chunk.byteLength;
1143
1197
  const transferredBuffer = TransferArrayBuffer(buffer);
1198
+ if (controller._pendingPullIntos.length > 0) {
1199
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1200
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1201
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1202
+ }
1203
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1144
1204
  if (ReadableStreamHasDefaultReader(stream)) {
1145
1205
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1146
1206
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1170,9 +1230,18 @@ function ReadableByteStreamControllerError(controller, e) {
1170
1230
  ReadableByteStreamControllerClearAlgorithms(controller);
1171
1231
  ReadableStreamError(stream, e);
1172
1232
  }
1233
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1234
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1235
+ const firstDescriptor = controller._pendingPullIntos.peek();
1236
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1237
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1238
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1239
+ controller._byobRequest = byobRequest;
1240
+ }
1241
+ return controller._byobRequest;
1242
+ }
1173
1243
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1174
- const stream = controller._controlledReadableByteStream;
1175
- const state = stream._state;
1244
+ const state = controller._controlledReadableByteStream._state;
1176
1245
  if (state === 'errored') {
1177
1246
  return null;
1178
1247
  }
@@ -1182,21 +1251,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1182
1251
  return controller._strategyHWM - controller._queueTotalSize;
1183
1252
  }
1184
1253
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1185
- bytesWritten = Number(bytesWritten);
1186
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1187
- throw new RangeError('bytesWritten must be a finite');
1254
+ const firstDescriptor = controller._pendingPullIntos.peek();
1255
+ const state = controller._controlledReadableByteStream._state;
1256
+ if (state === 'closed') {
1257
+ if (bytesWritten !== 0) {
1258
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1259
+ }
1260
+ }
1261
+ else {
1262
+ if (bytesWritten === 0) {
1263
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1264
+ }
1265
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1266
+ throw new RangeError('bytesWritten out of range');
1267
+ }
1188
1268
  }
1269
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1189
1270
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1190
1271
  }
1191
1272
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1192
1273
  const firstDescriptor = controller._pendingPullIntos.peek();
1274
+ const state = controller._controlledReadableByteStream._state;
1275
+ if (state === 'closed') {
1276
+ if (view.byteLength !== 0) {
1277
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1278
+ }
1279
+ }
1280
+ else {
1281
+ if (view.byteLength === 0) {
1282
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1283
+ }
1284
+ }
1193
1285
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1194
1286
  throw new RangeError('The region specified by view does not match byobRequest');
1195
1287
  }
1196
- if (firstDescriptor.byteLength !== view.byteLength) {
1288
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1197
1289
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1198
1290
  }
1199
- firstDescriptor.buffer = view.buffer;
1291
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1292
+ throw new RangeError('The region specified by view is larger than byobRequest');
1293
+ }
1294
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1200
1295
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1201
1296
  }
1202
1297
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1238,6 +1333,9 @@ function SetUpReadableByteStreamControllerFromUnderlyingSource(stream, underlyin
1238
1333
  cancelAlgorithm = reason => underlyingByteSource.cancel(reason);
1239
1334
  }
1240
1335
  const autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
1336
+ if (autoAllocateChunkSize === 0) {
1337
+ throw new TypeError('autoAllocateChunkSize must be greater than 0');
1338
+ }
1241
1339
  SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
1242
1340
  }
1243
1341
  function SetUpReadableStreamBYOBRequest(request, controller, view) {
@@ -1284,6 +1382,11 @@ function ReadableStreamHasBYOBReader(stream) {
1284
1382
  }
1285
1383
  return true;
1286
1384
  }
1385
+ /**
1386
+ * A BYOB reader vended by a {@link ReadableStream}.
1387
+ *
1388
+ * @public
1389
+ */
1287
1390
  class ReadableStreamBYOBReader {
1288
1391
  constructor(stream) {
1289
1392
  assertRequiredArgument(stream, 1, 'ReadableStreamBYOBReader');
@@ -1298,12 +1401,19 @@ class ReadableStreamBYOBReader {
1298
1401
  ReadableStreamReaderGenericInitialize(this, stream);
1299
1402
  this._readIntoRequests = new SimpleQueue();
1300
1403
  }
1404
+ /**
1405
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
1406
+ * the reader's lock is released before the stream finishes closing.
1407
+ */
1301
1408
  get closed() {
1302
1409
  if (!IsReadableStreamBYOBReader(this)) {
1303
1410
  return promiseRejectedWith(byobReaderBrandCheckException('closed'));
1304
1411
  }
1305
1412
  return this._closedPromise;
1306
1413
  }
1414
+ /**
1415
+ * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
1416
+ */
1307
1417
  cancel(reason = undefined) {
1308
1418
  if (!IsReadableStreamBYOBReader(this)) {
1309
1419
  return promiseRejectedWith(byobReaderBrandCheckException('cancel'));
@@ -1313,6 +1423,11 @@ class ReadableStreamBYOBReader {
1313
1423
  }
1314
1424
  return ReadableStreamReaderGenericCancel(this, reason);
1315
1425
  }
1426
+ /**
1427
+ * Attempts to reads bytes into view, and returns a promise resolved with the result.
1428
+ *
1429
+ * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
1430
+ */
1316
1431
  read(view) {
1317
1432
  if (!IsReadableStreamBYOBReader(this)) {
1318
1433
  return promiseRejectedWith(byobReaderBrandCheckException('read'));
@@ -1326,6 +1441,7 @@ class ReadableStreamBYOBReader {
1326
1441
  if (view.buffer.byteLength === 0) {
1327
1442
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1328
1443
  }
1444
+ if (IsDetachedBuffer(view.buffer)) ;
1329
1445
  if (this._ownerReadableStream === undefined) {
1330
1446
  return promiseRejectedWith(readerLockException('read from'));
1331
1447
  }
@@ -1343,6 +1459,15 @@ class ReadableStreamBYOBReader {
1343
1459
  ReadableStreamBYOBReaderRead(this, view, readIntoRequest);
1344
1460
  return promise;
1345
1461
  }
1462
+ /**
1463
+ * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
1464
+ * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
1465
+ * from now on; otherwise, the reader will appear closed.
1466
+ *
1467
+ * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
1468
+ * the reader's {@link ReadableStreamBYOBReader.read | read()} method has not yet been settled. Attempting to
1469
+ * do so will throw a `TypeError` and leave the reader locked to the stream.
1470
+ */
1346
1471
  releaseLock() {
1347
1472
  if (!IsReadableStreamBYOBReader(this)) {
1348
1473
  throw byobReaderBrandCheckException('releaseLock');
@@ -1376,7 +1501,7 @@ function IsReadableStreamBYOBReader(x) {
1376
1501
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1377
1502
  return false;
1378
1503
  }
1379
- return true;
1504
+ return x instanceof ReadableStreamBYOBReader;
1380
1505
  }
1381
1506
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1382
1507
  const stream = reader._ownerReadableStream;
@@ -1471,6 +1596,36 @@ function assertWritableStream(x, context) {
1471
1596
  }
1472
1597
  }
1473
1598
 
1599
+ function isAbortSignal(value) {
1600
+ if (typeof value !== 'object' || value === null) {
1601
+ return false;
1602
+ }
1603
+ try {
1604
+ return typeof value.aborted === 'boolean';
1605
+ }
1606
+ catch (_a) {
1607
+ // AbortSignal.prototype.aborted throws if its brand check fails
1608
+ return false;
1609
+ }
1610
+ }
1611
+ const supportsAbortController = typeof AbortController === 'function';
1612
+ /**
1613
+ * Construct a new AbortController, if supported by the platform.
1614
+ *
1615
+ * @internal
1616
+ */
1617
+ function createAbortController() {
1618
+ if (supportsAbortController) {
1619
+ return new AbortController();
1620
+ }
1621
+ return undefined;
1622
+ }
1623
+
1624
+ /**
1625
+ * A writable stream represents a destination for data, into which you can write.
1626
+ *
1627
+ * @public
1628
+ */
1474
1629
  class WritableStream {
1475
1630
  constructor(rawUnderlyingSink = {}, rawStrategy = {}) {
1476
1631
  if (rawUnderlyingSink === undefined) {
@@ -1490,24 +1645,44 @@ class WritableStream {
1490
1645
  const highWaterMark = ExtractHighWaterMark(strategy, 1);
1491
1646
  SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, highWaterMark, sizeAlgorithm);
1492
1647
  }
1648
+ /**
1649
+ * Returns whether or not the writable stream is locked to a writer.
1650
+ */
1493
1651
  get locked() {
1494
1652
  if (!IsWritableStream(this)) {
1495
- throw streamBrandCheckException('locked');
1653
+ throw streamBrandCheckException$2('locked');
1496
1654
  }
1497
1655
  return IsWritableStreamLocked(this);
1498
1656
  }
1657
+ /**
1658
+ * Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be
1659
+ * immediately moved to an errored state, with any queued-up writes discarded. This will also execute any abort
1660
+ * mechanism of the underlying sink.
1661
+ *
1662
+ * The returned promise will fulfill if the stream shuts down successfully, or reject if the underlying sink signaled
1663
+ * that there was an error doing so. Additionally, it will reject with a `TypeError` (without attempting to cancel
1664
+ * the stream) if the stream is currently locked.
1665
+ */
1499
1666
  abort(reason = undefined) {
1500
1667
  if (!IsWritableStream(this)) {
1501
- return promiseRejectedWith(streamBrandCheckException('abort'));
1668
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1502
1669
  }
1503
1670
  if (IsWritableStreamLocked(this)) {
1504
1671
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
1505
1672
  }
1506
1673
  return WritableStreamAbort(this, reason);
1507
1674
  }
1675
+ /**
1676
+ * Closes the stream. The underlying sink will finish processing any previously-written chunks, before invoking its
1677
+ * close behavior. During this time any further attempts to write will fail (without erroring the stream).
1678
+ *
1679
+ * The method returns a promise that will fulfill if all remaining chunks are successfully written and the stream
1680
+ * successfully closes, or rejects if an error is encountered during this process. Additionally, it will reject with
1681
+ * a `TypeError` (without attempting to cancel the stream) if the stream is currently locked.
1682
+ */
1508
1683
  close() {
1509
1684
  if (!IsWritableStream(this)) {
1510
- return promiseRejectedWith(streamBrandCheckException('close'));
1685
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1511
1686
  }
1512
1687
  if (IsWritableStreamLocked(this)) {
1513
1688
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1517,9 +1692,17 @@ class WritableStream {
1517
1692
  }
1518
1693
  return WritableStreamClose(this);
1519
1694
  }
1695
+ /**
1696
+ * Creates a {@link WritableStreamDefaultWriter | writer} and locks the stream to the new writer. While the stream
1697
+ * is locked, no other writer can be acquired until this one is released.
1698
+ *
1699
+ * This functionality is especially useful for creating abstractions that desire the ability to write to a stream
1700
+ * without interruption or interleaving. By getting a writer for the stream, you can ensure nobody else can write at
1701
+ * the same time, which would cause the resulting written data to be unpredictable and probably useless.
1702
+ */
1520
1703
  getWriter() {
1521
1704
  if (!IsWritableStream(this)) {
1522
- throw streamBrandCheckException('getWriter');
1705
+ throw streamBrandCheckException$2('getWriter');
1523
1706
  }
1524
1707
  return AcquireWritableStreamDefaultWriter(this);
1525
1708
  }
@@ -1581,7 +1764,7 @@ function IsWritableStream(x) {
1581
1764
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1582
1765
  return false;
1583
1766
  }
1584
- return true;
1767
+ return x instanceof WritableStream;
1585
1768
  }
1586
1769
  function IsWritableStreamLocked(stream) {
1587
1770
  if (stream._writer === undefined) {
@@ -1590,6 +1773,15 @@ function IsWritableStreamLocked(stream) {
1590
1773
  return true;
1591
1774
  }
1592
1775
  function WritableStreamAbort(stream, reason) {
1776
+ var _a;
1777
+ if (stream._state === 'closed' || stream._state === 'errored') {
1778
+ return promiseResolvedWith(undefined);
1779
+ }
1780
+ stream._writableStreamController._abortReason = reason;
1781
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1782
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1783
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1784
+ // Widen the type again by casting to WritableStreamState.
1593
1785
  const state = stream._state;
1594
1786
  if (state === 'closed' || state === 'errored') {
1595
1787
  return promiseResolvedWith(undefined);
@@ -1775,6 +1967,11 @@ function WritableStreamUpdateBackpressure(stream, backpressure) {
1775
1967
  }
1776
1968
  stream._backpressure = backpressure;
1777
1969
  }
1970
+ /**
1971
+ * A default writer vended by a {@link WritableStream}.
1972
+ *
1973
+ * @public
1974
+ */
1778
1975
  class WritableStreamDefaultWriter {
1779
1976
  constructor(stream) {
1780
1977
  assertRequiredArgument(stream, 1, 'WritableStreamDefaultWriter');
@@ -1808,12 +2005,24 @@ class WritableStreamDefaultWriter {
1808
2005
  defaultWriterClosedPromiseInitializeAsRejected(this, storedError);
1809
2006
  }
1810
2007
  }
2008
+ /**
2009
+ * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
2010
+ * the writer’s lock is released before the stream finishes closing.
2011
+ */
1811
2012
  get closed() {
1812
2013
  if (!IsWritableStreamDefaultWriter(this)) {
1813
2014
  return promiseRejectedWith(defaultWriterBrandCheckException('closed'));
1814
2015
  }
1815
2016
  return this._closedPromise;
1816
2017
  }
2018
+ /**
2019
+ * Returns the desired size to fill the stream’s internal queue. It can be negative, if the queue is over-full.
2020
+ * A producer can use this information to determine the right amount of data to write.
2021
+ *
2022
+ * It will be `null` if the stream cannot be successfully written to (due to either being errored, or having an abort
2023
+ * queued up). It will return zero if the stream is closed. And the getter will throw an exception if invoked when
2024
+ * the writer’s lock is released.
2025
+ */
1817
2026
  get desiredSize() {
1818
2027
  if (!IsWritableStreamDefaultWriter(this)) {
1819
2028
  throw defaultWriterBrandCheckException('desiredSize');
@@ -1823,12 +2032,23 @@ class WritableStreamDefaultWriter {
1823
2032
  }
1824
2033
  return WritableStreamDefaultWriterGetDesiredSize(this);
1825
2034
  }
2035
+ /**
2036
+ * Returns a promise that will be fulfilled when the desired size to fill the stream’s internal queue transitions
2037
+ * from non-positive to positive, signaling that it is no longer applying backpressure. Once the desired size dips
2038
+ * back to zero or below, the getter will return a new promise that stays pending until the next transition.
2039
+ *
2040
+ * If the stream becomes errored or aborted, or the writer’s lock is released, the returned promise will become
2041
+ * rejected.
2042
+ */
1826
2043
  get ready() {
1827
2044
  if (!IsWritableStreamDefaultWriter(this)) {
1828
2045
  return promiseRejectedWith(defaultWriterBrandCheckException('ready'));
1829
2046
  }
1830
2047
  return this._readyPromise;
1831
2048
  }
2049
+ /**
2050
+ * If the reader is active, behaves the same as {@link WritableStream.abort | stream.abort(reason)}.
2051
+ */
1832
2052
  abort(reason = undefined) {
1833
2053
  if (!IsWritableStreamDefaultWriter(this)) {
1834
2054
  return promiseRejectedWith(defaultWriterBrandCheckException('abort'));
@@ -1838,6 +2058,9 @@ class WritableStreamDefaultWriter {
1838
2058
  }
1839
2059
  return WritableStreamDefaultWriterAbort(this, reason);
1840
2060
  }
2061
+ /**
2062
+ * If the reader is active, behaves the same as {@link WritableStream.close | stream.close()}.
2063
+ */
1841
2064
  close() {
1842
2065
  if (!IsWritableStreamDefaultWriter(this)) {
1843
2066
  return promiseRejectedWith(defaultWriterBrandCheckException('close'));
@@ -1851,6 +2074,16 @@ class WritableStreamDefaultWriter {
1851
2074
  }
1852
2075
  return WritableStreamDefaultWriterClose(this);
1853
2076
  }
2077
+ /**
2078
+ * Releases the writer’s lock on the corresponding stream. After the lock is released, the writer is no longer active.
2079
+ * If the associated stream is errored when the lock is released, the writer will appear errored in the same way from
2080
+ * now on; otherwise, the writer will appear closed.
2081
+ *
2082
+ * Note that the lock can still be released even if some ongoing writes have not yet finished (i.e. even if the
2083
+ * promises returned from previous calls to {@link WritableStreamDefaultWriter.write | write()} have not yet settled).
2084
+ * It’s not necessary to hold the lock on the writer for the duration of the write; the lock instead simply prevents
2085
+ * other producers from writing in an interleaved manner.
2086
+ */
1854
2087
  releaseLock() {
1855
2088
  if (!IsWritableStreamDefaultWriter(this)) {
1856
2089
  throw defaultWriterBrandCheckException('releaseLock');
@@ -1894,7 +2127,7 @@ function IsWritableStreamDefaultWriter(x) {
1894
2127
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
1895
2128
  return false;
1896
2129
  }
1897
- return true;
2130
+ return x instanceof WritableStreamDefaultWriter;
1898
2131
  }
1899
2132
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
1900
2133
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -1975,13 +2208,49 @@ function WritableStreamDefaultWriterWrite(writer, chunk) {
1975
2208
  return promise;
1976
2209
  }
1977
2210
  const closeSentinel = {};
2211
+ /**
2212
+ * Allows control of a {@link WritableStream | writable stream}'s state and internal queue.
2213
+ *
2214
+ * @public
2215
+ */
1978
2216
  class WritableStreamDefaultController {
1979
2217
  constructor() {
1980
2218
  throw new TypeError('Illegal constructor');
1981
2219
  }
2220
+ /**
2221
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2222
+ */
2223
+ get abortReason() {
2224
+ if (!IsWritableStreamDefaultController(this)) {
2225
+ throw defaultControllerBrandCheckException$2('abortReason');
2226
+ }
2227
+ return this._abortReason;
2228
+ }
2229
+ /**
2230
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2231
+ */
2232
+ get signal() {
2233
+ if (!IsWritableStreamDefaultController(this)) {
2234
+ throw defaultControllerBrandCheckException$2('signal');
2235
+ }
2236
+ if (this._abortController === undefined) {
2237
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2238
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2239
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2240
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2241
+ }
2242
+ return this._abortController.signal;
2243
+ }
2244
+ /**
2245
+ * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2246
+ *
2247
+ * This method is rarely used, since usually it suffices to return a rejected promise from one of the underlying
2248
+ * sink's methods. However, it can be useful for suddenly shutting down a stream in response to an event outside the
2249
+ * normal lifecycle of interactions with the underlying sink.
2250
+ */
1982
2251
  error(e = undefined) {
1983
2252
  if (!IsWritableStreamDefaultController(this)) {
1984
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2253
+ throw defaultControllerBrandCheckException$2('error');
1985
2254
  }
1986
2255
  const state = this._controlledWritableStream._state;
1987
2256
  if (state !== 'writable') {
@@ -2019,7 +2288,7 @@ function IsWritableStreamDefaultController(x) {
2019
2288
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2020
2289
  return false;
2021
2290
  }
2022
- return true;
2291
+ return x instanceof WritableStreamDefaultController;
2023
2292
  }
2024
2293
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2025
2294
  controller._controlledWritableStream = stream;
@@ -2028,6 +2297,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2028
2297
  controller._queue = undefined;
2029
2298
  controller._queueTotalSize = undefined;
2030
2299
  ResetQueue(controller);
2300
+ controller._abortReason = undefined;
2301
+ controller._abortController = createAbortController();
2031
2302
  controller._started = false;
2032
2303
  controller._strategySizeAlgorithm = sizeAlgorithm;
2033
2304
  controller._strategyHWM = highWaterMark;
@@ -2177,9 +2448,13 @@ function WritableStreamDefaultControllerError(controller, error) {
2177
2448
  WritableStreamStartErroring(stream, error);
2178
2449
  }
2179
2450
  // Helper functions for the WritableStream.
2180
- function streamBrandCheckException(name) {
2451
+ function streamBrandCheckException$2(name) {
2181
2452
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2182
2453
  }
2454
+ // Helper functions for the WritableStreamDefaultController.
2455
+ function defaultControllerBrandCheckException$2(name) {
2456
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2457
+ }
2183
2458
  // Helper functions for the WritableStreamDefaultWriter.
2184
2459
  function defaultWriterBrandCheckException(name) {
2185
2460
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2203,6 +2478,9 @@ function defaultWriterClosedPromiseInitializeAsResolved(writer) {
2203
2478
  defaultWriterClosedPromiseResolve(writer);
2204
2479
  }
2205
2480
  function defaultWriterClosedPromiseReject(writer, reason) {
2481
+ if (writer._closedPromise_reject === undefined) {
2482
+ return;
2483
+ }
2206
2484
  setPromiseIsHandledToTrue(writer._closedPromise);
2207
2485
  writer._closedPromise_reject(reason);
2208
2486
  writer._closedPromise_resolve = undefined;
@@ -2213,6 +2491,9 @@ function defaultWriterClosedPromiseResetToRejected(writer, reason) {
2213
2491
  defaultWriterClosedPromiseInitializeAsRejected(writer, reason);
2214
2492
  }
2215
2493
  function defaultWriterClosedPromiseResolve(writer) {
2494
+ if (writer._closedPromise_resolve === undefined) {
2495
+ return;
2496
+ }
2216
2497
  writer._closedPromise_resolve(undefined);
2217
2498
  writer._closedPromise_resolve = undefined;
2218
2499
  writer._closedPromise_reject = undefined;
@@ -2234,6 +2515,9 @@ function defaultWriterReadyPromiseInitializeAsResolved(writer) {
2234
2515
  defaultWriterReadyPromiseResolve(writer);
2235
2516
  }
2236
2517
  function defaultWriterReadyPromiseReject(writer, reason) {
2518
+ if (writer._readyPromise_reject === undefined) {
2519
+ return;
2520
+ }
2237
2521
  setPromiseIsHandledToTrue(writer._readyPromise);
2238
2522
  writer._readyPromise_reject(reason);
2239
2523
  writer._readyPromise_resolve = undefined;
@@ -2247,25 +2531,15 @@ function defaultWriterReadyPromiseResetToRejected(writer, reason) {
2247
2531
  defaultWriterReadyPromiseInitializeAsRejected(writer, reason);
2248
2532
  }
2249
2533
  function defaultWriterReadyPromiseResolve(writer) {
2534
+ if (writer._readyPromise_resolve === undefined) {
2535
+ return;
2536
+ }
2250
2537
  writer._readyPromise_resolve(undefined);
2251
2538
  writer._readyPromise_resolve = undefined;
2252
2539
  writer._readyPromise_reject = undefined;
2253
2540
  writer._readyPromiseState = 'fulfilled';
2254
2541
  }
2255
2542
 
2256
- function isAbortSignal(value) {
2257
- if (typeof value !== 'object' || value === null) {
2258
- return false;
2259
- }
2260
- try {
2261
- return typeof value.aborted === 'boolean';
2262
- }
2263
- catch (_a) {
2264
- // AbortSignal.prototype.aborted throws if its brand check fails
2265
- return false;
2266
- }
2267
- }
2268
-
2269
2543
  /// <reference lib="dom" />
2270
2544
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2271
2545
 
@@ -2283,6 +2557,7 @@ function isDOMExceptionConstructor(ctor) {
2283
2557
  }
2284
2558
  }
2285
2559
  function createDOMExceptionPolyfill() {
2560
+ // eslint-disable-next-line no-shadow
2286
2561
  const ctor = function DOMException(message, name) {
2287
2562
  this.message = message || '';
2288
2563
  this.name = name || 'Error';
@@ -2294,6 +2569,7 @@ function createDOMExceptionPolyfill() {
2294
2569
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2295
2570
  return ctor;
2296
2571
  }
2572
+ // eslint-disable-next-line no-redeclare
2297
2573
  const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2298
2574
 
2299
2575
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2471,19 +2747,32 @@ function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventC
2471
2747
  });
2472
2748
  }
2473
2749
 
2750
+ /**
2751
+ * Allows control of a {@link ReadableStream | readable stream}'s state and internal queue.
2752
+ *
2753
+ * @public
2754
+ */
2474
2755
  class ReadableStreamDefaultController {
2475
2756
  constructor() {
2476
2757
  throw new TypeError('Illegal constructor');
2477
2758
  }
2759
+ /**
2760
+ * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
2761
+ * over-full. An underlying source ought to use this information to determine when and how to apply backpressure.
2762
+ */
2478
2763
  get desiredSize() {
2479
2764
  if (!IsReadableStreamDefaultController(this)) {
2480
- throw defaultControllerBrandCheckException('desiredSize');
2765
+ throw defaultControllerBrandCheckException$1('desiredSize');
2481
2766
  }
2482
2767
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2483
2768
  }
2769
+ /**
2770
+ * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
2771
+ * the stream, but once those are read, the stream will become closed.
2772
+ */
2484
2773
  close() {
2485
2774
  if (!IsReadableStreamDefaultController(this)) {
2486
- throw defaultControllerBrandCheckException('close');
2775
+ throw defaultControllerBrandCheckException$1('close');
2487
2776
  }
2488
2777
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2489
2778
  throw new TypeError('The stream is not in a state that permits close');
@@ -2492,16 +2781,19 @@ class ReadableStreamDefaultController {
2492
2781
  }
2493
2782
  enqueue(chunk = undefined) {
2494
2783
  if (!IsReadableStreamDefaultController(this)) {
2495
- throw defaultControllerBrandCheckException('enqueue');
2784
+ throw defaultControllerBrandCheckException$1('enqueue');
2496
2785
  }
2497
2786
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2498
2787
  throw new TypeError('The stream is not in a state that permits enqueue');
2499
2788
  }
2500
2789
  return ReadableStreamDefaultControllerEnqueue(this, chunk);
2501
2790
  }
2791
+ /**
2792
+ * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
2793
+ */
2502
2794
  error(e = undefined) {
2503
2795
  if (!IsReadableStreamDefaultController(this)) {
2504
- throw defaultControllerBrandCheckException('error');
2796
+ throw defaultControllerBrandCheckException$1('error');
2505
2797
  }
2506
2798
  ReadableStreamDefaultControllerError(this, e);
2507
2799
  }
@@ -2552,7 +2844,7 @@ function IsReadableStreamDefaultController(x) {
2552
2844
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2553
2845
  return false;
2554
2846
  }
2555
- return true;
2847
+ return x instanceof ReadableStreamDefaultController;
2556
2848
  }
2557
2849
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2558
2850
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2646,8 +2938,7 @@ function ReadableStreamDefaultControllerError(controller, e) {
2646
2938
  ReadableStreamError(stream, e);
2647
2939
  }
2648
2940
  function ReadableStreamDefaultControllerGetDesiredSize(controller) {
2649
- const stream = controller._controlledReadableStream;
2650
- const state = stream._state;
2941
+ const state = controller._controlledReadableStream._state;
2651
2942
  if (state === 'errored') {
2652
2943
  return null;
2653
2944
  }
@@ -2709,11 +3000,17 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underl
2709
3000
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2710
3001
  }
2711
3002
  // Helper functions for the ReadableStreamDefaultController.
2712
- function defaultControllerBrandCheckException(name) {
3003
+ function defaultControllerBrandCheckException$1(name) {
2713
3004
  return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
2714
3005
  }
2715
3006
 
2716
3007
  function ReadableStreamTee(stream, cloneForBranch2) {
3008
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3009
+ return ReadableByteStreamTee(stream);
3010
+ }
3011
+ return ReadableStreamDefaultTee(stream);
3012
+ }
3013
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2717
3014
  const reader = AcquireReadableStreamDefaultReader(stream);
2718
3015
  let reading = false;
2719
3016
  let canceled1 = false;
@@ -2732,24 +3029,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2732
3029
  }
2733
3030
  reading = true;
2734
3031
  const readRequest = {
2735
- _chunkSteps: value => {
3032
+ _chunkSteps: chunk => {
2736
3033
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2737
3034
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2738
3035
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2739
3036
  queueMicrotask(() => {
2740
3037
  reading = false;
2741
- const value1 = value;
2742
- const value2 = value;
3038
+ const chunk1 = chunk;
3039
+ const chunk2 = chunk;
2743
3040
  // There is no way to access the cloning code right now in the reference implementation.
2744
3041
  // If we add one then we'll need an implementation for serializable objects.
2745
3042
  // if (!canceled2 && cloneForBranch2) {
2746
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3043
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2747
3044
  // }
2748
3045
  if (!canceled1) {
2749
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3046
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2750
3047
  }
2751
3048
  if (!canceled2) {
2752
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3049
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2753
3050
  }
2754
3051
  });
2755
3052
  },
@@ -2761,6 +3058,9 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2761
3058
  if (!canceled2) {
2762
3059
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2763
3060
  }
3061
+ if (!canceled1 || !canceled2) {
3062
+ resolveCancelPromise(undefined);
3063
+ }
2764
3064
  },
2765
3065
  _errorSteps: () => {
2766
3066
  reading = false;
@@ -2797,9 +3097,217 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2797
3097
  uponRejection(reader._closedPromise, (r) => {
2798
3098
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
2799
3099
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3100
+ if (!canceled1 || !canceled2) {
3101
+ resolveCancelPromise(undefined);
3102
+ }
2800
3103
  });
2801
3104
  return [branch1, branch2];
2802
3105
  }
3106
+ function ReadableByteStreamTee(stream) {
3107
+ let reader = AcquireReadableStreamDefaultReader(stream);
3108
+ let reading = false;
3109
+ let canceled1 = false;
3110
+ let canceled2 = false;
3111
+ let reason1;
3112
+ let reason2;
3113
+ let branch1;
3114
+ let branch2;
3115
+ let resolveCancelPromise;
3116
+ const cancelPromise = newPromise(resolve => {
3117
+ resolveCancelPromise = resolve;
3118
+ });
3119
+ function forwardReaderError(thisReader) {
3120
+ uponRejection(thisReader._closedPromise, r => {
3121
+ if (thisReader !== reader) {
3122
+ return;
3123
+ }
3124
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3125
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3126
+ if (!canceled1 || !canceled2) {
3127
+ resolveCancelPromise(undefined);
3128
+ }
3129
+ });
3130
+ }
3131
+ function pullWithDefaultReader() {
3132
+ if (IsReadableStreamBYOBReader(reader)) {
3133
+ ReadableStreamReaderGenericRelease(reader);
3134
+ reader = AcquireReadableStreamDefaultReader(stream);
3135
+ forwardReaderError(reader);
3136
+ }
3137
+ const readRequest = {
3138
+ _chunkSteps: chunk => {
3139
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3140
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3141
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3142
+ queueMicrotask(() => {
3143
+ reading = false;
3144
+ const chunk1 = chunk;
3145
+ let chunk2 = chunk;
3146
+ if (!canceled1 && !canceled2) {
3147
+ try {
3148
+ chunk2 = CloneAsUint8Array(chunk);
3149
+ }
3150
+ catch (cloneE) {
3151
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3152
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3153
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3154
+ return;
3155
+ }
3156
+ }
3157
+ if (!canceled1) {
3158
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3159
+ }
3160
+ if (!canceled2) {
3161
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3162
+ }
3163
+ });
3164
+ },
3165
+ _closeSteps: () => {
3166
+ reading = false;
3167
+ if (!canceled1) {
3168
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3169
+ }
3170
+ if (!canceled2) {
3171
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3172
+ }
3173
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3174
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3175
+ }
3176
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3177
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3178
+ }
3179
+ if (!canceled1 || !canceled2) {
3180
+ resolveCancelPromise(undefined);
3181
+ }
3182
+ },
3183
+ _errorSteps: () => {
3184
+ reading = false;
3185
+ }
3186
+ };
3187
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3188
+ }
3189
+ function pullWithBYOBReader(view, forBranch2) {
3190
+ if (IsReadableStreamDefaultReader(reader)) {
3191
+ ReadableStreamReaderGenericRelease(reader);
3192
+ reader = AcquireReadableStreamBYOBReader(stream);
3193
+ forwardReaderError(reader);
3194
+ }
3195
+ const byobBranch = forBranch2 ? branch2 : branch1;
3196
+ const otherBranch = forBranch2 ? branch1 : branch2;
3197
+ const readIntoRequest = {
3198
+ _chunkSteps: chunk => {
3199
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3200
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3201
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3202
+ queueMicrotask(() => {
3203
+ reading = false;
3204
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3205
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3206
+ if (!otherCanceled) {
3207
+ let clonedChunk;
3208
+ try {
3209
+ clonedChunk = CloneAsUint8Array(chunk);
3210
+ }
3211
+ catch (cloneE) {
3212
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3213
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3214
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3215
+ return;
3216
+ }
3217
+ if (!byobCanceled) {
3218
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3219
+ }
3220
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3221
+ }
3222
+ else if (!byobCanceled) {
3223
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3224
+ }
3225
+ });
3226
+ },
3227
+ _closeSteps: chunk => {
3228
+ reading = false;
3229
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3230
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3231
+ if (!byobCanceled) {
3232
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3233
+ }
3234
+ if (!otherCanceled) {
3235
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3236
+ }
3237
+ if (chunk !== undefined) {
3238
+ if (!byobCanceled) {
3239
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3240
+ }
3241
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3242
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3243
+ }
3244
+ }
3245
+ if (!byobCanceled || !otherCanceled) {
3246
+ resolveCancelPromise(undefined);
3247
+ }
3248
+ },
3249
+ _errorSteps: () => {
3250
+ reading = false;
3251
+ }
3252
+ };
3253
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3254
+ }
3255
+ function pull1Algorithm() {
3256
+ if (reading) {
3257
+ return promiseResolvedWith(undefined);
3258
+ }
3259
+ reading = true;
3260
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3261
+ if (byobRequest === null) {
3262
+ pullWithDefaultReader();
3263
+ }
3264
+ else {
3265
+ pullWithBYOBReader(byobRequest._view, false);
3266
+ }
3267
+ return promiseResolvedWith(undefined);
3268
+ }
3269
+ function pull2Algorithm() {
3270
+ if (reading) {
3271
+ return promiseResolvedWith(undefined);
3272
+ }
3273
+ reading = true;
3274
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3275
+ if (byobRequest === null) {
3276
+ pullWithDefaultReader();
3277
+ }
3278
+ else {
3279
+ pullWithBYOBReader(byobRequest._view, true);
3280
+ }
3281
+ return promiseResolvedWith(undefined);
3282
+ }
3283
+ function cancel1Algorithm(reason) {
3284
+ canceled1 = true;
3285
+ reason1 = reason;
3286
+ if (canceled2) {
3287
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3288
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3289
+ resolveCancelPromise(cancelResult);
3290
+ }
3291
+ return cancelPromise;
3292
+ }
3293
+ function cancel2Algorithm(reason) {
3294
+ canceled2 = true;
3295
+ reason2 = reason;
3296
+ if (canceled1) {
3297
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3298
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3299
+ resolveCancelPromise(cancelResult);
3300
+ }
3301
+ return cancelPromise;
3302
+ }
3303
+ function startAlgorithm() {
3304
+ return;
3305
+ }
3306
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3307
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3308
+ forwardReaderError(reader);
3309
+ return [branch1, branch2];
3310
+ }
2803
3311
 
2804
3312
  function convertUnderlyingDefaultOrByteSource(source, context) {
2805
3313
  assertDictionary(source, context);
@@ -2899,6 +3407,11 @@ function convertReadableWritablePair(pair, context) {
2899
3407
  return { readable, writable };
2900
3408
  }
2901
3409
 
3410
+ /**
3411
+ * A readable stream represents a source of data, from which you can read.
3412
+ *
3413
+ * @public
3414
+ */
2902
3415
  class ReadableStream {
2903
3416
  constructor(rawUnderlyingSource = {}, rawStrategy = {}) {
2904
3417
  if (rawUnderlyingSource === undefined) {
@@ -2923,12 +3436,21 @@ class ReadableStream {
2923
3436
  SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, highWaterMark, sizeAlgorithm);
2924
3437
  }
2925
3438
  }
3439
+ /**
3440
+ * Whether or not the readable stream is locked to a {@link ReadableStreamDefaultReader | reader}.
3441
+ */
2926
3442
  get locked() {
2927
3443
  if (!IsReadableStream(this)) {
2928
3444
  throw streamBrandCheckException$1('locked');
2929
3445
  }
2930
3446
  return IsReadableStreamLocked(this);
2931
3447
  }
3448
+ /**
3449
+ * Cancels the stream, signaling a loss of interest in the stream by a consumer.
3450
+ *
3451
+ * The supplied `reason` argument will be given to the underlying source's {@link UnderlyingSource.cancel | cancel()}
3452
+ * method, which might or might not use it.
3453
+ */
2932
3454
  cancel(reason = undefined) {
2933
3455
  if (!IsReadableStream(this)) {
2934
3456
  return promiseRejectedWith(streamBrandCheckException$1('cancel'));
@@ -2990,6 +3512,17 @@ class ReadableStream {
2990
3512
  }
2991
3513
  return ReadableStreamPipeTo(this, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
2992
3514
  }
3515
+ /**
3516
+ * Tees this readable stream, returning a two-element array containing the two resulting branches as
3517
+ * new {@link ReadableStream} instances.
3518
+ *
3519
+ * Teeing a stream will lock it, preventing any other consumer from acquiring a reader.
3520
+ * To cancel the stream, cancel both of the resulting branches; a composite cancellation reason will then be
3521
+ * propagated to the stream's underlying source.
3522
+ *
3523
+ * Note that the chunks seen in each branch will be the same object. If the chunks are not immutable,
3524
+ * this could allow interference between the two branches.
3525
+ */
2993
3526
  tee() {
2994
3527
  if (!IsReadableStream(this)) {
2995
3528
  throw streamBrandCheckException$1('tee');
@@ -3036,6 +3569,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3036
3569
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3037
3570
  return stream;
3038
3571
  }
3572
+ // Throws if and only if startAlgorithm throws.
3573
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3574
+ const stream = Object.create(ReadableStream.prototype);
3575
+ InitializeReadableStream(stream);
3576
+ const controller = Object.create(ReadableByteStreamController.prototype);
3577
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3578
+ return stream;
3579
+ }
3039
3580
  function InitializeReadableStream(stream) {
3040
3581
  stream._state = 'readable';
3041
3582
  stream._reader = undefined;
@@ -3049,7 +3590,7 @@ function IsReadableStream(x) {
3049
3590
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3050
3591
  return false;
3051
3592
  }
3052
- return true;
3593
+ return x instanceof ReadableStream;
3053
3594
  }
3054
3595
  function IsReadableStreamLocked(stream) {
3055
3596
  if (stream._reader === undefined) {
@@ -3067,6 +3608,13 @@ function ReadableStreamCancel(stream, reason) {
3067
3608
  return promiseRejectedWith(stream._storedError);
3068
3609
  }
3069
3610
  ReadableStreamClose(stream);
3611
+ const reader = stream._reader;
3612
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3613
+ reader._readIntoRequests.forEach(readIntoRequest => {
3614
+ readIntoRequest._closeSteps(undefined);
3615
+ });
3616
+ reader._readIntoRequests = new SimpleQueue();
3617
+ }
3070
3618
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3071
3619
  return transformPromiseWith(sourceCancelPromise, noop);
3072
3620
  }
@@ -3076,13 +3624,13 @@ function ReadableStreamClose(stream) {
3076
3624
  if (reader === undefined) {
3077
3625
  return;
3078
3626
  }
3627
+ defaultReaderClosedPromiseResolve(reader);
3079
3628
  if (IsReadableStreamDefaultReader(reader)) {
3080
3629
  reader._readRequests.forEach(readRequest => {
3081
3630
  readRequest._closeSteps();
3082
3631
  });
3083
3632
  reader._readRequests = new SimpleQueue();
3084
3633
  }
3085
- defaultReaderClosedPromiseResolve(reader);
3086
3634
  }
3087
3635
  function ReadableStreamError(stream, e) {
3088
3636
  stream._state = 'errored';
@@ -3091,6 +3639,7 @@ function ReadableStreamError(stream, e) {
3091
3639
  if (reader === undefined) {
3092
3640
  return;
3093
3641
  }
3642
+ defaultReaderClosedPromiseReject(reader, e);
3094
3643
  if (IsReadableStreamDefaultReader(reader)) {
3095
3644
  reader._readRequests.forEach(readRequest => {
3096
3645
  readRequest._errorSteps(e);
@@ -3103,7 +3652,6 @@ function ReadableStreamError(stream, e) {
3103
3652
  });
3104
3653
  reader._readIntoRequests = new SimpleQueue();
3105
3654
  }
3106
- defaultReaderClosedPromiseReject(reader, e);
3107
3655
  }
3108
3656
  // Helper functions for the ReadableStream.
3109
3657
  function streamBrandCheckException$1(name) {
@@ -3119,21 +3667,37 @@ function convertQueuingStrategyInit(init, context) {
3119
3667
  };
3120
3668
  }
3121
3669
 
3122
- const byteLengthSizeFunction = function size(chunk) {
3670
+ // The size function must not have a prototype property nor be a constructor
3671
+ const byteLengthSizeFunction = (chunk) => {
3123
3672
  return chunk.byteLength;
3124
3673
  };
3674
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3675
+ value: 'size',
3676
+ configurable: true
3677
+ });
3678
+ /**
3679
+ * A queuing strategy that counts the number of bytes in each chunk.
3680
+ *
3681
+ * @public
3682
+ */
3125
3683
  class ByteLengthQueuingStrategy {
3126
3684
  constructor(options) {
3127
3685
  assertRequiredArgument(options, 1, 'ByteLengthQueuingStrategy');
3128
3686
  options = convertQueuingStrategyInit(options, 'First parameter');
3129
3687
  this._byteLengthQueuingStrategyHighWaterMark = options.highWaterMark;
3130
3688
  }
3689
+ /**
3690
+ * Returns the high water mark provided to the constructor.
3691
+ */
3131
3692
  get highWaterMark() {
3132
3693
  if (!IsByteLengthQueuingStrategy(this)) {
3133
3694
  throw byteLengthBrandCheckException('highWaterMark');
3134
3695
  }
3135
3696
  return this._byteLengthQueuingStrategyHighWaterMark;
3136
3697
  }
3698
+ /**
3699
+ * Measures the size of `chunk` by returning the value of its `byteLength` property.
3700
+ */
3137
3701
  get size() {
3138
3702
  if (!IsByteLengthQueuingStrategy(this)) {
3139
3703
  throw byteLengthBrandCheckException('size');
@@ -3162,24 +3726,41 @@ function IsByteLengthQueuingStrategy(x) {
3162
3726
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3163
3727
  return false;
3164
3728
  }
3165
- return true;
3729
+ return x instanceof ByteLengthQueuingStrategy;
3166
3730
  }
3167
3731
 
3168
- const countSizeFunction = function size() {
3732
+ // The size function must not have a prototype property nor be a constructor
3733
+ const countSizeFunction = () => {
3169
3734
  return 1;
3170
3735
  };
3736
+ Object.defineProperty(countSizeFunction, 'name', {
3737
+ value: 'size',
3738
+ configurable: true
3739
+ });
3740
+ /**
3741
+ * A queuing strategy that counts the number of chunks.
3742
+ *
3743
+ * @public
3744
+ */
3171
3745
  class CountQueuingStrategy {
3172
3746
  constructor(options) {
3173
3747
  assertRequiredArgument(options, 1, 'CountQueuingStrategy');
3174
3748
  options = convertQueuingStrategyInit(options, 'First parameter');
3175
3749
  this._countQueuingStrategyHighWaterMark = options.highWaterMark;
3176
3750
  }
3751
+ /**
3752
+ * Returns the high water mark provided to the constructor.
3753
+ */
3177
3754
  get highWaterMark() {
3178
3755
  if (!IsCountQueuingStrategy(this)) {
3179
3756
  throw countBrandCheckException('highWaterMark');
3180
3757
  }
3181
3758
  return this._countQueuingStrategyHighWaterMark;
3182
3759
  }
3760
+ /**
3761
+ * Measures the size of `chunk` by always returning 1.
3762
+ * This ensures that the total queue size is a count of the number of chunks in the queue.
3763
+ */
3183
3764
  get size() {
3184
3765
  if (!IsCountQueuingStrategy(this)) {
3185
3766
  throw countBrandCheckException('size');
@@ -3208,7 +3789,7 @@ function IsCountQueuingStrategy(x) {
3208
3789
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3209
3790
  return false;
3210
3791
  }
3211
- return true;
3792
+ return x instanceof CountQueuingStrategy;
3212
3793
  }
3213
3794
 
3214
3795
  function convertTransformer(original, context) {
@@ -3246,6 +3827,14 @@ function convertTransformerTransformCallback(fn, original, context) {
3246
3827
  }
3247
3828
 
3248
3829
  // Class TransformStream
3830
+ /**
3831
+ * A transform stream consists of a pair of streams: a {@link WritableStream | writable stream},
3832
+ * known as its writable side, and a {@link ReadableStream | readable stream}, known as its readable side.
3833
+ * In a manner specific to the transform stream in question, writes to the writable side result in new data being
3834
+ * made available for reading from the readable side.
3835
+ *
3836
+ * @public
3837
+ */
3249
3838
  class TransformStream {
3250
3839
  constructor(rawTransformer = {}, rawWritableStrategy = {}, rawReadableStrategy = {}) {
3251
3840
  if (rawTransformer === undefined) {
@@ -3277,15 +3866,21 @@ class TransformStream {
3277
3866
  startPromise_resolve(undefined);
3278
3867
  }
3279
3868
  }
3869
+ /**
3870
+ * The readable side of the transform stream.
3871
+ */
3280
3872
  get readable() {
3281
3873
  if (!IsTransformStream(this)) {
3282
- throw streamBrandCheckException$2('readable');
3874
+ throw streamBrandCheckException('readable');
3283
3875
  }
3284
3876
  return this._readable;
3285
3877
  }
3878
+ /**
3879
+ * The writable side of the transform stream.
3880
+ */
3286
3881
  get writable() {
3287
3882
  if (!IsTransformStream(this)) {
3288
- throw streamBrandCheckException$2('writable');
3883
+ throw streamBrandCheckException('writable');
3289
3884
  }
3290
3885
  return this._writable;
3291
3886
  }
@@ -3336,7 +3931,7 @@ function IsTransformStream(x) {
3336
3931
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3337
3932
  return false;
3338
3933
  }
3339
- return true;
3934
+ return x instanceof TransformStream;
3340
3935
  }
3341
3936
  // This is a no-op if both sides are already errored.
3342
3937
  function TransformStreamError(stream, e) {
@@ -3364,32 +3959,48 @@ function TransformStreamSetBackpressure(stream, backpressure) {
3364
3959
  stream._backpressure = backpressure;
3365
3960
  }
3366
3961
  // Class TransformStreamDefaultController
3962
+ /**
3963
+ * Allows control of the {@link ReadableStream} and {@link WritableStream} of the associated {@link TransformStream}.
3964
+ *
3965
+ * @public
3966
+ */
3367
3967
  class TransformStreamDefaultController {
3368
3968
  constructor() {
3369
3969
  throw new TypeError('Illegal constructor');
3370
3970
  }
3971
+ /**
3972
+ * Returns the desired size to fill the readable side’s internal queue. It can be negative, if the queue is over-full.
3973
+ */
3371
3974
  get desiredSize() {
3372
3975
  if (!IsTransformStreamDefaultController(this)) {
3373
- throw defaultControllerBrandCheckException$1('desiredSize');
3976
+ throw defaultControllerBrandCheckException('desiredSize');
3374
3977
  }
3375
3978
  const readableController = this._controlledTransformStream._readable._readableStreamController;
3376
3979
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
3377
3980
  }
3378
3981
  enqueue(chunk = undefined) {
3379
3982
  if (!IsTransformStreamDefaultController(this)) {
3380
- throw defaultControllerBrandCheckException$1('enqueue');
3983
+ throw defaultControllerBrandCheckException('enqueue');
3381
3984
  }
3382
3985
  TransformStreamDefaultControllerEnqueue(this, chunk);
3383
3986
  }
3987
+ /**
3988
+ * Errors both the readable side and the writable side of the controlled transform stream, making all future
3989
+ * interactions with it fail with the given error `e`. Any chunks queued for transformation will be discarded.
3990
+ */
3384
3991
  error(reason = undefined) {
3385
3992
  if (!IsTransformStreamDefaultController(this)) {
3386
- throw defaultControllerBrandCheckException$1('error');
3993
+ throw defaultControllerBrandCheckException('error');
3387
3994
  }
3388
3995
  TransformStreamDefaultControllerError(this, reason);
3389
3996
  }
3997
+ /**
3998
+ * Closes the readable side and errors the writable side of the controlled transform stream. This is useful when the
3999
+ * transformer only needs to consume a portion of the chunks written to the writable side.
4000
+ */
3390
4001
  terminate() {
3391
4002
  if (!IsTransformStreamDefaultController(this)) {
3392
- throw defaultControllerBrandCheckException$1('terminate');
4003
+ throw defaultControllerBrandCheckException('terminate');
3393
4004
  }
3394
4005
  TransformStreamDefaultControllerTerminate(this);
3395
4006
  }
@@ -3414,7 +4025,7 @@ function IsTransformStreamDefaultController(x) {
3414
4025
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3415
4026
  return false;
3416
4027
  }
3417
- return true;
4028
+ return x instanceof TransformStreamDefaultController;
3418
4029
  }
3419
4030
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3420
4031
  controller._controlledTransformStream = stream;
@@ -3531,11 +4142,11 @@ function TransformStreamDefaultSourcePullAlgorithm(stream) {
3531
4142
  return stream._backpressureChangePromise;
3532
4143
  }
3533
4144
  // Helper functions for the TransformStreamDefaultController.
3534
- function defaultControllerBrandCheckException$1(name) {
4145
+ function defaultControllerBrandCheckException(name) {
3535
4146
  return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
3536
4147
  }
3537
4148
  // Helper functions for the TransformStream.
3538
- function streamBrandCheckException$2(name) {
4149
+ function streamBrandCheckException(name) {
3539
4150
  return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
3540
4151
  }
3541
4152