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.
@@ -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) :
@@ -494,7 +494,7 @@
494
494
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
495
495
  return false;
496
496
  }
497
- return true;
497
+ return x instanceof ReadableStreamDefaultReader;
498
498
  }
499
499
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
500
500
  const stream = reader._ownerReadableStream;
@@ -637,7 +637,14 @@
637
637
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
638
638
  return false;
639
639
  }
640
- return true;
640
+ try {
641
+ // noinspection SuspiciousTypeOfGuard
642
+ return x._asyncIteratorImpl instanceof
643
+ ReadableStreamAsyncIteratorImpl;
644
+ }
645
+ catch (_a) {
646
+ return false;
647
+ }
641
648
  }
642
649
  // Helper functions for the ReadableStream.
643
650
  function streamAsyncIteratorBrandCheckException(name) {
@@ -651,15 +658,35 @@
651
658
  return x !== x;
652
659
  };
653
660
 
654
- function IsFiniteNonNegativeNumber(v) {
655
- if (!IsNonNegativeNumber(v)) {
656
- return false;
657
- }
658
- if (v === Infinity) {
659
- return false;
661
+ function CreateArrayFromList(elements) {
662
+ // We use arrays to represent lists, so this is basically a no-op.
663
+ // Do a slice though just in case we happen to depend on the unique-ness.
664
+ return elements.slice();
665
+ }
666
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
667
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
668
+ }
669
+ // Not implemented correctly
670
+ function TransferArrayBuffer(O) {
671
+ return O;
672
+ }
673
+ // Not implemented correctly
674
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
675
+ function IsDetachedBuffer(O) {
676
+ return false;
677
+ }
678
+ function ArrayBufferSlice(buffer, begin, end) {
679
+ // ArrayBuffer.prototype.slice is not available on IE10
680
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
681
+ if (buffer.slice) {
682
+ return buffer.slice(begin, end);
660
683
  }
661
- return true;
684
+ const length = end - begin;
685
+ const slice = new ArrayBuffer(length);
686
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
687
+ return slice;
662
688
  }
689
+
663
690
  function IsNonNegativeNumber(v) {
664
691
  if (typeof v !== 'number') {
665
692
  return false;
@@ -672,6 +699,10 @@
672
699
  }
673
700
  return true;
674
701
  }
702
+ function CloneAsUint8Array(O) {
703
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
704
+ return new Uint8Array(buffer);
705
+ }
675
706
 
676
707
  function DequeueValue(container) {
677
708
  const pair = container._queue.shift();
@@ -682,8 +713,7 @@
682
713
  return pair.value;
683
714
  }
684
715
  function EnqueueValueWithSize(container, value, size) {
685
- size = Number(size);
686
- if (!IsFiniteNonNegativeNumber(size)) {
716
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
687
717
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
688
718
  }
689
719
  container._queue.push({ value, size });
@@ -698,23 +728,6 @@
698
728
  container._queueTotalSize = 0;
699
729
  }
700
730
 
701
- function CreateArrayFromList(elements) {
702
- // We use arrays to represent lists, so this is basically a no-op.
703
- // Do a slice though just in case we happen to depend on the unique-ness.
704
- return elements.slice();
705
- }
706
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
707
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
708
- }
709
- // Not implemented correctly
710
- function TransferArrayBuffer(O) {
711
- return O;
712
- }
713
- // Not implemented correctly
714
- function IsDetachedBuffer(O) {
715
- return false;
716
- }
717
-
718
731
  /**
719
732
  * A pull-into request in a {@link ReadableByteStreamController}.
720
733
  *
@@ -753,15 +766,10 @@
753
766
  if (!ArrayBuffer.isView(view)) {
754
767
  throw new TypeError('You can only respond with array buffer views');
755
768
  }
756
- if (view.byteLength === 0) {
757
- throw new TypeError('chunk must have non-zero byteLength');
758
- }
759
- if (view.buffer.byteLength === 0) {
760
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
761
- }
762
769
  if (this._associatedReadableByteStreamController === undefined) {
763
770
  throw new TypeError('This BYOB request has been invalidated');
764
771
  }
772
+ if (IsDetachedBuffer(view.buffer)) ;
765
773
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
766
774
  }
767
775
  }
@@ -792,14 +800,7 @@
792
800
  if (!IsReadableByteStreamController(this)) {
793
801
  throw byteStreamControllerBrandCheckException('byobRequest');
794
802
  }
795
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
796
- const firstDescriptor = this._pendingPullIntos.peek();
797
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
798
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
799
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
800
- this._byobRequest = byobRequest;
801
- }
802
- return this._byobRequest;
803
+ return ReadableByteStreamControllerGetBYOBRequest(this);
803
804
  }
804
805
  /**
805
806
  * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
@@ -862,10 +863,7 @@
862
863
  }
863
864
  /** @internal */
864
865
  [CancelSteps](reason) {
865
- if (this._pendingPullIntos.length > 0) {
866
- const firstDescriptor = this._pendingPullIntos.peek();
867
- firstDescriptor.bytesFilled = 0;
868
- }
866
+ ReadableByteStreamControllerClearPendingPullIntos(this);
869
867
  ResetQueue(this);
870
868
  const result = this._cancelAlgorithm(reason);
871
869
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -894,6 +892,7 @@
894
892
  }
895
893
  const pullIntoDescriptor = {
896
894
  buffer,
895
+ bufferByteLength: autoAllocateChunkSize,
897
896
  byteOffset: 0,
898
897
  byteLength: autoAllocateChunkSize,
899
898
  bytesFilled: 0,
@@ -928,7 +927,7 @@
928
927
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
929
928
  return false;
930
929
  }
931
- return true;
930
+ return x instanceof ReadableByteStreamController;
932
931
  }
933
932
  function IsReadableStreamBYOBRequest(x) {
934
933
  if (!typeIsObject(x)) {
@@ -937,7 +936,7 @@
937
936
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
938
937
  return false;
939
938
  }
940
- return true;
939
+ return x instanceof ReadableStreamBYOBRequest;
941
940
  }
942
941
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
943
942
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1019,7 +1018,6 @@
1019
1018
  return ready;
1020
1019
  }
1021
1020
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1022
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1023
1021
  pullIntoDescriptor.bytesFilled += size;
1024
1022
  }
1025
1023
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1058,9 +1056,15 @@
1058
1056
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1059
1057
  }
1060
1058
  const ctor = view.constructor;
1059
+ // try {
1061
1060
  const buffer = TransferArrayBuffer(view.buffer);
1061
+ // } catch (e) {
1062
+ // readIntoRequest._errorSteps(e);
1063
+ // return;
1064
+ // }
1062
1065
  const pullIntoDescriptor = {
1063
1066
  buffer,
1067
+ bufferByteLength: buffer.byteLength,
1064
1068
  byteOffset: view.byteOffset,
1065
1069
  byteLength: view.byteLength,
1066
1070
  bytesFilled: 0,
@@ -1100,7 +1104,6 @@
1100
1104
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1101
1105
  }
1102
1106
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1103
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1104
1107
  const stream = controller._controlledReadableByteStream;
1105
1108
  if (ReadableStreamHasBYOBReader(stream)) {
1106
1109
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1110,34 +1113,27 @@
1110
1113
  }
1111
1114
  }
1112
1115
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1113
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1114
- throw new RangeError('bytesWritten out of range');
1115
- }
1116
1116
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1117
1117
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1118
- // TODO: Figure out whether we should detach the buffer or not here.
1119
1118
  return;
1120
1119
  }
1121
1120
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1122
1121
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1123
1122
  if (remainderSize > 0) {
1124
1123
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1125
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1124
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1126
1125
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1127
1126
  }
1128
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1129
1127
  pullIntoDescriptor.bytesFilled -= remainderSize;
1130
1128
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1131
1129
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1132
1130
  }
1133
1131
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1134
1132
  const firstDescriptor = controller._pendingPullIntos.peek();
1133
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1135
1134
  const state = controller._controlledReadableByteStream._state;
1136
1135
  if (state === 'closed') {
1137
- if (bytesWritten !== 0) {
1138
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1139
- }
1140
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1136
+ ReadableByteStreamControllerRespondInClosedState(controller);
1141
1137
  }
1142
1138
  else {
1143
1139
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1146,7 +1142,6 @@
1146
1142
  }
1147
1143
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1148
1144
  const descriptor = controller._pendingPullIntos.shift();
1149
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1150
1145
  return descriptor;
1151
1146
  }
1152
1147
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1206,6 +1201,12 @@
1206
1201
  const byteOffset = chunk.byteOffset;
1207
1202
  const byteLength = chunk.byteLength;
1208
1203
  const transferredBuffer = TransferArrayBuffer(buffer);
1204
+ if (controller._pendingPullIntos.length > 0) {
1205
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1206
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1207
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1208
+ }
1209
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1209
1210
  if (ReadableStreamHasDefaultReader(stream)) {
1210
1211
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1211
1212
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1235,6 +1236,16 @@
1235
1236
  ReadableByteStreamControllerClearAlgorithms(controller);
1236
1237
  ReadableStreamError(stream, e);
1237
1238
  }
1239
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1240
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1241
+ const firstDescriptor = controller._pendingPullIntos.peek();
1242
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1243
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1244
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1245
+ controller._byobRequest = byobRequest;
1246
+ }
1247
+ return controller._byobRequest;
1248
+ }
1238
1249
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1239
1250
  const state = controller._controlledReadableByteStream._state;
1240
1251
  if (state === 'errored') {
@@ -1246,21 +1257,47 @@
1246
1257
  return controller._strategyHWM - controller._queueTotalSize;
1247
1258
  }
1248
1259
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1249
- bytesWritten = Number(bytesWritten);
1250
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1251
- throw new RangeError('bytesWritten must be a finite');
1260
+ const firstDescriptor = controller._pendingPullIntos.peek();
1261
+ const state = controller._controlledReadableByteStream._state;
1262
+ if (state === 'closed') {
1263
+ if (bytesWritten !== 0) {
1264
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1265
+ }
1252
1266
  }
1267
+ else {
1268
+ if (bytesWritten === 0) {
1269
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1270
+ }
1271
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1272
+ throw new RangeError('bytesWritten out of range');
1273
+ }
1274
+ }
1275
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1253
1276
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1254
1277
  }
1255
1278
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1256
1279
  const firstDescriptor = controller._pendingPullIntos.peek();
1280
+ const state = controller._controlledReadableByteStream._state;
1281
+ if (state === 'closed') {
1282
+ if (view.byteLength !== 0) {
1283
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1284
+ }
1285
+ }
1286
+ else {
1287
+ if (view.byteLength === 0) {
1288
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1289
+ }
1290
+ }
1257
1291
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1258
1292
  throw new RangeError('The region specified by view does not match byobRequest');
1259
1293
  }
1260
- if (firstDescriptor.byteLength !== view.byteLength) {
1294
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1261
1295
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1262
1296
  }
1263
- firstDescriptor.buffer = view.buffer;
1297
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1298
+ throw new RangeError('The region specified by view is larger than byobRequest');
1299
+ }
1300
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1264
1301
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1265
1302
  }
1266
1303
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1410,6 +1447,7 @@
1410
1447
  if (view.buffer.byteLength === 0) {
1411
1448
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1412
1449
  }
1450
+ if (IsDetachedBuffer(view.buffer)) ;
1413
1451
  if (this._ownerReadableStream === undefined) {
1414
1452
  return promiseRejectedWith(readerLockException('read from'));
1415
1453
  }
@@ -1469,7 +1507,7 @@
1469
1507
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1470
1508
  return false;
1471
1509
  }
1472
- return true;
1510
+ return x instanceof ReadableStreamBYOBReader;
1473
1511
  }
1474
1512
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1475
1513
  const stream = reader._ownerReadableStream;
@@ -1564,6 +1602,31 @@
1564
1602
  }
1565
1603
  }
1566
1604
 
1605
+ function isAbortSignal(value) {
1606
+ if (typeof value !== 'object' || value === null) {
1607
+ return false;
1608
+ }
1609
+ try {
1610
+ return typeof value.aborted === 'boolean';
1611
+ }
1612
+ catch (_a) {
1613
+ // AbortSignal.prototype.aborted throws if its brand check fails
1614
+ return false;
1615
+ }
1616
+ }
1617
+ const supportsAbortController = typeof AbortController === 'function';
1618
+ /**
1619
+ * Construct a new AbortController, if supported by the platform.
1620
+ *
1621
+ * @internal
1622
+ */
1623
+ function createAbortController() {
1624
+ if (supportsAbortController) {
1625
+ return new AbortController();
1626
+ }
1627
+ return undefined;
1628
+ }
1629
+
1567
1630
  /**
1568
1631
  * A writable stream represents a destination for data, into which you can write.
1569
1632
  *
@@ -1707,7 +1770,7 @@
1707
1770
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1708
1771
  return false;
1709
1772
  }
1710
- return true;
1773
+ return x instanceof WritableStream;
1711
1774
  }
1712
1775
  function IsWritableStreamLocked(stream) {
1713
1776
  if (stream._writer === undefined) {
@@ -1716,6 +1779,15 @@
1716
1779
  return true;
1717
1780
  }
1718
1781
  function WritableStreamAbort(stream, reason) {
1782
+ var _a;
1783
+ if (stream._state === 'closed' || stream._state === 'errored') {
1784
+ return promiseResolvedWith(undefined);
1785
+ }
1786
+ stream._writableStreamController._abortReason = reason;
1787
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1788
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1789
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1790
+ // Widen the type again by casting to WritableStreamState.
1719
1791
  const state = stream._state;
1720
1792
  if (state === 'closed' || state === 'errored') {
1721
1793
  return promiseResolvedWith(undefined);
@@ -2061,7 +2133,7 @@
2061
2133
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2062
2134
  return false;
2063
2135
  }
2064
- return true;
2136
+ return x instanceof WritableStreamDefaultWriter;
2065
2137
  }
2066
2138
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2067
2139
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2151,6 +2223,30 @@
2151
2223
  constructor() {
2152
2224
  throw new TypeError('Illegal constructor');
2153
2225
  }
2226
+ /**
2227
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2228
+ */
2229
+ get abortReason() {
2230
+ if (!IsWritableStreamDefaultController(this)) {
2231
+ throw defaultControllerBrandCheckException$2('abortReason');
2232
+ }
2233
+ return this._abortReason;
2234
+ }
2235
+ /**
2236
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2237
+ */
2238
+ get signal() {
2239
+ if (!IsWritableStreamDefaultController(this)) {
2240
+ throw defaultControllerBrandCheckException$2('signal');
2241
+ }
2242
+ if (this._abortController === undefined) {
2243
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2244
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2245
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2246
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2247
+ }
2248
+ return this._abortController.signal;
2249
+ }
2154
2250
  /**
2155
2251
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2156
2252
  *
@@ -2160,7 +2256,7 @@
2160
2256
  */
2161
2257
  error(e = undefined) {
2162
2258
  if (!IsWritableStreamDefaultController(this)) {
2163
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2259
+ throw defaultControllerBrandCheckException$2('error');
2164
2260
  }
2165
2261
  const state = this._controlledWritableStream._state;
2166
2262
  if (state !== 'writable') {
@@ -2198,7 +2294,7 @@
2198
2294
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2199
2295
  return false;
2200
2296
  }
2201
- return true;
2297
+ return x instanceof WritableStreamDefaultController;
2202
2298
  }
2203
2299
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2204
2300
  controller._controlledWritableStream = stream;
@@ -2207,6 +2303,8 @@
2207
2303
  controller._queue = undefined;
2208
2304
  controller._queueTotalSize = undefined;
2209
2305
  ResetQueue(controller);
2306
+ controller._abortReason = undefined;
2307
+ controller._abortController = createAbortController();
2210
2308
  controller._started = false;
2211
2309
  controller._strategySizeAlgorithm = sizeAlgorithm;
2212
2310
  controller._strategyHWM = highWaterMark;
@@ -2359,6 +2457,10 @@
2359
2457
  function streamBrandCheckException$2(name) {
2360
2458
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2361
2459
  }
2460
+ // Helper functions for the WritableStreamDefaultController.
2461
+ function defaultControllerBrandCheckException$2(name) {
2462
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2463
+ }
2362
2464
  // Helper functions for the WritableStreamDefaultWriter.
2363
2465
  function defaultWriterBrandCheckException(name) {
2364
2466
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2444,19 +2546,6 @@
2444
2546
  writer._readyPromiseState = 'fulfilled';
2445
2547
  }
2446
2548
 
2447
- function isAbortSignal(value) {
2448
- if (typeof value !== 'object' || value === null) {
2449
- return false;
2450
- }
2451
- try {
2452
- return typeof value.aborted === 'boolean';
2453
- }
2454
- catch (_a) {
2455
- // AbortSignal.prototype.aborted throws if its brand check fails
2456
- return false;
2457
- }
2458
- }
2459
-
2460
2549
  /// <reference lib="dom" />
2461
2550
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2462
2551
 
@@ -2761,7 +2850,7 @@
2761
2850
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2762
2851
  return false;
2763
2852
  }
2764
- return true;
2853
+ return x instanceof ReadableStreamDefaultController;
2765
2854
  }
2766
2855
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2767
2856
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2922,6 +3011,12 @@
2922
3011
  }
2923
3012
 
2924
3013
  function ReadableStreamTee(stream, cloneForBranch2) {
3014
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3015
+ return ReadableByteStreamTee(stream);
3016
+ }
3017
+ return ReadableStreamDefaultTee(stream);
3018
+ }
3019
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2925
3020
  const reader = AcquireReadableStreamDefaultReader(stream);
2926
3021
  let reading = false;
2927
3022
  let canceled1 = false;
@@ -2940,24 +3035,24 @@
2940
3035
  }
2941
3036
  reading = true;
2942
3037
  const readRequest = {
2943
- _chunkSteps: value => {
3038
+ _chunkSteps: chunk => {
2944
3039
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2945
3040
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2946
3041
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2947
3042
  queueMicrotask(() => {
2948
3043
  reading = false;
2949
- const value1 = value;
2950
- const value2 = value;
3044
+ const chunk1 = chunk;
3045
+ const chunk2 = chunk;
2951
3046
  // There is no way to access the cloning code right now in the reference implementation.
2952
3047
  // If we add one then we'll need an implementation for serializable objects.
2953
3048
  // if (!canceled2 && cloneForBranch2) {
2954
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3049
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2955
3050
  // }
2956
3051
  if (!canceled1) {
2957
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3052
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2958
3053
  }
2959
3054
  if (!canceled2) {
2960
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3055
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2961
3056
  }
2962
3057
  });
2963
3058
  },
@@ -3014,6 +3109,211 @@
3014
3109
  });
3015
3110
  return [branch1, branch2];
3016
3111
  }
3112
+ function ReadableByteStreamTee(stream) {
3113
+ let reader = AcquireReadableStreamDefaultReader(stream);
3114
+ let reading = false;
3115
+ let canceled1 = false;
3116
+ let canceled2 = false;
3117
+ let reason1;
3118
+ let reason2;
3119
+ let branch1;
3120
+ let branch2;
3121
+ let resolveCancelPromise;
3122
+ const cancelPromise = newPromise(resolve => {
3123
+ resolveCancelPromise = resolve;
3124
+ });
3125
+ function forwardReaderError(thisReader) {
3126
+ uponRejection(thisReader._closedPromise, r => {
3127
+ if (thisReader !== reader) {
3128
+ return;
3129
+ }
3130
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3131
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3132
+ if (!canceled1 || !canceled2) {
3133
+ resolveCancelPromise(undefined);
3134
+ }
3135
+ });
3136
+ }
3137
+ function pullWithDefaultReader() {
3138
+ if (IsReadableStreamBYOBReader(reader)) {
3139
+ ReadableStreamReaderGenericRelease(reader);
3140
+ reader = AcquireReadableStreamDefaultReader(stream);
3141
+ forwardReaderError(reader);
3142
+ }
3143
+ const readRequest = {
3144
+ _chunkSteps: chunk => {
3145
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3146
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3147
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3148
+ queueMicrotask(() => {
3149
+ reading = false;
3150
+ const chunk1 = chunk;
3151
+ let chunk2 = chunk;
3152
+ if (!canceled1 && !canceled2) {
3153
+ try {
3154
+ chunk2 = CloneAsUint8Array(chunk);
3155
+ }
3156
+ catch (cloneE) {
3157
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3158
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3159
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3160
+ return;
3161
+ }
3162
+ }
3163
+ if (!canceled1) {
3164
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3165
+ }
3166
+ if (!canceled2) {
3167
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3168
+ }
3169
+ });
3170
+ },
3171
+ _closeSteps: () => {
3172
+ reading = false;
3173
+ if (!canceled1) {
3174
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3175
+ }
3176
+ if (!canceled2) {
3177
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3178
+ }
3179
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3180
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3181
+ }
3182
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3183
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3184
+ }
3185
+ if (!canceled1 || !canceled2) {
3186
+ resolveCancelPromise(undefined);
3187
+ }
3188
+ },
3189
+ _errorSteps: () => {
3190
+ reading = false;
3191
+ }
3192
+ };
3193
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3194
+ }
3195
+ function pullWithBYOBReader(view, forBranch2) {
3196
+ if (IsReadableStreamDefaultReader(reader)) {
3197
+ ReadableStreamReaderGenericRelease(reader);
3198
+ reader = AcquireReadableStreamBYOBReader(stream);
3199
+ forwardReaderError(reader);
3200
+ }
3201
+ const byobBranch = forBranch2 ? branch2 : branch1;
3202
+ const otherBranch = forBranch2 ? branch1 : branch2;
3203
+ const readIntoRequest = {
3204
+ _chunkSteps: chunk => {
3205
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3206
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3207
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3208
+ queueMicrotask(() => {
3209
+ reading = false;
3210
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3211
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3212
+ if (!otherCanceled) {
3213
+ let clonedChunk;
3214
+ try {
3215
+ clonedChunk = CloneAsUint8Array(chunk);
3216
+ }
3217
+ catch (cloneE) {
3218
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3219
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3220
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3221
+ return;
3222
+ }
3223
+ if (!byobCanceled) {
3224
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3225
+ }
3226
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3227
+ }
3228
+ else if (!byobCanceled) {
3229
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3230
+ }
3231
+ });
3232
+ },
3233
+ _closeSteps: chunk => {
3234
+ reading = false;
3235
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3236
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3237
+ if (!byobCanceled) {
3238
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3239
+ }
3240
+ if (!otherCanceled) {
3241
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3242
+ }
3243
+ if (chunk !== undefined) {
3244
+ if (!byobCanceled) {
3245
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3246
+ }
3247
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3248
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3249
+ }
3250
+ }
3251
+ if (!byobCanceled || !otherCanceled) {
3252
+ resolveCancelPromise(undefined);
3253
+ }
3254
+ },
3255
+ _errorSteps: () => {
3256
+ reading = false;
3257
+ }
3258
+ };
3259
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3260
+ }
3261
+ function pull1Algorithm() {
3262
+ if (reading) {
3263
+ return promiseResolvedWith(undefined);
3264
+ }
3265
+ reading = true;
3266
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3267
+ if (byobRequest === null) {
3268
+ pullWithDefaultReader();
3269
+ }
3270
+ else {
3271
+ pullWithBYOBReader(byobRequest._view, false);
3272
+ }
3273
+ return promiseResolvedWith(undefined);
3274
+ }
3275
+ function pull2Algorithm() {
3276
+ if (reading) {
3277
+ return promiseResolvedWith(undefined);
3278
+ }
3279
+ reading = true;
3280
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3281
+ if (byobRequest === null) {
3282
+ pullWithDefaultReader();
3283
+ }
3284
+ else {
3285
+ pullWithBYOBReader(byobRequest._view, true);
3286
+ }
3287
+ return promiseResolvedWith(undefined);
3288
+ }
3289
+ function cancel1Algorithm(reason) {
3290
+ canceled1 = true;
3291
+ reason1 = reason;
3292
+ if (canceled2) {
3293
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3294
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3295
+ resolveCancelPromise(cancelResult);
3296
+ }
3297
+ return cancelPromise;
3298
+ }
3299
+ function cancel2Algorithm(reason) {
3300
+ canceled2 = true;
3301
+ reason2 = reason;
3302
+ if (canceled1) {
3303
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3304
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3305
+ resolveCancelPromise(cancelResult);
3306
+ }
3307
+ return cancelPromise;
3308
+ }
3309
+ function startAlgorithm() {
3310
+ return;
3311
+ }
3312
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3313
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3314
+ forwardReaderError(reader);
3315
+ return [branch1, branch2];
3316
+ }
3017
3317
 
3018
3318
  function convertUnderlyingDefaultOrByteSource(source, context) {
3019
3319
  assertDictionary(source, context);
@@ -3275,6 +3575,14 @@
3275
3575
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3276
3576
  return stream;
3277
3577
  }
3578
+ // Throws if and only if startAlgorithm throws.
3579
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3580
+ const stream = Object.create(ReadableStream.prototype);
3581
+ InitializeReadableStream(stream);
3582
+ const controller = Object.create(ReadableByteStreamController.prototype);
3583
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3584
+ return stream;
3585
+ }
3278
3586
  function InitializeReadableStream(stream) {
3279
3587
  stream._state = 'readable';
3280
3588
  stream._reader = undefined;
@@ -3288,7 +3596,7 @@
3288
3596
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3289
3597
  return false;
3290
3598
  }
3291
- return true;
3599
+ return x instanceof ReadableStream;
3292
3600
  }
3293
3601
  function IsReadableStreamLocked(stream) {
3294
3602
  if (stream._reader === undefined) {
@@ -3306,6 +3614,13 @@
3306
3614
  return promiseRejectedWith(stream._storedError);
3307
3615
  }
3308
3616
  ReadableStreamClose(stream);
3617
+ const reader = stream._reader;
3618
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3619
+ reader._readIntoRequests.forEach(readIntoRequest => {
3620
+ readIntoRequest._closeSteps(undefined);
3621
+ });
3622
+ reader._readIntoRequests = new SimpleQueue();
3623
+ }
3309
3624
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3310
3625
  return transformPromiseWith(sourceCancelPromise, noop);
3311
3626
  }
@@ -3358,9 +3673,14 @@
3358
3673
  };
3359
3674
  }
3360
3675
 
3361
- const byteLengthSizeFunction = function size(chunk) {
3676
+ // The size function must not have a prototype property nor be a constructor
3677
+ const byteLengthSizeFunction = (chunk) => {
3362
3678
  return chunk.byteLength;
3363
3679
  };
3680
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3681
+ value: 'size',
3682
+ configurable: true
3683
+ });
3364
3684
  /**
3365
3685
  * A queuing strategy that counts the number of bytes in each chunk.
3366
3686
  *
@@ -3412,12 +3732,17 @@
3412
3732
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3413
3733
  return false;
3414
3734
  }
3415
- return true;
3735
+ return x instanceof ByteLengthQueuingStrategy;
3416
3736
  }
3417
3737
 
3418
- const countSizeFunction = function size() {
3738
+ // The size function must not have a prototype property nor be a constructor
3739
+ const countSizeFunction = () => {
3419
3740
  return 1;
3420
3741
  };
3742
+ Object.defineProperty(countSizeFunction, 'name', {
3743
+ value: 'size',
3744
+ configurable: true
3745
+ });
3421
3746
  /**
3422
3747
  * A queuing strategy that counts the number of chunks.
3423
3748
  *
@@ -3470,7 +3795,7 @@
3470
3795
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3471
3796
  return false;
3472
3797
  }
3473
- return true;
3798
+ return x instanceof CountQueuingStrategy;
3474
3799
  }
3475
3800
 
3476
3801
  function convertTransformer(original, context) {
@@ -3612,7 +3937,7 @@
3612
3937
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3613
3938
  return false;
3614
3939
  }
3615
- return true;
3940
+ return x instanceof TransformStream;
3616
3941
  }
3617
3942
  // This is a no-op if both sides are already errored.
3618
3943
  function TransformStreamError(stream, e) {
@@ -3706,7 +4031,7 @@
3706
4031
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3707
4032
  return false;
3708
4033
  }
3709
- return true;
4034
+ return x instanceof TransformStreamDefaultController;
3710
4035
  }
3711
4036
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3712
4037
  controller._controlledTransformStream = stream;