web-streams-polyfill 3.0.3 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/polyfill.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * web-streams-polyfill v3.0.3
2
+ * web-streams-polyfill v3.1.0
3
3
  */
4
4
  (function (global, factory) {
5
5
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
@@ -505,7 +505,7 @@
505
505
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
506
506
  return false;
507
507
  }
508
- return true;
508
+ return x instanceof ReadableStreamDefaultReader;
509
509
  }
510
510
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
511
511
  var stream = reader._ownerReadableStream;
@@ -653,7 +653,14 @@
653
653
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
654
654
  return false;
655
655
  }
656
- return true;
656
+ try {
657
+ // noinspection SuspiciousTypeOfGuard
658
+ return x._asyncIteratorImpl instanceof
659
+ ReadableStreamAsyncIteratorImpl;
660
+ }
661
+ catch (_a) {
662
+ return false;
663
+ }
657
664
  }
658
665
  // Helper functions for the ReadableStream.
659
666
  function streamAsyncIteratorBrandCheckException(name) {
@@ -667,15 +674,35 @@
667
674
  return x !== x;
668
675
  };
669
676
 
670
- function IsFiniteNonNegativeNumber(v) {
671
- if (!IsNonNegativeNumber(v)) {
672
- return false;
673
- }
674
- if (v === Infinity) {
675
- return false;
677
+ function CreateArrayFromList(elements) {
678
+ // We use arrays to represent lists, so this is basically a no-op.
679
+ // Do a slice though just in case we happen to depend on the unique-ness.
680
+ return elements.slice();
681
+ }
682
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
683
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
684
+ }
685
+ // Not implemented correctly
686
+ function TransferArrayBuffer(O) {
687
+ return O;
688
+ }
689
+ // Not implemented correctly
690
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
691
+ function IsDetachedBuffer(O) {
692
+ return false;
693
+ }
694
+ function ArrayBufferSlice(buffer, begin, end) {
695
+ // ArrayBuffer.prototype.slice is not available on IE10
696
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
697
+ if (buffer.slice) {
698
+ return buffer.slice(begin, end);
676
699
  }
677
- return true;
700
+ var length = end - begin;
701
+ var slice = new ArrayBuffer(length);
702
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
703
+ return slice;
678
704
  }
705
+
679
706
  function IsNonNegativeNumber(v) {
680
707
  if (typeof v !== 'number') {
681
708
  return false;
@@ -688,6 +715,10 @@
688
715
  }
689
716
  return true;
690
717
  }
718
+ function CloneAsUint8Array(O) {
719
+ var buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
720
+ return new Uint8Array(buffer);
721
+ }
691
722
 
692
723
  function DequeueValue(container) {
693
724
  var pair = container._queue.shift();
@@ -698,8 +729,7 @@
698
729
  return pair.value;
699
730
  }
700
731
  function EnqueueValueWithSize(container, value, size) {
701
- size = Number(size);
702
- if (!IsFiniteNonNegativeNumber(size)) {
732
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
703
733
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
704
734
  }
705
735
  container._queue.push({ value: value, size: size });
@@ -714,23 +744,6 @@
714
744
  container._queueTotalSize = 0;
715
745
  }
716
746
 
717
- function CreateArrayFromList(elements) {
718
- // We use arrays to represent lists, so this is basically a no-op.
719
- // Do a slice though just in case we happen to depend on the unique-ness.
720
- return elements.slice();
721
- }
722
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
723
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
724
- }
725
- // Not implemented correctly
726
- function TransferArrayBuffer(O) {
727
- return O;
728
- }
729
- // Not implemented correctly
730
- function IsDetachedBuffer(O) {
731
- return false;
732
- }
733
-
734
747
  /**
735
748
  * A pull-into request in a {@link ReadableByteStreamController}.
736
749
  *
@@ -773,15 +786,10 @@
773
786
  if (!ArrayBuffer.isView(view)) {
774
787
  throw new TypeError('You can only respond with array buffer views');
775
788
  }
776
- if (view.byteLength === 0) {
777
- throw new TypeError('chunk must have non-zero byteLength');
778
- }
779
- if (view.buffer.byteLength === 0) {
780
- throw new TypeError("chunk's buffer must have non-zero byteLength");
781
- }
782
789
  if (this._associatedReadableByteStreamController === undefined) {
783
790
  throw new TypeError('This BYOB request has been invalidated');
784
791
  }
792
+ if (IsDetachedBuffer(view.buffer)) ;
785
793
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
786
794
  };
787
795
  return ReadableStreamBYOBRequest;
@@ -814,14 +822,7 @@
814
822
  if (!IsReadableByteStreamController(this)) {
815
823
  throw byteStreamControllerBrandCheckException('byobRequest');
816
824
  }
817
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
818
- var firstDescriptor = this._pendingPullIntos.peek();
819
- var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
820
- var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
821
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
822
- this._byobRequest = byobRequest;
823
- }
824
- return this._byobRequest;
825
+ return ReadableByteStreamControllerGetBYOBRequest(this);
825
826
  },
826
827
  enumerable: false,
827
828
  configurable: true
@@ -892,10 +893,7 @@
892
893
  };
893
894
  /** @internal */
894
895
  ReadableByteStreamController.prototype[CancelSteps] = function (reason) {
895
- if (this._pendingPullIntos.length > 0) {
896
- var firstDescriptor = this._pendingPullIntos.peek();
897
- firstDescriptor.bytesFilled = 0;
898
- }
896
+ ReadableByteStreamControllerClearPendingPullIntos(this);
899
897
  ResetQueue(this);
900
898
  var result = this._cancelAlgorithm(reason);
901
899
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -924,6 +922,7 @@
924
922
  }
925
923
  var pullIntoDescriptor = {
926
924
  buffer: buffer,
925
+ bufferByteLength: autoAllocateChunkSize,
927
926
  byteOffset: 0,
928
927
  byteLength: autoAllocateChunkSize,
929
928
  bytesFilled: 0,
@@ -959,7 +958,7 @@
959
958
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
960
959
  return false;
961
960
  }
962
- return true;
961
+ return x instanceof ReadableByteStreamController;
963
962
  }
964
963
  function IsReadableStreamBYOBRequest(x) {
965
964
  if (!typeIsObject(x)) {
@@ -968,7 +967,7 @@
968
967
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
969
968
  return false;
970
969
  }
971
- return true;
970
+ return x instanceof ReadableStreamBYOBRequest;
972
971
  }
973
972
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
974
973
  var shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1050,7 +1049,6 @@
1050
1049
  return ready;
1051
1050
  }
1052
1051
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1053
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1054
1052
  pullIntoDescriptor.bytesFilled += size;
1055
1053
  }
1056
1054
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1089,9 +1087,15 @@
1089
1087
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1090
1088
  }
1091
1089
  var ctor = view.constructor;
1090
+ // try {
1092
1091
  var buffer = TransferArrayBuffer(view.buffer);
1092
+ // } catch (e) {
1093
+ // readIntoRequest._errorSteps(e);
1094
+ // return;
1095
+ // }
1093
1096
  var pullIntoDescriptor = {
1094
1097
  buffer: buffer,
1098
+ bufferByteLength: buffer.byteLength,
1095
1099
  byteOffset: view.byteOffset,
1096
1100
  byteLength: view.byteLength,
1097
1101
  bytesFilled: 0,
@@ -1131,7 +1135,6 @@
1131
1135
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1132
1136
  }
1133
1137
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1134
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1135
1138
  var stream = controller._controlledReadableByteStream;
1136
1139
  if (ReadableStreamHasBYOBReader(stream)) {
1137
1140
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1141,34 +1144,27 @@
1141
1144
  }
1142
1145
  }
1143
1146
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1144
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1145
- throw new RangeError('bytesWritten out of range');
1146
- }
1147
1147
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1148
1148
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1149
- // TODO: Figure out whether we should detach the buffer or not here.
1150
1149
  return;
1151
1150
  }
1152
1151
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1153
1152
  var remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1154
1153
  if (remainderSize > 0) {
1155
1154
  var end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1156
- var remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1155
+ var remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1157
1156
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1158
1157
  }
1159
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1160
1158
  pullIntoDescriptor.bytesFilled -= remainderSize;
1161
1159
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1162
1160
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1163
1161
  }
1164
1162
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1165
1163
  var firstDescriptor = controller._pendingPullIntos.peek();
1164
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1166
1165
  var state = controller._controlledReadableByteStream._state;
1167
1166
  if (state === 'closed') {
1168
- if (bytesWritten !== 0) {
1169
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1170
- }
1171
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1167
+ ReadableByteStreamControllerRespondInClosedState(controller);
1172
1168
  }
1173
1169
  else {
1174
1170
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1177,7 +1173,6 @@
1177
1173
  }
1178
1174
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1179
1175
  var descriptor = controller._pendingPullIntos.shift();
1180
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1181
1176
  return descriptor;
1182
1177
  }
1183
1178
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1237,6 +1232,12 @@
1237
1232
  var byteOffset = chunk.byteOffset;
1238
1233
  var byteLength = chunk.byteLength;
1239
1234
  var transferredBuffer = TransferArrayBuffer(buffer);
1235
+ if (controller._pendingPullIntos.length > 0) {
1236
+ var firstPendingPullInto = controller._pendingPullIntos.peek();
1237
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1238
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1239
+ }
1240
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1240
1241
  if (ReadableStreamHasDefaultReader(stream)) {
1241
1242
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1242
1243
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1266,6 +1267,16 @@
1266
1267
  ReadableByteStreamControllerClearAlgorithms(controller);
1267
1268
  ReadableStreamError(stream, e);
1268
1269
  }
1270
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1271
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1272
+ var firstDescriptor = controller._pendingPullIntos.peek();
1273
+ var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1274
+ var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1275
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1276
+ controller._byobRequest = byobRequest;
1277
+ }
1278
+ return controller._byobRequest;
1279
+ }
1269
1280
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1270
1281
  var state = controller._controlledReadableByteStream._state;
1271
1282
  if (state === 'errored') {
@@ -1277,21 +1288,47 @@
1277
1288
  return controller._strategyHWM - controller._queueTotalSize;
1278
1289
  }
1279
1290
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1280
- bytesWritten = Number(bytesWritten);
1281
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1282
- throw new RangeError('bytesWritten must be a finite');
1291
+ var firstDescriptor = controller._pendingPullIntos.peek();
1292
+ var state = controller._controlledReadableByteStream._state;
1293
+ if (state === 'closed') {
1294
+ if (bytesWritten !== 0) {
1295
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1296
+ }
1297
+ }
1298
+ else {
1299
+ if (bytesWritten === 0) {
1300
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1301
+ }
1302
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1303
+ throw new RangeError('bytesWritten out of range');
1304
+ }
1283
1305
  }
1306
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1284
1307
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1285
1308
  }
1286
1309
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1287
1310
  var firstDescriptor = controller._pendingPullIntos.peek();
1311
+ var state = controller._controlledReadableByteStream._state;
1312
+ if (state === 'closed') {
1313
+ if (view.byteLength !== 0) {
1314
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1315
+ }
1316
+ }
1317
+ else {
1318
+ if (view.byteLength === 0) {
1319
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1320
+ }
1321
+ }
1288
1322
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1289
1323
  throw new RangeError('The region specified by view does not match byobRequest');
1290
1324
  }
1291
- if (firstDescriptor.byteLength !== view.byteLength) {
1325
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1292
1326
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1293
1327
  }
1294
- firstDescriptor.buffer = view.buffer;
1328
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1329
+ throw new RangeError('The region specified by view is larger than byobRequest');
1330
+ }
1331
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1295
1332
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1296
1333
  }
1297
1334
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1446,6 +1483,7 @@
1446
1483
  if (view.buffer.byteLength === 0) {
1447
1484
  return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
1448
1485
  }
1486
+ if (IsDetachedBuffer(view.buffer)) ;
1449
1487
  if (this._ownerReadableStream === undefined) {
1450
1488
  return promiseRejectedWith(readerLockException('read from'));
1451
1489
  }
@@ -1506,7 +1544,7 @@
1506
1544
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1507
1545
  return false;
1508
1546
  }
1509
- return true;
1547
+ return x instanceof ReadableStreamBYOBReader;
1510
1548
  }
1511
1549
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1512
1550
  var stream = reader._ownerReadableStream;
@@ -1601,6 +1639,31 @@
1601
1639
  }
1602
1640
  }
1603
1641
 
1642
+ function isAbortSignal(value) {
1643
+ if (typeof value !== 'object' || value === null) {
1644
+ return false;
1645
+ }
1646
+ try {
1647
+ return typeof value.aborted === 'boolean';
1648
+ }
1649
+ catch (_a) {
1650
+ // AbortSignal.prototype.aborted throws if its brand check fails
1651
+ return false;
1652
+ }
1653
+ }
1654
+ var supportsAbortController = typeof AbortController === 'function';
1655
+ /**
1656
+ * Construct a new AbortController, if supported by the platform.
1657
+ *
1658
+ * @internal
1659
+ */
1660
+ function createAbortController() {
1661
+ if (supportsAbortController) {
1662
+ return new AbortController();
1663
+ }
1664
+ return undefined;
1665
+ }
1666
+
1604
1667
  /**
1605
1668
  * A writable stream represents a destination for data, into which you can write.
1606
1669
  *
@@ -1754,7 +1817,7 @@
1754
1817
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1755
1818
  return false;
1756
1819
  }
1757
- return true;
1820
+ return x instanceof WritableStream;
1758
1821
  }
1759
1822
  function IsWritableStreamLocked(stream) {
1760
1823
  if (stream._writer === undefined) {
@@ -1763,6 +1826,15 @@
1763
1826
  return true;
1764
1827
  }
1765
1828
  function WritableStreamAbort(stream, reason) {
1829
+ var _a;
1830
+ if (stream._state === 'closed' || stream._state === 'errored') {
1831
+ return promiseResolvedWith(undefined);
1832
+ }
1833
+ stream._writableStreamController._abortReason = reason;
1834
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1835
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1836
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1837
+ // Widen the type again by casting to WritableStreamState.
1766
1838
  var state = stream._state;
1767
1839
  if (state === 'closed' || state === 'errored') {
1768
1840
  return promiseResolvedWith(undefined);
@@ -2123,7 +2195,7 @@
2123
2195
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2124
2196
  return false;
2125
2197
  }
2126
- return true;
2198
+ return x instanceof WritableStreamDefaultWriter;
2127
2199
  }
2128
2200
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2129
2201
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2213,6 +2285,38 @@
2213
2285
  function WritableStreamDefaultController() {
2214
2286
  throw new TypeError('Illegal constructor');
2215
2287
  }
2288
+ Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
2289
+ /**
2290
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2291
+ */
2292
+ get: function () {
2293
+ if (!IsWritableStreamDefaultController(this)) {
2294
+ throw defaultControllerBrandCheckException$2('abortReason');
2295
+ }
2296
+ return this._abortReason;
2297
+ },
2298
+ enumerable: false,
2299
+ configurable: true
2300
+ });
2301
+ Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
2302
+ /**
2303
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2304
+ */
2305
+ get: function () {
2306
+ if (!IsWritableStreamDefaultController(this)) {
2307
+ throw defaultControllerBrandCheckException$2('signal');
2308
+ }
2309
+ if (this._abortController === undefined) {
2310
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2311
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2312
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2313
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2314
+ }
2315
+ return this._abortController.signal;
2316
+ },
2317
+ enumerable: false,
2318
+ configurable: true
2319
+ });
2216
2320
  /**
2217
2321
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2218
2322
  *
@@ -2223,7 +2327,7 @@
2223
2327
  WritableStreamDefaultController.prototype.error = function (e) {
2224
2328
  if (e === void 0) { e = undefined; }
2225
2329
  if (!IsWritableStreamDefaultController(this)) {
2226
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2330
+ throw defaultControllerBrandCheckException$2('error');
2227
2331
  }
2228
2332
  var state = this._controlledWritableStream._state;
2229
2333
  if (state !== 'writable') {
@@ -2262,7 +2366,7 @@
2262
2366
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2263
2367
  return false;
2264
2368
  }
2265
- return true;
2369
+ return x instanceof WritableStreamDefaultController;
2266
2370
  }
2267
2371
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2268
2372
  controller._controlledWritableStream = stream;
@@ -2271,6 +2375,8 @@
2271
2375
  controller._queue = undefined;
2272
2376
  controller._queueTotalSize = undefined;
2273
2377
  ResetQueue(controller);
2378
+ controller._abortReason = undefined;
2379
+ controller._abortController = createAbortController();
2274
2380
  controller._started = false;
2275
2381
  controller._strategySizeAlgorithm = sizeAlgorithm;
2276
2382
  controller._strategyHWM = highWaterMark;
@@ -2423,6 +2529,10 @@
2423
2529
  function streamBrandCheckException$2(name) {
2424
2530
  return new TypeError("WritableStream.prototype." + name + " can only be used on a WritableStream");
2425
2531
  }
2532
+ // Helper functions for the WritableStreamDefaultController.
2533
+ function defaultControllerBrandCheckException$2(name) {
2534
+ return new TypeError("WritableStreamDefaultController.prototype." + name + " can only be used on a WritableStreamDefaultController");
2535
+ }
2426
2536
  // Helper functions for the WritableStreamDefaultWriter.
2427
2537
  function defaultWriterBrandCheckException(name) {
2428
2538
  return new TypeError("WritableStreamDefaultWriter.prototype." + name + " can only be used on a WritableStreamDefaultWriter");
@@ -2508,19 +2618,6 @@
2508
2618
  writer._readyPromiseState = 'fulfilled';
2509
2619
  }
2510
2620
 
2511
- function isAbortSignal(value) {
2512
- if (typeof value !== 'object' || value === null) {
2513
- return false;
2514
- }
2515
- try {
2516
- return typeof value.aborted === 'boolean';
2517
- }
2518
- catch (_a) {
2519
- // AbortSignal.prototype.aborted throws if its brand check fails
2520
- return false;
2521
- }
2522
- }
2523
-
2524
2621
  /// <reference lib="dom" />
2525
2622
  var NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2526
2623
 
@@ -2832,7 +2929,7 @@
2832
2929
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2833
2930
  return false;
2834
2931
  }
2835
- return true;
2932
+ return x instanceof ReadableStreamDefaultController;
2836
2933
  }
2837
2934
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2838
2935
  var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2993,6 +3090,12 @@
2993
3090
  }
2994
3091
 
2995
3092
  function ReadableStreamTee(stream, cloneForBranch2) {
3093
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3094
+ return ReadableByteStreamTee(stream);
3095
+ }
3096
+ return ReadableStreamDefaultTee(stream);
3097
+ }
3098
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2996
3099
  var reader = AcquireReadableStreamDefaultReader(stream);
2997
3100
  var reading = false;
2998
3101
  var canceled1 = false;
@@ -3011,24 +3114,24 @@
3011
3114
  }
3012
3115
  reading = true;
3013
3116
  var readRequest = {
3014
- _chunkSteps: function (value) {
3117
+ _chunkSteps: function (chunk) {
3015
3118
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3016
3119
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3017
3120
  // successful synchronously-available reads get ahead of asynchronously-available errors.
3018
3121
  queueMicrotask(function () {
3019
3122
  reading = false;
3020
- var value1 = value;
3021
- var value2 = value;
3123
+ var chunk1 = chunk;
3124
+ var chunk2 = chunk;
3022
3125
  // There is no way to access the cloning code right now in the reference implementation.
3023
3126
  // If we add one then we'll need an implementation for serializable objects.
3024
3127
  // if (!canceled2 && cloneForBranch2) {
3025
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3128
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
3026
3129
  // }
3027
3130
  if (!canceled1) {
3028
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3131
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
3029
3132
  }
3030
3133
  if (!canceled2) {
3031
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3134
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3032
3135
  }
3033
3136
  });
3034
3137
  },
@@ -3085,6 +3188,211 @@
3085
3188
  });
3086
3189
  return [branch1, branch2];
3087
3190
  }
3191
+ function ReadableByteStreamTee(stream) {
3192
+ var reader = AcquireReadableStreamDefaultReader(stream);
3193
+ var reading = false;
3194
+ var canceled1 = false;
3195
+ var canceled2 = false;
3196
+ var reason1;
3197
+ var reason2;
3198
+ var branch1;
3199
+ var branch2;
3200
+ var resolveCancelPromise;
3201
+ var cancelPromise = newPromise(function (resolve) {
3202
+ resolveCancelPromise = resolve;
3203
+ });
3204
+ function forwardReaderError(thisReader) {
3205
+ uponRejection(thisReader._closedPromise, function (r) {
3206
+ if (thisReader !== reader) {
3207
+ return;
3208
+ }
3209
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3210
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3211
+ if (!canceled1 || !canceled2) {
3212
+ resolveCancelPromise(undefined);
3213
+ }
3214
+ });
3215
+ }
3216
+ function pullWithDefaultReader() {
3217
+ if (IsReadableStreamBYOBReader(reader)) {
3218
+ ReadableStreamReaderGenericRelease(reader);
3219
+ reader = AcquireReadableStreamDefaultReader(stream);
3220
+ forwardReaderError(reader);
3221
+ }
3222
+ var readRequest = {
3223
+ _chunkSteps: function (chunk) {
3224
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3225
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3226
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3227
+ queueMicrotask(function () {
3228
+ reading = false;
3229
+ var chunk1 = chunk;
3230
+ var chunk2 = chunk;
3231
+ if (!canceled1 && !canceled2) {
3232
+ try {
3233
+ chunk2 = CloneAsUint8Array(chunk);
3234
+ }
3235
+ catch (cloneE) {
3236
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3237
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3238
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3239
+ return;
3240
+ }
3241
+ }
3242
+ if (!canceled1) {
3243
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3244
+ }
3245
+ if (!canceled2) {
3246
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3247
+ }
3248
+ });
3249
+ },
3250
+ _closeSteps: function () {
3251
+ reading = false;
3252
+ if (!canceled1) {
3253
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3254
+ }
3255
+ if (!canceled2) {
3256
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3257
+ }
3258
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3259
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3260
+ }
3261
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3262
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3263
+ }
3264
+ if (!canceled1 || !canceled2) {
3265
+ resolveCancelPromise(undefined);
3266
+ }
3267
+ },
3268
+ _errorSteps: function () {
3269
+ reading = false;
3270
+ }
3271
+ };
3272
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3273
+ }
3274
+ function pullWithBYOBReader(view, forBranch2) {
3275
+ if (IsReadableStreamDefaultReader(reader)) {
3276
+ ReadableStreamReaderGenericRelease(reader);
3277
+ reader = AcquireReadableStreamBYOBReader(stream);
3278
+ forwardReaderError(reader);
3279
+ }
3280
+ var byobBranch = forBranch2 ? branch2 : branch1;
3281
+ var otherBranch = forBranch2 ? branch1 : branch2;
3282
+ var readIntoRequest = {
3283
+ _chunkSteps: function (chunk) {
3284
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3285
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3286
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3287
+ queueMicrotask(function () {
3288
+ reading = false;
3289
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3290
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3291
+ if (!otherCanceled) {
3292
+ var clonedChunk = void 0;
3293
+ try {
3294
+ clonedChunk = CloneAsUint8Array(chunk);
3295
+ }
3296
+ catch (cloneE) {
3297
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3298
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3299
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3300
+ return;
3301
+ }
3302
+ if (!byobCanceled) {
3303
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3304
+ }
3305
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3306
+ }
3307
+ else if (!byobCanceled) {
3308
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3309
+ }
3310
+ });
3311
+ },
3312
+ _closeSteps: function (chunk) {
3313
+ reading = false;
3314
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3315
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3316
+ if (!byobCanceled) {
3317
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3318
+ }
3319
+ if (!otherCanceled) {
3320
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3321
+ }
3322
+ if (chunk !== undefined) {
3323
+ if (!byobCanceled) {
3324
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3325
+ }
3326
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3327
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3328
+ }
3329
+ }
3330
+ if (!byobCanceled || !otherCanceled) {
3331
+ resolveCancelPromise(undefined);
3332
+ }
3333
+ },
3334
+ _errorSteps: function () {
3335
+ reading = false;
3336
+ }
3337
+ };
3338
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3339
+ }
3340
+ function pull1Algorithm() {
3341
+ if (reading) {
3342
+ return promiseResolvedWith(undefined);
3343
+ }
3344
+ reading = true;
3345
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3346
+ if (byobRequest === null) {
3347
+ pullWithDefaultReader();
3348
+ }
3349
+ else {
3350
+ pullWithBYOBReader(byobRequest._view, false);
3351
+ }
3352
+ return promiseResolvedWith(undefined);
3353
+ }
3354
+ function pull2Algorithm() {
3355
+ if (reading) {
3356
+ return promiseResolvedWith(undefined);
3357
+ }
3358
+ reading = true;
3359
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3360
+ if (byobRequest === null) {
3361
+ pullWithDefaultReader();
3362
+ }
3363
+ else {
3364
+ pullWithBYOBReader(byobRequest._view, true);
3365
+ }
3366
+ return promiseResolvedWith(undefined);
3367
+ }
3368
+ function cancel1Algorithm(reason) {
3369
+ canceled1 = true;
3370
+ reason1 = reason;
3371
+ if (canceled2) {
3372
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3373
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3374
+ resolveCancelPromise(cancelResult);
3375
+ }
3376
+ return cancelPromise;
3377
+ }
3378
+ function cancel2Algorithm(reason) {
3379
+ canceled2 = true;
3380
+ reason2 = reason;
3381
+ if (canceled1) {
3382
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3383
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3384
+ resolveCancelPromise(cancelResult);
3385
+ }
3386
+ return cancelPromise;
3387
+ }
3388
+ function startAlgorithm() {
3389
+ return;
3390
+ }
3391
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3392
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3393
+ forwardReaderError(reader);
3394
+ return [branch1, branch2];
3395
+ }
3088
3396
 
3089
3397
  function convertUnderlyingDefaultOrByteSource(source, context) {
3090
3398
  assertDictionary(source, context);
@@ -3360,6 +3668,14 @@
3360
3668
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3361
3669
  return stream;
3362
3670
  }
3671
+ // Throws if and only if startAlgorithm throws.
3672
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3673
+ var stream = Object.create(ReadableStream.prototype);
3674
+ InitializeReadableStream(stream);
3675
+ var controller = Object.create(ReadableByteStreamController.prototype);
3676
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3677
+ return stream;
3678
+ }
3363
3679
  function InitializeReadableStream(stream) {
3364
3680
  stream._state = 'readable';
3365
3681
  stream._reader = undefined;
@@ -3373,7 +3689,7 @@
3373
3689
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3374
3690
  return false;
3375
3691
  }
3376
- return true;
3692
+ return x instanceof ReadableStream;
3377
3693
  }
3378
3694
  function IsReadableStreamLocked(stream) {
3379
3695
  if (stream._reader === undefined) {
@@ -3391,6 +3707,13 @@
3391
3707
  return promiseRejectedWith(stream._storedError);
3392
3708
  }
3393
3709
  ReadableStreamClose(stream);
3710
+ var reader = stream._reader;
3711
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3712
+ reader._readIntoRequests.forEach(function (readIntoRequest) {
3713
+ readIntoRequest._closeSteps(undefined);
3714
+ });
3715
+ reader._readIntoRequests = new SimpleQueue();
3716
+ }
3394
3717
  var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3395
3718
  return transformPromiseWith(sourceCancelPromise, noop);
3396
3719
  }
@@ -3443,9 +3766,14 @@
3443
3766
  };
3444
3767
  }
3445
3768
 
3446
- var byteLengthSizeFunction = function size(chunk) {
3769
+ // The size function must not have a prototype property nor be a constructor
3770
+ var byteLengthSizeFunction = function (chunk) {
3447
3771
  return chunk.byteLength;
3448
3772
  };
3773
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3774
+ value: 'size',
3775
+ configurable: true
3776
+ });
3449
3777
  /**
3450
3778
  * A queuing strategy that counts the number of bytes in each chunk.
3451
3779
  *
@@ -3506,12 +3834,17 @@
3506
3834
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3507
3835
  return false;
3508
3836
  }
3509
- return true;
3837
+ return x instanceof ByteLengthQueuingStrategy;
3510
3838
  }
3511
3839
 
3512
- var countSizeFunction = function size() {
3840
+ // The size function must not have a prototype property nor be a constructor
3841
+ var countSizeFunction = function () {
3513
3842
  return 1;
3514
3843
  };
3844
+ Object.defineProperty(countSizeFunction, 'name', {
3845
+ value: 'size',
3846
+ configurable: true
3847
+ });
3515
3848
  /**
3516
3849
  * A queuing strategy that counts the number of chunks.
3517
3850
  *
@@ -3573,7 +3906,7 @@
3573
3906
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3574
3907
  return false;
3575
3908
  }
3576
- return true;
3909
+ return x instanceof CountQueuingStrategy;
3577
3910
  }
3578
3911
 
3579
3912
  function convertTransformer(original, context) {
@@ -3727,7 +4060,7 @@
3727
4060
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3728
4061
  return false;
3729
4062
  }
3730
- return true;
4063
+ return x instanceof TransformStream;
3731
4064
  }
3732
4065
  // This is a no-op if both sides are already errored.
3733
4066
  function TransformStreamError(stream, e) {
@@ -3828,7 +4161,7 @@
3828
4161
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3829
4162
  return false;
3830
4163
  }
3831
- return true;
4164
+ return x instanceof TransformStreamDefaultController;
3832
4165
  }
3833
4166
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3834
4167
  controller._controlledTransformStream = stream;