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.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /**
2
- * web-streams-polyfill v3.0.2
2
+ * web-streams-polyfill v3.2.0
3
3
  */
4
4
  (function (global, factory) {
5
5
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
6
6
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
7
- (global = global || self, factory(global.WebStreamsPolyfill = {}));
7
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.WebStreamsPolyfill = {}));
8
8
  }(this, (function (exports) { 'use strict';
9
9
 
10
10
  /// <reference lib="es2015.symbol" />
@@ -14,7 +14,7 @@
14
14
 
15
15
  /// <reference lib="dom" />
16
16
  function noop() {
17
- // do nothing
17
+ return undefined;
18
18
  }
19
19
  function getGlobals() {
20
20
  if (typeof self !== 'undefined') {
@@ -33,7 +33,7 @@
33
33
  function typeIsObject(x) {
34
34
  return (typeof x === 'object' && x !== null) || typeof x === 'function';
35
35
  }
36
- var rethrowAssertionErrorRejection = noop;
36
+ var rethrowAssertionErrorRejection = noop;
37
37
 
38
38
  var originalPromise = Promise;
39
39
  var originalPromiseThen = Promise.prototype.then;
@@ -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,11 +1232,20 @@
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);
1243
1244
  }
1244
1245
  else {
1246
+ if (controller._pendingPullIntos.length > 0) {
1247
+ ReadableByteStreamControllerShiftPendingPullInto(controller);
1248
+ }
1245
1249
  var transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
1246
1250
  ReadableStreamFulfillReadRequest(stream, transferredView, false);
1247
1251
  }
@@ -1266,6 +1270,16 @@
1266
1270
  ReadableByteStreamControllerClearAlgorithms(controller);
1267
1271
  ReadableStreamError(stream, e);
1268
1272
  }
1273
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1274
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1275
+ var firstDescriptor = controller._pendingPullIntos.peek();
1276
+ var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1277
+ var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1278
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1279
+ controller._byobRequest = byobRequest;
1280
+ }
1281
+ return controller._byobRequest;
1282
+ }
1269
1283
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1270
1284
  var state = controller._controlledReadableByteStream._state;
1271
1285
  if (state === 'errored') {
@@ -1277,22 +1291,49 @@
1277
1291
  return controller._strategyHWM - controller._queueTotalSize;
1278
1292
  }
1279
1293
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1280
- bytesWritten = Number(bytesWritten);
1281
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1282
- throw new RangeError('bytesWritten must be a finite');
1294
+ var firstDescriptor = controller._pendingPullIntos.peek();
1295
+ var state = controller._controlledReadableByteStream._state;
1296
+ if (state === 'closed') {
1297
+ if (bytesWritten !== 0) {
1298
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1299
+ }
1300
+ }
1301
+ else {
1302
+ if (bytesWritten === 0) {
1303
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1304
+ }
1305
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1306
+ throw new RangeError('bytesWritten out of range');
1307
+ }
1283
1308
  }
1309
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1284
1310
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1285
1311
  }
1286
1312
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1287
1313
  var firstDescriptor = controller._pendingPullIntos.peek();
1314
+ var state = controller._controlledReadableByteStream._state;
1315
+ if (state === 'closed') {
1316
+ if (view.byteLength !== 0) {
1317
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1318
+ }
1319
+ }
1320
+ else {
1321
+ if (view.byteLength === 0) {
1322
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1323
+ }
1324
+ }
1288
1325
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1289
1326
  throw new RangeError('The region specified by view does not match byobRequest');
1290
1327
  }
1291
- if (firstDescriptor.byteLength !== view.byteLength) {
1328
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1292
1329
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1293
1330
  }
1294
- firstDescriptor.buffer = view.buffer;
1295
- ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1331
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1332
+ throw new RangeError('The region specified by view is larger than byobRequest');
1333
+ }
1334
+ var viewByteLength = view.byteLength;
1335
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1336
+ ReadableByteStreamControllerRespondInternal(controller, viewByteLength);
1296
1337
  }
1297
1338
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
1298
1339
  controller._controlledReadableByteStream = stream;
@@ -1446,6 +1487,7 @@
1446
1487
  if (view.buffer.byteLength === 0) {
1447
1488
  return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
1448
1489
  }
1490
+ if (IsDetachedBuffer(view.buffer)) ;
1449
1491
  if (this._ownerReadableStream === undefined) {
1450
1492
  return promiseRejectedWith(readerLockException('read from'));
1451
1493
  }
@@ -1506,7 +1548,7 @@
1506
1548
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1507
1549
  return false;
1508
1550
  }
1509
- return true;
1551
+ return x instanceof ReadableStreamBYOBReader;
1510
1552
  }
1511
1553
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1512
1554
  var stream = reader._ownerReadableStream;
@@ -1601,6 +1643,31 @@
1601
1643
  }
1602
1644
  }
1603
1645
 
1646
+ function isAbortSignal(value) {
1647
+ if (typeof value !== 'object' || value === null) {
1648
+ return false;
1649
+ }
1650
+ try {
1651
+ return typeof value.aborted === 'boolean';
1652
+ }
1653
+ catch (_a) {
1654
+ // AbortSignal.prototype.aborted throws if its brand check fails
1655
+ return false;
1656
+ }
1657
+ }
1658
+ var supportsAbortController = typeof AbortController === 'function';
1659
+ /**
1660
+ * Construct a new AbortController, if supported by the platform.
1661
+ *
1662
+ * @internal
1663
+ */
1664
+ function createAbortController() {
1665
+ if (supportsAbortController) {
1666
+ return new AbortController();
1667
+ }
1668
+ return undefined;
1669
+ }
1670
+
1604
1671
  /**
1605
1672
  * A writable stream represents a destination for data, into which you can write.
1606
1673
  *
@@ -1633,7 +1700,7 @@
1633
1700
  */
1634
1701
  get: function () {
1635
1702
  if (!IsWritableStream(this)) {
1636
- throw streamBrandCheckException('locked');
1703
+ throw streamBrandCheckException$2('locked');
1637
1704
  }
1638
1705
  return IsWritableStreamLocked(this);
1639
1706
  },
@@ -1652,7 +1719,7 @@
1652
1719
  WritableStream.prototype.abort = function (reason) {
1653
1720
  if (reason === void 0) { reason = undefined; }
1654
1721
  if (!IsWritableStream(this)) {
1655
- return promiseRejectedWith(streamBrandCheckException('abort'));
1722
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1656
1723
  }
1657
1724
  if (IsWritableStreamLocked(this)) {
1658
1725
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
@@ -1669,7 +1736,7 @@
1669
1736
  */
1670
1737
  WritableStream.prototype.close = function () {
1671
1738
  if (!IsWritableStream(this)) {
1672
- return promiseRejectedWith(streamBrandCheckException('close'));
1739
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1673
1740
  }
1674
1741
  if (IsWritableStreamLocked(this)) {
1675
1742
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1689,7 +1756,7 @@
1689
1756
  */
1690
1757
  WritableStream.prototype.getWriter = function () {
1691
1758
  if (!IsWritableStream(this)) {
1692
- throw streamBrandCheckException('getWriter');
1759
+ throw streamBrandCheckException$2('getWriter');
1693
1760
  }
1694
1761
  return AcquireWritableStreamDefaultWriter(this);
1695
1762
  };
@@ -1754,7 +1821,7 @@
1754
1821
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1755
1822
  return false;
1756
1823
  }
1757
- return true;
1824
+ return x instanceof WritableStream;
1758
1825
  }
1759
1826
  function IsWritableStreamLocked(stream) {
1760
1827
  if (stream._writer === undefined) {
@@ -1763,6 +1830,15 @@
1763
1830
  return true;
1764
1831
  }
1765
1832
  function WritableStreamAbort(stream, reason) {
1833
+ var _a;
1834
+ if (stream._state === 'closed' || stream._state === 'errored') {
1835
+ return promiseResolvedWith(undefined);
1836
+ }
1837
+ stream._writableStreamController._abortReason = reason;
1838
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1839
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1840
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1841
+ // Widen the type again by casting to WritableStreamState.
1766
1842
  var state = stream._state;
1767
1843
  if (state === 'closed' || state === 'errored') {
1768
1844
  return promiseResolvedWith(undefined);
@@ -2123,7 +2199,7 @@
2123
2199
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2124
2200
  return false;
2125
2201
  }
2126
- return true;
2202
+ return x instanceof WritableStreamDefaultWriter;
2127
2203
  }
2128
2204
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2129
2205
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2213,6 +2289,42 @@
2213
2289
  function WritableStreamDefaultController() {
2214
2290
  throw new TypeError('Illegal constructor');
2215
2291
  }
2292
+ Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
2293
+ /**
2294
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2295
+ *
2296
+ * @deprecated
2297
+ * This property has been removed from the specification, see https://github.com/whatwg/streams/pull/1177.
2298
+ * Use {@link WritableStreamDefaultController.signal}'s `reason` instead.
2299
+ */
2300
+ get: function () {
2301
+ if (!IsWritableStreamDefaultController(this)) {
2302
+ throw defaultControllerBrandCheckException$2('abortReason');
2303
+ }
2304
+ return this._abortReason;
2305
+ },
2306
+ enumerable: false,
2307
+ configurable: true
2308
+ });
2309
+ Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
2310
+ /**
2311
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2312
+ */
2313
+ get: function () {
2314
+ if (!IsWritableStreamDefaultController(this)) {
2315
+ throw defaultControllerBrandCheckException$2('signal');
2316
+ }
2317
+ if (this._abortController === undefined) {
2318
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2319
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2320
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2321
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2322
+ }
2323
+ return this._abortController.signal;
2324
+ },
2325
+ enumerable: false,
2326
+ configurable: true
2327
+ });
2216
2328
  /**
2217
2329
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2218
2330
  *
@@ -2223,7 +2335,7 @@
2223
2335
  WritableStreamDefaultController.prototype.error = function (e) {
2224
2336
  if (e === void 0) { e = undefined; }
2225
2337
  if (!IsWritableStreamDefaultController(this)) {
2226
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2338
+ throw defaultControllerBrandCheckException$2('error');
2227
2339
  }
2228
2340
  var state = this._controlledWritableStream._state;
2229
2341
  if (state !== 'writable') {
@@ -2246,6 +2358,8 @@
2246
2358
  return WritableStreamDefaultController;
2247
2359
  }());
2248
2360
  Object.defineProperties(WritableStreamDefaultController.prototype, {
2361
+ abortReason: { enumerable: true },
2362
+ signal: { enumerable: true },
2249
2363
  error: { enumerable: true }
2250
2364
  });
2251
2365
  if (typeof SymbolPolyfill.toStringTag === 'symbol') {
@@ -2262,7 +2376,7 @@
2262
2376
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2263
2377
  return false;
2264
2378
  }
2265
- return true;
2379
+ return x instanceof WritableStreamDefaultController;
2266
2380
  }
2267
2381
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2268
2382
  controller._controlledWritableStream = stream;
@@ -2271,6 +2385,8 @@
2271
2385
  controller._queue = undefined;
2272
2386
  controller._queueTotalSize = undefined;
2273
2387
  ResetQueue(controller);
2388
+ controller._abortReason = undefined;
2389
+ controller._abortController = createAbortController();
2274
2390
  controller._started = false;
2275
2391
  controller._strategySizeAlgorithm = sizeAlgorithm;
2276
2392
  controller._strategyHWM = highWaterMark;
@@ -2420,9 +2536,13 @@
2420
2536
  WritableStreamStartErroring(stream, error);
2421
2537
  }
2422
2538
  // Helper functions for the WritableStream.
2423
- function streamBrandCheckException(name) {
2539
+ function streamBrandCheckException$2(name) {
2424
2540
  return new TypeError("WritableStream.prototype." + name + " can only be used on a WritableStream");
2425
2541
  }
2542
+ // Helper functions for the WritableStreamDefaultController.
2543
+ function defaultControllerBrandCheckException$2(name) {
2544
+ return new TypeError("WritableStreamDefaultController.prototype." + name + " can only be used on a WritableStreamDefaultController");
2545
+ }
2426
2546
  // Helper functions for the WritableStreamDefaultWriter.
2427
2547
  function defaultWriterBrandCheckException(name) {
2428
2548
  return new TypeError("WritableStreamDefaultWriter.prototype." + name + " can only be used on a WritableStreamDefaultWriter");
@@ -2508,19 +2628,6 @@
2508
2628
  writer._readyPromiseState = 'fulfilled';
2509
2629
  }
2510
2630
 
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
2631
  /// <reference lib="dom" />
2525
2632
  var NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2526
2633
 
@@ -2538,6 +2645,7 @@
2538
2645
  }
2539
2646
  }
2540
2647
  function createDOMExceptionPolyfill() {
2648
+ // eslint-disable-next-line no-shadow
2541
2649
  var ctor = function DOMException(message, name) {
2542
2650
  this.message = message || '';
2543
2651
  this.name = name || 'Error';
@@ -2549,6 +2657,7 @@
2549
2657
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2550
2658
  return ctor;
2551
2659
  }
2660
+ // eslint-disable-next-line no-redeclare
2552
2661
  var DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2553
2662
 
2554
2663
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2742,7 +2851,7 @@
2742
2851
  */
2743
2852
  get: function () {
2744
2853
  if (!IsReadableStreamDefaultController(this)) {
2745
- throw defaultControllerBrandCheckException('desiredSize');
2854
+ throw defaultControllerBrandCheckException$1('desiredSize');
2746
2855
  }
2747
2856
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2748
2857
  },
@@ -2755,7 +2864,7 @@
2755
2864
  */
2756
2865
  ReadableStreamDefaultController.prototype.close = function () {
2757
2866
  if (!IsReadableStreamDefaultController(this)) {
2758
- throw defaultControllerBrandCheckException('close');
2867
+ throw defaultControllerBrandCheckException$1('close');
2759
2868
  }
2760
2869
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2761
2870
  throw new TypeError('The stream is not in a state that permits close');
@@ -2765,7 +2874,7 @@
2765
2874
  ReadableStreamDefaultController.prototype.enqueue = function (chunk) {
2766
2875
  if (chunk === void 0) { chunk = undefined; }
2767
2876
  if (!IsReadableStreamDefaultController(this)) {
2768
- throw defaultControllerBrandCheckException('enqueue');
2877
+ throw defaultControllerBrandCheckException$1('enqueue');
2769
2878
  }
2770
2879
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2771
2880
  throw new TypeError('The stream is not in a state that permits enqueue');
@@ -2778,7 +2887,7 @@
2778
2887
  ReadableStreamDefaultController.prototype.error = function (e) {
2779
2888
  if (e === void 0) { e = undefined; }
2780
2889
  if (!IsReadableStreamDefaultController(this)) {
2781
- throw defaultControllerBrandCheckException('error');
2890
+ throw defaultControllerBrandCheckException$1('error');
2782
2891
  }
2783
2892
  ReadableStreamDefaultControllerError(this, e);
2784
2893
  };
@@ -2830,7 +2939,7 @@
2830
2939
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2831
2940
  return false;
2832
2941
  }
2833
- return true;
2942
+ return x instanceof ReadableStreamDefaultController;
2834
2943
  }
2835
2944
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2836
2945
  var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2986,13 +3095,20 @@
2986
3095
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2987
3096
  }
2988
3097
  // Helper functions for the ReadableStreamDefaultController.
2989
- function defaultControllerBrandCheckException(name) {
3098
+ function defaultControllerBrandCheckException$1(name) {
2990
3099
  return new TypeError("ReadableStreamDefaultController.prototype." + name + " can only be used on a ReadableStreamDefaultController");
2991
3100
  }
2992
3101
 
2993
3102
  function ReadableStreamTee(stream, cloneForBranch2) {
3103
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3104
+ return ReadableByteStreamTee(stream);
3105
+ }
3106
+ return ReadableStreamDefaultTee(stream);
3107
+ }
3108
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2994
3109
  var reader = AcquireReadableStreamDefaultReader(stream);
2995
3110
  var reading = false;
3111
+ var readAgain = false;
2996
3112
  var canceled1 = false;
2997
3113
  var canceled2 = false;
2998
3114
  var reason1;
@@ -3005,30 +3121,34 @@
3005
3121
  });
3006
3122
  function pullAlgorithm() {
3007
3123
  if (reading) {
3124
+ readAgain = true;
3008
3125
  return promiseResolvedWith(undefined);
3009
3126
  }
3010
3127
  reading = true;
3011
3128
  var readRequest = {
3012
- _chunkSteps: function (value) {
3129
+ _chunkSteps: function (chunk) {
3013
3130
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3014
3131
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3015
3132
  // successful synchronously-available reads get ahead of asynchronously-available errors.
3016
3133
  queueMicrotask(function () {
3017
- reading = false;
3018
- var value1 = value;
3019
- var value2 = value;
3134
+ readAgain = false;
3135
+ var chunk1 = chunk;
3136
+ var chunk2 = chunk;
3020
3137
  // There is no way to access the cloning code right now in the reference implementation.
3021
3138
  // If we add one then we'll need an implementation for serializable objects.
3022
3139
  // if (!canceled2 && cloneForBranch2) {
3023
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3140
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
3024
3141
  // }
3025
3142
  if (!canceled1) {
3026
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3143
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
3027
3144
  }
3028
3145
  if (!canceled2) {
3029
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3146
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3147
+ }
3148
+ reading = false;
3149
+ if (readAgain) {
3150
+ pullAlgorithm();
3030
3151
  }
3031
- resolveCancelPromise(undefined);
3032
3152
  });
3033
3153
  },
3034
3154
  _closeSteps: function () {
@@ -3039,6 +3159,9 @@
3039
3159
  if (!canceled2) {
3040
3160
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
3041
3161
  }
3162
+ if (!canceled1 || !canceled2) {
3163
+ resolveCancelPromise(undefined);
3164
+ }
3042
3165
  },
3043
3166
  _errorSteps: function () {
3044
3167
  reading = false;
@@ -3075,10 +3198,237 @@
3075
3198
  uponRejection(reader._closedPromise, function (r) {
3076
3199
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
3077
3200
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3078
- resolveCancelPromise(undefined);
3201
+ if (!canceled1 || !canceled2) {
3202
+ resolveCancelPromise(undefined);
3203
+ }
3079
3204
  });
3080
3205
  return [branch1, branch2];
3081
3206
  }
3207
+ function ReadableByteStreamTee(stream) {
3208
+ var reader = AcquireReadableStreamDefaultReader(stream);
3209
+ var reading = false;
3210
+ var readAgainForBranch1 = false;
3211
+ var readAgainForBranch2 = false;
3212
+ var canceled1 = false;
3213
+ var canceled2 = false;
3214
+ var reason1;
3215
+ var reason2;
3216
+ var branch1;
3217
+ var branch2;
3218
+ var resolveCancelPromise;
3219
+ var cancelPromise = newPromise(function (resolve) {
3220
+ resolveCancelPromise = resolve;
3221
+ });
3222
+ function forwardReaderError(thisReader) {
3223
+ uponRejection(thisReader._closedPromise, function (r) {
3224
+ if (thisReader !== reader) {
3225
+ return;
3226
+ }
3227
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3228
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3229
+ if (!canceled1 || !canceled2) {
3230
+ resolveCancelPromise(undefined);
3231
+ }
3232
+ });
3233
+ }
3234
+ function pullWithDefaultReader() {
3235
+ if (IsReadableStreamBYOBReader(reader)) {
3236
+ ReadableStreamReaderGenericRelease(reader);
3237
+ reader = AcquireReadableStreamDefaultReader(stream);
3238
+ forwardReaderError(reader);
3239
+ }
3240
+ var readRequest = {
3241
+ _chunkSteps: function (chunk) {
3242
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3243
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3244
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3245
+ queueMicrotask(function () {
3246
+ readAgainForBranch1 = false;
3247
+ readAgainForBranch2 = false;
3248
+ var chunk1 = chunk;
3249
+ var chunk2 = chunk;
3250
+ if (!canceled1 && !canceled2) {
3251
+ try {
3252
+ chunk2 = CloneAsUint8Array(chunk);
3253
+ }
3254
+ catch (cloneE) {
3255
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3256
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3257
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3258
+ return;
3259
+ }
3260
+ }
3261
+ if (!canceled1) {
3262
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3263
+ }
3264
+ if (!canceled2) {
3265
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3266
+ }
3267
+ reading = false;
3268
+ if (readAgainForBranch1) {
3269
+ pull1Algorithm();
3270
+ }
3271
+ else if (readAgainForBranch2) {
3272
+ pull2Algorithm();
3273
+ }
3274
+ });
3275
+ },
3276
+ _closeSteps: function () {
3277
+ reading = false;
3278
+ if (!canceled1) {
3279
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3280
+ }
3281
+ if (!canceled2) {
3282
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3283
+ }
3284
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3285
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3286
+ }
3287
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3288
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3289
+ }
3290
+ if (!canceled1 || !canceled2) {
3291
+ resolveCancelPromise(undefined);
3292
+ }
3293
+ },
3294
+ _errorSteps: function () {
3295
+ reading = false;
3296
+ }
3297
+ };
3298
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3299
+ }
3300
+ function pullWithBYOBReader(view, forBranch2) {
3301
+ if (IsReadableStreamDefaultReader(reader)) {
3302
+ ReadableStreamReaderGenericRelease(reader);
3303
+ reader = AcquireReadableStreamBYOBReader(stream);
3304
+ forwardReaderError(reader);
3305
+ }
3306
+ var byobBranch = forBranch2 ? branch2 : branch1;
3307
+ var otherBranch = forBranch2 ? branch1 : branch2;
3308
+ var readIntoRequest = {
3309
+ _chunkSteps: function (chunk) {
3310
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3311
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3312
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3313
+ queueMicrotask(function () {
3314
+ readAgainForBranch1 = false;
3315
+ readAgainForBranch2 = false;
3316
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3317
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3318
+ if (!otherCanceled) {
3319
+ var clonedChunk = void 0;
3320
+ try {
3321
+ clonedChunk = CloneAsUint8Array(chunk);
3322
+ }
3323
+ catch (cloneE) {
3324
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3325
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3326
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3327
+ return;
3328
+ }
3329
+ if (!byobCanceled) {
3330
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3331
+ }
3332
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3333
+ }
3334
+ else if (!byobCanceled) {
3335
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3336
+ }
3337
+ reading = false;
3338
+ if (readAgainForBranch1) {
3339
+ pull1Algorithm();
3340
+ }
3341
+ else if (readAgainForBranch2) {
3342
+ pull2Algorithm();
3343
+ }
3344
+ });
3345
+ },
3346
+ _closeSteps: function (chunk) {
3347
+ reading = false;
3348
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3349
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3350
+ if (!byobCanceled) {
3351
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3352
+ }
3353
+ if (!otherCanceled) {
3354
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3355
+ }
3356
+ if (chunk !== undefined) {
3357
+ if (!byobCanceled) {
3358
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3359
+ }
3360
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3361
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3362
+ }
3363
+ }
3364
+ if (!byobCanceled || !otherCanceled) {
3365
+ resolveCancelPromise(undefined);
3366
+ }
3367
+ },
3368
+ _errorSteps: function () {
3369
+ reading = false;
3370
+ }
3371
+ };
3372
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3373
+ }
3374
+ function pull1Algorithm() {
3375
+ if (reading) {
3376
+ readAgainForBranch1 = true;
3377
+ return promiseResolvedWith(undefined);
3378
+ }
3379
+ reading = true;
3380
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3381
+ if (byobRequest === null) {
3382
+ pullWithDefaultReader();
3383
+ }
3384
+ else {
3385
+ pullWithBYOBReader(byobRequest._view, false);
3386
+ }
3387
+ return promiseResolvedWith(undefined);
3388
+ }
3389
+ function pull2Algorithm() {
3390
+ if (reading) {
3391
+ readAgainForBranch2 = true;
3392
+ return promiseResolvedWith(undefined);
3393
+ }
3394
+ reading = true;
3395
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3396
+ if (byobRequest === null) {
3397
+ pullWithDefaultReader();
3398
+ }
3399
+ else {
3400
+ pullWithBYOBReader(byobRequest._view, true);
3401
+ }
3402
+ return promiseResolvedWith(undefined);
3403
+ }
3404
+ function cancel1Algorithm(reason) {
3405
+ canceled1 = true;
3406
+ reason1 = reason;
3407
+ if (canceled2) {
3408
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3409
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3410
+ resolveCancelPromise(cancelResult);
3411
+ }
3412
+ return cancelPromise;
3413
+ }
3414
+ function cancel2Algorithm(reason) {
3415
+ canceled2 = true;
3416
+ reason2 = reason;
3417
+ if (canceled1) {
3418
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3419
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3420
+ resolveCancelPromise(cancelResult);
3421
+ }
3422
+ return cancelPromise;
3423
+ }
3424
+ function startAlgorithm() {
3425
+ return;
3426
+ }
3427
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3428
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3429
+ forwardReaderError(reader);
3430
+ return [branch1, branch2];
3431
+ }
3082
3432
 
3083
3433
  function convertUnderlyingDefaultOrByteSource(source, context) {
3084
3434
  assertDictionary(source, context);
@@ -3354,6 +3704,14 @@
3354
3704
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3355
3705
  return stream;
3356
3706
  }
3707
+ // Throws if and only if startAlgorithm throws.
3708
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3709
+ var stream = Object.create(ReadableStream.prototype);
3710
+ InitializeReadableStream(stream);
3711
+ var controller = Object.create(ReadableByteStreamController.prototype);
3712
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3713
+ return stream;
3714
+ }
3357
3715
  function InitializeReadableStream(stream) {
3358
3716
  stream._state = 'readable';
3359
3717
  stream._reader = undefined;
@@ -3367,7 +3725,7 @@
3367
3725
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3368
3726
  return false;
3369
3727
  }
3370
- return true;
3728
+ return x instanceof ReadableStream;
3371
3729
  }
3372
3730
  function IsReadableStreamLocked(stream) {
3373
3731
  if (stream._reader === undefined) {
@@ -3385,6 +3743,13 @@
3385
3743
  return promiseRejectedWith(stream._storedError);
3386
3744
  }
3387
3745
  ReadableStreamClose(stream);
3746
+ var reader = stream._reader;
3747
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3748
+ reader._readIntoRequests.forEach(function (readIntoRequest) {
3749
+ readIntoRequest._closeSteps(undefined);
3750
+ });
3751
+ reader._readIntoRequests = new SimpleQueue();
3752
+ }
3388
3753
  var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3389
3754
  return transformPromiseWith(sourceCancelPromise, noop);
3390
3755
  }
@@ -3437,9 +3802,14 @@
3437
3802
  };
3438
3803
  }
3439
3804
 
3440
- var byteLengthSizeFunction = function size(chunk) {
3805
+ // The size function must not have a prototype property nor be a constructor
3806
+ var byteLengthSizeFunction = function (chunk) {
3441
3807
  return chunk.byteLength;
3442
3808
  };
3809
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3810
+ value: 'size',
3811
+ configurable: true
3812
+ });
3443
3813
  /**
3444
3814
  * A queuing strategy that counts the number of bytes in each chunk.
3445
3815
  *
@@ -3500,12 +3870,17 @@
3500
3870
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3501
3871
  return false;
3502
3872
  }
3503
- return true;
3873
+ return x instanceof ByteLengthQueuingStrategy;
3504
3874
  }
3505
3875
 
3506
- var countSizeFunction = function size() {
3876
+ // The size function must not have a prototype property nor be a constructor
3877
+ var countSizeFunction = function () {
3507
3878
  return 1;
3508
3879
  };
3880
+ Object.defineProperty(countSizeFunction, 'name', {
3881
+ value: 'size',
3882
+ configurable: true
3883
+ });
3509
3884
  /**
3510
3885
  * A queuing strategy that counts the number of chunks.
3511
3886
  *
@@ -3567,7 +3942,7 @@
3567
3942
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3568
3943
  return false;
3569
3944
  }
3570
- return true;
3945
+ return x instanceof CountQueuingStrategy;
3571
3946
  }
3572
3947
 
3573
3948
  function convertTransformer(original, context) {
@@ -3653,7 +4028,7 @@
3653
4028
  */
3654
4029
  get: function () {
3655
4030
  if (!IsTransformStream(this)) {
3656
- throw streamBrandCheckException$2('readable');
4031
+ throw streamBrandCheckException('readable');
3657
4032
  }
3658
4033
  return this._readable;
3659
4034
  },
@@ -3666,7 +4041,7 @@
3666
4041
  */
3667
4042
  get: function () {
3668
4043
  if (!IsTransformStream(this)) {
3669
- throw streamBrandCheckException$2('writable');
4044
+ throw streamBrandCheckException('writable');
3670
4045
  }
3671
4046
  return this._writable;
3672
4047
  },
@@ -3721,7 +4096,7 @@
3721
4096
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3722
4097
  return false;
3723
4098
  }
3724
- return true;
4099
+ return x instanceof TransformStream;
3725
4100
  }
3726
4101
  // This is a no-op if both sides are already errored.
3727
4102
  function TransformStreamError(stream, e) {
@@ -3764,7 +4139,7 @@
3764
4139
  */
3765
4140
  get: function () {
3766
4141
  if (!IsTransformStreamDefaultController(this)) {
3767
- throw defaultControllerBrandCheckException$1('desiredSize');
4142
+ throw defaultControllerBrandCheckException('desiredSize');
3768
4143
  }
3769
4144
  var readableController = this._controlledTransformStream._readable._readableStreamController;
3770
4145
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
@@ -3775,7 +4150,7 @@
3775
4150
  TransformStreamDefaultController.prototype.enqueue = function (chunk) {
3776
4151
  if (chunk === void 0) { chunk = undefined; }
3777
4152
  if (!IsTransformStreamDefaultController(this)) {
3778
- throw defaultControllerBrandCheckException$1('enqueue');
4153
+ throw defaultControllerBrandCheckException('enqueue');
3779
4154
  }
3780
4155
  TransformStreamDefaultControllerEnqueue(this, chunk);
3781
4156
  };
@@ -3786,7 +4161,7 @@
3786
4161
  TransformStreamDefaultController.prototype.error = function (reason) {
3787
4162
  if (reason === void 0) { reason = undefined; }
3788
4163
  if (!IsTransformStreamDefaultController(this)) {
3789
- throw defaultControllerBrandCheckException$1('error');
4164
+ throw defaultControllerBrandCheckException('error');
3790
4165
  }
3791
4166
  TransformStreamDefaultControllerError(this, reason);
3792
4167
  };
@@ -3796,7 +4171,7 @@
3796
4171
  */
3797
4172
  TransformStreamDefaultController.prototype.terminate = function () {
3798
4173
  if (!IsTransformStreamDefaultController(this)) {
3799
- throw defaultControllerBrandCheckException$1('terminate');
4174
+ throw defaultControllerBrandCheckException('terminate');
3800
4175
  }
3801
4176
  TransformStreamDefaultControllerTerminate(this);
3802
4177
  };
@@ -3822,7 +4197,7 @@
3822
4197
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3823
4198
  return false;
3824
4199
  }
3825
- return true;
4200
+ return x instanceof TransformStreamDefaultController;
3826
4201
  }
3827
4202
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3828
4203
  controller._controlledTransformStream = stream;
@@ -3939,11 +4314,11 @@
3939
4314
  return stream._backpressureChangePromise;
3940
4315
  }
3941
4316
  // Helper functions for the TransformStreamDefaultController.
3942
- function defaultControllerBrandCheckException$1(name) {
4317
+ function defaultControllerBrandCheckException(name) {
3943
4318
  return new TypeError("TransformStreamDefaultController.prototype." + name + " can only be used on a TransformStreamDefaultController");
3944
4319
  }
3945
4320
  // Helper functions for the TransformStream.
3946
- function streamBrandCheckException$2(name) {
4321
+ function streamBrandCheckException(name) {
3947
4322
  return new TypeError("TransformStream.prototype." + name + " can only be used on a TransformStream");
3948
4323
  }
3949
4324