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;
@@ -620,7 +620,14 @@ function IsReadableStreamAsyncIterator(x) {
620
620
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
621
621
  return false;
622
622
  }
623
- return true;
623
+ try {
624
+ // noinspection SuspiciousTypeOfGuard
625
+ return x._asyncIteratorImpl instanceof
626
+ ReadableStreamAsyncIteratorImpl;
627
+ }
628
+ catch (_a) {
629
+ return false;
630
+ }
624
631
  }
625
632
  // Helper functions for the ReadableStream.
626
633
  function streamAsyncIteratorBrandCheckException(name) {
@@ -634,15 +641,35 @@ const NumberIsNaN = Number.isNaN || function (x) {
634
641
  return x !== x;
635
642
  };
636
643
 
637
- function IsFiniteNonNegativeNumber(v) {
638
- if (!IsNonNegativeNumber(v)) {
639
- return false;
640
- }
641
- if (v === Infinity) {
642
- return false;
644
+ function CreateArrayFromList(elements) {
645
+ // We use arrays to represent lists, so this is basically a no-op.
646
+ // Do a slice though just in case we happen to depend on the unique-ness.
647
+ return elements.slice();
648
+ }
649
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
650
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
651
+ }
652
+ // Not implemented correctly
653
+ function TransferArrayBuffer(O) {
654
+ return O;
655
+ }
656
+ // Not implemented correctly
657
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
658
+ function IsDetachedBuffer(O) {
659
+ return false;
660
+ }
661
+ function ArrayBufferSlice(buffer, begin, end) {
662
+ // ArrayBuffer.prototype.slice is not available on IE10
663
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
664
+ if (buffer.slice) {
665
+ return buffer.slice(begin, end);
643
666
  }
644
- return true;
667
+ const length = end - begin;
668
+ const slice = new ArrayBuffer(length);
669
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
670
+ return slice;
645
671
  }
672
+
646
673
  function IsNonNegativeNumber(v) {
647
674
  if (typeof v !== 'number') {
648
675
  return false;
@@ -655,6 +682,10 @@ function IsNonNegativeNumber(v) {
655
682
  }
656
683
  return true;
657
684
  }
685
+ function CloneAsUint8Array(O) {
686
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
687
+ return new Uint8Array(buffer);
688
+ }
658
689
 
659
690
  function DequeueValue(container) {
660
691
  const pair = container._queue.shift();
@@ -665,8 +696,7 @@ function DequeueValue(container) {
665
696
  return pair.value;
666
697
  }
667
698
  function EnqueueValueWithSize(container, value, size) {
668
- size = Number(size);
669
- if (!IsFiniteNonNegativeNumber(size)) {
699
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
670
700
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
671
701
  }
672
702
  container._queue.push({ value, size });
@@ -681,23 +711,6 @@ function ResetQueue(container) {
681
711
  container._queueTotalSize = 0;
682
712
  }
683
713
 
684
- function CreateArrayFromList(elements) {
685
- // We use arrays to represent lists, so this is basically a no-op.
686
- // Do a slice though just in case we happen to depend on the unique-ness.
687
- return elements.slice();
688
- }
689
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
690
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
691
- }
692
- // Not implemented correctly
693
- function TransferArrayBuffer(O) {
694
- return O;
695
- }
696
- // Not implemented correctly
697
- function IsDetachedBuffer(O) {
698
- return false;
699
- }
700
-
701
714
  /**
702
715
  * A pull-into request in a {@link ReadableByteStreamController}.
703
716
  *
@@ -736,15 +749,10 @@ class ReadableStreamBYOBRequest {
736
749
  if (!ArrayBuffer.isView(view)) {
737
750
  throw new TypeError('You can only respond with array buffer views');
738
751
  }
739
- if (view.byteLength === 0) {
740
- throw new TypeError('chunk must have non-zero byteLength');
741
- }
742
- if (view.buffer.byteLength === 0) {
743
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
744
- }
745
752
  if (this._associatedReadableByteStreamController === undefined) {
746
753
  throw new TypeError('This BYOB request has been invalidated');
747
754
  }
755
+ if (IsDetachedBuffer(view.buffer)) ;
748
756
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
749
757
  }
750
758
  }
@@ -775,14 +783,7 @@ class ReadableByteStreamController {
775
783
  if (!IsReadableByteStreamController(this)) {
776
784
  throw byteStreamControllerBrandCheckException('byobRequest');
777
785
  }
778
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
779
- const firstDescriptor = this._pendingPullIntos.peek();
780
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
781
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
782
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
783
- this._byobRequest = byobRequest;
784
- }
785
- return this._byobRequest;
786
+ return ReadableByteStreamControllerGetBYOBRequest(this);
786
787
  }
787
788
  /**
788
789
  * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
@@ -845,10 +846,7 @@ class ReadableByteStreamController {
845
846
  }
846
847
  /** @internal */
847
848
  [CancelSteps](reason) {
848
- if (this._pendingPullIntos.length > 0) {
849
- const firstDescriptor = this._pendingPullIntos.peek();
850
- firstDescriptor.bytesFilled = 0;
851
- }
849
+ ReadableByteStreamControllerClearPendingPullIntos(this);
852
850
  ResetQueue(this);
853
851
  const result = this._cancelAlgorithm(reason);
854
852
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -877,6 +875,7 @@ class ReadableByteStreamController {
877
875
  }
878
876
  const pullIntoDescriptor = {
879
877
  buffer,
878
+ bufferByteLength: autoAllocateChunkSize,
880
879
  byteOffset: 0,
881
880
  byteLength: autoAllocateChunkSize,
882
881
  bytesFilled: 0,
@@ -911,7 +910,7 @@ function IsReadableByteStreamController(x) {
911
910
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
912
911
  return false;
913
912
  }
914
- return true;
913
+ return x instanceof ReadableByteStreamController;
915
914
  }
916
915
  function IsReadableStreamBYOBRequest(x) {
917
916
  if (!typeIsObject(x)) {
@@ -920,7 +919,7 @@ function IsReadableStreamBYOBRequest(x) {
920
919
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
921
920
  return false;
922
921
  }
923
- return true;
922
+ return x instanceof ReadableStreamBYOBRequest;
924
923
  }
925
924
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
926
925
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1002,7 +1001,6 @@ function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller,
1002
1001
  return ready;
1003
1002
  }
1004
1003
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1005
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1006
1004
  pullIntoDescriptor.bytesFilled += size;
1007
1005
  }
1008
1006
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1041,9 +1039,15 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1041
1039
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1042
1040
  }
1043
1041
  const ctor = view.constructor;
1042
+ // try {
1044
1043
  const buffer = TransferArrayBuffer(view.buffer);
1044
+ // } catch (e) {
1045
+ // readIntoRequest._errorSteps(e);
1046
+ // return;
1047
+ // }
1045
1048
  const pullIntoDescriptor = {
1046
1049
  buffer,
1050
+ bufferByteLength: buffer.byteLength,
1047
1051
  byteOffset: view.byteOffset,
1048
1052
  byteLength: view.byteLength,
1049
1053
  bytesFilled: 0,
@@ -1083,7 +1087,6 @@ function ReadableByteStreamControllerPullInto(controller, view, readIntoRequest)
1083
1087
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1084
1088
  }
1085
1089
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1086
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1087
1090
  const stream = controller._controlledReadableByteStream;
1088
1091
  if (ReadableStreamHasBYOBReader(stream)) {
1089
1092
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1093,34 +1096,27 @@ function ReadableByteStreamControllerRespondInClosedState(controller, firstDescr
1093
1096
  }
1094
1097
  }
1095
1098
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1096
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1097
- throw new RangeError('bytesWritten out of range');
1098
- }
1099
1099
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1100
1100
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1101
- // TODO: Figure out whether we should detach the buffer or not here.
1102
1101
  return;
1103
1102
  }
1104
1103
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1105
1104
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1106
1105
  if (remainderSize > 0) {
1107
1106
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1108
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1107
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1109
1108
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1110
1109
  }
1111
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1112
1110
  pullIntoDescriptor.bytesFilled -= remainderSize;
1113
1111
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1114
1112
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1115
1113
  }
1116
1114
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1117
1115
  const firstDescriptor = controller._pendingPullIntos.peek();
1116
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1118
1117
  const state = controller._controlledReadableByteStream._state;
1119
1118
  if (state === 'closed') {
1120
- if (bytesWritten !== 0) {
1121
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1122
- }
1123
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1119
+ ReadableByteStreamControllerRespondInClosedState(controller);
1124
1120
  }
1125
1121
  else {
1126
1122
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1129,7 +1125,6 @@ function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1129
1125
  }
1130
1126
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1131
1127
  const descriptor = controller._pendingPullIntos.shift();
1132
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1133
1128
  return descriptor;
1134
1129
  }
1135
1130
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1189,6 +1184,12 @@ function ReadableByteStreamControllerEnqueue(controller, chunk) {
1189
1184
  const byteOffset = chunk.byteOffset;
1190
1185
  const byteLength = chunk.byteLength;
1191
1186
  const transferredBuffer = TransferArrayBuffer(buffer);
1187
+ if (controller._pendingPullIntos.length > 0) {
1188
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1189
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1190
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1191
+ }
1192
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1192
1193
  if (ReadableStreamHasDefaultReader(stream)) {
1193
1194
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1194
1195
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
@@ -1218,6 +1219,16 @@ function ReadableByteStreamControllerError(controller, e) {
1218
1219
  ReadableByteStreamControllerClearAlgorithms(controller);
1219
1220
  ReadableStreamError(stream, e);
1220
1221
  }
1222
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1223
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1224
+ const firstDescriptor = controller._pendingPullIntos.peek();
1225
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1226
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1227
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1228
+ controller._byobRequest = byobRequest;
1229
+ }
1230
+ return controller._byobRequest;
1231
+ }
1221
1232
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1222
1233
  const state = controller._controlledReadableByteStream._state;
1223
1234
  if (state === 'errored') {
@@ -1229,21 +1240,47 @@ function ReadableByteStreamControllerGetDesiredSize(controller) {
1229
1240
  return controller._strategyHWM - controller._queueTotalSize;
1230
1241
  }
1231
1242
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1232
- bytesWritten = Number(bytesWritten);
1233
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1234
- throw new RangeError('bytesWritten must be a finite');
1243
+ const firstDescriptor = controller._pendingPullIntos.peek();
1244
+ const state = controller._controlledReadableByteStream._state;
1245
+ if (state === 'closed') {
1246
+ if (bytesWritten !== 0) {
1247
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1248
+ }
1235
1249
  }
1250
+ else {
1251
+ if (bytesWritten === 0) {
1252
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1253
+ }
1254
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1255
+ throw new RangeError('bytesWritten out of range');
1256
+ }
1257
+ }
1258
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1236
1259
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1237
1260
  }
1238
1261
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1239
1262
  const firstDescriptor = controller._pendingPullIntos.peek();
1263
+ const state = controller._controlledReadableByteStream._state;
1264
+ if (state === 'closed') {
1265
+ if (view.byteLength !== 0) {
1266
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1267
+ }
1268
+ }
1269
+ else {
1270
+ if (view.byteLength === 0) {
1271
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1272
+ }
1273
+ }
1240
1274
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1241
1275
  throw new RangeError('The region specified by view does not match byobRequest');
1242
1276
  }
1243
- if (firstDescriptor.byteLength !== view.byteLength) {
1277
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1244
1278
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1245
1279
  }
1246
- firstDescriptor.buffer = view.buffer;
1280
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1281
+ throw new RangeError('The region specified by view is larger than byobRequest');
1282
+ }
1283
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1247
1284
  ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1248
1285
  }
1249
1286
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
@@ -1393,6 +1430,7 @@ class ReadableStreamBYOBReader {
1393
1430
  if (view.buffer.byteLength === 0) {
1394
1431
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1395
1432
  }
1433
+ if (IsDetachedBuffer(view.buffer)) ;
1396
1434
  if (this._ownerReadableStream === undefined) {
1397
1435
  return promiseRejectedWith(readerLockException('read from'));
1398
1436
  }
@@ -1452,7 +1490,7 @@ function IsReadableStreamBYOBReader(x) {
1452
1490
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1453
1491
  return false;
1454
1492
  }
1455
- return true;
1493
+ return x instanceof ReadableStreamBYOBReader;
1456
1494
  }
1457
1495
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1458
1496
  const stream = reader._ownerReadableStream;
@@ -1547,6 +1585,31 @@ function assertWritableStream(x, context) {
1547
1585
  }
1548
1586
  }
1549
1587
 
1588
+ function isAbortSignal(value) {
1589
+ if (typeof value !== 'object' || value === null) {
1590
+ return false;
1591
+ }
1592
+ try {
1593
+ return typeof value.aborted === 'boolean';
1594
+ }
1595
+ catch (_a) {
1596
+ // AbortSignal.prototype.aborted throws if its brand check fails
1597
+ return false;
1598
+ }
1599
+ }
1600
+ const supportsAbortController = typeof AbortController === 'function';
1601
+ /**
1602
+ * Construct a new AbortController, if supported by the platform.
1603
+ *
1604
+ * @internal
1605
+ */
1606
+ function createAbortController() {
1607
+ if (supportsAbortController) {
1608
+ return new AbortController();
1609
+ }
1610
+ return undefined;
1611
+ }
1612
+
1550
1613
  /**
1551
1614
  * A writable stream represents a destination for data, into which you can write.
1552
1615
  *
@@ -1690,7 +1753,7 @@ function IsWritableStream(x) {
1690
1753
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1691
1754
  return false;
1692
1755
  }
1693
- return true;
1756
+ return x instanceof WritableStream;
1694
1757
  }
1695
1758
  function IsWritableStreamLocked(stream) {
1696
1759
  if (stream._writer === undefined) {
@@ -1699,6 +1762,15 @@ function IsWritableStreamLocked(stream) {
1699
1762
  return true;
1700
1763
  }
1701
1764
  function WritableStreamAbort(stream, reason) {
1765
+ var _a;
1766
+ if (stream._state === 'closed' || stream._state === 'errored') {
1767
+ return promiseResolvedWith(undefined);
1768
+ }
1769
+ stream._writableStreamController._abortReason = reason;
1770
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1771
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1772
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1773
+ // Widen the type again by casting to WritableStreamState.
1702
1774
  const state = stream._state;
1703
1775
  if (state === 'closed' || state === 'errored') {
1704
1776
  return promiseResolvedWith(undefined);
@@ -2044,7 +2116,7 @@ function IsWritableStreamDefaultWriter(x) {
2044
2116
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2045
2117
  return false;
2046
2118
  }
2047
- return true;
2119
+ return x instanceof WritableStreamDefaultWriter;
2048
2120
  }
2049
2121
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2050
2122
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2134,6 +2206,30 @@ class WritableStreamDefaultController {
2134
2206
  constructor() {
2135
2207
  throw new TypeError('Illegal constructor');
2136
2208
  }
2209
+ /**
2210
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2211
+ */
2212
+ get abortReason() {
2213
+ if (!IsWritableStreamDefaultController(this)) {
2214
+ throw defaultControllerBrandCheckException$2('abortReason');
2215
+ }
2216
+ return this._abortReason;
2217
+ }
2218
+ /**
2219
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2220
+ */
2221
+ get signal() {
2222
+ if (!IsWritableStreamDefaultController(this)) {
2223
+ throw defaultControllerBrandCheckException$2('signal');
2224
+ }
2225
+ if (this._abortController === undefined) {
2226
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2227
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2228
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2229
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2230
+ }
2231
+ return this._abortController.signal;
2232
+ }
2137
2233
  /**
2138
2234
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2139
2235
  *
@@ -2143,7 +2239,7 @@ class WritableStreamDefaultController {
2143
2239
  */
2144
2240
  error(e = undefined) {
2145
2241
  if (!IsWritableStreamDefaultController(this)) {
2146
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2242
+ throw defaultControllerBrandCheckException$2('error');
2147
2243
  }
2148
2244
  const state = this._controlledWritableStream._state;
2149
2245
  if (state !== 'writable') {
@@ -2181,7 +2277,7 @@ function IsWritableStreamDefaultController(x) {
2181
2277
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2182
2278
  return false;
2183
2279
  }
2184
- return true;
2280
+ return x instanceof WritableStreamDefaultController;
2185
2281
  }
2186
2282
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2187
2283
  controller._controlledWritableStream = stream;
@@ -2190,6 +2286,8 @@ function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm
2190
2286
  controller._queue = undefined;
2191
2287
  controller._queueTotalSize = undefined;
2192
2288
  ResetQueue(controller);
2289
+ controller._abortReason = undefined;
2290
+ controller._abortController = createAbortController();
2193
2291
  controller._started = false;
2194
2292
  controller._strategySizeAlgorithm = sizeAlgorithm;
2195
2293
  controller._strategyHWM = highWaterMark;
@@ -2342,6 +2440,10 @@ function WritableStreamDefaultControllerError(controller, error) {
2342
2440
  function streamBrandCheckException$2(name) {
2343
2441
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2344
2442
  }
2443
+ // Helper functions for the WritableStreamDefaultController.
2444
+ function defaultControllerBrandCheckException$2(name) {
2445
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2446
+ }
2345
2447
  // Helper functions for the WritableStreamDefaultWriter.
2346
2448
  function defaultWriterBrandCheckException(name) {
2347
2449
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2427,19 +2529,6 @@ function defaultWriterReadyPromiseResolve(writer) {
2427
2529
  writer._readyPromiseState = 'fulfilled';
2428
2530
  }
2429
2531
 
2430
- function isAbortSignal(value) {
2431
- if (typeof value !== 'object' || value === null) {
2432
- return false;
2433
- }
2434
- try {
2435
- return typeof value.aborted === 'boolean';
2436
- }
2437
- catch (_a) {
2438
- // AbortSignal.prototype.aborted throws if its brand check fails
2439
- return false;
2440
- }
2441
- }
2442
-
2443
2532
  /// <reference lib="dom" />
2444
2533
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2445
2534
 
@@ -2744,7 +2833,7 @@ function IsReadableStreamDefaultController(x) {
2744
2833
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2745
2834
  return false;
2746
2835
  }
2747
- return true;
2836
+ return x instanceof ReadableStreamDefaultController;
2748
2837
  }
2749
2838
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2750
2839
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2905,6 +2994,12 @@ function defaultControllerBrandCheckException$1(name) {
2905
2994
  }
2906
2995
 
2907
2996
  function ReadableStreamTee(stream, cloneForBranch2) {
2997
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
2998
+ return ReadableByteStreamTee(stream);
2999
+ }
3000
+ return ReadableStreamDefaultTee(stream);
3001
+ }
3002
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2908
3003
  const reader = AcquireReadableStreamDefaultReader(stream);
2909
3004
  let reading = false;
2910
3005
  let canceled1 = false;
@@ -2923,24 +3018,24 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2923
3018
  }
2924
3019
  reading = true;
2925
3020
  const readRequest = {
2926
- _chunkSteps: value => {
3021
+ _chunkSteps: chunk => {
2927
3022
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2928
3023
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2929
3024
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2930
3025
  queueMicrotask(() => {
2931
3026
  reading = false;
2932
- const value1 = value;
2933
- const value2 = value;
3027
+ const chunk1 = chunk;
3028
+ const chunk2 = chunk;
2934
3029
  // There is no way to access the cloning code right now in the reference implementation.
2935
3030
  // If we add one then we'll need an implementation for serializable objects.
2936
3031
  // if (!canceled2 && cloneForBranch2) {
2937
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3032
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2938
3033
  // }
2939
3034
  if (!canceled1) {
2940
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3035
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2941
3036
  }
2942
3037
  if (!canceled2) {
2943
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3038
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2944
3039
  }
2945
3040
  });
2946
3041
  },
@@ -2997,6 +3092,211 @@ function ReadableStreamTee(stream, cloneForBranch2) {
2997
3092
  });
2998
3093
  return [branch1, branch2];
2999
3094
  }
3095
+ function ReadableByteStreamTee(stream) {
3096
+ let reader = AcquireReadableStreamDefaultReader(stream);
3097
+ let reading = false;
3098
+ let canceled1 = false;
3099
+ let canceled2 = false;
3100
+ let reason1;
3101
+ let reason2;
3102
+ let branch1;
3103
+ let branch2;
3104
+ let resolveCancelPromise;
3105
+ const cancelPromise = newPromise(resolve => {
3106
+ resolveCancelPromise = resolve;
3107
+ });
3108
+ function forwardReaderError(thisReader) {
3109
+ uponRejection(thisReader._closedPromise, r => {
3110
+ if (thisReader !== reader) {
3111
+ return;
3112
+ }
3113
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3114
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3115
+ if (!canceled1 || !canceled2) {
3116
+ resolveCancelPromise(undefined);
3117
+ }
3118
+ });
3119
+ }
3120
+ function pullWithDefaultReader() {
3121
+ if (IsReadableStreamBYOBReader(reader)) {
3122
+ ReadableStreamReaderGenericRelease(reader);
3123
+ reader = AcquireReadableStreamDefaultReader(stream);
3124
+ forwardReaderError(reader);
3125
+ }
3126
+ const readRequest = {
3127
+ _chunkSteps: chunk => {
3128
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3129
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3130
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3131
+ queueMicrotask(() => {
3132
+ reading = false;
3133
+ const chunk1 = chunk;
3134
+ let chunk2 = chunk;
3135
+ if (!canceled1 && !canceled2) {
3136
+ try {
3137
+ chunk2 = CloneAsUint8Array(chunk);
3138
+ }
3139
+ catch (cloneE) {
3140
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3141
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3142
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3143
+ return;
3144
+ }
3145
+ }
3146
+ if (!canceled1) {
3147
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3148
+ }
3149
+ if (!canceled2) {
3150
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3151
+ }
3152
+ });
3153
+ },
3154
+ _closeSteps: () => {
3155
+ reading = false;
3156
+ if (!canceled1) {
3157
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3158
+ }
3159
+ if (!canceled2) {
3160
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3161
+ }
3162
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3163
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3164
+ }
3165
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3166
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3167
+ }
3168
+ if (!canceled1 || !canceled2) {
3169
+ resolveCancelPromise(undefined);
3170
+ }
3171
+ },
3172
+ _errorSteps: () => {
3173
+ reading = false;
3174
+ }
3175
+ };
3176
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3177
+ }
3178
+ function pullWithBYOBReader(view, forBranch2) {
3179
+ if (IsReadableStreamDefaultReader(reader)) {
3180
+ ReadableStreamReaderGenericRelease(reader);
3181
+ reader = AcquireReadableStreamBYOBReader(stream);
3182
+ forwardReaderError(reader);
3183
+ }
3184
+ const byobBranch = forBranch2 ? branch2 : branch1;
3185
+ const otherBranch = forBranch2 ? branch1 : branch2;
3186
+ const readIntoRequest = {
3187
+ _chunkSteps: chunk => {
3188
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3189
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3190
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3191
+ queueMicrotask(() => {
3192
+ reading = false;
3193
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3194
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3195
+ if (!otherCanceled) {
3196
+ let clonedChunk;
3197
+ try {
3198
+ clonedChunk = CloneAsUint8Array(chunk);
3199
+ }
3200
+ catch (cloneE) {
3201
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3202
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3203
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3204
+ return;
3205
+ }
3206
+ if (!byobCanceled) {
3207
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3208
+ }
3209
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3210
+ }
3211
+ else if (!byobCanceled) {
3212
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3213
+ }
3214
+ });
3215
+ },
3216
+ _closeSteps: chunk => {
3217
+ reading = false;
3218
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3219
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3220
+ if (!byobCanceled) {
3221
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3222
+ }
3223
+ if (!otherCanceled) {
3224
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3225
+ }
3226
+ if (chunk !== undefined) {
3227
+ if (!byobCanceled) {
3228
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3229
+ }
3230
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3231
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3232
+ }
3233
+ }
3234
+ if (!byobCanceled || !otherCanceled) {
3235
+ resolveCancelPromise(undefined);
3236
+ }
3237
+ },
3238
+ _errorSteps: () => {
3239
+ reading = false;
3240
+ }
3241
+ };
3242
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3243
+ }
3244
+ function pull1Algorithm() {
3245
+ if (reading) {
3246
+ return promiseResolvedWith(undefined);
3247
+ }
3248
+ reading = true;
3249
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3250
+ if (byobRequest === null) {
3251
+ pullWithDefaultReader();
3252
+ }
3253
+ else {
3254
+ pullWithBYOBReader(byobRequest._view, false);
3255
+ }
3256
+ return promiseResolvedWith(undefined);
3257
+ }
3258
+ function pull2Algorithm() {
3259
+ if (reading) {
3260
+ return promiseResolvedWith(undefined);
3261
+ }
3262
+ reading = true;
3263
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3264
+ if (byobRequest === null) {
3265
+ pullWithDefaultReader();
3266
+ }
3267
+ else {
3268
+ pullWithBYOBReader(byobRequest._view, true);
3269
+ }
3270
+ return promiseResolvedWith(undefined);
3271
+ }
3272
+ function cancel1Algorithm(reason) {
3273
+ canceled1 = true;
3274
+ reason1 = reason;
3275
+ if (canceled2) {
3276
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3277
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3278
+ resolveCancelPromise(cancelResult);
3279
+ }
3280
+ return cancelPromise;
3281
+ }
3282
+ function cancel2Algorithm(reason) {
3283
+ canceled2 = true;
3284
+ reason2 = reason;
3285
+ if (canceled1) {
3286
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3287
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3288
+ resolveCancelPromise(cancelResult);
3289
+ }
3290
+ return cancelPromise;
3291
+ }
3292
+ function startAlgorithm() {
3293
+ return;
3294
+ }
3295
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3296
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3297
+ forwardReaderError(reader);
3298
+ return [branch1, branch2];
3299
+ }
3000
3300
 
3001
3301
  function convertUnderlyingDefaultOrByteSource(source, context) {
3002
3302
  assertDictionary(source, context);
@@ -3258,6 +3558,14 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
3258
3558
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3259
3559
  return stream;
3260
3560
  }
3561
+ // Throws if and only if startAlgorithm throws.
3562
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3563
+ const stream = Object.create(ReadableStream.prototype);
3564
+ InitializeReadableStream(stream);
3565
+ const controller = Object.create(ReadableByteStreamController.prototype);
3566
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3567
+ return stream;
3568
+ }
3261
3569
  function InitializeReadableStream(stream) {
3262
3570
  stream._state = 'readable';
3263
3571
  stream._reader = undefined;
@@ -3271,7 +3579,7 @@ function IsReadableStream(x) {
3271
3579
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3272
3580
  return false;
3273
3581
  }
3274
- return true;
3582
+ return x instanceof ReadableStream;
3275
3583
  }
3276
3584
  function IsReadableStreamLocked(stream) {
3277
3585
  if (stream._reader === undefined) {
@@ -3289,6 +3597,13 @@ function ReadableStreamCancel(stream, reason) {
3289
3597
  return promiseRejectedWith(stream._storedError);
3290
3598
  }
3291
3599
  ReadableStreamClose(stream);
3600
+ const reader = stream._reader;
3601
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3602
+ reader._readIntoRequests.forEach(readIntoRequest => {
3603
+ readIntoRequest._closeSteps(undefined);
3604
+ });
3605
+ reader._readIntoRequests = new SimpleQueue();
3606
+ }
3292
3607
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3293
3608
  return transformPromiseWith(sourceCancelPromise, noop);
3294
3609
  }
@@ -3341,9 +3656,14 @@ function convertQueuingStrategyInit(init, context) {
3341
3656
  };
3342
3657
  }
3343
3658
 
3344
- const byteLengthSizeFunction = function size(chunk) {
3659
+ // The size function must not have a prototype property nor be a constructor
3660
+ const byteLengthSizeFunction = (chunk) => {
3345
3661
  return chunk.byteLength;
3346
3662
  };
3663
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3664
+ value: 'size',
3665
+ configurable: true
3666
+ });
3347
3667
  /**
3348
3668
  * A queuing strategy that counts the number of bytes in each chunk.
3349
3669
  *
@@ -3395,12 +3715,17 @@ function IsByteLengthQueuingStrategy(x) {
3395
3715
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3396
3716
  return false;
3397
3717
  }
3398
- return true;
3718
+ return x instanceof ByteLengthQueuingStrategy;
3399
3719
  }
3400
3720
 
3401
- const countSizeFunction = function size() {
3721
+ // The size function must not have a prototype property nor be a constructor
3722
+ const countSizeFunction = () => {
3402
3723
  return 1;
3403
3724
  };
3725
+ Object.defineProperty(countSizeFunction, 'name', {
3726
+ value: 'size',
3727
+ configurable: true
3728
+ });
3404
3729
  /**
3405
3730
  * A queuing strategy that counts the number of chunks.
3406
3731
  *
@@ -3453,7 +3778,7 @@ function IsCountQueuingStrategy(x) {
3453
3778
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3454
3779
  return false;
3455
3780
  }
3456
- return true;
3781
+ return x instanceof CountQueuingStrategy;
3457
3782
  }
3458
3783
 
3459
3784
  function convertTransformer(original, context) {
@@ -3595,7 +3920,7 @@ function IsTransformStream(x) {
3595
3920
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3596
3921
  return false;
3597
3922
  }
3598
- return true;
3923
+ return x instanceof TransformStream;
3599
3924
  }
3600
3925
  // This is a no-op if both sides are already errored.
3601
3926
  function TransformStreamError(stream, e) {
@@ -3689,7 +4014,7 @@ function IsTransformStreamDefaultController(x) {
3689
4014
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3690
4015
  return false;
3691
4016
  }
3692
- return true;
4017
+ return x instanceof TransformStreamDefaultController;
3693
4018
  }
3694
4019
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3695
4020
  controller._controlledTransformStream = stream;