web-streams-polyfill 3.0.1 → 3.1.1

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,10 +1,10 @@
1
1
  /**
2
- * web-streams-polyfill v3.0.1
2
+ * web-streams-polyfill v3.1.1
3
3
  */
4
4
  (function (global, factory) {
5
5
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
6
6
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
7
- (global = global || self, factory(global.WebStreamsPolyfill = {}));
7
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.WebStreamsPolyfill = {}));
8
8
  }(this, (function (exports) { 'use strict';
9
9
 
10
10
  /// <reference lib="es2015.symbol" />
@@ -14,7 +14,7 @@
14
14
 
15
15
  /// <reference lib="dom" />
16
16
  function noop() {
17
- // do nothing
17
+ return undefined;
18
18
  }
19
19
  function getGlobals() {
20
20
  if (typeof self !== 'undefined') {
@@ -33,7 +33,7 @@
33
33
  function typeIsObject(x) {
34
34
  return (typeof x === 'object' && x !== null) || typeof x === 'function';
35
35
  }
36
- const rethrowAssertionErrorRejection = noop;
36
+ const rethrowAssertionErrorRejection = noop;
37
37
 
38
38
  const originalPromise = Promise;
39
39
  const originalPromiseThen = Promise.prototype.then;
@@ -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
+ }
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
+ }
1252
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) {
@@ -1302,6 +1339,9 @@
1302
1339
  cancelAlgorithm = reason => underlyingByteSource.cancel(reason);
1303
1340
  }
1304
1341
  const autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
1342
+ if (autoAllocateChunkSize === 0) {
1343
+ throw new TypeError('autoAllocateChunkSize must be greater than 0');
1344
+ }
1305
1345
  SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
1306
1346
  }
1307
1347
  function SetUpReadableStreamBYOBRequest(request, controller, view) {
@@ -1407,6 +1447,7 @@
1407
1447
  if (view.buffer.byteLength === 0) {
1408
1448
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1409
1449
  }
1450
+ if (IsDetachedBuffer(view.buffer)) ;
1410
1451
  if (this._ownerReadableStream === undefined) {
1411
1452
  return promiseRejectedWith(readerLockException('read from'));
1412
1453
  }
@@ -1466,7 +1507,7 @@
1466
1507
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1467
1508
  return false;
1468
1509
  }
1469
- return true;
1510
+ return x instanceof ReadableStreamBYOBReader;
1470
1511
  }
1471
1512
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1472
1513
  const stream = reader._ownerReadableStream;
@@ -1561,6 +1602,31 @@
1561
1602
  }
1562
1603
  }
1563
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
+
1564
1630
  /**
1565
1631
  * A writable stream represents a destination for data, into which you can write.
1566
1632
  *
@@ -1590,7 +1656,7 @@
1590
1656
  */
1591
1657
  get locked() {
1592
1658
  if (!IsWritableStream(this)) {
1593
- throw streamBrandCheckException('locked');
1659
+ throw streamBrandCheckException$2('locked');
1594
1660
  }
1595
1661
  return IsWritableStreamLocked(this);
1596
1662
  }
@@ -1605,7 +1671,7 @@
1605
1671
  */
1606
1672
  abort(reason = undefined) {
1607
1673
  if (!IsWritableStream(this)) {
1608
- return promiseRejectedWith(streamBrandCheckException('abort'));
1674
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1609
1675
  }
1610
1676
  if (IsWritableStreamLocked(this)) {
1611
1677
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
@@ -1622,7 +1688,7 @@
1622
1688
  */
1623
1689
  close() {
1624
1690
  if (!IsWritableStream(this)) {
1625
- return promiseRejectedWith(streamBrandCheckException('close'));
1691
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1626
1692
  }
1627
1693
  if (IsWritableStreamLocked(this)) {
1628
1694
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1642,7 +1708,7 @@
1642
1708
  */
1643
1709
  getWriter() {
1644
1710
  if (!IsWritableStream(this)) {
1645
- throw streamBrandCheckException('getWriter');
1711
+ throw streamBrandCheckException$2('getWriter');
1646
1712
  }
1647
1713
  return AcquireWritableStreamDefaultWriter(this);
1648
1714
  }
@@ -1704,7 +1770,7 @@
1704
1770
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1705
1771
  return false;
1706
1772
  }
1707
- return true;
1773
+ return x instanceof WritableStream;
1708
1774
  }
1709
1775
  function IsWritableStreamLocked(stream) {
1710
1776
  if (stream._writer === undefined) {
@@ -1713,6 +1779,15 @@
1713
1779
  return true;
1714
1780
  }
1715
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.
1716
1791
  const state = stream._state;
1717
1792
  if (state === 'closed' || state === 'errored') {
1718
1793
  return promiseResolvedWith(undefined);
@@ -2058,7 +2133,7 @@
2058
2133
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2059
2134
  return false;
2060
2135
  }
2061
- return true;
2136
+ return x instanceof WritableStreamDefaultWriter;
2062
2137
  }
2063
2138
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2064
2139
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2148,6 +2223,30 @@
2148
2223
  constructor() {
2149
2224
  throw new TypeError('Illegal constructor');
2150
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
+ }
2151
2250
  /**
2152
2251
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2153
2252
  *
@@ -2157,7 +2256,7 @@
2157
2256
  */
2158
2257
  error(e = undefined) {
2159
2258
  if (!IsWritableStreamDefaultController(this)) {
2160
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2259
+ throw defaultControllerBrandCheckException$2('error');
2161
2260
  }
2162
2261
  const state = this._controlledWritableStream._state;
2163
2262
  if (state !== 'writable') {
@@ -2195,7 +2294,7 @@
2195
2294
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2196
2295
  return false;
2197
2296
  }
2198
- return true;
2297
+ return x instanceof WritableStreamDefaultController;
2199
2298
  }
2200
2299
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2201
2300
  controller._controlledWritableStream = stream;
@@ -2204,6 +2303,8 @@
2204
2303
  controller._queue = undefined;
2205
2304
  controller._queueTotalSize = undefined;
2206
2305
  ResetQueue(controller);
2306
+ controller._abortReason = undefined;
2307
+ controller._abortController = createAbortController();
2207
2308
  controller._started = false;
2208
2309
  controller._strategySizeAlgorithm = sizeAlgorithm;
2209
2310
  controller._strategyHWM = highWaterMark;
@@ -2353,9 +2454,13 @@
2353
2454
  WritableStreamStartErroring(stream, error);
2354
2455
  }
2355
2456
  // Helper functions for the WritableStream.
2356
- function streamBrandCheckException(name) {
2457
+ function streamBrandCheckException$2(name) {
2357
2458
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2358
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
+ }
2359
2464
  // Helper functions for the WritableStreamDefaultWriter.
2360
2465
  function defaultWriterBrandCheckException(name) {
2361
2466
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2441,19 +2546,6 @@
2441
2546
  writer._readyPromiseState = 'fulfilled';
2442
2547
  }
2443
2548
 
2444
- function isAbortSignal(value) {
2445
- if (typeof value !== 'object' || value === null) {
2446
- return false;
2447
- }
2448
- try {
2449
- return typeof value.aborted === 'boolean';
2450
- }
2451
- catch (_a) {
2452
- // AbortSignal.prototype.aborted throws if its brand check fails
2453
- return false;
2454
- }
2455
- }
2456
-
2457
2549
  /// <reference lib="dom" />
2458
2550
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2459
2551
 
@@ -2471,6 +2563,7 @@
2471
2563
  }
2472
2564
  }
2473
2565
  function createDOMExceptionPolyfill() {
2566
+ // eslint-disable-next-line no-shadow
2474
2567
  const ctor = function DOMException(message, name) {
2475
2568
  this.message = message || '';
2476
2569
  this.name = name || 'Error';
@@ -2482,6 +2575,7 @@
2482
2575
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2483
2576
  return ctor;
2484
2577
  }
2578
+ // eslint-disable-next-line no-redeclare
2485
2579
  const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2486
2580
 
2487
2581
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2674,7 +2768,7 @@
2674
2768
  */
2675
2769
  get desiredSize() {
2676
2770
  if (!IsReadableStreamDefaultController(this)) {
2677
- throw defaultControllerBrandCheckException('desiredSize');
2771
+ throw defaultControllerBrandCheckException$1('desiredSize');
2678
2772
  }
2679
2773
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2680
2774
  }
@@ -2684,7 +2778,7 @@
2684
2778
  */
2685
2779
  close() {
2686
2780
  if (!IsReadableStreamDefaultController(this)) {
2687
- throw defaultControllerBrandCheckException('close');
2781
+ throw defaultControllerBrandCheckException$1('close');
2688
2782
  }
2689
2783
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2690
2784
  throw new TypeError('The stream is not in a state that permits close');
@@ -2693,7 +2787,7 @@
2693
2787
  }
2694
2788
  enqueue(chunk = undefined) {
2695
2789
  if (!IsReadableStreamDefaultController(this)) {
2696
- throw defaultControllerBrandCheckException('enqueue');
2790
+ throw defaultControllerBrandCheckException$1('enqueue');
2697
2791
  }
2698
2792
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2699
2793
  throw new TypeError('The stream is not in a state that permits enqueue');
@@ -2705,7 +2799,7 @@
2705
2799
  */
2706
2800
  error(e = undefined) {
2707
2801
  if (!IsReadableStreamDefaultController(this)) {
2708
- throw defaultControllerBrandCheckException('error');
2802
+ throw defaultControllerBrandCheckException$1('error');
2709
2803
  }
2710
2804
  ReadableStreamDefaultControllerError(this, e);
2711
2805
  }
@@ -2756,7 +2850,7 @@
2756
2850
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2757
2851
  return false;
2758
2852
  }
2759
- return true;
2853
+ return x instanceof ReadableStreamDefaultController;
2760
2854
  }
2761
2855
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2762
2856
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2912,11 +3006,17 @@
2912
3006
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2913
3007
  }
2914
3008
  // Helper functions for the ReadableStreamDefaultController.
2915
- function defaultControllerBrandCheckException(name) {
3009
+ function defaultControllerBrandCheckException$1(name) {
2916
3010
  return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
2917
3011
  }
2918
3012
 
2919
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) {
2920
3020
  const reader = AcquireReadableStreamDefaultReader(stream);
2921
3021
  let reading = false;
2922
3022
  let canceled1 = false;
@@ -2935,26 +3035,25 @@
2935
3035
  }
2936
3036
  reading = true;
2937
3037
  const readRequest = {
2938
- _chunkSteps: value => {
3038
+ _chunkSteps: chunk => {
2939
3039
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2940
3040
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2941
3041
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2942
3042
  queueMicrotask(() => {
2943
3043
  reading = false;
2944
- const value1 = value;
2945
- const value2 = value;
3044
+ const chunk1 = chunk;
3045
+ const chunk2 = chunk;
2946
3046
  // There is no way to access the cloning code right now in the reference implementation.
2947
3047
  // If we add one then we'll need an implementation for serializable objects.
2948
3048
  // if (!canceled2 && cloneForBranch2) {
2949
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3049
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2950
3050
  // }
2951
3051
  if (!canceled1) {
2952
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3052
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2953
3053
  }
2954
3054
  if (!canceled2) {
2955
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3055
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
2956
3056
  }
2957
- resolveCancelPromise(undefined);
2958
3057
  });
2959
3058
  },
2960
3059
  _closeSteps: () => {
@@ -2965,6 +3064,9 @@
2965
3064
  if (!canceled2) {
2966
3065
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2967
3066
  }
3067
+ if (!canceled1 || !canceled2) {
3068
+ resolveCancelPromise(undefined);
3069
+ }
2968
3070
  },
2969
3071
  _errorSteps: () => {
2970
3072
  reading = false;
@@ -3001,8 +3103,215 @@
3001
3103
  uponRejection(reader._closedPromise, (r) => {
3002
3104
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
3003
3105
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
3004
- resolveCancelPromise(undefined);
3106
+ if (!canceled1 || !canceled2) {
3107
+ resolveCancelPromise(undefined);
3108
+ }
3109
+ });
3110
+ return [branch1, branch2];
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;
3005
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);
3006
3315
  return [branch1, branch2];
3007
3316
  }
3008
3317
 
@@ -3266,6 +3575,14 @@
3266
3575
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3267
3576
  return stream;
3268
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
+ }
3269
3586
  function InitializeReadableStream(stream) {
3270
3587
  stream._state = 'readable';
3271
3588
  stream._reader = undefined;
@@ -3279,7 +3596,7 @@
3279
3596
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3280
3597
  return false;
3281
3598
  }
3282
- return true;
3599
+ return x instanceof ReadableStream;
3283
3600
  }
3284
3601
  function IsReadableStreamLocked(stream) {
3285
3602
  if (stream._reader === undefined) {
@@ -3297,6 +3614,13 @@
3297
3614
  return promiseRejectedWith(stream._storedError);
3298
3615
  }
3299
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
+ }
3300
3624
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3301
3625
  return transformPromiseWith(sourceCancelPromise, noop);
3302
3626
  }
@@ -3306,13 +3630,13 @@
3306
3630
  if (reader === undefined) {
3307
3631
  return;
3308
3632
  }
3633
+ defaultReaderClosedPromiseResolve(reader);
3309
3634
  if (IsReadableStreamDefaultReader(reader)) {
3310
3635
  reader._readRequests.forEach(readRequest => {
3311
3636
  readRequest._closeSteps();
3312
3637
  });
3313
3638
  reader._readRequests = new SimpleQueue();
3314
3639
  }
3315
- defaultReaderClosedPromiseResolve(reader);
3316
3640
  }
3317
3641
  function ReadableStreamError(stream, e) {
3318
3642
  stream._state = 'errored';
@@ -3321,6 +3645,7 @@
3321
3645
  if (reader === undefined) {
3322
3646
  return;
3323
3647
  }
3648
+ defaultReaderClosedPromiseReject(reader, e);
3324
3649
  if (IsReadableStreamDefaultReader(reader)) {
3325
3650
  reader._readRequests.forEach(readRequest => {
3326
3651
  readRequest._errorSteps(e);
@@ -3333,7 +3658,6 @@
3333
3658
  });
3334
3659
  reader._readIntoRequests = new SimpleQueue();
3335
3660
  }
3336
- defaultReaderClosedPromiseReject(reader, e);
3337
3661
  }
3338
3662
  // Helper functions for the ReadableStream.
3339
3663
  function streamBrandCheckException$1(name) {
@@ -3349,9 +3673,14 @@
3349
3673
  };
3350
3674
  }
3351
3675
 
3352
- const byteLengthSizeFunction = function size(chunk) {
3676
+ // The size function must not have a prototype property nor be a constructor
3677
+ const byteLengthSizeFunction = (chunk) => {
3353
3678
  return chunk.byteLength;
3354
3679
  };
3680
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3681
+ value: 'size',
3682
+ configurable: true
3683
+ });
3355
3684
  /**
3356
3685
  * A queuing strategy that counts the number of bytes in each chunk.
3357
3686
  *
@@ -3403,12 +3732,17 @@
3403
3732
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3404
3733
  return false;
3405
3734
  }
3406
- return true;
3735
+ return x instanceof ByteLengthQueuingStrategy;
3407
3736
  }
3408
3737
 
3409
- const countSizeFunction = function size() {
3738
+ // The size function must not have a prototype property nor be a constructor
3739
+ const countSizeFunction = () => {
3410
3740
  return 1;
3411
3741
  };
3742
+ Object.defineProperty(countSizeFunction, 'name', {
3743
+ value: 'size',
3744
+ configurable: true
3745
+ });
3412
3746
  /**
3413
3747
  * A queuing strategy that counts the number of chunks.
3414
3748
  *
@@ -3461,7 +3795,7 @@
3461
3795
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3462
3796
  return false;
3463
3797
  }
3464
- return true;
3798
+ return x instanceof CountQueuingStrategy;
3465
3799
  }
3466
3800
 
3467
3801
  function convertTransformer(original, context) {
@@ -3543,7 +3877,7 @@
3543
3877
  */
3544
3878
  get readable() {
3545
3879
  if (!IsTransformStream(this)) {
3546
- throw streamBrandCheckException$2('readable');
3880
+ throw streamBrandCheckException('readable');
3547
3881
  }
3548
3882
  return this._readable;
3549
3883
  }
@@ -3552,7 +3886,7 @@
3552
3886
  */
3553
3887
  get writable() {
3554
3888
  if (!IsTransformStream(this)) {
3555
- throw streamBrandCheckException$2('writable');
3889
+ throw streamBrandCheckException('writable');
3556
3890
  }
3557
3891
  return this._writable;
3558
3892
  }
@@ -3603,7 +3937,7 @@
3603
3937
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3604
3938
  return false;
3605
3939
  }
3606
- return true;
3940
+ return x instanceof TransformStream;
3607
3941
  }
3608
3942
  // This is a no-op if both sides are already errored.
3609
3943
  function TransformStreamError(stream, e) {
@@ -3645,14 +3979,14 @@
3645
3979
  */
3646
3980
  get desiredSize() {
3647
3981
  if (!IsTransformStreamDefaultController(this)) {
3648
- throw defaultControllerBrandCheckException$1('desiredSize');
3982
+ throw defaultControllerBrandCheckException('desiredSize');
3649
3983
  }
3650
3984
  const readableController = this._controlledTransformStream._readable._readableStreamController;
3651
3985
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
3652
3986
  }
3653
3987
  enqueue(chunk = undefined) {
3654
3988
  if (!IsTransformStreamDefaultController(this)) {
3655
- throw defaultControllerBrandCheckException$1('enqueue');
3989
+ throw defaultControllerBrandCheckException('enqueue');
3656
3990
  }
3657
3991
  TransformStreamDefaultControllerEnqueue(this, chunk);
3658
3992
  }
@@ -3662,7 +3996,7 @@
3662
3996
  */
3663
3997
  error(reason = undefined) {
3664
3998
  if (!IsTransformStreamDefaultController(this)) {
3665
- throw defaultControllerBrandCheckException$1('error');
3999
+ throw defaultControllerBrandCheckException('error');
3666
4000
  }
3667
4001
  TransformStreamDefaultControllerError(this, reason);
3668
4002
  }
@@ -3672,7 +4006,7 @@
3672
4006
  */
3673
4007
  terminate() {
3674
4008
  if (!IsTransformStreamDefaultController(this)) {
3675
- throw defaultControllerBrandCheckException$1('terminate');
4009
+ throw defaultControllerBrandCheckException('terminate');
3676
4010
  }
3677
4011
  TransformStreamDefaultControllerTerminate(this);
3678
4012
  }
@@ -3697,7 +4031,7 @@
3697
4031
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3698
4032
  return false;
3699
4033
  }
3700
- return true;
4034
+ return x instanceof TransformStreamDefaultController;
3701
4035
  }
3702
4036
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3703
4037
  controller._controlledTransformStream = stream;
@@ -3814,11 +4148,11 @@
3814
4148
  return stream._backpressureChangePromise;
3815
4149
  }
3816
4150
  // Helper functions for the TransformStreamDefaultController.
3817
- function defaultControllerBrandCheckException$1(name) {
4151
+ function defaultControllerBrandCheckException(name) {
3818
4152
  return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
3819
4153
  }
3820
4154
  // Helper functions for the TransformStream.
3821
- function streamBrandCheckException$2(name) {
4155
+ function streamBrandCheckException(name) {
3822
4156
  return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
3823
4157
  }
3824
4158