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.
package/dist/polyfill.mjs CHANGED
@@ -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
  var SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
@@ -8,7 +8,7 @@ var SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator ===
8
8
 
9
9
  /// <reference lib="dom" />
10
10
  function noop() {
11
- // do nothing
11
+ return undefined;
12
12
  }
13
13
  function getGlobals() {
14
14
  if (typeof self !== 'undefined') {
@@ -27,7 +27,7 @@ var globals = getGlobals();
27
27
  function typeIsObject(x) {
28
28
  return (typeof x === 'object' && x !== null) || typeof x === 'function';
29
29
  }
30
- var rethrowAssertionErrorRejection = noop;
30
+ var rethrowAssertionErrorRejection = noop;
31
31
 
32
32
  var originalPromise = Promise;
33
33
  var originalPromiseThen = Promise.prototype.then;
@@ -499,7 +499,7 @@ function IsReadableStreamDefaultReader(x) {
499
499
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
500
500
  return false;
501
501
  }
502
- return true;
502
+ return x instanceof ReadableStreamDefaultReader;
503
503
  }
504
504
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
505
505
  var stream = reader._ownerReadableStream;
@@ -647,7 +647,14 @@ function IsReadableStreamAsyncIterator(x) {
647
647
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
648
648
  return false;
649
649
  }
650
- return true;
650
+ try {
651
+ // noinspection SuspiciousTypeOfGuard
652
+ return x._asyncIteratorImpl instanceof
653
+ ReadableStreamAsyncIteratorImpl;
654
+ }
655
+ catch (_a) {
656
+ return false;
657
+ }
651
658
  }
652
659
  // Helper functions for the ReadableStream.
653
660
  function streamAsyncIteratorBrandCheckException(name) {
@@ -661,15 +668,35 @@ var NumberIsNaN = Number.isNaN || function (x) {
661
668
  return x !== x;
662
669
  };
663
670
 
664
- function IsFiniteNonNegativeNumber(v) {
665
- if (!IsNonNegativeNumber(v)) {
666
- return false;
667
- }
668
- if (v === Infinity) {
669
- return false;
671
+ function CreateArrayFromList(elements) {
672
+ // We use arrays to represent lists, so this is basically a no-op.
673
+ // Do a slice though just in case we happen to depend on the unique-ness.
674
+ return elements.slice();
675
+ }
676
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
677
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
678
+ }
679
+ // Not implemented correctly
680
+ function TransferArrayBuffer(O) {
681
+ return O;
682
+ }
683
+ // Not implemented correctly
684
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
685
+ function IsDetachedBuffer(O) {
686
+ return false;
687
+ }
688
+ function ArrayBufferSlice(buffer, begin, end) {
689
+ // ArrayBuffer.prototype.slice is not available on IE10
690
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
691
+ if (buffer.slice) {
692
+ return buffer.slice(begin, end);
670
693
  }
671
- return true;
694
+ var length = end - begin;
695
+ var slice = new ArrayBuffer(length);
696
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
697
+ return slice;
672
698
  }
699
+
673
700
  function IsNonNegativeNumber(v) {
674
701
  if (typeof v !== 'number') {
675
702
  return false;
@@ -682,6 +709,10 @@ function IsNonNegativeNumber(v) {
682
709
  }
683
710
  return true;
684
711
  }
712
+ function CloneAsUint8Array(O) {
713
+ var buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
714
+ return new Uint8Array(buffer);
715
+ }
685
716
 
686
717
  function DequeueValue(container) {
687
718
  var pair = container._queue.shift();
@@ -692,8 +723,7 @@ function DequeueValue(container) {
692
723
  return pair.value;
693
724
  }
694
725
  function EnqueueValueWithSize(container, value, size) {
695
- size = Number(size);
696
- if (!IsFiniteNonNegativeNumber(size)) {
726
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
697
727
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
698
728
  }
699
729
  container._queue.push({ value: value, size: size });
@@ -708,23 +738,6 @@ function ResetQueue(container) {
708
738
  container._queueTotalSize = 0;
709
739
  }
710
740
 
711
- function CreateArrayFromList(elements) {
712
- // We use arrays to represent lists, so this is basically a no-op.
713
- // Do a slice though just in case we happen to depend on the unique-ness.
714
- return elements.slice();
715
- }
716
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
717
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
718
- }
719
- // Not implemented correctly
720
- function TransferArrayBuffer(O) {
721
- return O;
722
- }
723
- // Not implemented correctly
724
- function IsDetachedBuffer(O) {
725
- return false;
726
- }
727
-
728
741
  /**
729
742
  * A pull-into request in a {@link ReadableByteStreamController}.
730
743
  *
@@ -767,15 +780,10 @@ var ReadableStreamBYOBRequest = /** @class */ (function () {
767
780
  if (!ArrayBuffer.isView(view)) {
768
781
  throw new TypeError('You can only respond with array buffer views');
769
782
  }
770
- if (view.byteLength === 0) {
771
- throw new TypeError('chunk must have non-zero byteLength');
772
- }
773
- if (view.buffer.byteLength === 0) {
774
- throw new TypeError("chunk's buffer must have non-zero byteLength");
775
- }
776
783
  if (this._associatedReadableByteStreamController === undefined) {
777
784
  throw new TypeError('This BYOB request has been invalidated');
778
785
  }
786
+ if (IsDetachedBuffer(view.buffer)) ;
779
787
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
780
788
  };
781
789
  return ReadableStreamBYOBRequest;
@@ -808,14 +816,7 @@ var ReadableByteStreamController = /** @class */ (function () {
808
816
  if (!IsReadableByteStreamController(this)) {
809
817
  throw byteStreamControllerBrandCheckException('byobRequest');
810
818
  }
811
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
812
- var firstDescriptor = this._pendingPullIntos.peek();
813
- var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
814
- var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
815
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
816
- this._byobRequest = byobRequest;
817
- }
818
- return this._byobRequest;
819
+ return ReadableByteStreamControllerGetBYOBRequest(this);
819
820
  },
820
821
  enumerable: false,
821
822
  configurable: true
@@ -886,10 +887,7 @@ var ReadableByteStreamController = /** @class */ (function () {
886
887
  };
887
888
  /** @internal */
888
889
  ReadableByteStreamController.prototype[CancelSteps] = function (reason) {
889
- if (this._pendingPullIntos.length > 0) {
890
- var firstDescriptor = this._pendingPullIntos.peek();
891
- firstDescriptor.bytesFilled = 0;
892
- }
890
+ ReadableByteStreamControllerClearPendingPullIntos(this);
893
891
  ResetQueue(this);
894
892
  var result = this._cancelAlgorithm(reason);
895
893
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -918,6 +916,7 @@ var ReadableByteStreamController = /** @class */ (function () {
918
916
  }
919
917
  var pullIntoDescriptor = {
920
918
  buffer: buffer,
919
+ bufferByteLength: autoAllocateChunkSize,
921
920
  byteOffset: 0,
922
921
  byteLength: autoAllocateChunkSize,
923
922
  bytesFilled: 0,
@@ -953,7 +952,7 @@ function IsReadableByteStreamController(x) {
953
952
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
954
953
  return false;
955
954
  }
956
- return true;
955
+ return x instanceof ReadableByteStreamController;
957
956
  }
958
957
  function IsReadableStreamBYOBRequest(x) {
959
958
  if (!typeIsObject(x)) {
@@ -962,7 +961,7 @@ function IsReadableStreamBYOBRequest(x) {
962
961
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
963
962
  return false;
964
963
  }
965
- return true;
964
+ return x instanceof ReadableStreamBYOBRequest;
966
965
  }
967
966
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
968
967
  var shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1044,7 +1043,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
1044
1043
  return ready;
1045
1044
  }
1046
1045
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1047
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1048
1046
  pullIntoDescriptor.bytesFilled += size;
1049
1047
  }
1050
1048
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1083,9 +1081,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1083
1081
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1084
1082
  }
1085
1083
  var ctor = view.constructor;
1084
+ // try {
1086
1085
  var buffer = TransferArrayBuffer(view.buffer);
1086
+ // } catch (e) {
1087
+ // readIntoRequest._errorSteps(e);
1088
+ // return;
1089
+ // }
1087
1090
  var pullIntoDescriptor = {
1088
1091
  buffer: buffer,
1092
+ bufferByteLength: buffer.byteLength,
1089
1093
  byteOffset: view.byteOffset,
1090
1094
  byteLength: view.byteLength,
1091
1095
  bytesFilled: 0,
@@ -1125,7 +1129,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1125
1129
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1126
1130
  }
1127
1131
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1128
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1129
1132
  var stream = controller._controlledReadableByteStream;
1130
1133
  if (ReadableStreamHasBYOBReader(stream)) {
1131
1134
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1135,34 +1138,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1135
1138
  }
1136
1139
  }
1137
1140
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1138
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1139
- throw new RangeError('bytesWritten out of range');
1140
- }
1141
1141
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1142
1142
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1143
- // TODO: Figure out whether we should detach the buffer or not here.
1144
1143
  return;
1145
1144
  }
1146
1145
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1147
1146
  var remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1148
1147
  if (remainderSize > 0) {
1149
1148
  var end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1150
- var remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1149
+ var remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1151
1150
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1152
1151
  }
1153
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1154
1152
  pullIntoDescriptor.bytesFilled -= remainderSize;
1155
1153
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1156
1154
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1157
1155
  }
1158
1156
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1159
1157
  var firstDescriptor = controller._pendingPullIntos.peek();
1158
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1160
1159
  var state = controller._controlledReadableByteStream._state;
1161
1160
  if (state === 'closed') {
1162
- if (bytesWritten !== 0) {
1163
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1164
- }
1165
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1161
+ ReadableByteStreamControllerRespondInClosedState(controller);
1166
1162
  }
1167
1163
  else {
1168
1164
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1171,7 +1167,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1171
1167
  }
1172
1168
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1173
1169
  var descriptor = controller._pendingPullIntos.shift();
1174
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1175
1170
  return descriptor;
1176
1171
  }
1177
1172
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1231,11 +1226,20 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1231
1226
  var byteOffset = chunk.byteOffset;
1232
1227
  var byteLength = chunk.byteLength;
1233
1228
  var transferredBuffer = TransferArrayBuffer(buffer);
1229
+ if (controller._pendingPullIntos.length > 0) {
1230
+ var firstPendingPullInto = controller._pendingPullIntos.peek();
1231
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1232
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1233
+ }
1234
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1234
1235
  if (ReadableStreamHasDefaultReader(stream)) {
1235
1236
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1236
1237
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
1237
1238
  }
1238
1239
  else {
1240
+ if (controller._pendingPullIntos.length > 0) {
1241
+ ReadableByteStreamControllerShiftPendingPullInto(controller);
1242
+ }
1239
1243
  var transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
1240
1244
  ReadableStreamFulfillReadRequest(stream, transferredView, false);
1241
1245
  }
@@ -1260,6 +1264,16 @@ function ReadableByteStreamControllerError(controller, e) {
1260
1264
  ReadableByteStreamControllerClearAlgorithms(controller);
1261
1265
  ReadableStreamError(stream, e);
1262
1266
  }
1267
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1268
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1269
+ var firstDescriptor = controller._pendingPullIntos.peek();
1270
+ var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1271
+ var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1272
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1273
+ controller._byobRequest = byobRequest;
1274
+ }
1275
+ return controller._byobRequest;
1276
+ }
1263
1277
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1264
1278
  var state = controller._controlledReadableByteStream._state;
1265
1279
  if (state === 'errored') {
@@ -1271,22 +1285,49 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1271
1285
  return controller._strategyHWM - controller._queueTotalSize;
1272
1286
  }
1273
1287
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1274
- bytesWritten = Number(bytesWritten);
1275
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1276
- throw new RangeError('bytesWritten must be a finite');
1288
+ var firstDescriptor = controller._pendingPullIntos.peek();
1289
+ var state = controller._controlledReadableByteStream._state;
1290
+ if (state === 'closed') {
1291
+ if (bytesWritten !== 0) {
1292
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1293
+ }
1294
+ }
1295
+ else {
1296
+ if (bytesWritten === 0) {
1297
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1298
+ }
1299
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1300
+ throw new RangeError('bytesWritten out of range');
1301
+ }
1277
1302
  }
1303
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1278
1304
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1279
1305
  }
1280
1306
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1281
1307
  var firstDescriptor = controller._pendingPullIntos.peek();
1308
+ var state = controller._controlledReadableByteStream._state;
1309
+ if (state === 'closed') {
1310
+ if (view.byteLength !== 0) {
1311
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1312
+ }
1313
+ }
1314
+ else {
1315
+ if (view.byteLength === 0) {
1316
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1317
+ }
1318
+ }
1282
1319
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1283
1320
  throw new RangeError('The region specified by view does not match byobRequest');
1284
1321
  }
1285
- if (firstDescriptor.byteLength !== view.byteLength) {
1322
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1286
1323
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1287
1324
  }
1288
- firstDescriptor.buffer = view.buffer;
1289
- ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1325
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1326
+ throw new RangeError('The region specified by view is larger than byobRequest');
1327
+ }
1328
+ var viewByteLength = view.byteLength;
1329
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1330
+ ReadableByteStreamControllerRespondInternal(controller, viewByteLength);
1290
1331
  }
1291
1332
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
1292
1333
  controller._controlledReadableByteStream = stream;
@@ -1440,6 +1481,7 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1440
1481
  if (view.buffer.byteLength === 0) {
1441
1482
  return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
1442
1483
  }
1484
+ if (IsDetachedBuffer(view.buffer)) ;
1443
1485
  if (this._ownerReadableStream === undefined) {
1444
1486
  return promiseRejectedWith(readerLockException('read from'));
1445
1487
  }
@@ -1500,7 +1542,7 @@ function IsReadableStreamBYOBReader(x) {
1500
1542
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1501
1543
  return false;
1502
1544
  }
1503
- return true;
1545
+ return x instanceof ReadableStreamBYOBReader;
1504
1546
  }
1505
1547
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1506
1548
  var stream = reader._ownerReadableStream;
@@ -1595,6 +1637,31 @@ function assertWritableStream(x, context) {
1595
1637
  }
1596
1638
  }
1597
1639
 
1640
+ function isAbortSignal(value) {
1641
+ if (typeof value !== 'object' || value === null) {
1642
+ return false;
1643
+ }
1644
+ try {
1645
+ return typeof value.aborted === 'boolean';
1646
+ }
1647
+ catch (_a) {
1648
+ // AbortSignal.prototype.aborted throws if its brand check fails
1649
+ return false;
1650
+ }
1651
+ }
1652
+ var supportsAbortController = typeof AbortController === 'function';
1653
+ /**
1654
+ * Construct a new AbortController, if supported by the platform.
1655
+ *
1656
+ * @internal
1657
+ */
1658
+ function createAbortController() {
1659
+ if (supportsAbortController) {
1660
+ return new AbortController();
1661
+ }
1662
+ return undefined;
1663
+ }
1664
+
1598
1665
  /**
1599
1666
  * A writable stream represents a destination for data, into which you can write.
1600
1667
  *
@@ -1627,7 +1694,7 @@ var WritableStream = /** @class */ (function () {
1627
1694
  */
1628
1695
  get: function () {
1629
1696
  if (!IsWritableStream(this)) {
1630
- throw streamBrandCheckException('locked');
1697
+ throw streamBrandCheckException$2('locked');
1631
1698
  }
1632
1699
  return IsWritableStreamLocked(this);
1633
1700
  },
@@ -1646,7 +1713,7 @@ var WritableStream = /** @class */ (function () {
1646
1713
  WritableStream.prototype.abort = function (reason) {
1647
1714
  if (reason === void 0) { reason = undefined; }
1648
1715
  if (!IsWritableStream(this)) {
1649
- return promiseRejectedWith(streamBrandCheckException('abort'));
1716
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1650
1717
  }
1651
1718
  if (IsWritableStreamLocked(this)) {
1652
1719
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
@@ -1663,7 +1730,7 @@ var WritableStream = /** @class */ (function () {
1663
1730
  */
1664
1731
  WritableStream.prototype.close = function () {
1665
1732
  if (!IsWritableStream(this)) {
1666
- return promiseRejectedWith(streamBrandCheckException('close'));
1733
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1667
1734
  }
1668
1735
  if (IsWritableStreamLocked(this)) {
1669
1736
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1683,7 +1750,7 @@ var WritableStream = /** @class */ (function () {
1683
1750
  */
1684
1751
  WritableStream.prototype.getWriter = function () {
1685
1752
  if (!IsWritableStream(this)) {
1686
- throw streamBrandCheckException('getWriter');
1753
+ throw streamBrandCheckException$2('getWriter');
1687
1754
  }
1688
1755
  return AcquireWritableStreamDefaultWriter(this);
1689
1756
  };
@@ -1748,7 +1815,7 @@ function IsWritableStream(x) {
1748
1815
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1749
1816
  return false;
1750
1817
  }
1751
- return true;
1818
+ return x instanceof WritableStream;
1752
1819
  }
1753
1820
  function IsWritableStreamLocked(stream) {
1754
1821
  if (stream._writer === undefined) {
@@ -1757,6 +1824,15 @@ function IsWritableStreamLocked(stream) {
1757
1824
  return true;
1758
1825
  }
1759
1826
  function WritableStreamAbort(stream, reason) {
1827
+ var _a;
1828
+ if (stream._state === 'closed' || stream._state === 'errored') {
1829
+ return promiseResolvedWith(undefined);
1830
+ }
1831
+ stream._writableStreamController._abortReason = reason;
1832
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1833
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1834
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1835
+ // Widen the type again by casting to WritableStreamState.
1760
1836
  var state = stream._state;
1761
1837
  if (state === 'closed' || state === 'errored') {
1762
1838
  return promiseResolvedWith(undefined);
@@ -2117,7 +2193,7 @@ function IsWritableStreamDefaultWriter(x) {
2117
2193
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2118
2194
  return false;
2119
2195
  }
2120
- return true;
2196
+ return x instanceof WritableStreamDefaultWriter;
2121
2197
  }
2122
2198
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2123
2199
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2207,6 +2283,42 @@ var WritableStreamDefaultController = /** @class */ (function () {
2207
2283
  function WritableStreamDefaultController() {
2208
2284
  throw new TypeError('Illegal constructor');
2209
2285
  }
2286
+ Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
2287
+ /**
2288
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2289
+ *
2290
+ * @deprecated
2291
+ * This property has been removed from the specification, see https://github.com/whatwg/streams/pull/1177.
2292
+ * Use {@link WritableStreamDefaultController.signal}'s `reason` instead.
2293
+ */
2294
+ get: function () {
2295
+ if (!IsWritableStreamDefaultController(this)) {
2296
+ throw defaultControllerBrandCheckException$2('abortReason');
2297
+ }
2298
+ return this._abortReason;
2299
+ },
2300
+ enumerable: false,
2301
+ configurable: true
2302
+ });
2303
+ Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
2304
+ /**
2305
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2306
+ */
2307
+ get: function () {
2308
+ if (!IsWritableStreamDefaultController(this)) {
2309
+ throw defaultControllerBrandCheckException$2('signal');
2310
+ }
2311
+ if (this._abortController === undefined) {
2312
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2313
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2314
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2315
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2316
+ }
2317
+ return this._abortController.signal;
2318
+ },
2319
+ enumerable: false,
2320
+ configurable: true
2321
+ });
2210
2322
  /**
2211
2323
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2212
2324
  *
@@ -2217,7 +2329,7 @@ var WritableStreamDefaultController = /** @class */ (function () {
2217
2329
  WritableStreamDefaultController.prototype.error = function (e) {
2218
2330
  if (e === void 0) { e = undefined; }
2219
2331
  if (!IsWritableStreamDefaultController(this)) {
2220
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2332
+ throw defaultControllerBrandCheckException$2('error');
2221
2333
  }
2222
2334
  var state = this._controlledWritableStream._state;
2223
2335
  if (state !== 'writable') {
@@ -2240,6 +2352,8 @@ var WritableStreamDefaultController = /** @class */ (function () {
2240
2352
  return WritableStreamDefaultController;
2241
2353
  }());
2242
2354
  Object.defineProperties(WritableStreamDefaultController.prototype, {
2355
+ abortReason: { enumerable: true },
2356
+ signal: { enumerable: true },
2243
2357
  error: { enumerable: true }
2244
2358
  });
2245
2359
  if (typeof SymbolPolyfill.toStringTag === 'symbol') {
@@ -2256,7 +2370,7 @@ function IsWritableStreamDefaultController(x) {
2256
2370
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2257
2371
  return false;
2258
2372
  }
2259
- return true;
2373
+ return x instanceof WritableStreamDefaultController;
2260
2374
  }
2261
2375
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2262
2376
  controller._controlledWritableStream = stream;
@@ -2265,6 +2379,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2265
2379
  controller._queue = undefined;
2266
2380
  controller._queueTotalSize = undefined;
2267
2381
  ResetQueue(controller);
2382
+ controller._abortReason = undefined;
2383
+ controller._abortController = createAbortController();
2268
2384
  controller._started = false;
2269
2385
  controller._strategySizeAlgorithm = sizeAlgorithm;
2270
2386
  controller._strategyHWM = highWaterMark;
@@ -2414,9 +2530,13 @@ function WritableStreamDefaultControllerError(controller, error) {
2414
2530
  WritableStreamStartErroring(stream, error);
2415
2531
  }
2416
2532
  // Helper functions for the WritableStream.
2417
- function streamBrandCheckException(name) {
2533
+ function streamBrandCheckException$2(name) {
2418
2534
  return new TypeError("WritableStream.prototype." + name + " can only be used on a WritableStream");
2419
2535
  }
2536
+ // Helper functions for the WritableStreamDefaultController.
2537
+ function defaultControllerBrandCheckException$2(name) {
2538
+ return new TypeError("WritableStreamDefaultController.prototype." + name + " can only be used on a WritableStreamDefaultController");
2539
+ }
2420
2540
  // Helper functions for the WritableStreamDefaultWriter.
2421
2541
  function defaultWriterBrandCheckException(name) {
2422
2542
  return new TypeError("WritableStreamDefaultWriter.prototype." + name + " can only be used on a WritableStreamDefaultWriter");
@@ -2502,19 +2622,6 @@ function defaultWriterReadyPromiseResolve(writer) {
2502
2622
  writer._readyPromiseState = 'fulfilled';
2503
2623
  }
2504
2624
 
2505
- function isAbortSignal(value) {
2506
- if (typeof value !== 'object' || value === null) {
2507
- return false;
2508
- }
2509
- try {
2510
- return typeof value.aborted === 'boolean';
2511
- }
2512
- catch (_a) {
2513
- // AbortSignal.prototype.aborted throws if its brand check fails
2514
- return false;
2515
- }
2516
- }
2517
-
2518
2625
  /// <reference lib="dom" />
2519
2626
  var NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2520
2627
 
@@ -2532,6 +2639,7 @@ function isDOMExceptionConstructor(ctor) {
2532
2639
  }
2533
2640
  }
2534
2641
  function createDOMExceptionPolyfill() {
2642
+ // eslint-disable-next-line no-shadow
2535
2643
  var ctor = function DOMException(message, name) {
2536
2644
  this.message = message || '';
2537
2645
  this.name = name || 'Error';
@@ -2543,6 +2651,7 @@ function createDOMExceptionPolyfill() {
2543
2651
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2544
2652
  return ctor;
2545
2653
  }
2654
+ // eslint-disable-next-line no-redeclare
2546
2655
  var DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2547
2656
 
2548
2657
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2736,7 +2845,7 @@ var ReadableStreamDefaultController = /** @class */ (function () {
2736
2845
  */
2737
2846
  get: function () {
2738
2847
  if (!IsReadableStreamDefaultController(this)) {
2739
- throw defaultControllerBrandCheckException('desiredSize');
2848
+ throw defaultControllerBrandCheckException$1('desiredSize');
2740
2849
  }
2741
2850
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2742
2851
  },
@@ -2749,7 +2858,7 @@ var ReadableStreamDefaultController = /** @class */ (function () {
2749
2858
  */
2750
2859
  ReadableStreamDefaultController.prototype.close = function () {
2751
2860
  if (!IsReadableStreamDefaultController(this)) {
2752
- throw defaultControllerBrandCheckException('close');
2861
+ throw defaultControllerBrandCheckException$1('close');
2753
2862
  }
2754
2863
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2755
2864
  throw new TypeError('The stream is not in a state that permits close');
@@ -2759,7 +2868,7 @@ var ReadableStreamDefaultController = /** @class */ (function () {
2759
2868
  ReadableStreamDefaultController.prototype.enqueue = function (chunk) {
2760
2869
  if (chunk === void 0) { chunk = undefined; }
2761
2870
  if (!IsReadableStreamDefaultController(this)) {
2762
- throw defaultControllerBrandCheckException('enqueue');
2871
+ throw defaultControllerBrandCheckException$1('enqueue');
2763
2872
  }
2764
2873
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2765
2874
  throw new TypeError('The stream is not in a state that permits enqueue');
@@ -2772,7 +2881,7 @@ var ReadableStreamDefaultController = /** @class */ (function () {
2772
2881
  ReadableStreamDefaultController.prototype.error = function (e) {
2773
2882
  if (e === void 0) { e = undefined; }
2774
2883
  if (!IsReadableStreamDefaultController(this)) {
2775
- throw defaultControllerBrandCheckException('error');
2884
+ throw defaultControllerBrandCheckException$1('error');
2776
2885
  }
2777
2886
  ReadableStreamDefaultControllerError(this, e);
2778
2887
  };
@@ -2824,7 +2933,7 @@ function IsReadableStreamDefaultController(x) {
2824
2933
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2825
2934
  return false;
2826
2935
  }
2827
- return true;
2936
+ return x instanceof ReadableStreamDefaultController;
2828
2937
  }
2829
2938
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2830
2939
  var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2980,13 +3089,20 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underl
2980
3089
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2981
3090
  }
2982
3091
  // Helper functions for the ReadableStreamDefaultController.
2983
- function defaultControllerBrandCheckException(name) {
3092
+ function defaultControllerBrandCheckException$1(name) {
2984
3093
  return new TypeError("ReadableStreamDefaultController.prototype." + name + " can only be used on a ReadableStreamDefaultController");
2985
3094
  }
2986
3095
 
2987
3096
  function ReadableStreamTee(stream, cloneForBranch2) {
3097
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3098
+ return ReadableByteStreamTee(stream);
3099
+ }
3100
+ return ReadableStreamDefaultTee(stream);
3101
+ }
3102
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2988
3103
  var reader = AcquireReadableStreamDefaultReader(stream);
2989
3104
  var reading = false;
3105
+ var readAgain = false;
2990
3106
  var canceled1 = false;
2991
3107
  var canceled2 = false;
2992
3108
  var reason1;
@@ -2999,30 +3115,34 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2999
3115
  });
3000
3116
  function pullAlgorithm() {
3001
3117
  if (reading) {
3118
+ readAgain = true;
3002
3119
  return promiseResolvedWith(undefined);
3003
3120
  }
3004
3121
  reading = true;
3005
3122
  var readRequest = {
3006
- _chunkSteps: function (value) {
3123
+ _chunkSteps: function (chunk) {
3007
3124
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3008
3125
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3009
3126
  // successful synchronously-available reads get ahead of asynchronously-available errors.
3010
3127
  queueMicrotask(function () {
3011
- reading = false;
3012
- var value1 = value;
3013
- var value2 = value;
3128
+ readAgain = false;
3129
+ var chunk1 = chunk;
3130
+ var chunk2 = chunk;
3014
3131
  // There is no way to access the cloning code right now in the reference implementation.
3015
3132
  // If we add one then we'll need an implementation for serializable objects.
3016
3133
  // if (!canceled2 && cloneForBranch2) {
3017
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3134
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
3018
3135
  // }
3019
3136
  if (!canceled1) {
3020
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3137
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
3021
3138
  }
3022
3139
  if (!canceled2) {
3023
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3140
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3141
+ }
3142
+ reading = false;
3143
+ if (readAgain) {
3144
+ pullAlgorithm();
3024
3145
  }
3025
- resolveCancelPromise(undefined);
3026
3146
  });
3027
3147
  },
3028
3148
  _closeSteps: function () {
@@ -3033,6 +3153,9 @@ function ReadableStreamTee(stream, cloneForBranch2) {
3033
3153
  if (!canceled2) {
3034
3154
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
3035
3155
  }
3156
+ if (!canceled1 || !canceled2) {
3157
+ resolveCancelPromise(undefined);
3158
+ }
3036
3159
  },
3037
3160
  _errorSteps: function () {
3038
3161
  reading = false;
@@ -3069,10 +3192,237 @@ function ReadableStreamTee(stream, cloneForBranch2) {
3069
3192
  uponRejection(reader._closedPromise, function (r) {
3070
3193
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
3071
3194
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3072
- resolveCancelPromise(undefined);
3195
+ if (!canceled1 || !canceled2) {
3196
+ resolveCancelPromise(undefined);
3197
+ }
3073
3198
  });
3074
3199
  return [branch1, branch2];
3075
3200
  }
3201
+ function ReadableByteStreamTee(stream) {
3202
+ var reader = AcquireReadableStreamDefaultReader(stream);
3203
+ var reading = false;
3204
+ var readAgainForBranch1 = false;
3205
+ var readAgainForBranch2 = false;
3206
+ var canceled1 = false;
3207
+ var canceled2 = false;
3208
+ var reason1;
3209
+ var reason2;
3210
+ var branch1;
3211
+ var branch2;
3212
+ var resolveCancelPromise;
3213
+ var cancelPromise = newPromise(function (resolve) {
3214
+ resolveCancelPromise = resolve;
3215
+ });
3216
+ function forwardReaderError(thisReader) {
3217
+ uponRejection(thisReader._closedPromise, function (r) {
3218
+ if (thisReader !== reader) {
3219
+ return;
3220
+ }
3221
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3222
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3223
+ if (!canceled1 || !canceled2) {
3224
+ resolveCancelPromise(undefined);
3225
+ }
3226
+ });
3227
+ }
3228
+ function pullWithDefaultReader() {
3229
+ if (IsReadableStreamBYOBReader(reader)) {
3230
+ ReadableStreamReaderGenericRelease(reader);
3231
+ reader = AcquireReadableStreamDefaultReader(stream);
3232
+ forwardReaderError(reader);
3233
+ }
3234
+ var readRequest = {
3235
+ _chunkSteps: function (chunk) {
3236
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3237
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3238
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3239
+ queueMicrotask(function () {
3240
+ readAgainForBranch1 = false;
3241
+ readAgainForBranch2 = false;
3242
+ var chunk1 = chunk;
3243
+ var chunk2 = chunk;
3244
+ if (!canceled1 && !canceled2) {
3245
+ try {
3246
+ chunk2 = CloneAsUint8Array(chunk);
3247
+ }
3248
+ catch (cloneE) {
3249
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3250
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3251
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3252
+ return;
3253
+ }
3254
+ }
3255
+ if (!canceled1) {
3256
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3257
+ }
3258
+ if (!canceled2) {
3259
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3260
+ }
3261
+ reading = false;
3262
+ if (readAgainForBranch1) {
3263
+ pull1Algorithm();
3264
+ }
3265
+ else if (readAgainForBranch2) {
3266
+ pull2Algorithm();
3267
+ }
3268
+ });
3269
+ },
3270
+ _closeSteps: function () {
3271
+ reading = false;
3272
+ if (!canceled1) {
3273
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3274
+ }
3275
+ if (!canceled2) {
3276
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3277
+ }
3278
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3279
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3280
+ }
3281
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3282
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3283
+ }
3284
+ if (!canceled1 || !canceled2) {
3285
+ resolveCancelPromise(undefined);
3286
+ }
3287
+ },
3288
+ _errorSteps: function () {
3289
+ reading = false;
3290
+ }
3291
+ };
3292
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3293
+ }
3294
+ function pullWithBYOBReader(view, forBranch2) {
3295
+ if (IsReadableStreamDefaultReader(reader)) {
3296
+ ReadableStreamReaderGenericRelease(reader);
3297
+ reader = AcquireReadableStreamBYOBReader(stream);
3298
+ forwardReaderError(reader);
3299
+ }
3300
+ var byobBranch = forBranch2 ? branch2 : branch1;
3301
+ var otherBranch = forBranch2 ? branch1 : branch2;
3302
+ var readIntoRequest = {
3303
+ _chunkSteps: function (chunk) {
3304
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3305
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3306
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3307
+ queueMicrotask(function () {
3308
+ readAgainForBranch1 = false;
3309
+ readAgainForBranch2 = false;
3310
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3311
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3312
+ if (!otherCanceled) {
3313
+ var clonedChunk = void 0;
3314
+ try {
3315
+ clonedChunk = CloneAsUint8Array(chunk);
3316
+ }
3317
+ catch (cloneE) {
3318
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3319
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3320
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3321
+ return;
3322
+ }
3323
+ if (!byobCanceled) {
3324
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3325
+ }
3326
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3327
+ }
3328
+ else if (!byobCanceled) {
3329
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3330
+ }
3331
+ reading = false;
3332
+ if (readAgainForBranch1) {
3333
+ pull1Algorithm();
3334
+ }
3335
+ else if (readAgainForBranch2) {
3336
+ pull2Algorithm();
3337
+ }
3338
+ });
3339
+ },
3340
+ _closeSteps: function (chunk) {
3341
+ reading = false;
3342
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3343
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3344
+ if (!byobCanceled) {
3345
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3346
+ }
3347
+ if (!otherCanceled) {
3348
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3349
+ }
3350
+ if (chunk !== undefined) {
3351
+ if (!byobCanceled) {
3352
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3353
+ }
3354
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3355
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3356
+ }
3357
+ }
3358
+ if (!byobCanceled || !otherCanceled) {
3359
+ resolveCancelPromise(undefined);
3360
+ }
3361
+ },
3362
+ _errorSteps: function () {
3363
+ reading = false;
3364
+ }
3365
+ };
3366
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3367
+ }
3368
+ function pull1Algorithm() {
3369
+ if (reading) {
3370
+ readAgainForBranch1 = true;
3371
+ return promiseResolvedWith(undefined);
3372
+ }
3373
+ reading = true;
3374
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3375
+ if (byobRequest === null) {
3376
+ pullWithDefaultReader();
3377
+ }
3378
+ else {
3379
+ pullWithBYOBReader(byobRequest._view, false);
3380
+ }
3381
+ return promiseResolvedWith(undefined);
3382
+ }
3383
+ function pull2Algorithm() {
3384
+ if (reading) {
3385
+ readAgainForBranch2 = true;
3386
+ return promiseResolvedWith(undefined);
3387
+ }
3388
+ reading = true;
3389
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3390
+ if (byobRequest === null) {
3391
+ pullWithDefaultReader();
3392
+ }
3393
+ else {
3394
+ pullWithBYOBReader(byobRequest._view, true);
3395
+ }
3396
+ return promiseResolvedWith(undefined);
3397
+ }
3398
+ function cancel1Algorithm(reason) {
3399
+ canceled1 = true;
3400
+ reason1 = reason;
3401
+ if (canceled2) {
3402
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3403
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3404
+ resolveCancelPromise(cancelResult);
3405
+ }
3406
+ return cancelPromise;
3407
+ }
3408
+ function cancel2Algorithm(reason) {
3409
+ canceled2 = true;
3410
+ reason2 = reason;
3411
+ if (canceled1) {
3412
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3413
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3414
+ resolveCancelPromise(cancelResult);
3415
+ }
3416
+ return cancelPromise;
3417
+ }
3418
+ function startAlgorithm() {
3419
+ return;
3420
+ }
3421
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3422
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3423
+ forwardReaderError(reader);
3424
+ return [branch1, branch2];
3425
+ }
3076
3426
 
3077
3427
  function convertUnderlyingDefaultOrByteSource(source, context) {
3078
3428
  assertDictionary(source, context);
@@ -3348,6 +3698,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3348
3698
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3349
3699
  return stream;
3350
3700
  }
3701
+ // Throws if and only if startAlgorithm throws.
3702
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3703
+ var stream = Object.create(ReadableStream.prototype);
3704
+ InitializeReadableStream(stream);
3705
+ var controller = Object.create(ReadableByteStreamController.prototype);
3706
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3707
+ return stream;
3708
+ }
3351
3709
  function InitializeReadableStream(stream) {
3352
3710
  stream._state = 'readable';
3353
3711
  stream._reader = undefined;
@@ -3361,7 +3719,7 @@ function IsReadableStream(x) {
3361
3719
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3362
3720
  return false;
3363
3721
  }
3364
- return true;
3722
+ return x instanceof ReadableStream;
3365
3723
  }
3366
3724
  function IsReadableStreamLocked(stream) {
3367
3725
  if (stream._reader === undefined) {
@@ -3379,6 +3737,13 @@ function ReadableStreamCancel(stream, reason) {
3379
3737
  return promiseRejectedWith(stream._storedError);
3380
3738
  }
3381
3739
  ReadableStreamClose(stream);
3740
+ var reader = stream._reader;
3741
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3742
+ reader._readIntoRequests.forEach(function (readIntoRequest) {
3743
+ readIntoRequest._closeSteps(undefined);
3744
+ });
3745
+ reader._readIntoRequests = new SimpleQueue();
3746
+ }
3382
3747
  var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3383
3748
  return transformPromiseWith(sourceCancelPromise, noop);
3384
3749
  }
@@ -3431,9 +3796,14 @@ function convertQueuingStrategyInit(init, context) {
3431
3796
  };
3432
3797
  }
3433
3798
 
3434
- var byteLengthSizeFunction = function size(chunk) {
3799
+ // The size function must not have a prototype property nor be a constructor
3800
+ var byteLengthSizeFunction = function (chunk) {
3435
3801
  return chunk.byteLength;
3436
3802
  };
3803
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3804
+ value: 'size',
3805
+ configurable: true
3806
+ });
3437
3807
  /**
3438
3808
  * A queuing strategy that counts the number of bytes in each chunk.
3439
3809
  *
@@ -3494,12 +3864,17 @@ function IsByteLengthQueuingStrategy(x) {
3494
3864
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3495
3865
  return false;
3496
3866
  }
3497
- return true;
3867
+ return x instanceof ByteLengthQueuingStrategy;
3498
3868
  }
3499
3869
 
3500
- var countSizeFunction = function size() {
3870
+ // The size function must not have a prototype property nor be a constructor
3871
+ var countSizeFunction = function () {
3501
3872
  return 1;
3502
3873
  };
3874
+ Object.defineProperty(countSizeFunction, 'name', {
3875
+ value: 'size',
3876
+ configurable: true
3877
+ });
3503
3878
  /**
3504
3879
  * A queuing strategy that counts the number of chunks.
3505
3880
  *
@@ -3561,7 +3936,7 @@ function IsCountQueuingStrategy(x) {
3561
3936
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3562
3937
  return false;
3563
3938
  }
3564
- return true;
3939
+ return x instanceof CountQueuingStrategy;
3565
3940
  }
3566
3941
 
3567
3942
  function convertTransformer(original, context) {
@@ -3647,7 +4022,7 @@ var TransformStream = /** @class */ (function () {
3647
4022
  */
3648
4023
  get: function () {
3649
4024
  if (!IsTransformStream(this)) {
3650
- throw streamBrandCheckException$2('readable');
4025
+ throw streamBrandCheckException('readable');
3651
4026
  }
3652
4027
  return this._readable;
3653
4028
  },
@@ -3660,7 +4035,7 @@ var TransformStream = /** @class */ (function () {
3660
4035
  */
3661
4036
  get: function () {
3662
4037
  if (!IsTransformStream(this)) {
3663
- throw streamBrandCheckException$2('writable');
4038
+ throw streamBrandCheckException('writable');
3664
4039
  }
3665
4040
  return this._writable;
3666
4041
  },
@@ -3715,7 +4090,7 @@ function IsTransformStream(x) {
3715
4090
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3716
4091
  return false;
3717
4092
  }
3718
- return true;
4093
+ return x instanceof TransformStream;
3719
4094
  }
3720
4095
  // This is a no-op if both sides are already errored.
3721
4096
  function TransformStreamError(stream, e) {
@@ -3758,7 +4133,7 @@ var TransformStreamDefaultController = /** @class */ (function () {
3758
4133
  */
3759
4134
  get: function () {
3760
4135
  if (!IsTransformStreamDefaultController(this)) {
3761
- throw defaultControllerBrandCheckException$1('desiredSize');
4136
+ throw defaultControllerBrandCheckException('desiredSize');
3762
4137
  }
3763
4138
  var readableController = this._controlledTransformStream._readable._readableStreamController;
3764
4139
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
@@ -3769,7 +4144,7 @@ var TransformStreamDefaultController = /** @class */ (function () {
3769
4144
  TransformStreamDefaultController.prototype.enqueue = function (chunk) {
3770
4145
  if (chunk === void 0) { chunk = undefined; }
3771
4146
  if (!IsTransformStreamDefaultController(this)) {
3772
- throw defaultControllerBrandCheckException$1('enqueue');
4147
+ throw defaultControllerBrandCheckException('enqueue');
3773
4148
  }
3774
4149
  TransformStreamDefaultControllerEnqueue(this, chunk);
3775
4150
  };
@@ -3780,7 +4155,7 @@ var TransformStreamDefaultController = /** @class */ (function () {
3780
4155
  TransformStreamDefaultController.prototype.error = function (reason) {
3781
4156
  if (reason === void 0) { reason = undefined; }
3782
4157
  if (!IsTransformStreamDefaultController(this)) {
3783
- throw defaultControllerBrandCheckException$1('error');
4158
+ throw defaultControllerBrandCheckException('error');
3784
4159
  }
3785
4160
  TransformStreamDefaultControllerError(this, reason);
3786
4161
  };
@@ -3790,7 +4165,7 @@ var TransformStreamDefaultController = /** @class */ (function () {
3790
4165
  */
3791
4166
  TransformStreamDefaultController.prototype.terminate = function () {
3792
4167
  if (!IsTransformStreamDefaultController(this)) {
3793
- throw defaultControllerBrandCheckException$1('terminate');
4168
+ throw defaultControllerBrandCheckException('terminate');
3794
4169
  }
3795
4170
  TransformStreamDefaultControllerTerminate(this);
3796
4171
  };
@@ -3816,7 +4191,7 @@ function IsTransformStreamDefaultController(x) {
3816
4191
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3817
4192
  return false;
3818
4193
  }
3819
- return true;
4194
+ return x instanceof TransformStreamDefaultController;
3820
4195
  }
3821
4196
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3822
4197
  controller._controlledTransformStream = stream;
@@ -3933,11 +4308,11 @@ function TransformStreamDefaultSourcePullAlgorithm(stream) {
3933
4308
  return stream._backpressureChangePromise;
3934
4309
  }
3935
4310
  // Helper functions for the TransformStreamDefaultController.
3936
- function defaultControllerBrandCheckException$1(name) {
4311
+ function defaultControllerBrandCheckException(name) {
3937
4312
  return new TypeError("TransformStreamDefaultController.prototype." + name + " can only be used on a TransformStreamDefaultController");
3938
4313
  }
3939
4314
  // Helper functions for the TransformStream.
3940
- function streamBrandCheckException$2(name) {
4315
+ function streamBrandCheckException(name) {
3941
4316
  return new TypeError("TransformStream.prototype." + name + " can only be used on a TransformStream");
3942
4317
  }
3943
4318