web-streams-polyfill 3.0.2 → 3.2.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.2
2
+ * web-streams-polyfill v3.2.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;
@@ -488,7 +488,7 @@ function IsReadableStreamDefaultReader(x) {
488
488
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
489
489
  return false;
490
490
  }
491
- return true;
491
+ return x instanceof ReadableStreamDefaultReader;
492
492
  }
493
493
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
494
494
  const stream = reader._ownerReadableStream;
@@ -620,7 +620,14 @@ function IsReadableStreamAsyncIterator(x) {
620
620
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
621
621
  return false;
622
622
  }
623
- return true;
623
+ try {
624
+ // noinspection SuspiciousTypeOfGuard
625
+ return x._asyncIteratorImpl instanceof
626
+ ReadableStreamAsyncIteratorImpl;
627
+ }
628
+ catch (_a) {
629
+ return false;
630
+ }
624
631
  }
625
632
  // Helper functions for the ReadableStream.
626
633
  function streamAsyncIteratorBrandCheckException(name) {
@@ -634,15 +641,35 @@ const NumberIsNaN = Number.isNaN || function (x) {
634
641
  return x !== x;
635
642
  };
636
643
 
637
- function IsFiniteNonNegativeNumber(v) {
638
- if (!IsNonNegativeNumber(v)) {
639
- return false;
640
- }
641
- if (v === Infinity) {
642
- return false;
644
+ function CreateArrayFromList(elements) {
645
+ // We use arrays to represent lists, so this is basically a no-op.
646
+ // Do a slice though just in case we happen to depend on the unique-ness.
647
+ return elements.slice();
648
+ }
649
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
650
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
651
+ }
652
+ // Not implemented correctly
653
+ function TransferArrayBuffer(O) {
654
+ return O;
655
+ }
656
+ // Not implemented correctly
657
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
658
+ function IsDetachedBuffer(O) {
659
+ return false;
660
+ }
661
+ function ArrayBufferSlice(buffer, begin, end) {
662
+ // ArrayBuffer.prototype.slice is not available on IE10
663
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
664
+ if (buffer.slice) {
665
+ return buffer.slice(begin, end);
643
666
  }
644
- return true;
667
+ const length = end - begin;
668
+ const slice = new ArrayBuffer(length);
669
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
670
+ return slice;
645
671
  }
672
+
646
673
  function IsNonNegativeNumber(v) {
647
674
  if (typeof v !== 'number') {
648
675
  return false;
@@ -655,6 +682,10 @@ function IsNonNegativeNumber(v) {
655
682
  }
656
683
  return true;
657
684
  }
685
+ function CloneAsUint8Array(O) {
686
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
687
+ return new Uint8Array(buffer);
688
+ }
658
689
 
659
690
  function DequeueValue(container) {
660
691
  const pair = container._queue.shift();
@@ -665,8 +696,7 @@ function DequeueValue(container) {
665
696
  return pair.value;
666
697
  }
667
698
  function EnqueueValueWithSize(container, value, size) {
668
- size = Number(size);
669
- if (!IsFiniteNonNegativeNumber(size)) {
699
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
670
700
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
671
701
  }
672
702
  container._queue.push({ value, size });
@@ -681,23 +711,6 @@ function ResetQueue(container) {
681
711
  container._queueTotalSize = 0;
682
712
  }
683
713
 
684
- function CreateArrayFromList(elements) {
685
- // We use arrays to represent lists, so this is basically a no-op.
686
- // Do a slice though just in case we happen to depend on the unique-ness.
687
- return elements.slice();
688
- }
689
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
690
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
691
- }
692
- // Not implemented correctly
693
- function TransferArrayBuffer(O) {
694
- return O;
695
- }
696
- // Not implemented correctly
697
- function IsDetachedBuffer(O) {
698
- return false;
699
- }
700
-
701
714
  /**
702
715
  * A pull-into request in a {@link ReadableByteStreamController}.
703
716
  *
@@ -736,15 +749,10 @@ class ReadableStreamBYOBRequest {
736
749
  if (!ArrayBuffer.isView(view)) {
737
750
  throw new TypeError('You can only respond with array buffer views');
738
751
  }
739
- if (view.byteLength === 0) {
740
- throw new TypeError('chunk must have non-zero byteLength');
741
- }
742
- if (view.buffer.byteLength === 0) {
743
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
744
- }
745
752
  if (this._associatedReadableByteStreamController === undefined) {
746
753
  throw new TypeError('This BYOB request has been invalidated');
747
754
  }
755
+ if (IsDetachedBuffer(view.buffer)) ;
748
756
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
749
757
  }
750
758
  }
@@ -775,14 +783,7 @@ class ReadableByteStreamController {
775
783
  if (!IsReadableByteStreamController(this)) {
776
784
  throw byteStreamControllerBrandCheckException('byobRequest');
777
785
  }
778
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
779
- const firstDescriptor = this._pendingPullIntos.peek();
780
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
781
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
782
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
783
- this._byobRequest = byobRequest;
784
- }
785
- return this._byobRequest;
786
+ return ReadableByteStreamControllerGetBYOBRequest(this);
786
787
  }
787
788
  /**
788
789
  * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
@@ -845,10 +846,7 @@ class ReadableByteStreamController {
845
846
  }
846
847
  /** @internal */
847
848
  [CancelSteps](reason) {
848
- if (this._pendingPullIntos.length > 0) {
849
- const firstDescriptor = this._pendingPullIntos.peek();
850
- firstDescriptor.bytesFilled = 0;
851
- }
849
+ ReadableByteStreamControllerClearPendingPullIntos(this);
852
850
  ResetQueue(this);
853
851
  const result = this._cancelAlgorithm(reason);
854
852
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -877,6 +875,7 @@ class ReadableByteStreamController {
877
875
  }
878
876
  const pullIntoDescriptor = {
879
877
  buffer,
878
+ bufferByteLength: autoAllocateChunkSize,
880
879
  byteOffset: 0,
881
880
  byteLength: autoAllocateChunkSize,
882
881
  bytesFilled: 0,
@@ -911,7 +910,7 @@ function IsReadableByteStreamController(x) {
911
910
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
912
911
  return false;
913
912
  }
914
- return true;
913
+ return x instanceof ReadableByteStreamController;
915
914
  }
916
915
  function IsReadableStreamBYOBRequest(x) {
917
916
  if (!typeIsObject(x)) {
@@ -920,7 +919,7 @@ function IsReadableStreamBYOBRequest(x) {
920
919
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
921
920
  return false;
922
921
  }
923
- return true;
922
+ return x instanceof ReadableStreamBYOBRequest;
924
923
  }
925
924
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
926
925
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1002,7 +1001,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
1002
1001
  return ready;
1003
1002
  }
1004
1003
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1005
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1006
1004
  pullIntoDescriptor.bytesFilled += size;
1007
1005
  }
1008
1006
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1041,9 +1039,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1041
1039
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1042
1040
  }
1043
1041
  const ctor = view.constructor;
1042
+ // try {
1044
1043
  const buffer = TransferArrayBuffer(view.buffer);
1044
+ // } catch (e) {
1045
+ // readIntoRequest._errorSteps(e);
1046
+ // return;
1047
+ // }
1045
1048
  const pullIntoDescriptor = {
1046
1049
  buffer,
1050
+ bufferByteLength: buffer.byteLength,
1047
1051
  byteOffset: view.byteOffset,
1048
1052
  byteLength: view.byteLength,
1049
1053
  bytesFilled: 0,
@@ -1083,7 +1087,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1083
1087
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1084
1088
  }
1085
1089
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1086
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1087
1090
  const stream = controller._controlledReadableByteStream;
1088
1091
  if (ReadableStreamHasBYOBReader(stream)) {
1089
1092
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1093,34 +1096,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1093
1096
  }
1094
1097
  }
1095
1098
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1096
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1097
- throw new RangeError('bytesWritten out of range');
1098
- }
1099
1099
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1100
1100
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1101
- // TODO: Figure out whether we should detach the buffer or not here.
1102
1101
  return;
1103
1102
  }
1104
1103
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1105
1104
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1106
1105
  if (remainderSize > 0) {
1107
1106
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1108
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1107
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1109
1108
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1110
1109
  }
1111
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1112
1110
  pullIntoDescriptor.bytesFilled -= remainderSize;
1113
1111
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1114
1112
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1115
1113
  }
1116
1114
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1117
1115
  const firstDescriptor = controller._pendingPullIntos.peek();
1116
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1118
1117
  const state = controller._controlledReadableByteStream._state;
1119
1118
  if (state === 'closed') {
1120
- if (bytesWritten !== 0) {
1121
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1122
- }
1123
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1119
+ ReadableByteStreamControllerRespondInClosedState(controller);
1124
1120
  }
1125
1121
  else {
1126
1122
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1129,7 +1125,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1129
1125
  }
1130
1126
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1131
1127
  const descriptor = controller._pendingPullIntos.shift();
1132
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1133
1128
  return descriptor;
1134
1129
  }
1135
1130
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1189,11 +1184,20 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1189
1184
  const byteOffset = chunk.byteOffset;
1190
1185
  const byteLength = chunk.byteLength;
1191
1186
  const transferredBuffer = TransferArrayBuffer(buffer);
1187
+ if (controller._pendingPullIntos.length > 0) {
1188
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1189
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1190
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1191
+ }
1192
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1192
1193
  if (ReadableStreamHasDefaultReader(stream)) {
1193
1194
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1194
1195
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
1195
1196
  }
1196
1197
  else {
1198
+ if (controller._pendingPullIntos.length > 0) {
1199
+ ReadableByteStreamControllerShiftPendingPullInto(controller);
1200
+ }
1197
1201
  const transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
1198
1202
  ReadableStreamFulfillReadRequest(stream, transferredView, false);
1199
1203
  }
@@ -1218,6 +1222,16 @@ function ReadableByteStreamControllerError(controller, e) {
1218
1222
  ReadableByteStreamControllerClearAlgorithms(controller);
1219
1223
  ReadableStreamError(stream, e);
1220
1224
  }
1225
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1226
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1227
+ const firstDescriptor = controller._pendingPullIntos.peek();
1228
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1229
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1230
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1231
+ controller._byobRequest = byobRequest;
1232
+ }
1233
+ return controller._byobRequest;
1234
+ }
1221
1235
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1222
1236
  const state = controller._controlledReadableByteStream._state;
1223
1237
  if (state === 'errored') {
@@ -1229,22 +1243,49 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1229
1243
  return controller._strategyHWM - controller._queueTotalSize;
1230
1244
  }
1231
1245
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1232
- bytesWritten = Number(bytesWritten);
1233
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1234
- throw new RangeError('bytesWritten must be a finite');
1246
+ const firstDescriptor = controller._pendingPullIntos.peek();
1247
+ const state = controller._controlledReadableByteStream._state;
1248
+ if (state === 'closed') {
1249
+ if (bytesWritten !== 0) {
1250
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1251
+ }
1252
+ }
1253
+ else {
1254
+ if (bytesWritten === 0) {
1255
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1256
+ }
1257
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1258
+ throw new RangeError('bytesWritten out of range');
1259
+ }
1235
1260
  }
1261
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1236
1262
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1237
1263
  }
1238
1264
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1239
1265
  const firstDescriptor = controller._pendingPullIntos.peek();
1266
+ const state = controller._controlledReadableByteStream._state;
1267
+ if (state === 'closed') {
1268
+ if (view.byteLength !== 0) {
1269
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1270
+ }
1271
+ }
1272
+ else {
1273
+ if (view.byteLength === 0) {
1274
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1275
+ }
1276
+ }
1240
1277
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1241
1278
  throw new RangeError('The region specified by view does not match byobRequest');
1242
1279
  }
1243
- if (firstDescriptor.byteLength !== view.byteLength) {
1280
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1244
1281
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1245
1282
  }
1246
- firstDescriptor.buffer = view.buffer;
1247
- ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1283
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1284
+ throw new RangeError('The region specified by view is larger than byobRequest');
1285
+ }
1286
+ const viewByteLength = view.byteLength;
1287
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1288
+ ReadableByteStreamControllerRespondInternal(controller, viewByteLength);
1248
1289
  }
1249
1290
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
1250
1291
  controller._controlledReadableByteStream = stream;
@@ -1393,6 +1434,7 @@ class ReadableStreamBYOBReader {
1393
1434
  if (view.buffer.byteLength === 0) {
1394
1435
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1395
1436
  }
1437
+ if (IsDetachedBuffer(view.buffer)) ;
1396
1438
  if (this._ownerReadableStream === undefined) {
1397
1439
  return promiseRejectedWith(readerLockException('read from'));
1398
1440
  }
@@ -1452,7 +1494,7 @@ function IsReadableStreamBYOBReader(x) {
1452
1494
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1453
1495
  return false;
1454
1496
  }
1455
- return true;
1497
+ return x instanceof ReadableStreamBYOBReader;
1456
1498
  }
1457
1499
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1458
1500
  const stream = reader._ownerReadableStream;
@@ -1547,6 +1589,31 @@ function assertWritableStream(x, context) {
1547
1589
  }
1548
1590
  }
1549
1591
 
1592
+ function isAbortSignal(value) {
1593
+ if (typeof value !== 'object' || value === null) {
1594
+ return false;
1595
+ }
1596
+ try {
1597
+ return typeof value.aborted === 'boolean';
1598
+ }
1599
+ catch (_a) {
1600
+ // AbortSignal.prototype.aborted throws if its brand check fails
1601
+ return false;
1602
+ }
1603
+ }
1604
+ const supportsAbortController = typeof AbortController === 'function';
1605
+ /**
1606
+ * Construct a new AbortController, if supported by the platform.
1607
+ *
1608
+ * @internal
1609
+ */
1610
+ function createAbortController() {
1611
+ if (supportsAbortController) {
1612
+ return new AbortController();
1613
+ }
1614
+ return undefined;
1615
+ }
1616
+
1550
1617
  /**
1551
1618
  * A writable stream represents a destination for data, into which you can write.
1552
1619
  *
@@ -1576,7 +1643,7 @@ class WritableStream {
1576
1643
  */
1577
1644
  get locked() {
1578
1645
  if (!IsWritableStream(this)) {
1579
- throw streamBrandCheckException('locked');
1646
+ throw streamBrandCheckException$2('locked');
1580
1647
  }
1581
1648
  return IsWritableStreamLocked(this);
1582
1649
  }
@@ -1591,7 +1658,7 @@ class WritableStream {
1591
1658
  */
1592
1659
  abort(reason = undefined) {
1593
1660
  if (!IsWritableStream(this)) {
1594
- return promiseRejectedWith(streamBrandCheckException('abort'));
1661
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1595
1662
  }
1596
1663
  if (IsWritableStreamLocked(this)) {
1597
1664
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
@@ -1608,7 +1675,7 @@ class WritableStream {
1608
1675
  */
1609
1676
  close() {
1610
1677
  if (!IsWritableStream(this)) {
1611
- return promiseRejectedWith(streamBrandCheckException('close'));
1678
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1612
1679
  }
1613
1680
  if (IsWritableStreamLocked(this)) {
1614
1681
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1628,7 +1695,7 @@ class WritableStream {
1628
1695
  */
1629
1696
  getWriter() {
1630
1697
  if (!IsWritableStream(this)) {
1631
- throw streamBrandCheckException('getWriter');
1698
+ throw streamBrandCheckException$2('getWriter');
1632
1699
  }
1633
1700
  return AcquireWritableStreamDefaultWriter(this);
1634
1701
  }
@@ -1690,7 +1757,7 @@ function IsWritableStream(x) {
1690
1757
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1691
1758
  return false;
1692
1759
  }
1693
- return true;
1760
+ return x instanceof WritableStream;
1694
1761
  }
1695
1762
  function IsWritableStreamLocked(stream) {
1696
1763
  if (stream._writer === undefined) {
@@ -1699,6 +1766,15 @@ function IsWritableStreamLocked(stream) {
1699
1766
  return true;
1700
1767
  }
1701
1768
  function WritableStreamAbort(stream, reason) {
1769
+ var _a;
1770
+ if (stream._state === 'closed' || stream._state === 'errored') {
1771
+ return promiseResolvedWith(undefined);
1772
+ }
1773
+ stream._writableStreamController._abortReason = reason;
1774
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1775
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1776
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1777
+ // Widen the type again by casting to WritableStreamState.
1702
1778
  const state = stream._state;
1703
1779
  if (state === 'closed' || state === 'errored') {
1704
1780
  return promiseResolvedWith(undefined);
@@ -2044,7 +2120,7 @@ function IsWritableStreamDefaultWriter(x) {
2044
2120
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2045
2121
  return false;
2046
2122
  }
2047
- return true;
2123
+ return x instanceof WritableStreamDefaultWriter;
2048
2124
  }
2049
2125
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2050
2126
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2134,6 +2210,34 @@ class WritableStreamDefaultController {
2134
2210
  constructor() {
2135
2211
  throw new TypeError('Illegal constructor');
2136
2212
  }
2213
+ /**
2214
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2215
+ *
2216
+ * @deprecated
2217
+ * This property has been removed from the specification, see https://github.com/whatwg/streams/pull/1177.
2218
+ * Use {@link WritableStreamDefaultController.signal}'s `reason` instead.
2219
+ */
2220
+ get abortReason() {
2221
+ if (!IsWritableStreamDefaultController(this)) {
2222
+ throw defaultControllerBrandCheckException$2('abortReason');
2223
+ }
2224
+ return this._abortReason;
2225
+ }
2226
+ /**
2227
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2228
+ */
2229
+ get signal() {
2230
+ if (!IsWritableStreamDefaultController(this)) {
2231
+ throw defaultControllerBrandCheckException$2('signal');
2232
+ }
2233
+ if (this._abortController === undefined) {
2234
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2235
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2236
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2237
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2238
+ }
2239
+ return this._abortController.signal;
2240
+ }
2137
2241
  /**
2138
2242
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2139
2243
  *
@@ -2143,7 +2247,7 @@ class WritableStreamDefaultController {
2143
2247
  */
2144
2248
  error(e = undefined) {
2145
2249
  if (!IsWritableStreamDefaultController(this)) {
2146
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2250
+ throw defaultControllerBrandCheckException$2('error');
2147
2251
  }
2148
2252
  const state = this._controlledWritableStream._state;
2149
2253
  if (state !== 'writable') {
@@ -2165,6 +2269,8 @@ class WritableStreamDefaultController {
2165
2269
  }
2166
2270
  }
2167
2271
  Object.defineProperties(WritableStreamDefaultController.prototype, {
2272
+ abortReason: { enumerable: true },
2273
+ signal: { enumerable: true },
2168
2274
  error: { enumerable: true }
2169
2275
  });
2170
2276
  if (typeof SymbolPolyfill.toStringTag === 'symbol') {
@@ -2181,7 +2287,7 @@ function IsWritableStreamDefaultController(x) {
2181
2287
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2182
2288
  return false;
2183
2289
  }
2184
- return true;
2290
+ return x instanceof WritableStreamDefaultController;
2185
2291
  }
2186
2292
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2187
2293
  controller._controlledWritableStream = stream;
@@ -2190,6 +2296,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2190
2296
  controller._queue = undefined;
2191
2297
  controller._queueTotalSize = undefined;
2192
2298
  ResetQueue(controller);
2299
+ controller._abortReason = undefined;
2300
+ controller._abortController = createAbortController();
2193
2301
  controller._started = false;
2194
2302
  controller._strategySizeAlgorithm = sizeAlgorithm;
2195
2303
  controller._strategyHWM = highWaterMark;
@@ -2339,9 +2447,13 @@ function WritableStreamDefaultControllerError(controller, error) {
2339
2447
  WritableStreamStartErroring(stream, error);
2340
2448
  }
2341
2449
  // Helper functions for the WritableStream.
2342
- function streamBrandCheckException(name) {
2450
+ function streamBrandCheckException$2(name) {
2343
2451
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2344
2452
  }
2453
+ // Helper functions for the WritableStreamDefaultController.
2454
+ function defaultControllerBrandCheckException$2(name) {
2455
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2456
+ }
2345
2457
  // Helper functions for the WritableStreamDefaultWriter.
2346
2458
  function defaultWriterBrandCheckException(name) {
2347
2459
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2427,19 +2539,6 @@ function defaultWriterReadyPromiseResolve(writer) {
2427
2539
  writer._readyPromiseState = 'fulfilled';
2428
2540
  }
2429
2541
 
2430
- function isAbortSignal(value) {
2431
- if (typeof value !== 'object' || value === null) {
2432
- return false;
2433
- }
2434
- try {
2435
- return typeof value.aborted === 'boolean';
2436
- }
2437
- catch (_a) {
2438
- // AbortSignal.prototype.aborted throws if its brand check fails
2439
- return false;
2440
- }
2441
- }
2442
-
2443
2542
  /// <reference lib="dom" />
2444
2543
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2445
2544
 
@@ -2457,6 +2556,7 @@ function isDOMExceptionConstructor(ctor) {
2457
2556
  }
2458
2557
  }
2459
2558
  function createDOMExceptionPolyfill() {
2559
+ // eslint-disable-next-line no-shadow
2460
2560
  const ctor = function DOMException(message, name) {
2461
2561
  this.message = message || '';
2462
2562
  this.name = name || 'Error';
@@ -2468,6 +2568,7 @@ function createDOMExceptionPolyfill() {
2468
2568
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2469
2569
  return ctor;
2470
2570
  }
2571
+ // eslint-disable-next-line no-redeclare
2471
2572
  const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2472
2573
 
2473
2574
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2660,7 +2761,7 @@ class ReadableStreamDefaultController {
2660
2761
  */
2661
2762
  get desiredSize() {
2662
2763
  if (!IsReadableStreamDefaultController(this)) {
2663
- throw defaultControllerBrandCheckException('desiredSize');
2764
+ throw defaultControllerBrandCheckException$1('desiredSize');
2664
2765
  }
2665
2766
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2666
2767
  }
@@ -2670,7 +2771,7 @@ class ReadableStreamDefaultController {
2670
2771
  */
2671
2772
  close() {
2672
2773
  if (!IsReadableStreamDefaultController(this)) {
2673
- throw defaultControllerBrandCheckException('close');
2774
+ throw defaultControllerBrandCheckException$1('close');
2674
2775
  }
2675
2776
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2676
2777
  throw new TypeError('The stream is not in a state that permits close');
@@ -2679,7 +2780,7 @@ class ReadableStreamDefaultController {
2679
2780
  }
2680
2781
  enqueue(chunk = undefined) {
2681
2782
  if (!IsReadableStreamDefaultController(this)) {
2682
- throw defaultControllerBrandCheckException('enqueue');
2783
+ throw defaultControllerBrandCheckException$1('enqueue');
2683
2784
  }
2684
2785
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2685
2786
  throw new TypeError('The stream is not in a state that permits enqueue');
@@ -2691,7 +2792,7 @@ class ReadableStreamDefaultController {
2691
2792
  */
2692
2793
  error(e = undefined) {
2693
2794
  if (!IsReadableStreamDefaultController(this)) {
2694
- throw defaultControllerBrandCheckException('error');
2795
+ throw defaultControllerBrandCheckException$1('error');
2695
2796
  }
2696
2797
  ReadableStreamDefaultControllerError(this, e);
2697
2798
  }
@@ -2742,7 +2843,7 @@ function IsReadableStreamDefaultController(x) {
2742
2843
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2743
2844
  return false;
2744
2845
  }
2745
- return true;
2846
+ return x instanceof ReadableStreamDefaultController;
2746
2847
  }
2747
2848
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2748
2849
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2898,13 +2999,20 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underl
2898
2999
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2899
3000
  }
2900
3001
  // Helper functions for the ReadableStreamDefaultController.
2901
- function defaultControllerBrandCheckException(name) {
3002
+ function defaultControllerBrandCheckException$1(name) {
2902
3003
  return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
2903
3004
  }
2904
3005
 
2905
3006
  function ReadableStreamTee(stream, cloneForBranch2) {
3007
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3008
+ return ReadableByteStreamTee(stream);
3009
+ }
3010
+ return ReadableStreamDefaultTee(stream);
3011
+ }
3012
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2906
3013
  const reader = AcquireReadableStreamDefaultReader(stream);
2907
3014
  let reading = false;
3015
+ let readAgain = false;
2908
3016
  let canceled1 = false;
2909
3017
  let canceled2 = false;
2910
3018
  let reason1;
@@ -2917,30 +3025,34 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2917
3025
  });
2918
3026
  function pullAlgorithm() {
2919
3027
  if (reading) {
3028
+ readAgain = true;
2920
3029
  return promiseResolvedWith(undefined);
2921
3030
  }
2922
3031
  reading = true;
2923
3032
  const readRequest = {
2924
- _chunkSteps: value => {
3033
+ _chunkSteps: chunk => {
2925
3034
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2926
3035
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2927
3036
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2928
3037
  queueMicrotask(() => {
2929
- reading = false;
2930
- const value1 = value;
2931
- const value2 = value;
3038
+ readAgain = false;
3039
+ const chunk1 = chunk;
3040
+ const chunk2 = chunk;
2932
3041
  // There is no way to access the cloning code right now in the reference implementation.
2933
3042
  // If we add one then we'll need an implementation for serializable objects.
2934
3043
  // if (!canceled2 && cloneForBranch2) {
2935
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3044
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2936
3045
  // }
2937
3046
  if (!canceled1) {
2938
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3047
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2939
3048
  }
2940
3049
  if (!canceled2) {
2941
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3050
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3051
+ }
3052
+ reading = false;
3053
+ if (readAgain) {
3054
+ pullAlgorithm();
2942
3055
  }
2943
- resolveCancelPromise(undefined);
2944
3056
  });
2945
3057
  },
2946
3058
  _closeSteps: () => {
@@ -2951,6 +3063,9 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2951
3063
  if (!canceled2) {
2952
3064
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2953
3065
  }
3066
+ if (!canceled1 || !canceled2) {
3067
+ resolveCancelPromise(undefined);
3068
+ }
2954
3069
  },
2955
3070
  _errorSteps: () => {
2956
3071
  reading = false;
@@ -2987,8 +3102,235 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2987
3102
  uponRejection(reader._closedPromise, (r) => {
2988
3103
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
2989
3104
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
2990
- resolveCancelPromise(undefined);
3105
+ if (!canceled1 || !canceled2) {
3106
+ resolveCancelPromise(undefined);
3107
+ }
3108
+ });
3109
+ return [branch1, branch2];
3110
+ }
3111
+ function ReadableByteStreamTee(stream) {
3112
+ let reader = AcquireReadableStreamDefaultReader(stream);
3113
+ let reading = false;
3114
+ let readAgainForBranch1 = false;
3115
+ let readAgainForBranch2 = false;
3116
+ let canceled1 = false;
3117
+ let canceled2 = false;
3118
+ let reason1;
3119
+ let reason2;
3120
+ let branch1;
3121
+ let branch2;
3122
+ let resolveCancelPromise;
3123
+ const cancelPromise = newPromise(resolve => {
3124
+ resolveCancelPromise = resolve;
2991
3125
  });
3126
+ function forwardReaderError(thisReader) {
3127
+ uponRejection(thisReader._closedPromise, r => {
3128
+ if (thisReader !== reader) {
3129
+ return;
3130
+ }
3131
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3132
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3133
+ if (!canceled1 || !canceled2) {
3134
+ resolveCancelPromise(undefined);
3135
+ }
3136
+ });
3137
+ }
3138
+ function pullWithDefaultReader() {
3139
+ if (IsReadableStreamBYOBReader(reader)) {
3140
+ ReadableStreamReaderGenericRelease(reader);
3141
+ reader = AcquireReadableStreamDefaultReader(stream);
3142
+ forwardReaderError(reader);
3143
+ }
3144
+ const readRequest = {
3145
+ _chunkSteps: chunk => {
3146
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3147
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3148
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3149
+ queueMicrotask(() => {
3150
+ readAgainForBranch1 = false;
3151
+ readAgainForBranch2 = false;
3152
+ const chunk1 = chunk;
3153
+ let chunk2 = chunk;
3154
+ if (!canceled1 && !canceled2) {
3155
+ try {
3156
+ chunk2 = CloneAsUint8Array(chunk);
3157
+ }
3158
+ catch (cloneE) {
3159
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3160
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3161
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3162
+ return;
3163
+ }
3164
+ }
3165
+ if (!canceled1) {
3166
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3167
+ }
3168
+ if (!canceled2) {
3169
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3170
+ }
3171
+ reading = false;
3172
+ if (readAgainForBranch1) {
3173
+ pull1Algorithm();
3174
+ }
3175
+ else if (readAgainForBranch2) {
3176
+ pull2Algorithm();
3177
+ }
3178
+ });
3179
+ },
3180
+ _closeSteps: () => {
3181
+ reading = false;
3182
+ if (!canceled1) {
3183
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3184
+ }
3185
+ if (!canceled2) {
3186
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3187
+ }
3188
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3189
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3190
+ }
3191
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3192
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3193
+ }
3194
+ if (!canceled1 || !canceled2) {
3195
+ resolveCancelPromise(undefined);
3196
+ }
3197
+ },
3198
+ _errorSteps: () => {
3199
+ reading = false;
3200
+ }
3201
+ };
3202
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3203
+ }
3204
+ function pullWithBYOBReader(view, forBranch2) {
3205
+ if (IsReadableStreamDefaultReader(reader)) {
3206
+ ReadableStreamReaderGenericRelease(reader);
3207
+ reader = AcquireReadableStreamBYOBReader(stream);
3208
+ forwardReaderError(reader);
3209
+ }
3210
+ const byobBranch = forBranch2 ? branch2 : branch1;
3211
+ const otherBranch = forBranch2 ? branch1 : branch2;
3212
+ const readIntoRequest = {
3213
+ _chunkSteps: chunk => {
3214
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3215
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3216
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3217
+ queueMicrotask(() => {
3218
+ readAgainForBranch1 = false;
3219
+ readAgainForBranch2 = false;
3220
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3221
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3222
+ if (!otherCanceled) {
3223
+ let clonedChunk;
3224
+ try {
3225
+ clonedChunk = CloneAsUint8Array(chunk);
3226
+ }
3227
+ catch (cloneE) {
3228
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3229
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3230
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3231
+ return;
3232
+ }
3233
+ if (!byobCanceled) {
3234
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3235
+ }
3236
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3237
+ }
3238
+ else if (!byobCanceled) {
3239
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3240
+ }
3241
+ reading = false;
3242
+ if (readAgainForBranch1) {
3243
+ pull1Algorithm();
3244
+ }
3245
+ else if (readAgainForBranch2) {
3246
+ pull2Algorithm();
3247
+ }
3248
+ });
3249
+ },
3250
+ _closeSteps: chunk => {
3251
+ reading = false;
3252
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3253
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3254
+ if (!byobCanceled) {
3255
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3256
+ }
3257
+ if (!otherCanceled) {
3258
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3259
+ }
3260
+ if (chunk !== undefined) {
3261
+ if (!byobCanceled) {
3262
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3263
+ }
3264
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3265
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3266
+ }
3267
+ }
3268
+ if (!byobCanceled || !otherCanceled) {
3269
+ resolveCancelPromise(undefined);
3270
+ }
3271
+ },
3272
+ _errorSteps: () => {
3273
+ reading = false;
3274
+ }
3275
+ };
3276
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3277
+ }
3278
+ function pull1Algorithm() {
3279
+ if (reading) {
3280
+ readAgainForBranch1 = true;
3281
+ return promiseResolvedWith(undefined);
3282
+ }
3283
+ reading = true;
3284
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3285
+ if (byobRequest === null) {
3286
+ pullWithDefaultReader();
3287
+ }
3288
+ else {
3289
+ pullWithBYOBReader(byobRequest._view, false);
3290
+ }
3291
+ return promiseResolvedWith(undefined);
3292
+ }
3293
+ function pull2Algorithm() {
3294
+ if (reading) {
3295
+ readAgainForBranch2 = true;
3296
+ return promiseResolvedWith(undefined);
3297
+ }
3298
+ reading = true;
3299
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3300
+ if (byobRequest === null) {
3301
+ pullWithDefaultReader();
3302
+ }
3303
+ else {
3304
+ pullWithBYOBReader(byobRequest._view, true);
3305
+ }
3306
+ return promiseResolvedWith(undefined);
3307
+ }
3308
+ function cancel1Algorithm(reason) {
3309
+ canceled1 = true;
3310
+ reason1 = reason;
3311
+ if (canceled2) {
3312
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3313
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3314
+ resolveCancelPromise(cancelResult);
3315
+ }
3316
+ return cancelPromise;
3317
+ }
3318
+ function cancel2Algorithm(reason) {
3319
+ canceled2 = true;
3320
+ reason2 = reason;
3321
+ if (canceled1) {
3322
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3323
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3324
+ resolveCancelPromise(cancelResult);
3325
+ }
3326
+ return cancelPromise;
3327
+ }
3328
+ function startAlgorithm() {
3329
+ return;
3330
+ }
3331
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3332
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3333
+ forwardReaderError(reader);
2992
3334
  return [branch1, branch2];
2993
3335
  }
2994
3336
 
@@ -3252,6 +3594,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3252
3594
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3253
3595
  return stream;
3254
3596
  }
3597
+ // Throws if and only if startAlgorithm throws.
3598
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3599
+ const stream = Object.create(ReadableStream.prototype);
3600
+ InitializeReadableStream(stream);
3601
+ const controller = Object.create(ReadableByteStreamController.prototype);
3602
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3603
+ return stream;
3604
+ }
3255
3605
  function InitializeReadableStream(stream) {
3256
3606
  stream._state = 'readable';
3257
3607
  stream._reader = undefined;
@@ -3265,7 +3615,7 @@ function IsReadableStream(x) {
3265
3615
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3266
3616
  return false;
3267
3617
  }
3268
- return true;
3618
+ return x instanceof ReadableStream;
3269
3619
  }
3270
3620
  function IsReadableStreamLocked(stream) {
3271
3621
  if (stream._reader === undefined) {
@@ -3283,6 +3633,13 @@ function ReadableStreamCancel(stream, reason) {
3283
3633
  return promiseRejectedWith(stream._storedError);
3284
3634
  }
3285
3635
  ReadableStreamClose(stream);
3636
+ const reader = stream._reader;
3637
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3638
+ reader._readIntoRequests.forEach(readIntoRequest => {
3639
+ readIntoRequest._closeSteps(undefined);
3640
+ });
3641
+ reader._readIntoRequests = new SimpleQueue();
3642
+ }
3286
3643
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3287
3644
  return transformPromiseWith(sourceCancelPromise, noop);
3288
3645
  }
@@ -3335,9 +3692,14 @@ function convertQueuingStrategyInit(init, context) {
3335
3692
  };
3336
3693
  }
3337
3694
 
3338
- const byteLengthSizeFunction = function size(chunk) {
3695
+ // The size function must not have a prototype property nor be a constructor
3696
+ const byteLengthSizeFunction = (chunk) => {
3339
3697
  return chunk.byteLength;
3340
3698
  };
3699
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3700
+ value: 'size',
3701
+ configurable: true
3702
+ });
3341
3703
  /**
3342
3704
  * A queuing strategy that counts the number of bytes in each chunk.
3343
3705
  *
@@ -3389,12 +3751,17 @@ function IsByteLengthQueuingStrategy(x) {
3389
3751
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3390
3752
  return false;
3391
3753
  }
3392
- return true;
3754
+ return x instanceof ByteLengthQueuingStrategy;
3393
3755
  }
3394
3756
 
3395
- const countSizeFunction = function size() {
3757
+ // The size function must not have a prototype property nor be a constructor
3758
+ const countSizeFunction = () => {
3396
3759
  return 1;
3397
3760
  };
3761
+ Object.defineProperty(countSizeFunction, 'name', {
3762
+ value: 'size',
3763
+ configurable: true
3764
+ });
3398
3765
  /**
3399
3766
  * A queuing strategy that counts the number of chunks.
3400
3767
  *
@@ -3447,7 +3814,7 @@ function IsCountQueuingStrategy(x) {
3447
3814
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3448
3815
  return false;
3449
3816
  }
3450
- return true;
3817
+ return x instanceof CountQueuingStrategy;
3451
3818
  }
3452
3819
 
3453
3820
  function convertTransformer(original, context) {
@@ -3529,7 +3896,7 @@ class TransformStream {
3529
3896
  */
3530
3897
  get readable() {
3531
3898
  if (!IsTransformStream(this)) {
3532
- throw streamBrandCheckException$2('readable');
3899
+ throw streamBrandCheckException('readable');
3533
3900
  }
3534
3901
  return this._readable;
3535
3902
  }
@@ -3538,7 +3905,7 @@ class TransformStream {
3538
3905
  */
3539
3906
  get writable() {
3540
3907
  if (!IsTransformStream(this)) {
3541
- throw streamBrandCheckException$2('writable');
3908
+ throw streamBrandCheckException('writable');
3542
3909
  }
3543
3910
  return this._writable;
3544
3911
  }
@@ -3589,7 +3956,7 @@ function IsTransformStream(x) {
3589
3956
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3590
3957
  return false;
3591
3958
  }
3592
- return true;
3959
+ return x instanceof TransformStream;
3593
3960
  }
3594
3961
  // This is a no-op if both sides are already errored.
3595
3962
  function TransformStreamError(stream, e) {
@@ -3631,14 +3998,14 @@ class TransformStreamDefaultController {
3631
3998
  */
3632
3999
  get desiredSize() {
3633
4000
  if (!IsTransformStreamDefaultController(this)) {
3634
- throw defaultControllerBrandCheckException$1('desiredSize');
4001
+ throw defaultControllerBrandCheckException('desiredSize');
3635
4002
  }
3636
4003
  const readableController = this._controlledTransformStream._readable._readableStreamController;
3637
4004
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
3638
4005
  }
3639
4006
  enqueue(chunk = undefined) {
3640
4007
  if (!IsTransformStreamDefaultController(this)) {
3641
- throw defaultControllerBrandCheckException$1('enqueue');
4008
+ throw defaultControllerBrandCheckException('enqueue');
3642
4009
  }
3643
4010
  TransformStreamDefaultControllerEnqueue(this, chunk);
3644
4011
  }
@@ -3648,7 +4015,7 @@ class TransformStreamDefaultController {
3648
4015
  */
3649
4016
  error(reason = undefined) {
3650
4017
  if (!IsTransformStreamDefaultController(this)) {
3651
- throw defaultControllerBrandCheckException$1('error');
4018
+ throw defaultControllerBrandCheckException('error');
3652
4019
  }
3653
4020
  TransformStreamDefaultControllerError(this, reason);
3654
4021
  }
@@ -3658,7 +4025,7 @@ class TransformStreamDefaultController {
3658
4025
  */
3659
4026
  terminate() {
3660
4027
  if (!IsTransformStreamDefaultController(this)) {
3661
- throw defaultControllerBrandCheckException$1('terminate');
4028
+ throw defaultControllerBrandCheckException('terminate');
3662
4029
  }
3663
4030
  TransformStreamDefaultControllerTerminate(this);
3664
4031
  }
@@ -3683,7 +4050,7 @@ function IsTransformStreamDefaultController(x) {
3683
4050
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3684
4051
  return false;
3685
4052
  }
3686
- return true;
4053
+ return x instanceof TransformStreamDefaultController;
3687
4054
  }
3688
4055
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3689
4056
  controller._controlledTransformStream = stream;
@@ -3800,11 +4167,11 @@ function TransformStreamDefaultSourcePullAlgorithm(stream) {
3800
4167
  return stream._backpressureChangePromise;
3801
4168
  }
3802
4169
  // Helper functions for the TransformStreamDefaultController.
3803
- function defaultControllerBrandCheckException$1(name) {
4170
+ function defaultControllerBrandCheckException(name) {
3804
4171
  return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
3805
4172
  }
3806
4173
  // Helper functions for the TransformStream.
3807
- function streamBrandCheckException$2(name) {
4174
+ function streamBrandCheckException(name) {
3808
4175
  return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
3809
4176
  }
3810
4177