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/ponyfill.mjs 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
  /// <reference lib="es2015.symbol" />
5
5
  var SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
@@ -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,6 +1226,12 @@ 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);
@@ -1260,6 +1261,16 @@ function ReadableByteStreamControllerError(controller, e) {
1260
1261
  ReadableByteStreamControllerClearAlgorithms(controller);
1261
1262
  ReadableStreamError(stream, e);
1262
1263
  }
1264
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1265
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1266
+ var firstDescriptor = controller._pendingPullIntos.peek();
1267
+ var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1268
+ var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1269
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1270
+ controller._byobRequest = byobRequest;
1271
+ }
1272
+ return controller._byobRequest;
1273
+ }
1263
1274
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1264
1275
  var state = controller._controlledReadableByteStream._state;
1265
1276
  if (state === 'errored') {
@@ -1271,21 +1282,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1271
1282
  return controller._strategyHWM - controller._queueTotalSize;
1272
1283
  }
1273
1284
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1274
- bytesWritten = Number(bytesWritten);
1275
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1276
- throw new RangeError('bytesWritten must be a finite');
1285
+ var firstDescriptor = controller._pendingPullIntos.peek();
1286
+ var state = controller._controlledReadableByteStream._state;
1287
+ if (state === 'closed') {
1288
+ if (bytesWritten !== 0) {
1289
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1290
+ }
1291
+ }
1292
+ else {
1293
+ if (bytesWritten === 0) {
1294
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1295
+ }
1296
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1297
+ throw new RangeError('bytesWritten out of range');
1298
+ }
1277
1299
  }
1300
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1278
1301
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1279
1302
  }
1280
1303
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1281
1304
  var firstDescriptor = controller._pendingPullIntos.peek();
1305
+ var state = controller._controlledReadableByteStream._state;
1306
+ if (state === 'closed') {
1307
+ if (view.byteLength !== 0) {
1308
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1309
+ }
1310
+ }
1311
+ else {
1312
+ if (view.byteLength === 0) {
1313
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1314
+ }
1315
+ }
1282
1316
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1283
1317
  throw new RangeError('The region specified by view does not match byobRequest');
1284
1318
  }
1285
- if (firstDescriptor.byteLength !== view.byteLength) {
1319
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1286
1320
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1287
1321
  }
1288
- firstDescriptor.buffer = view.buffer;
1322
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1323
+ throw new RangeError('The region specified by view is larger than byobRequest');
1324
+ }
1325
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1289
1326
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1290
1327
  }
1291
1328
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1440,6 +1477,7 @@ var ReadableStreamBYOBReader = /** @class */ (function () {
1440
1477
  if (view.buffer.byteLength === 0) {
1441
1478
  return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
1442
1479
  }
1480
+ if (IsDetachedBuffer(view.buffer)) ;
1443
1481
  if (this._ownerReadableStream === undefined) {
1444
1482
  return promiseRejectedWith(readerLockException('read from'));
1445
1483
  }
@@ -1500,7 +1538,7 @@ function IsReadableStreamBYOBReader(x) {
1500
1538
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1501
1539
  return false;
1502
1540
  }
1503
- return true;
1541
+ return x instanceof ReadableStreamBYOBReader;
1504
1542
  }
1505
1543
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1506
1544
  var stream = reader._ownerReadableStream;
@@ -1595,6 +1633,31 @@ function assertWritableStream(x, context) {
1595
1633
  }
1596
1634
  }
1597
1635
 
1636
+ function isAbortSignal(value) {
1637
+ if (typeof value !== 'object' || value === null) {
1638
+ return false;
1639
+ }
1640
+ try {
1641
+ return typeof value.aborted === 'boolean';
1642
+ }
1643
+ catch (_a) {
1644
+ // AbortSignal.prototype.aborted throws if its brand check fails
1645
+ return false;
1646
+ }
1647
+ }
1648
+ var supportsAbortController = typeof AbortController === 'function';
1649
+ /**
1650
+ * Construct a new AbortController, if supported by the platform.
1651
+ *
1652
+ * @internal
1653
+ */
1654
+ function createAbortController() {
1655
+ if (supportsAbortController) {
1656
+ return new AbortController();
1657
+ }
1658
+ return undefined;
1659
+ }
1660
+
1598
1661
  /**
1599
1662
  * A writable stream represents a destination for data, into which you can write.
1600
1663
  *
@@ -1748,7 +1811,7 @@ function IsWritableStream(x) {
1748
1811
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1749
1812
  return false;
1750
1813
  }
1751
- return true;
1814
+ return x instanceof WritableStream;
1752
1815
  }
1753
1816
  function IsWritableStreamLocked(stream) {
1754
1817
  if (stream._writer === undefined) {
@@ -1757,6 +1820,15 @@ function IsWritableStreamLocked(stream) {
1757
1820
  return true;
1758
1821
  }
1759
1822
  function WritableStreamAbort(stream, reason) {
1823
+ var _a;
1824
+ if (stream._state === 'closed' || stream._state === 'errored') {
1825
+ return promiseResolvedWith(undefined);
1826
+ }
1827
+ stream._writableStreamController._abortReason = reason;
1828
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1829
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1830
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1831
+ // Widen the type again by casting to WritableStreamState.
1760
1832
  var state = stream._state;
1761
1833
  if (state === 'closed' || state === 'errored') {
1762
1834
  return promiseResolvedWith(undefined);
@@ -2117,7 +2189,7 @@ function IsWritableStreamDefaultWriter(x) {
2117
2189
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2118
2190
  return false;
2119
2191
  }
2120
- return true;
2192
+ return x instanceof WritableStreamDefaultWriter;
2121
2193
  }
2122
2194
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2123
2195
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2207,6 +2279,38 @@ var WritableStreamDefaultController = /** @class */ (function () {
2207
2279
  function WritableStreamDefaultController() {
2208
2280
  throw new TypeError('Illegal constructor');
2209
2281
  }
2282
+ Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
2283
+ /**
2284
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2285
+ */
2286
+ get: function () {
2287
+ if (!IsWritableStreamDefaultController(this)) {
2288
+ throw defaultControllerBrandCheckException$2('abortReason');
2289
+ }
2290
+ return this._abortReason;
2291
+ },
2292
+ enumerable: false,
2293
+ configurable: true
2294
+ });
2295
+ Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
2296
+ /**
2297
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2298
+ */
2299
+ get: function () {
2300
+ if (!IsWritableStreamDefaultController(this)) {
2301
+ throw defaultControllerBrandCheckException$2('signal');
2302
+ }
2303
+ if (this._abortController === undefined) {
2304
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2305
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2306
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2307
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2308
+ }
2309
+ return this._abortController.signal;
2310
+ },
2311
+ enumerable: false,
2312
+ configurable: true
2313
+ });
2210
2314
  /**
2211
2315
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2212
2316
  *
@@ -2217,7 +2321,7 @@ var WritableStreamDefaultController = /** @class */ (function () {
2217
2321
  WritableStreamDefaultController.prototype.error = function (e) {
2218
2322
  if (e === void 0) { e = undefined; }
2219
2323
  if (!IsWritableStreamDefaultController(this)) {
2220
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2324
+ throw defaultControllerBrandCheckException$2('error');
2221
2325
  }
2222
2326
  var state = this._controlledWritableStream._state;
2223
2327
  if (state !== 'writable') {
@@ -2256,7 +2360,7 @@ function IsWritableStreamDefaultController(x) {
2256
2360
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2257
2361
  return false;
2258
2362
  }
2259
- return true;
2363
+ return x instanceof WritableStreamDefaultController;
2260
2364
  }
2261
2365
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2262
2366
  controller._controlledWritableStream = stream;
@@ -2265,6 +2369,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2265
2369
  controller._queue = undefined;
2266
2370
  controller._queueTotalSize = undefined;
2267
2371
  ResetQueue(controller);
2372
+ controller._abortReason = undefined;
2373
+ controller._abortController = createAbortController();
2268
2374
  controller._started = false;
2269
2375
  controller._strategySizeAlgorithm = sizeAlgorithm;
2270
2376
  controller._strategyHWM = highWaterMark;
@@ -2417,6 +2523,10 @@ function WritableStreamDefaultControllerError(controller, error) {
2417
2523
  function streamBrandCheckException$2(name) {
2418
2524
  return new TypeError("WritableStream.prototype." + name + " can only be used on a WritableStream");
2419
2525
  }
2526
+ // Helper functions for the WritableStreamDefaultController.
2527
+ function defaultControllerBrandCheckException$2(name) {
2528
+ return new TypeError("WritableStreamDefaultController.prototype." + name + " can only be used on a WritableStreamDefaultController");
2529
+ }
2420
2530
  // Helper functions for the WritableStreamDefaultWriter.
2421
2531
  function defaultWriterBrandCheckException(name) {
2422
2532
  return new TypeError("WritableStreamDefaultWriter.prototype." + name + " can only be used on a WritableStreamDefaultWriter");
@@ -2502,19 +2612,6 @@ function defaultWriterReadyPromiseResolve(writer) {
2502
2612
  writer._readyPromiseState = 'fulfilled';
2503
2613
  }
2504
2614
 
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
2615
  /// <reference lib="dom" />
2519
2616
  var NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2520
2617
 
@@ -2826,7 +2923,7 @@ function IsReadableStreamDefaultController(x) {
2826
2923
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2827
2924
  return false;
2828
2925
  }
2829
- return true;
2926
+ return x instanceof ReadableStreamDefaultController;
2830
2927
  }
2831
2928
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2832
2929
  var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2987,6 +3084,12 @@ function defaultControllerBrandCheckException$1(name) {
2987
3084
  }
2988
3085
 
2989
3086
  function ReadableStreamTee(stream, cloneForBranch2) {
3087
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3088
+ return ReadableByteStreamTee(stream);
3089
+ }
3090
+ return ReadableStreamDefaultTee(stream);
3091
+ }
3092
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2990
3093
  var reader = AcquireReadableStreamDefaultReader(stream);
2991
3094
  var reading = false;
2992
3095
  var canceled1 = false;
@@ -3005,24 +3108,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
3005
3108
  }
3006
3109
  reading = true;
3007
3110
  var readRequest = {
3008
- _chunkSteps: function (value) {
3111
+ _chunkSteps: function (chunk) {
3009
3112
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3010
3113
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3011
3114
  // successful synchronously-available reads get ahead of asynchronously-available errors.
3012
3115
  queueMicrotask(function () {
3013
3116
  reading = false;
3014
- var value1 = value;
3015
- var value2 = value;
3117
+ var chunk1 = chunk;
3118
+ var chunk2 = chunk;
3016
3119
  // There is no way to access the cloning code right now in the reference implementation.
3017
3120
  // If we add one then we'll need an implementation for serializable objects.
3018
3121
  // if (!canceled2 && cloneForBranch2) {
3019
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3122
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
3020
3123
  // }
3021
3124
  if (!canceled1) {
3022
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3125
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
3023
3126
  }
3024
3127
  if (!canceled2) {
3025
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3128
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3026
3129
  }
3027
3130
  });
3028
3131
  },
@@ -3079,6 +3182,211 @@ function ReadableStreamTee(stream, cloneForBranch2) {
3079
3182
  });
3080
3183
  return [branch1, branch2];
3081
3184
  }
3185
+ function ReadableByteStreamTee(stream) {
3186
+ var reader = AcquireReadableStreamDefaultReader(stream);
3187
+ var reading = false;
3188
+ var canceled1 = false;
3189
+ var canceled2 = false;
3190
+ var reason1;
3191
+ var reason2;
3192
+ var branch1;
3193
+ var branch2;
3194
+ var resolveCancelPromise;
3195
+ var cancelPromise = newPromise(function (resolve) {
3196
+ resolveCancelPromise = resolve;
3197
+ });
3198
+ function forwardReaderError(thisReader) {
3199
+ uponRejection(thisReader._closedPromise, function (r) {
3200
+ if (thisReader !== reader) {
3201
+ return;
3202
+ }
3203
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3204
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3205
+ if (!canceled1 || !canceled2) {
3206
+ resolveCancelPromise(undefined);
3207
+ }
3208
+ });
3209
+ }
3210
+ function pullWithDefaultReader() {
3211
+ if (IsReadableStreamBYOBReader(reader)) {
3212
+ ReadableStreamReaderGenericRelease(reader);
3213
+ reader = AcquireReadableStreamDefaultReader(stream);
3214
+ forwardReaderError(reader);
3215
+ }
3216
+ var readRequest = {
3217
+ _chunkSteps: function (chunk) {
3218
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3219
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3220
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3221
+ queueMicrotask(function () {
3222
+ reading = false;
3223
+ var chunk1 = chunk;
3224
+ var chunk2 = chunk;
3225
+ if (!canceled1 && !canceled2) {
3226
+ try {
3227
+ chunk2 = CloneAsUint8Array(chunk);
3228
+ }
3229
+ catch (cloneE) {
3230
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3231
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3232
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3233
+ return;
3234
+ }
3235
+ }
3236
+ if (!canceled1) {
3237
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3238
+ }
3239
+ if (!canceled2) {
3240
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3241
+ }
3242
+ });
3243
+ },
3244
+ _closeSteps: function () {
3245
+ reading = false;
3246
+ if (!canceled1) {
3247
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3248
+ }
3249
+ if (!canceled2) {
3250
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3251
+ }
3252
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3253
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3254
+ }
3255
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3256
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3257
+ }
3258
+ if (!canceled1 || !canceled2) {
3259
+ resolveCancelPromise(undefined);
3260
+ }
3261
+ },
3262
+ _errorSteps: function () {
3263
+ reading = false;
3264
+ }
3265
+ };
3266
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3267
+ }
3268
+ function pullWithBYOBReader(view, forBranch2) {
3269
+ if (IsReadableStreamDefaultReader(reader)) {
3270
+ ReadableStreamReaderGenericRelease(reader);
3271
+ reader = AcquireReadableStreamBYOBReader(stream);
3272
+ forwardReaderError(reader);
3273
+ }
3274
+ var byobBranch = forBranch2 ? branch2 : branch1;
3275
+ var otherBranch = forBranch2 ? branch1 : branch2;
3276
+ var readIntoRequest = {
3277
+ _chunkSteps: function (chunk) {
3278
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3279
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3280
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3281
+ queueMicrotask(function () {
3282
+ reading = false;
3283
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3284
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3285
+ if (!otherCanceled) {
3286
+ var clonedChunk = void 0;
3287
+ try {
3288
+ clonedChunk = CloneAsUint8Array(chunk);
3289
+ }
3290
+ catch (cloneE) {
3291
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3292
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3293
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3294
+ return;
3295
+ }
3296
+ if (!byobCanceled) {
3297
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3298
+ }
3299
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3300
+ }
3301
+ else if (!byobCanceled) {
3302
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3303
+ }
3304
+ });
3305
+ },
3306
+ _closeSteps: function (chunk) {
3307
+ reading = false;
3308
+ var byobCanceled = forBranch2 ? canceled2 : canceled1;
3309
+ var otherCanceled = forBranch2 ? canceled1 : canceled2;
3310
+ if (!byobCanceled) {
3311
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3312
+ }
3313
+ if (!otherCanceled) {
3314
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3315
+ }
3316
+ if (chunk !== undefined) {
3317
+ if (!byobCanceled) {
3318
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3319
+ }
3320
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3321
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3322
+ }
3323
+ }
3324
+ if (!byobCanceled || !otherCanceled) {
3325
+ resolveCancelPromise(undefined);
3326
+ }
3327
+ },
3328
+ _errorSteps: function () {
3329
+ reading = false;
3330
+ }
3331
+ };
3332
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3333
+ }
3334
+ function pull1Algorithm() {
3335
+ if (reading) {
3336
+ return promiseResolvedWith(undefined);
3337
+ }
3338
+ reading = true;
3339
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3340
+ if (byobRequest === null) {
3341
+ pullWithDefaultReader();
3342
+ }
3343
+ else {
3344
+ pullWithBYOBReader(byobRequest._view, false);
3345
+ }
3346
+ return promiseResolvedWith(undefined);
3347
+ }
3348
+ function pull2Algorithm() {
3349
+ if (reading) {
3350
+ return promiseResolvedWith(undefined);
3351
+ }
3352
+ reading = true;
3353
+ var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3354
+ if (byobRequest === null) {
3355
+ pullWithDefaultReader();
3356
+ }
3357
+ else {
3358
+ pullWithBYOBReader(byobRequest._view, true);
3359
+ }
3360
+ return promiseResolvedWith(undefined);
3361
+ }
3362
+ function cancel1Algorithm(reason) {
3363
+ canceled1 = true;
3364
+ reason1 = reason;
3365
+ if (canceled2) {
3366
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3367
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3368
+ resolveCancelPromise(cancelResult);
3369
+ }
3370
+ return cancelPromise;
3371
+ }
3372
+ function cancel2Algorithm(reason) {
3373
+ canceled2 = true;
3374
+ reason2 = reason;
3375
+ if (canceled1) {
3376
+ var compositeReason = CreateArrayFromList([reason1, reason2]);
3377
+ var cancelResult = ReadableStreamCancel(stream, compositeReason);
3378
+ resolveCancelPromise(cancelResult);
3379
+ }
3380
+ return cancelPromise;
3381
+ }
3382
+ function startAlgorithm() {
3383
+ return;
3384
+ }
3385
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3386
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3387
+ forwardReaderError(reader);
3388
+ return [branch1, branch2];
3389
+ }
3082
3390
 
3083
3391
  function convertUnderlyingDefaultOrByteSource(source, context) {
3084
3392
  assertDictionary(source, context);
@@ -3354,6 +3662,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3354
3662
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3355
3663
  return stream;
3356
3664
  }
3665
+ // Throws if and only if startAlgorithm throws.
3666
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3667
+ var stream = Object.create(ReadableStream.prototype);
3668
+ InitializeReadableStream(stream);
3669
+ var controller = Object.create(ReadableByteStreamController.prototype);
3670
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3671
+ return stream;
3672
+ }
3357
3673
  function InitializeReadableStream(stream) {
3358
3674
  stream._state = 'readable';
3359
3675
  stream._reader = undefined;
@@ -3367,7 +3683,7 @@ function IsReadableStream(x) {
3367
3683
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3368
3684
  return false;
3369
3685
  }
3370
- return true;
3686
+ return x instanceof ReadableStream;
3371
3687
  }
3372
3688
  function IsReadableStreamLocked(stream) {
3373
3689
  if (stream._reader === undefined) {
@@ -3385,6 +3701,13 @@ function ReadableStreamCancel(stream, reason) {
3385
3701
  return promiseRejectedWith(stream._storedError);
3386
3702
  }
3387
3703
  ReadableStreamClose(stream);
3704
+ var reader = stream._reader;
3705
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3706
+ reader._readIntoRequests.forEach(function (readIntoRequest) {
3707
+ readIntoRequest._closeSteps(undefined);
3708
+ });
3709
+ reader._readIntoRequests = new SimpleQueue();
3710
+ }
3388
3711
  var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3389
3712
  return transformPromiseWith(sourceCancelPromise, noop);
3390
3713
  }
@@ -3437,9 +3760,14 @@ function convertQueuingStrategyInit(init, context) {
3437
3760
  };
3438
3761
  }
3439
3762
 
3440
- var byteLengthSizeFunction = function size(chunk) {
3763
+ // The size function must not have a prototype property nor be a constructor
3764
+ var byteLengthSizeFunction = function (chunk) {
3441
3765
  return chunk.byteLength;
3442
3766
  };
3767
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3768
+ value: 'size',
3769
+ configurable: true
3770
+ });
3443
3771
  /**
3444
3772
  * A queuing strategy that counts the number of bytes in each chunk.
3445
3773
  *
@@ -3500,12 +3828,17 @@ function IsByteLengthQueuingStrategy(x) {
3500
3828
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3501
3829
  return false;
3502
3830
  }
3503
- return true;
3831
+ return x instanceof ByteLengthQueuingStrategy;
3504
3832
  }
3505
3833
 
3506
- var countSizeFunction = function size() {
3834
+ // The size function must not have a prototype property nor be a constructor
3835
+ var countSizeFunction = function () {
3507
3836
  return 1;
3508
3837
  };
3838
+ Object.defineProperty(countSizeFunction, 'name', {
3839
+ value: 'size',
3840
+ configurable: true
3841
+ });
3509
3842
  /**
3510
3843
  * A queuing strategy that counts the number of chunks.
3511
3844
  *
@@ -3567,7 +3900,7 @@ function IsCountQueuingStrategy(x) {
3567
3900
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3568
3901
  return false;
3569
3902
  }
3570
- return true;
3903
+ return x instanceof CountQueuingStrategy;
3571
3904
  }
3572
3905
 
3573
3906
  function convertTransformer(original, context) {
@@ -3721,7 +4054,7 @@ function IsTransformStream(x) {
3721
4054
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3722
4055
  return false;
3723
4056
  }
3724
- return true;
4057
+ return x instanceof TransformStream;
3725
4058
  }
3726
4059
  // This is a no-op if both sides are already errored.
3727
4060
  function TransformStreamError(stream, e) {
@@ -3822,7 +4155,7 @@ function IsTransformStreamDefaultController(x) {
3822
4155
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3823
4156
  return false;
3824
4157
  }
3825
- return true;
4158
+ return x instanceof TransformStreamDefaultController;
3826
4159
  }
3827
4160
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3828
4161
  controller._controlledTransformStream = stream;