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
  /// <reference lib="es2015.symbol" />
5
5
  const SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
@@ -488,7 +488,7 @@ function IsReadableStreamDefaultReader(x) {
488
488
  if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
489
489
  return false;
490
490
  }
491
- return true;
491
+ return x instanceof ReadableStreamDefaultReader;
492
492
  }
493
493
  function ReadableStreamDefaultReaderRead(reader, readRequest) {
494
494
  const stream = reader._ownerReadableStream;
@@ -631,7 +631,14 @@ function IsReadableStreamAsyncIterator(x) {
631
631
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
632
632
  return false;
633
633
  }
634
- return true;
634
+ try {
635
+ // noinspection SuspiciousTypeOfGuard
636
+ return x._asyncIteratorImpl instanceof
637
+ ReadableStreamAsyncIteratorImpl;
638
+ }
639
+ catch (_a) {
640
+ return false;
641
+ }
635
642
  }
636
643
  // Helper functions for the ReadableStream.
637
644
  function streamAsyncIteratorBrandCheckException(name) {
@@ -645,15 +652,35 @@ const NumberIsNaN = Number.isNaN || function (x) {
645
652
  return x !== x;
646
653
  };
647
654
 
648
- function IsFiniteNonNegativeNumber(v) {
649
- if (!IsNonNegativeNumber(v)) {
650
- return false;
651
- }
652
- if (v === Infinity) {
653
- return false;
655
+ function CreateArrayFromList(elements) {
656
+ // We use arrays to represent lists, so this is basically a no-op.
657
+ // Do a slice though just in case we happen to depend on the unique-ness.
658
+ return elements.slice();
659
+ }
660
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
661
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
662
+ }
663
+ // Not implemented correctly
664
+ function TransferArrayBuffer(O) {
665
+ return O;
666
+ }
667
+ // Not implemented correctly
668
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
669
+ function IsDetachedBuffer(O) {
670
+ return false;
671
+ }
672
+ function ArrayBufferSlice(buffer, begin, end) {
673
+ // ArrayBuffer.prototype.slice is not available on IE10
674
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
675
+ if (buffer.slice) {
676
+ return buffer.slice(begin, end);
654
677
  }
655
- return true;
678
+ const length = end - begin;
679
+ const slice = new ArrayBuffer(length);
680
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
681
+ return slice;
656
682
  }
683
+
657
684
  function IsNonNegativeNumber(v) {
658
685
  if (typeof v !== 'number') {
659
686
  return false;
@@ -666,6 +693,10 @@ function IsNonNegativeNumber(v) {
666
693
  }
667
694
  return true;
668
695
  }
696
+ function CloneAsUint8Array(O) {
697
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
698
+ return new Uint8Array(buffer);
699
+ }
669
700
 
670
701
  function DequeueValue(container) {
671
702
  const pair = container._queue.shift();
@@ -676,8 +707,7 @@ function DequeueValue(container) {
676
707
  return pair.value;
677
708
  }
678
709
  function EnqueueValueWithSize(container, value, size) {
679
- size = Number(size);
680
- if (!IsFiniteNonNegativeNumber(size)) {
710
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
681
711
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
682
712
  }
683
713
  container._queue.push({ value, size });
@@ -692,23 +722,6 @@ function ResetQueue(container) {
692
722
  container._queueTotalSize = 0;
693
723
  }
694
724
 
695
- function CreateArrayFromList(elements) {
696
- // We use arrays to represent lists, so this is basically a no-op.
697
- // Do a slice though just in case we happen to depend on the unique-ness.
698
- return elements.slice();
699
- }
700
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
701
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
702
- }
703
- // Not implemented correctly
704
- function TransferArrayBuffer(O) {
705
- return O;
706
- }
707
- // Not implemented correctly
708
- function IsDetachedBuffer(O) {
709
- return false;
710
- }
711
-
712
725
  /**
713
726
  * A pull-into request in a {@link ReadableByteStreamController}.
714
727
  *
@@ -747,15 +760,10 @@ class ReadableStreamBYOBRequest {
747
760
  if (!ArrayBuffer.isView(view)) {
748
761
  throw new TypeError('You can only respond with array buffer views');
749
762
  }
750
- if (view.byteLength === 0) {
751
- throw new TypeError('chunk must have non-zero byteLength');
752
- }
753
- if (view.buffer.byteLength === 0) {
754
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
755
- }
756
763
  if (this._associatedReadableByteStreamController === undefined) {
757
764
  throw new TypeError('This BYOB request has been invalidated');
758
765
  }
766
+ if (IsDetachedBuffer(view.buffer)) ;
759
767
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
760
768
  }
761
769
  }
@@ -786,14 +794,7 @@ class ReadableByteStreamController {
786
794
  if (!IsReadableByteStreamController(this)) {
787
795
  throw byteStreamControllerBrandCheckException('byobRequest');
788
796
  }
789
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
790
- const firstDescriptor = this._pendingPullIntos.peek();
791
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
792
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
793
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
794
- this._byobRequest = byobRequest;
795
- }
796
- return this._byobRequest;
797
+ return ReadableByteStreamControllerGetBYOBRequest(this);
797
798
  }
798
799
  /**
799
800
  * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
@@ -856,10 +857,7 @@ class ReadableByteStreamController {
856
857
  }
857
858
  /** @internal */
858
859
  [CancelSteps](reason) {
859
- if (this._pendingPullIntos.length > 0) {
860
- const firstDescriptor = this._pendingPullIntos.peek();
861
- firstDescriptor.bytesFilled = 0;
862
- }
860
+ ReadableByteStreamControllerClearPendingPullIntos(this);
863
861
  ResetQueue(this);
864
862
  const result = this._cancelAlgorithm(reason);
865
863
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -888,6 +886,7 @@ class ReadableByteStreamController {
888
886
  }
889
887
  const pullIntoDescriptor = {
890
888
  buffer,
889
+ bufferByteLength: autoAllocateChunkSize,
891
890
  byteOffset: 0,
892
891
  byteLength: autoAllocateChunkSize,
893
892
  bytesFilled: 0,
@@ -922,7 +921,7 @@ function IsReadableByteStreamController(x) {
922
921
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
923
922
  return false;
924
923
  }
925
- return true;
924
+ return x instanceof ReadableByteStreamController;
926
925
  }
927
926
  function IsReadableStreamBYOBRequest(x) {
928
927
  if (!typeIsObject(x)) {
@@ -931,7 +930,7 @@ function IsReadableStreamBYOBRequest(x) {
931
930
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
932
931
  return false;
933
932
  }
934
- return true;
933
+ return x instanceof ReadableStreamBYOBRequest;
935
934
  }
936
935
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
937
936
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1013,7 +1012,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
1013
1012
  return ready;
1014
1013
  }
1015
1014
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1016
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1017
1015
  pullIntoDescriptor.bytesFilled += size;
1018
1016
  }
1019
1017
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1052,9 +1050,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1052
1050
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1053
1051
  }
1054
1052
  const ctor = view.constructor;
1053
+ // try {
1055
1054
  const buffer = TransferArrayBuffer(view.buffer);
1055
+ // } catch (e) {
1056
+ // readIntoRequest._errorSteps(e);
1057
+ // return;
1058
+ // }
1056
1059
  const pullIntoDescriptor = {
1057
1060
  buffer,
1061
+ bufferByteLength: buffer.byteLength,
1058
1062
  byteOffset: view.byteOffset,
1059
1063
  byteLength: view.byteLength,
1060
1064
  bytesFilled: 0,
@@ -1094,7 +1098,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1094
1098
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1095
1099
  }
1096
1100
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1097
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1098
1101
  const stream = controller._controlledReadableByteStream;
1099
1102
  if (ReadableStreamHasBYOBReader(stream)) {
1100
1103
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1104,34 +1107,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1104
1107
  }
1105
1108
  }
1106
1109
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1107
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1108
- throw new RangeError('bytesWritten out of range');
1109
- }
1110
1110
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1111
1111
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1112
- // TODO: Figure out whether we should detach the buffer or not here.
1113
1112
  return;
1114
1113
  }
1115
1114
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1116
1115
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1117
1116
  if (remainderSize > 0) {
1118
1117
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1119
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1118
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1120
1119
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1121
1120
  }
1122
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1123
1121
  pullIntoDescriptor.bytesFilled -= remainderSize;
1124
1122
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1125
1123
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1126
1124
  }
1127
1125
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1128
1126
  const firstDescriptor = controller._pendingPullIntos.peek();
1127
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1129
1128
  const state = controller._controlledReadableByteStream._state;
1130
1129
  if (state === 'closed') {
1131
- if (bytesWritten !== 0) {
1132
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1133
- }
1134
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1130
+ ReadableByteStreamControllerRespondInClosedState(controller);
1135
1131
  }
1136
1132
  else {
1137
1133
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1140,7 +1136,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1140
1136
  }
1141
1137
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1142
1138
  const descriptor = controller._pendingPullIntos.shift();
1143
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1144
1139
  return descriptor;
1145
1140
  }
1146
1141
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1200,6 +1195,12 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1200
1195
  const byteOffset = chunk.byteOffset;
1201
1196
  const byteLength = chunk.byteLength;
1202
1197
  const transferredBuffer = TransferArrayBuffer(buffer);
1198
+ if (controller._pendingPullIntos.length > 0) {
1199
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1200
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1201
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1202
+ }
1203
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1203
1204
  if (ReadableStreamHasDefaultReader(stream)) {
1204
1205
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1205
1206
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1229,6 +1230,16 @@ function ReadableByteStreamControllerError(controller, e) {
1229
1230
  ReadableByteStreamControllerClearAlgorithms(controller);
1230
1231
  ReadableStreamError(stream, e);
1231
1232
  }
1233
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1234
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1235
+ const firstDescriptor = controller._pendingPullIntos.peek();
1236
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1237
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1238
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1239
+ controller._byobRequest = byobRequest;
1240
+ }
1241
+ return controller._byobRequest;
1242
+ }
1232
1243
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1233
1244
  const state = controller._controlledReadableByteStream._state;
1234
1245
  if (state === 'errored') {
@@ -1240,21 +1251,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1240
1251
  return controller._strategyHWM - controller._queueTotalSize;
1241
1252
  }
1242
1253
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1243
- bytesWritten = Number(bytesWritten);
1244
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1245
- throw new RangeError('bytesWritten must be a finite');
1254
+ const firstDescriptor = controller._pendingPullIntos.peek();
1255
+ const state = controller._controlledReadableByteStream._state;
1256
+ if (state === 'closed') {
1257
+ if (bytesWritten !== 0) {
1258
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1259
+ }
1246
1260
  }
1261
+ else {
1262
+ if (bytesWritten === 0) {
1263
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1264
+ }
1265
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1266
+ throw new RangeError('bytesWritten out of range');
1267
+ }
1268
+ }
1269
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1247
1270
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1248
1271
  }
1249
1272
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1250
1273
  const firstDescriptor = controller._pendingPullIntos.peek();
1274
+ const state = controller._controlledReadableByteStream._state;
1275
+ if (state === 'closed') {
1276
+ if (view.byteLength !== 0) {
1277
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1278
+ }
1279
+ }
1280
+ else {
1281
+ if (view.byteLength === 0) {
1282
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1283
+ }
1284
+ }
1251
1285
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1252
1286
  throw new RangeError('The region specified by view does not match byobRequest');
1253
1287
  }
1254
- if (firstDescriptor.byteLength !== view.byteLength) {
1288
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1255
1289
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1256
1290
  }
1257
- firstDescriptor.buffer = view.buffer;
1291
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1292
+ throw new RangeError('The region specified by view is larger than byobRequest');
1293
+ }
1294
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1258
1295
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1259
1296
  }
1260
1297
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1404,6 +1441,7 @@ class ReadableStreamBYOBReader {
1404
1441
  if (view.buffer.byteLength === 0) {
1405
1442
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1406
1443
  }
1444
+ if (IsDetachedBuffer(view.buffer)) ;
1407
1445
  if (this._ownerReadableStream === undefined) {
1408
1446
  return promiseRejectedWith(readerLockException('read from'));
1409
1447
  }
@@ -1463,7 +1501,7 @@ function IsReadableStreamBYOBReader(x) {
1463
1501
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1464
1502
  return false;
1465
1503
  }
1466
- return true;
1504
+ return x instanceof ReadableStreamBYOBReader;
1467
1505
  }
1468
1506
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1469
1507
  const stream = reader._ownerReadableStream;
@@ -1558,6 +1596,31 @@ function assertWritableStream(x, context) {
1558
1596
  }
1559
1597
  }
1560
1598
 
1599
+ function isAbortSignal(value) {
1600
+ if (typeof value !== 'object' || value === null) {
1601
+ return false;
1602
+ }
1603
+ try {
1604
+ return typeof value.aborted === 'boolean';
1605
+ }
1606
+ catch (_a) {
1607
+ // AbortSignal.prototype.aborted throws if its brand check fails
1608
+ return false;
1609
+ }
1610
+ }
1611
+ const supportsAbortController = typeof AbortController === 'function';
1612
+ /**
1613
+ * Construct a new AbortController, if supported by the platform.
1614
+ *
1615
+ * @internal
1616
+ */
1617
+ function createAbortController() {
1618
+ if (supportsAbortController) {
1619
+ return new AbortController();
1620
+ }
1621
+ return undefined;
1622
+ }
1623
+
1561
1624
  /**
1562
1625
  * A writable stream represents a destination for data, into which you can write.
1563
1626
  *
@@ -1701,7 +1764,7 @@ function IsWritableStream(x) {
1701
1764
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1702
1765
  return false;
1703
1766
  }
1704
- return true;
1767
+ return x instanceof WritableStream;
1705
1768
  }
1706
1769
  function IsWritableStreamLocked(stream) {
1707
1770
  if (stream._writer === undefined) {
@@ -1710,6 +1773,15 @@ function IsWritableStreamLocked(stream) {
1710
1773
  return true;
1711
1774
  }
1712
1775
  function WritableStreamAbort(stream, reason) {
1776
+ var _a;
1777
+ if (stream._state === 'closed' || stream._state === 'errored') {
1778
+ return promiseResolvedWith(undefined);
1779
+ }
1780
+ stream._writableStreamController._abortReason = reason;
1781
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1782
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1783
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1784
+ // Widen the type again by casting to WritableStreamState.
1713
1785
  const state = stream._state;
1714
1786
  if (state === 'closed' || state === 'errored') {
1715
1787
  return promiseResolvedWith(undefined);
@@ -2055,7 +2127,7 @@ function IsWritableStreamDefaultWriter(x) {
2055
2127
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2056
2128
  return false;
2057
2129
  }
2058
- return true;
2130
+ return x instanceof WritableStreamDefaultWriter;
2059
2131
  }
2060
2132
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2061
2133
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2145,6 +2217,30 @@ class WritableStreamDefaultController {
2145
2217
  constructor() {
2146
2218
  throw new TypeError('Illegal constructor');
2147
2219
  }
2220
+ /**
2221
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2222
+ */
2223
+ get abortReason() {
2224
+ if (!IsWritableStreamDefaultController(this)) {
2225
+ throw defaultControllerBrandCheckException$2('abortReason');
2226
+ }
2227
+ return this._abortReason;
2228
+ }
2229
+ /**
2230
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2231
+ */
2232
+ get signal() {
2233
+ if (!IsWritableStreamDefaultController(this)) {
2234
+ throw defaultControllerBrandCheckException$2('signal');
2235
+ }
2236
+ if (this._abortController === undefined) {
2237
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2238
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2239
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2240
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2241
+ }
2242
+ return this._abortController.signal;
2243
+ }
2148
2244
  /**
2149
2245
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2150
2246
  *
@@ -2154,7 +2250,7 @@ class WritableStreamDefaultController {
2154
2250
  */
2155
2251
  error(e = undefined) {
2156
2252
  if (!IsWritableStreamDefaultController(this)) {
2157
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2253
+ throw defaultControllerBrandCheckException$2('error');
2158
2254
  }
2159
2255
  const state = this._controlledWritableStream._state;
2160
2256
  if (state !== 'writable') {
@@ -2192,7 +2288,7 @@ function IsWritableStreamDefaultController(x) {
2192
2288
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2193
2289
  return false;
2194
2290
  }
2195
- return true;
2291
+ return x instanceof WritableStreamDefaultController;
2196
2292
  }
2197
2293
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2198
2294
  controller._controlledWritableStream = stream;
@@ -2201,6 +2297,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2201
2297
  controller._queue = undefined;
2202
2298
  controller._queueTotalSize = undefined;
2203
2299
  ResetQueue(controller);
2300
+ controller._abortReason = undefined;
2301
+ controller._abortController = createAbortController();
2204
2302
  controller._started = false;
2205
2303
  controller._strategySizeAlgorithm = sizeAlgorithm;
2206
2304
  controller._strategyHWM = highWaterMark;
@@ -2353,6 +2451,10 @@ function WritableStreamDefaultControllerError(controller, error) {
2353
2451
  function streamBrandCheckException$2(name) {
2354
2452
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2355
2453
  }
2454
+ // Helper functions for the WritableStreamDefaultController.
2455
+ function defaultControllerBrandCheckException$2(name) {
2456
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2457
+ }
2356
2458
  // Helper functions for the WritableStreamDefaultWriter.
2357
2459
  function defaultWriterBrandCheckException(name) {
2358
2460
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2438,19 +2540,6 @@ function defaultWriterReadyPromiseResolve(writer) {
2438
2540
  writer._readyPromiseState = 'fulfilled';
2439
2541
  }
2440
2542
 
2441
- function isAbortSignal(value) {
2442
- if (typeof value !== 'object' || value === null) {
2443
- return false;
2444
- }
2445
- try {
2446
- return typeof value.aborted === 'boolean';
2447
- }
2448
- catch (_a) {
2449
- // AbortSignal.prototype.aborted throws if its brand check fails
2450
- return false;
2451
- }
2452
- }
2453
-
2454
2543
  /// <reference lib="dom" />
2455
2544
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2456
2545
 
@@ -2755,7 +2844,7 @@ function IsReadableStreamDefaultController(x) {
2755
2844
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2756
2845
  return false;
2757
2846
  }
2758
- return true;
2847
+ return x instanceof ReadableStreamDefaultController;
2759
2848
  }
2760
2849
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2761
2850
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2916,6 +3005,12 @@ function defaultControllerBrandCheckException$1(name) {
2916
3005
  }
2917
3006
 
2918
3007
  function ReadableStreamTee(stream, cloneForBranch2) {
3008
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3009
+ return ReadableByteStreamTee(stream);
3010
+ }
3011
+ return ReadableStreamDefaultTee(stream);
3012
+ }
3013
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2919
3014
  const reader = AcquireReadableStreamDefaultReader(stream);
2920
3015
  let reading = false;
2921
3016
  let canceled1 = false;
@@ -2934,24 +3029,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2934
3029
  }
2935
3030
  reading = true;
2936
3031
  const readRequest = {
2937
- _chunkSteps: value => {
3032
+ _chunkSteps: chunk => {
2938
3033
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2939
3034
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2940
3035
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2941
3036
  queueMicrotask(() => {
2942
3037
  reading = false;
2943
- const value1 = value;
2944
- const value2 = value;
3038
+ const chunk1 = chunk;
3039
+ const chunk2 = chunk;
2945
3040
  // There is no way to access the cloning code right now in the reference implementation.
2946
3041
  // If we add one then we'll need an implementation for serializable objects.
2947
3042
  // if (!canceled2 && cloneForBranch2) {
2948
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3043
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2949
3044
  // }
2950
3045
  if (!canceled1) {
2951
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3046
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2952
3047
  }
2953
3048
  if (!canceled2) {
2954
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3049
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2955
3050
  }
2956
3051
  });
2957
3052
  },
@@ -3008,6 +3103,211 @@ function ReadableStreamTee(stream, cloneForBranch2) {
3008
3103
  });
3009
3104
  return [branch1, branch2];
3010
3105
  }
3106
+ function ReadableByteStreamTee(stream) {
3107
+ let reader = AcquireReadableStreamDefaultReader(stream);
3108
+ let reading = false;
3109
+ let canceled1 = false;
3110
+ let canceled2 = false;
3111
+ let reason1;
3112
+ let reason2;
3113
+ let branch1;
3114
+ let branch2;
3115
+ let resolveCancelPromise;
3116
+ const cancelPromise = newPromise(resolve => {
3117
+ resolveCancelPromise = resolve;
3118
+ });
3119
+ function forwardReaderError(thisReader) {
3120
+ uponRejection(thisReader._closedPromise, r => {
3121
+ if (thisReader !== reader) {
3122
+ return;
3123
+ }
3124
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3125
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3126
+ if (!canceled1 || !canceled2) {
3127
+ resolveCancelPromise(undefined);
3128
+ }
3129
+ });
3130
+ }
3131
+ function pullWithDefaultReader() {
3132
+ if (IsReadableStreamBYOBReader(reader)) {
3133
+ ReadableStreamReaderGenericRelease(reader);
3134
+ reader = AcquireReadableStreamDefaultReader(stream);
3135
+ forwardReaderError(reader);
3136
+ }
3137
+ const readRequest = {
3138
+ _chunkSteps: chunk => {
3139
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3140
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3141
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3142
+ queueMicrotask(() => {
3143
+ reading = false;
3144
+ const chunk1 = chunk;
3145
+ let chunk2 = chunk;
3146
+ if (!canceled1 && !canceled2) {
3147
+ try {
3148
+ chunk2 = CloneAsUint8Array(chunk);
3149
+ }
3150
+ catch (cloneE) {
3151
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3152
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3153
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3154
+ return;
3155
+ }
3156
+ }
3157
+ if (!canceled1) {
3158
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3159
+ }
3160
+ if (!canceled2) {
3161
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3162
+ }
3163
+ });
3164
+ },
3165
+ _closeSteps: () => {
3166
+ reading = false;
3167
+ if (!canceled1) {
3168
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3169
+ }
3170
+ if (!canceled2) {
3171
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3172
+ }
3173
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3174
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3175
+ }
3176
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3177
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3178
+ }
3179
+ if (!canceled1 || !canceled2) {
3180
+ resolveCancelPromise(undefined);
3181
+ }
3182
+ },
3183
+ _errorSteps: () => {
3184
+ reading = false;
3185
+ }
3186
+ };
3187
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3188
+ }
3189
+ function pullWithBYOBReader(view, forBranch2) {
3190
+ if (IsReadableStreamDefaultReader(reader)) {
3191
+ ReadableStreamReaderGenericRelease(reader);
3192
+ reader = AcquireReadableStreamBYOBReader(stream);
3193
+ forwardReaderError(reader);
3194
+ }
3195
+ const byobBranch = forBranch2 ? branch2 : branch1;
3196
+ const otherBranch = forBranch2 ? branch1 : branch2;
3197
+ const readIntoRequest = {
3198
+ _chunkSteps: chunk => {
3199
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3200
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3201
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3202
+ queueMicrotask(() => {
3203
+ reading = false;
3204
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3205
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3206
+ if (!otherCanceled) {
3207
+ let clonedChunk;
3208
+ try {
3209
+ clonedChunk = CloneAsUint8Array(chunk);
3210
+ }
3211
+ catch (cloneE) {
3212
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3213
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3214
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3215
+ return;
3216
+ }
3217
+ if (!byobCanceled) {
3218
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3219
+ }
3220
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3221
+ }
3222
+ else if (!byobCanceled) {
3223
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3224
+ }
3225
+ });
3226
+ },
3227
+ _closeSteps: chunk => {
3228
+ reading = false;
3229
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3230
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3231
+ if (!byobCanceled) {
3232
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3233
+ }
3234
+ if (!otherCanceled) {
3235
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3236
+ }
3237
+ if (chunk !== undefined) {
3238
+ if (!byobCanceled) {
3239
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3240
+ }
3241
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3242
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3243
+ }
3244
+ }
3245
+ if (!byobCanceled || !otherCanceled) {
3246
+ resolveCancelPromise(undefined);
3247
+ }
3248
+ },
3249
+ _errorSteps: () => {
3250
+ reading = false;
3251
+ }
3252
+ };
3253
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3254
+ }
3255
+ function pull1Algorithm() {
3256
+ if (reading) {
3257
+ return promiseResolvedWith(undefined);
3258
+ }
3259
+ reading = true;
3260
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3261
+ if (byobRequest === null) {
3262
+ pullWithDefaultReader();
3263
+ }
3264
+ else {
3265
+ pullWithBYOBReader(byobRequest._view, false);
3266
+ }
3267
+ return promiseResolvedWith(undefined);
3268
+ }
3269
+ function pull2Algorithm() {
3270
+ if (reading) {
3271
+ return promiseResolvedWith(undefined);
3272
+ }
3273
+ reading = true;
3274
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3275
+ if (byobRequest === null) {
3276
+ pullWithDefaultReader();
3277
+ }
3278
+ else {
3279
+ pullWithBYOBReader(byobRequest._view, true);
3280
+ }
3281
+ return promiseResolvedWith(undefined);
3282
+ }
3283
+ function cancel1Algorithm(reason) {
3284
+ canceled1 = true;
3285
+ reason1 = reason;
3286
+ if (canceled2) {
3287
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3288
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3289
+ resolveCancelPromise(cancelResult);
3290
+ }
3291
+ return cancelPromise;
3292
+ }
3293
+ function cancel2Algorithm(reason) {
3294
+ canceled2 = true;
3295
+ reason2 = reason;
3296
+ if (canceled1) {
3297
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3298
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3299
+ resolveCancelPromise(cancelResult);
3300
+ }
3301
+ return cancelPromise;
3302
+ }
3303
+ function startAlgorithm() {
3304
+ return;
3305
+ }
3306
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3307
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3308
+ forwardReaderError(reader);
3309
+ return [branch1, branch2];
3310
+ }
3011
3311
 
3012
3312
  function convertUnderlyingDefaultOrByteSource(source, context) {
3013
3313
  assertDictionary(source, context);
@@ -3269,6 +3569,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3269
3569
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3270
3570
  return stream;
3271
3571
  }
3572
+ // Throws if and only if startAlgorithm throws.
3573
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3574
+ const stream = Object.create(ReadableStream.prototype);
3575
+ InitializeReadableStream(stream);
3576
+ const controller = Object.create(ReadableByteStreamController.prototype);
3577
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3578
+ return stream;
3579
+ }
3272
3580
  function InitializeReadableStream(stream) {
3273
3581
  stream._state = 'readable';
3274
3582
  stream._reader = undefined;
@@ -3282,7 +3590,7 @@ function IsReadableStream(x) {
3282
3590
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3283
3591
  return false;
3284
3592
  }
3285
- return true;
3593
+ return x instanceof ReadableStream;
3286
3594
  }
3287
3595
  function IsReadableStreamLocked(stream) {
3288
3596
  if (stream._reader === undefined) {
@@ -3300,6 +3608,13 @@ function ReadableStreamCancel(stream, reason) {
3300
3608
  return promiseRejectedWith(stream._storedError);
3301
3609
  }
3302
3610
  ReadableStreamClose(stream);
3611
+ const reader = stream._reader;
3612
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3613
+ reader._readIntoRequests.forEach(readIntoRequest => {
3614
+ readIntoRequest._closeSteps(undefined);
3615
+ });
3616
+ reader._readIntoRequests = new SimpleQueue();
3617
+ }
3303
3618
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3304
3619
  return transformPromiseWith(sourceCancelPromise, noop);
3305
3620
  }
@@ -3352,9 +3667,14 @@ function convertQueuingStrategyInit(init, context) {
3352
3667
  };
3353
3668
  }
3354
3669
 
3355
- const byteLengthSizeFunction = function size(chunk) {
3670
+ // The size function must not have a prototype property nor be a constructor
3671
+ const byteLengthSizeFunction = (chunk) => {
3356
3672
  return chunk.byteLength;
3357
3673
  };
3674
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3675
+ value: 'size',
3676
+ configurable: true
3677
+ });
3358
3678
  /**
3359
3679
  * A queuing strategy that counts the number of bytes in each chunk.
3360
3680
  *
@@ -3406,12 +3726,17 @@ function IsByteLengthQueuingStrategy(x) {
3406
3726
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3407
3727
  return false;
3408
3728
  }
3409
- return true;
3729
+ return x instanceof ByteLengthQueuingStrategy;
3410
3730
  }
3411
3731
 
3412
- const countSizeFunction = function size() {
3732
+ // The size function must not have a prototype property nor be a constructor
3733
+ const countSizeFunction = () => {
3413
3734
  return 1;
3414
3735
  };
3736
+ Object.defineProperty(countSizeFunction, 'name', {
3737
+ value: 'size',
3738
+ configurable: true
3739
+ });
3415
3740
  /**
3416
3741
  * A queuing strategy that counts the number of chunks.
3417
3742
  *
@@ -3464,7 +3789,7 @@ function IsCountQueuingStrategy(x) {
3464
3789
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3465
3790
  return false;
3466
3791
  }
3467
- return true;
3792
+ return x instanceof CountQueuingStrategy;
3468
3793
  }
3469
3794
 
3470
3795
  function convertTransformer(original, context) {
@@ -3606,7 +3931,7 @@ function IsTransformStream(x) {
3606
3931
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3607
3932
  return false;
3608
3933
  }
3609
- return true;
3934
+ return x instanceof TransformStream;
3610
3935
  }
3611
3936
  // This is a no-op if both sides are already errored.
3612
3937
  function TransformStreamError(stream, e) {
@@ -3700,7 +4025,7 @@ function IsTransformStreamDefaultController(x) {
3700
4025
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3701
4026
  return false;
3702
4027
  }
3703
- return true;
4028
+ return x instanceof TransformStreamDefaultController;
3704
4029
  }
3705
4030
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3706
4031
  controller._controlledTransformStream = stream;