web-streams-polyfill 3.0.2 → 3.2.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,10 +1,10 @@
1
1
  /**
2
- * web-streams-polyfill v3.0.2
2
+ * web-streams-polyfill v3.2.0
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;
@@ -626,7 +626,14 @@
626
626
  if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
627
627
  return false;
628
628
  }
629
- return true;
629
+ try {
630
+ // noinspection SuspiciousTypeOfGuard
631
+ return x._asyncIteratorImpl instanceof
632
+ ReadableStreamAsyncIteratorImpl;
633
+ }
634
+ catch (_a) {
635
+ return false;
636
+ }
630
637
  }
631
638
  // Helper functions for the ReadableStream.
632
639
  function streamAsyncIteratorBrandCheckException(name) {
@@ -640,15 +647,35 @@
640
647
  return x !== x;
641
648
  };
642
649
 
643
- function IsFiniteNonNegativeNumber(v) {
644
- if (!IsNonNegativeNumber(v)) {
645
- return false;
646
- }
647
- if (v === Infinity) {
648
- return false;
650
+ function CreateArrayFromList(elements) {
651
+ // We use arrays to represent lists, so this is basically a no-op.
652
+ // Do a slice though just in case we happen to depend on the unique-ness.
653
+ return elements.slice();
654
+ }
655
+ function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
656
+ new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
657
+ }
658
+ // Not implemented correctly
659
+ function TransferArrayBuffer(O) {
660
+ return O;
661
+ }
662
+ // Not implemented correctly
663
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
664
+ function IsDetachedBuffer(O) {
665
+ return false;
666
+ }
667
+ function ArrayBufferSlice(buffer, begin, end) {
668
+ // ArrayBuffer.prototype.slice is not available on IE10
669
+ // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
670
+ if (buffer.slice) {
671
+ return buffer.slice(begin, end);
649
672
  }
650
- return true;
673
+ const length = end - begin;
674
+ const slice = new ArrayBuffer(length);
675
+ CopyDataBlockBytes(slice, 0, buffer, begin, length);
676
+ return slice;
651
677
  }
678
+
652
679
  function IsNonNegativeNumber(v) {
653
680
  if (typeof v !== 'number') {
654
681
  return false;
@@ -661,6 +688,10 @@
661
688
  }
662
689
  return true;
663
690
  }
691
+ function CloneAsUint8Array(O) {
692
+ const buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
693
+ return new Uint8Array(buffer);
694
+ }
664
695
 
665
696
  function DequeueValue(container) {
666
697
  const pair = container._queue.shift();
@@ -671,8 +702,7 @@
671
702
  return pair.value;
672
703
  }
673
704
  function EnqueueValueWithSize(container, value, size) {
674
- size = Number(size);
675
- if (!IsFiniteNonNegativeNumber(size)) {
705
+ if (!IsNonNegativeNumber(size) || size === Infinity) {
676
706
  throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
677
707
  }
678
708
  container._queue.push({ value, size });
@@ -687,23 +717,6 @@
687
717
  container._queueTotalSize = 0;
688
718
  }
689
719
 
690
- function CreateArrayFromList(elements) {
691
- // We use arrays to represent lists, so this is basically a no-op.
692
- // Do a slice though just in case we happen to depend on the unique-ness.
693
- return elements.slice();
694
- }
695
- function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
696
- new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
697
- }
698
- // Not implemented correctly
699
- function TransferArrayBuffer(O) {
700
- return O;
701
- }
702
- // Not implemented correctly
703
- function IsDetachedBuffer(O) {
704
- return false;
705
- }
706
-
707
720
  /**
708
721
  * A pull-into request in a {@link ReadableByteStreamController}.
709
722
  *
@@ -742,15 +755,10 @@
742
755
  if (!ArrayBuffer.isView(view)) {
743
756
  throw new TypeError('You can only respond with array buffer views');
744
757
  }
745
- if (view.byteLength === 0) {
746
- throw new TypeError('chunk must have non-zero byteLength');
747
- }
748
- if (view.buffer.byteLength === 0) {
749
- throw new TypeError(`chunk's buffer must have non-zero byteLength`);
750
- }
751
758
  if (this._associatedReadableByteStreamController === undefined) {
752
759
  throw new TypeError('This BYOB request has been invalidated');
753
760
  }
761
+ if (IsDetachedBuffer(view.buffer)) ;
754
762
  ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
755
763
  }
756
764
  }
@@ -781,14 +789,7 @@
781
789
  if (!IsReadableByteStreamController(this)) {
782
790
  throw byteStreamControllerBrandCheckException('byobRequest');
783
791
  }
784
- if (this._byobRequest === null && this._pendingPullIntos.length > 0) {
785
- const firstDescriptor = this._pendingPullIntos.peek();
786
- const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
787
- const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
788
- SetUpReadableStreamBYOBRequest(byobRequest, this, view);
789
- this._byobRequest = byobRequest;
790
- }
791
- return this._byobRequest;
792
+ return ReadableByteStreamControllerGetBYOBRequest(this);
792
793
  }
793
794
  /**
794
795
  * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
@@ -851,10 +852,7 @@
851
852
  }
852
853
  /** @internal */
853
854
  [CancelSteps](reason) {
854
- if (this._pendingPullIntos.length > 0) {
855
- const firstDescriptor = this._pendingPullIntos.peek();
856
- firstDescriptor.bytesFilled = 0;
857
- }
855
+ ReadableByteStreamControllerClearPendingPullIntos(this);
858
856
  ResetQueue(this);
859
857
  const result = this._cancelAlgorithm(reason);
860
858
  ReadableByteStreamControllerClearAlgorithms(this);
@@ -883,6 +881,7 @@
883
881
  }
884
882
  const pullIntoDescriptor = {
885
883
  buffer,
884
+ bufferByteLength: autoAllocateChunkSize,
886
885
  byteOffset: 0,
887
886
  byteLength: autoAllocateChunkSize,
888
887
  bytesFilled: 0,
@@ -917,7 +916,7 @@
917
916
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
918
917
  return false;
919
918
  }
920
- return true;
919
+ return x instanceof ReadableByteStreamController;
921
920
  }
922
921
  function IsReadableStreamBYOBRequest(x) {
923
922
  if (!typeIsObject(x)) {
@@ -926,7 +925,7 @@
926
925
  if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
927
926
  return false;
928
927
  }
929
- return true;
928
+ return x instanceof ReadableStreamBYOBRequest;
930
929
  }
931
930
  function ReadableByteStreamControllerCallPullIfNeeded(controller) {
932
931
  const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
@@ -1008,7 +1007,6 @@
1008
1007
  return ready;
1009
1008
  }
1010
1009
  function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
1011
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1012
1010
  pullIntoDescriptor.bytesFilled += size;
1013
1011
  }
1014
1012
  function ReadableByteStreamControllerHandleQueueDrain(controller) {
@@ -1047,9 +1045,15 @@
1047
1045
  elementSize = view.constructor.BYTES_PER_ELEMENT;
1048
1046
  }
1049
1047
  const ctor = view.constructor;
1048
+ // try {
1050
1049
  const buffer = TransferArrayBuffer(view.buffer);
1050
+ // } catch (e) {
1051
+ // readIntoRequest._errorSteps(e);
1052
+ // return;
1053
+ // }
1051
1054
  const pullIntoDescriptor = {
1052
1055
  buffer,
1056
+ bufferByteLength: buffer.byteLength,
1053
1057
  byteOffset: view.byteOffset,
1054
1058
  byteLength: view.byteLength,
1055
1059
  bytesFilled: 0,
@@ -1089,7 +1093,6 @@
1089
1093
  ReadableByteStreamControllerCallPullIfNeeded(controller);
1090
1094
  }
1091
1095
  function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
1092
- firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1093
1096
  const stream = controller._controlledReadableByteStream;
1094
1097
  if (ReadableStreamHasBYOBReader(stream)) {
1095
1098
  while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
@@ -1099,34 +1102,27 @@
1099
1102
  }
1100
1103
  }
1101
1104
  function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
1102
- if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
1103
- throw new RangeError('bytesWritten out of range');
1104
- }
1105
1105
  ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
1106
1106
  if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
1107
- // TODO: Figure out whether we should detach the buffer or not here.
1108
1107
  return;
1109
1108
  }
1110
1109
  ReadableByteStreamControllerShiftPendingPullInto(controller);
1111
1110
  const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
1112
1111
  if (remainderSize > 0) {
1113
1112
  const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
1114
- const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
1113
+ const remainder = ArrayBufferSlice(pullIntoDescriptor.buffer, end - remainderSize, end);
1115
1114
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
1116
1115
  }
1117
- pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
1118
1116
  pullIntoDescriptor.bytesFilled -= remainderSize;
1119
1117
  ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
1120
1118
  ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
1121
1119
  }
1122
1120
  function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
1123
1121
  const firstDescriptor = controller._pendingPullIntos.peek();
1122
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1124
1123
  const state = controller._controlledReadableByteStream._state;
1125
1124
  if (state === 'closed') {
1126
- if (bytesWritten !== 0) {
1127
- throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1128
- }
1129
- ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
1125
+ ReadableByteStreamControllerRespondInClosedState(controller);
1130
1126
  }
1131
1127
  else {
1132
1128
  ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
@@ -1135,7 +1131,6 @@
1135
1131
  }
1136
1132
  function ReadableByteStreamControllerShiftPendingPullInto(controller) {
1137
1133
  const descriptor = controller._pendingPullIntos.shift();
1138
- ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1139
1134
  return descriptor;
1140
1135
  }
1141
1136
  function ReadableByteStreamControllerShouldCallPull(controller) {
@@ -1195,11 +1190,20 @@
1195
1190
  const byteOffset = chunk.byteOffset;
1196
1191
  const byteLength = chunk.byteLength;
1197
1192
  const transferredBuffer = TransferArrayBuffer(buffer);
1193
+ if (controller._pendingPullIntos.length > 0) {
1194
+ const firstPendingPullInto = controller._pendingPullIntos.peek();
1195
+ if (IsDetachedBuffer(firstPendingPullInto.buffer)) ;
1196
+ firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
1197
+ }
1198
+ ReadableByteStreamControllerInvalidateBYOBRequest(controller);
1198
1199
  if (ReadableStreamHasDefaultReader(stream)) {
1199
1200
  if (ReadableStreamGetNumReadRequests(stream) === 0) {
1200
1201
  ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
1201
1202
  }
1202
1203
  else {
1204
+ if (controller._pendingPullIntos.length > 0) {
1205
+ ReadableByteStreamControllerShiftPendingPullInto(controller);
1206
+ }
1203
1207
  const transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
1204
1208
  ReadableStreamFulfillReadRequest(stream, transferredView, false);
1205
1209
  }
@@ -1224,6 +1228,16 @@
1224
1228
  ReadableByteStreamControllerClearAlgorithms(controller);
1225
1229
  ReadableStreamError(stream, e);
1226
1230
  }
1231
+ function ReadableByteStreamControllerGetBYOBRequest(controller) {
1232
+ if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
1233
+ const firstDescriptor = controller._pendingPullIntos.peek();
1234
+ const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
1235
+ const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
1236
+ SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
1237
+ controller._byobRequest = byobRequest;
1238
+ }
1239
+ return controller._byobRequest;
1240
+ }
1227
1241
  function ReadableByteStreamControllerGetDesiredSize(controller) {
1228
1242
  const state = controller._controlledReadableByteStream._state;
1229
1243
  if (state === 'errored') {
@@ -1235,22 +1249,49 @@
1235
1249
  return controller._strategyHWM - controller._queueTotalSize;
1236
1250
  }
1237
1251
  function ReadableByteStreamControllerRespond(controller, bytesWritten) {
1238
- bytesWritten = Number(bytesWritten);
1239
- if (!IsFiniteNonNegativeNumber(bytesWritten)) {
1240
- throw new RangeError('bytesWritten must be a finite');
1252
+ const firstDescriptor = controller._pendingPullIntos.peek();
1253
+ const state = controller._controlledReadableByteStream._state;
1254
+ if (state === 'closed') {
1255
+ if (bytesWritten !== 0) {
1256
+ throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
1257
+ }
1258
+ }
1259
+ else {
1260
+ if (bytesWritten === 0) {
1261
+ throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
1262
+ }
1263
+ if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
1264
+ throw new RangeError('bytesWritten out of range');
1265
+ }
1241
1266
  }
1267
+ firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
1242
1268
  ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
1243
1269
  }
1244
1270
  function ReadableByteStreamControllerRespondWithNewView(controller, view) {
1245
1271
  const firstDescriptor = controller._pendingPullIntos.peek();
1272
+ const state = controller._controlledReadableByteStream._state;
1273
+ if (state === 'closed') {
1274
+ if (view.byteLength !== 0) {
1275
+ throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
1276
+ }
1277
+ }
1278
+ else {
1279
+ if (view.byteLength === 0) {
1280
+ throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
1281
+ }
1282
+ }
1246
1283
  if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
1247
1284
  throw new RangeError('The region specified by view does not match byobRequest');
1248
1285
  }
1249
- if (firstDescriptor.byteLength !== view.byteLength) {
1286
+ if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
1250
1287
  throw new RangeError('The buffer of view has different capacity than byobRequest');
1251
1288
  }
1252
- firstDescriptor.buffer = view.buffer;
1253
- ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
1289
+ if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
1290
+ throw new RangeError('The region specified by view is larger than byobRequest');
1291
+ }
1292
+ const viewByteLength = view.byteLength;
1293
+ firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
1294
+ ReadableByteStreamControllerRespondInternal(controller, viewByteLength);
1254
1295
  }
1255
1296
  function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
1256
1297
  controller._controlledReadableByteStream = stream;
@@ -1399,6 +1440,7 @@
1399
1440
  if (view.buffer.byteLength === 0) {
1400
1441
  return promiseRejectedWith(new TypeError(`view's buffer must have non-zero byteLength`));
1401
1442
  }
1443
+ if (IsDetachedBuffer(view.buffer)) ;
1402
1444
  if (this._ownerReadableStream === undefined) {
1403
1445
  return promiseRejectedWith(readerLockException('read from'));
1404
1446
  }
@@ -1458,7 +1500,7 @@
1458
1500
  if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
1459
1501
  return false;
1460
1502
  }
1461
- return true;
1503
+ return x instanceof ReadableStreamBYOBReader;
1462
1504
  }
1463
1505
  function ReadableStreamBYOBReaderRead(reader, view, readIntoRequest) {
1464
1506
  const stream = reader._ownerReadableStream;
@@ -1553,6 +1595,31 @@
1553
1595
  }
1554
1596
  }
1555
1597
 
1598
+ function isAbortSignal(value) {
1599
+ if (typeof value !== 'object' || value === null) {
1600
+ return false;
1601
+ }
1602
+ try {
1603
+ return typeof value.aborted === 'boolean';
1604
+ }
1605
+ catch (_a) {
1606
+ // AbortSignal.prototype.aborted throws if its brand check fails
1607
+ return false;
1608
+ }
1609
+ }
1610
+ const supportsAbortController = typeof AbortController === 'function';
1611
+ /**
1612
+ * Construct a new AbortController, if supported by the platform.
1613
+ *
1614
+ * @internal
1615
+ */
1616
+ function createAbortController() {
1617
+ if (supportsAbortController) {
1618
+ return new AbortController();
1619
+ }
1620
+ return undefined;
1621
+ }
1622
+
1556
1623
  /**
1557
1624
  * A writable stream represents a destination for data, into which you can write.
1558
1625
  *
@@ -1582,7 +1649,7 @@
1582
1649
  */
1583
1650
  get locked() {
1584
1651
  if (!IsWritableStream(this)) {
1585
- throw streamBrandCheckException('locked');
1652
+ throw streamBrandCheckException$2('locked');
1586
1653
  }
1587
1654
  return IsWritableStreamLocked(this);
1588
1655
  }
@@ -1597,7 +1664,7 @@
1597
1664
  */
1598
1665
  abort(reason = undefined) {
1599
1666
  if (!IsWritableStream(this)) {
1600
- return promiseRejectedWith(streamBrandCheckException('abort'));
1667
+ return promiseRejectedWith(streamBrandCheckException$2('abort'));
1601
1668
  }
1602
1669
  if (IsWritableStreamLocked(this)) {
1603
1670
  return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
@@ -1614,7 +1681,7 @@
1614
1681
  */
1615
1682
  close() {
1616
1683
  if (!IsWritableStream(this)) {
1617
- return promiseRejectedWith(streamBrandCheckException('close'));
1684
+ return promiseRejectedWith(streamBrandCheckException$2('close'));
1618
1685
  }
1619
1686
  if (IsWritableStreamLocked(this)) {
1620
1687
  return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
@@ -1634,7 +1701,7 @@
1634
1701
  */
1635
1702
  getWriter() {
1636
1703
  if (!IsWritableStream(this)) {
1637
- throw streamBrandCheckException('getWriter');
1704
+ throw streamBrandCheckException$2('getWriter');
1638
1705
  }
1639
1706
  return AcquireWritableStreamDefaultWriter(this);
1640
1707
  }
@@ -1696,7 +1763,7 @@
1696
1763
  if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
1697
1764
  return false;
1698
1765
  }
1699
- return true;
1766
+ return x instanceof WritableStream;
1700
1767
  }
1701
1768
  function IsWritableStreamLocked(stream) {
1702
1769
  if (stream._writer === undefined) {
@@ -1705,6 +1772,15 @@
1705
1772
  return true;
1706
1773
  }
1707
1774
  function WritableStreamAbort(stream, reason) {
1775
+ var _a;
1776
+ if (stream._state === 'closed' || stream._state === 'errored') {
1777
+ return promiseResolvedWith(undefined);
1778
+ }
1779
+ stream._writableStreamController._abortReason = reason;
1780
+ (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort();
1781
+ // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
1782
+ // but it doesn't know that signaling abort runs author code that might have changed the state.
1783
+ // Widen the type again by casting to WritableStreamState.
1708
1784
  const state = stream._state;
1709
1785
  if (state === 'closed' || state === 'errored') {
1710
1786
  return promiseResolvedWith(undefined);
@@ -2050,7 +2126,7 @@
2050
2126
  if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
2051
2127
  return false;
2052
2128
  }
2053
- return true;
2129
+ return x instanceof WritableStreamDefaultWriter;
2054
2130
  }
2055
2131
  // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
2056
2132
  function WritableStreamDefaultWriterAbort(writer, reason) {
@@ -2140,6 +2216,34 @@
2140
2216
  constructor() {
2141
2217
  throw new TypeError('Illegal constructor');
2142
2218
  }
2219
+ /**
2220
+ * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
2221
+ *
2222
+ * @deprecated
2223
+ * This property has been removed from the specification, see https://github.com/whatwg/streams/pull/1177.
2224
+ * Use {@link WritableStreamDefaultController.signal}'s `reason` instead.
2225
+ */
2226
+ get abortReason() {
2227
+ if (!IsWritableStreamDefaultController(this)) {
2228
+ throw defaultControllerBrandCheckException$2('abortReason');
2229
+ }
2230
+ return this._abortReason;
2231
+ }
2232
+ /**
2233
+ * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
2234
+ */
2235
+ get signal() {
2236
+ if (!IsWritableStreamDefaultController(this)) {
2237
+ throw defaultControllerBrandCheckException$2('signal');
2238
+ }
2239
+ if (this._abortController === undefined) {
2240
+ // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
2241
+ // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
2242
+ // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
2243
+ throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
2244
+ }
2245
+ return this._abortController.signal;
2246
+ }
2143
2247
  /**
2144
2248
  * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
2145
2249
  *
@@ -2149,7 +2253,7 @@
2149
2253
  */
2150
2254
  error(e = undefined) {
2151
2255
  if (!IsWritableStreamDefaultController(this)) {
2152
- throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
2256
+ throw defaultControllerBrandCheckException$2('error');
2153
2257
  }
2154
2258
  const state = this._controlledWritableStream._state;
2155
2259
  if (state !== 'writable') {
@@ -2171,6 +2275,8 @@
2171
2275
  }
2172
2276
  }
2173
2277
  Object.defineProperties(WritableStreamDefaultController.prototype, {
2278
+ abortReason: { enumerable: true },
2279
+ signal: { enumerable: true },
2174
2280
  error: { enumerable: true }
2175
2281
  });
2176
2282
  if (typeof SymbolPolyfill.toStringTag === 'symbol') {
@@ -2187,7 +2293,7 @@
2187
2293
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
2188
2294
  return false;
2189
2295
  }
2190
- return true;
2296
+ return x instanceof WritableStreamDefaultController;
2191
2297
  }
2192
2298
  function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
2193
2299
  controller._controlledWritableStream = stream;
@@ -2196,6 +2302,8 @@
2196
2302
  controller._queue = undefined;
2197
2303
  controller._queueTotalSize = undefined;
2198
2304
  ResetQueue(controller);
2305
+ controller._abortReason = undefined;
2306
+ controller._abortController = createAbortController();
2199
2307
  controller._started = false;
2200
2308
  controller._strategySizeAlgorithm = sizeAlgorithm;
2201
2309
  controller._strategyHWM = highWaterMark;
@@ -2345,9 +2453,13 @@
2345
2453
  WritableStreamStartErroring(stream, error);
2346
2454
  }
2347
2455
  // Helper functions for the WritableStream.
2348
- function streamBrandCheckException(name) {
2456
+ function streamBrandCheckException$2(name) {
2349
2457
  return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
2350
2458
  }
2459
+ // Helper functions for the WritableStreamDefaultController.
2460
+ function defaultControllerBrandCheckException$2(name) {
2461
+ return new TypeError(`WritableStreamDefaultController.prototype.${name} can only be used on a WritableStreamDefaultController`);
2462
+ }
2351
2463
  // Helper functions for the WritableStreamDefaultWriter.
2352
2464
  function defaultWriterBrandCheckException(name) {
2353
2465
  return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
@@ -2433,19 +2545,6 @@
2433
2545
  writer._readyPromiseState = 'fulfilled';
2434
2546
  }
2435
2547
 
2436
- function isAbortSignal(value) {
2437
- if (typeof value !== 'object' || value === null) {
2438
- return false;
2439
- }
2440
- try {
2441
- return typeof value.aborted === 'boolean';
2442
- }
2443
- catch (_a) {
2444
- // AbortSignal.prototype.aborted throws if its brand check fails
2445
- return false;
2446
- }
2447
- }
2448
-
2449
2548
  /// <reference lib="dom" />
2450
2549
  const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
2451
2550
 
@@ -2463,6 +2562,7 @@
2463
2562
  }
2464
2563
  }
2465
2564
  function createDOMExceptionPolyfill() {
2565
+ // eslint-disable-next-line no-shadow
2466
2566
  const ctor = function DOMException(message, name) {
2467
2567
  this.message = message || '';
2468
2568
  this.name = name || 'Error';
@@ -2474,6 +2574,7 @@
2474
2574
  Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
2475
2575
  return ctor;
2476
2576
  }
2577
+ // eslint-disable-next-line no-redeclare
2477
2578
  const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
2478
2579
 
2479
2580
  function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
@@ -2666,7 +2767,7 @@
2666
2767
  */
2667
2768
  get desiredSize() {
2668
2769
  if (!IsReadableStreamDefaultController(this)) {
2669
- throw defaultControllerBrandCheckException('desiredSize');
2770
+ throw defaultControllerBrandCheckException$1('desiredSize');
2670
2771
  }
2671
2772
  return ReadableStreamDefaultControllerGetDesiredSize(this);
2672
2773
  }
@@ -2676,7 +2777,7 @@
2676
2777
  */
2677
2778
  close() {
2678
2779
  if (!IsReadableStreamDefaultController(this)) {
2679
- throw defaultControllerBrandCheckException('close');
2780
+ throw defaultControllerBrandCheckException$1('close');
2680
2781
  }
2681
2782
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2682
2783
  throw new TypeError('The stream is not in a state that permits close');
@@ -2685,7 +2786,7 @@
2685
2786
  }
2686
2787
  enqueue(chunk = undefined) {
2687
2788
  if (!IsReadableStreamDefaultController(this)) {
2688
- throw defaultControllerBrandCheckException('enqueue');
2789
+ throw defaultControllerBrandCheckException$1('enqueue');
2689
2790
  }
2690
2791
  if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
2691
2792
  throw new TypeError('The stream is not in a state that permits enqueue');
@@ -2697,7 +2798,7 @@
2697
2798
  */
2698
2799
  error(e = undefined) {
2699
2800
  if (!IsReadableStreamDefaultController(this)) {
2700
- throw defaultControllerBrandCheckException('error');
2801
+ throw defaultControllerBrandCheckException$1('error');
2701
2802
  }
2702
2803
  ReadableStreamDefaultControllerError(this, e);
2703
2804
  }
@@ -2748,7 +2849,7 @@
2748
2849
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
2749
2850
  return false;
2750
2851
  }
2751
- return true;
2852
+ return x instanceof ReadableStreamDefaultController;
2752
2853
  }
2753
2854
  function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
2754
2855
  const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
@@ -2904,13 +3005,20 @@
2904
3005
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
2905
3006
  }
2906
3007
  // Helper functions for the ReadableStreamDefaultController.
2907
- function defaultControllerBrandCheckException(name) {
3008
+ function defaultControllerBrandCheckException$1(name) {
2908
3009
  return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
2909
3010
  }
2910
3011
 
2911
3012
  function ReadableStreamTee(stream, cloneForBranch2) {
3013
+ if (IsReadableByteStreamController(stream._readableStreamController)) {
3014
+ return ReadableByteStreamTee(stream);
3015
+ }
3016
+ return ReadableStreamDefaultTee(stream);
3017
+ }
3018
+ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
2912
3019
  const reader = AcquireReadableStreamDefaultReader(stream);
2913
3020
  let reading = false;
3021
+ let readAgain = false;
2914
3022
  let canceled1 = false;
2915
3023
  let canceled2 = false;
2916
3024
  let reason1;
@@ -2923,30 +3031,34 @@
2923
3031
  });
2924
3032
  function pullAlgorithm() {
2925
3033
  if (reading) {
3034
+ readAgain = true;
2926
3035
  return promiseResolvedWith(undefined);
2927
3036
  }
2928
3037
  reading = true;
2929
3038
  const readRequest = {
2930
- _chunkSteps: value => {
3039
+ _chunkSteps: chunk => {
2931
3040
  // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
2932
3041
  // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
2933
3042
  // successful synchronously-available reads get ahead of asynchronously-available errors.
2934
3043
  queueMicrotask(() => {
2935
- reading = false;
2936
- const value1 = value;
2937
- const value2 = value;
3044
+ readAgain = false;
3045
+ const chunk1 = chunk;
3046
+ const chunk2 = chunk;
2938
3047
  // There is no way to access the cloning code right now in the reference implementation.
2939
3048
  // If we add one then we'll need an implementation for serializable objects.
2940
3049
  // if (!canceled2 && cloneForBranch2) {
2941
- // value2 = StructuredDeserialize(StructuredSerialize(value2));
3050
+ // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
2942
3051
  // }
2943
3052
  if (!canceled1) {
2944
- ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
3053
+ ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
2945
3054
  }
2946
3055
  if (!canceled2) {
2947
- ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
3056
+ ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
3057
+ }
3058
+ reading = false;
3059
+ if (readAgain) {
3060
+ pullAlgorithm();
2948
3061
  }
2949
- resolveCancelPromise(undefined);
2950
3062
  });
2951
3063
  },
2952
3064
  _closeSteps: () => {
@@ -2957,6 +3069,9 @@
2957
3069
  if (!canceled2) {
2958
3070
  ReadableStreamDefaultControllerClose(branch2._readableStreamController);
2959
3071
  }
3072
+ if (!canceled1 || !canceled2) {
3073
+ resolveCancelPromise(undefined);
3074
+ }
2960
3075
  },
2961
3076
  _errorSteps: () => {
2962
3077
  reading = false;
@@ -2993,8 +3108,235 @@
2993
3108
  uponRejection(reader._closedPromise, (r) => {
2994
3109
  ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
2995
3110
  ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
2996
- resolveCancelPromise(undefined);
3111
+ if (!canceled1 || !canceled2) {
3112
+ resolveCancelPromise(undefined);
3113
+ }
3114
+ });
3115
+ return [branch1, branch2];
3116
+ }
3117
+ function ReadableByteStreamTee(stream) {
3118
+ let reader = AcquireReadableStreamDefaultReader(stream);
3119
+ let reading = false;
3120
+ let readAgainForBranch1 = false;
3121
+ let readAgainForBranch2 = false;
3122
+ let canceled1 = false;
3123
+ let canceled2 = false;
3124
+ let reason1;
3125
+ let reason2;
3126
+ let branch1;
3127
+ let branch2;
3128
+ let resolveCancelPromise;
3129
+ const cancelPromise = newPromise(resolve => {
3130
+ resolveCancelPromise = resolve;
2997
3131
  });
3132
+ function forwardReaderError(thisReader) {
3133
+ uponRejection(thisReader._closedPromise, r => {
3134
+ if (thisReader !== reader) {
3135
+ return;
3136
+ }
3137
+ ReadableByteStreamControllerError(branch1._readableStreamController, r);
3138
+ ReadableByteStreamControllerError(branch2._readableStreamController, r);
3139
+ if (!canceled1 || !canceled2) {
3140
+ resolveCancelPromise(undefined);
3141
+ }
3142
+ });
3143
+ }
3144
+ function pullWithDefaultReader() {
3145
+ if (IsReadableStreamBYOBReader(reader)) {
3146
+ ReadableStreamReaderGenericRelease(reader);
3147
+ reader = AcquireReadableStreamDefaultReader(stream);
3148
+ forwardReaderError(reader);
3149
+ }
3150
+ const readRequest = {
3151
+ _chunkSteps: chunk => {
3152
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3153
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3154
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3155
+ queueMicrotask(() => {
3156
+ readAgainForBranch1 = false;
3157
+ readAgainForBranch2 = false;
3158
+ const chunk1 = chunk;
3159
+ let chunk2 = chunk;
3160
+ if (!canceled1 && !canceled2) {
3161
+ try {
3162
+ chunk2 = CloneAsUint8Array(chunk);
3163
+ }
3164
+ catch (cloneE) {
3165
+ ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
3166
+ ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
3167
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3168
+ return;
3169
+ }
3170
+ }
3171
+ if (!canceled1) {
3172
+ ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
3173
+ }
3174
+ if (!canceled2) {
3175
+ ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
3176
+ }
3177
+ reading = false;
3178
+ if (readAgainForBranch1) {
3179
+ pull1Algorithm();
3180
+ }
3181
+ else if (readAgainForBranch2) {
3182
+ pull2Algorithm();
3183
+ }
3184
+ });
3185
+ },
3186
+ _closeSteps: () => {
3187
+ reading = false;
3188
+ if (!canceled1) {
3189
+ ReadableByteStreamControllerClose(branch1._readableStreamController);
3190
+ }
3191
+ if (!canceled2) {
3192
+ ReadableByteStreamControllerClose(branch2._readableStreamController);
3193
+ }
3194
+ if (branch1._readableStreamController._pendingPullIntos.length > 0) {
3195
+ ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
3196
+ }
3197
+ if (branch2._readableStreamController._pendingPullIntos.length > 0) {
3198
+ ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
3199
+ }
3200
+ if (!canceled1 || !canceled2) {
3201
+ resolveCancelPromise(undefined);
3202
+ }
3203
+ },
3204
+ _errorSteps: () => {
3205
+ reading = false;
3206
+ }
3207
+ };
3208
+ ReadableStreamDefaultReaderRead(reader, readRequest);
3209
+ }
3210
+ function pullWithBYOBReader(view, forBranch2) {
3211
+ if (IsReadableStreamDefaultReader(reader)) {
3212
+ ReadableStreamReaderGenericRelease(reader);
3213
+ reader = AcquireReadableStreamBYOBReader(stream);
3214
+ forwardReaderError(reader);
3215
+ }
3216
+ const byobBranch = forBranch2 ? branch2 : branch1;
3217
+ const otherBranch = forBranch2 ? branch1 : branch2;
3218
+ const readIntoRequest = {
3219
+ _chunkSteps: chunk => {
3220
+ // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
3221
+ // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
3222
+ // successful synchronously-available reads get ahead of asynchronously-available errors.
3223
+ queueMicrotask(() => {
3224
+ readAgainForBranch1 = false;
3225
+ readAgainForBranch2 = false;
3226
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3227
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3228
+ if (!otherCanceled) {
3229
+ let clonedChunk;
3230
+ try {
3231
+ clonedChunk = CloneAsUint8Array(chunk);
3232
+ }
3233
+ catch (cloneE) {
3234
+ ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
3235
+ ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
3236
+ resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
3237
+ return;
3238
+ }
3239
+ if (!byobCanceled) {
3240
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3241
+ }
3242
+ ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
3243
+ }
3244
+ else if (!byobCanceled) {
3245
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3246
+ }
3247
+ reading = false;
3248
+ if (readAgainForBranch1) {
3249
+ pull1Algorithm();
3250
+ }
3251
+ else if (readAgainForBranch2) {
3252
+ pull2Algorithm();
3253
+ }
3254
+ });
3255
+ },
3256
+ _closeSteps: chunk => {
3257
+ reading = false;
3258
+ const byobCanceled = forBranch2 ? canceled2 : canceled1;
3259
+ const otherCanceled = forBranch2 ? canceled1 : canceled2;
3260
+ if (!byobCanceled) {
3261
+ ReadableByteStreamControllerClose(byobBranch._readableStreamController);
3262
+ }
3263
+ if (!otherCanceled) {
3264
+ ReadableByteStreamControllerClose(otherBranch._readableStreamController);
3265
+ }
3266
+ if (chunk !== undefined) {
3267
+ if (!byobCanceled) {
3268
+ ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
3269
+ }
3270
+ if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
3271
+ ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
3272
+ }
3273
+ }
3274
+ if (!byobCanceled || !otherCanceled) {
3275
+ resolveCancelPromise(undefined);
3276
+ }
3277
+ },
3278
+ _errorSteps: () => {
3279
+ reading = false;
3280
+ }
3281
+ };
3282
+ ReadableStreamBYOBReaderRead(reader, view, readIntoRequest);
3283
+ }
3284
+ function pull1Algorithm() {
3285
+ if (reading) {
3286
+ readAgainForBranch1 = true;
3287
+ return promiseResolvedWith(undefined);
3288
+ }
3289
+ reading = true;
3290
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
3291
+ if (byobRequest === null) {
3292
+ pullWithDefaultReader();
3293
+ }
3294
+ else {
3295
+ pullWithBYOBReader(byobRequest._view, false);
3296
+ }
3297
+ return promiseResolvedWith(undefined);
3298
+ }
3299
+ function pull2Algorithm() {
3300
+ if (reading) {
3301
+ readAgainForBranch2 = true;
3302
+ return promiseResolvedWith(undefined);
3303
+ }
3304
+ reading = true;
3305
+ const byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
3306
+ if (byobRequest === null) {
3307
+ pullWithDefaultReader();
3308
+ }
3309
+ else {
3310
+ pullWithBYOBReader(byobRequest._view, true);
3311
+ }
3312
+ return promiseResolvedWith(undefined);
3313
+ }
3314
+ function cancel1Algorithm(reason) {
3315
+ canceled1 = true;
3316
+ reason1 = reason;
3317
+ if (canceled2) {
3318
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3319
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3320
+ resolveCancelPromise(cancelResult);
3321
+ }
3322
+ return cancelPromise;
3323
+ }
3324
+ function cancel2Algorithm(reason) {
3325
+ canceled2 = true;
3326
+ reason2 = reason;
3327
+ if (canceled1) {
3328
+ const compositeReason = CreateArrayFromList([reason1, reason2]);
3329
+ const cancelResult = ReadableStreamCancel(stream, compositeReason);
3330
+ resolveCancelPromise(cancelResult);
3331
+ }
3332
+ return cancelPromise;
3333
+ }
3334
+ function startAlgorithm() {
3335
+ return;
3336
+ }
3337
+ branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
3338
+ branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
3339
+ forwardReaderError(reader);
2998
3340
  return [branch1, branch2];
2999
3341
  }
3000
3342
 
@@ -3258,6 +3600,14 @@
3258
3600
  SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
3259
3601
  return stream;
3260
3602
  }
3603
+ // Throws if and only if startAlgorithm throws.
3604
+ function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
3605
+ const stream = Object.create(ReadableStream.prototype);
3606
+ InitializeReadableStream(stream);
3607
+ const controller = Object.create(ReadableByteStreamController.prototype);
3608
+ SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
3609
+ return stream;
3610
+ }
3261
3611
  function InitializeReadableStream(stream) {
3262
3612
  stream._state = 'readable';
3263
3613
  stream._reader = undefined;
@@ -3271,7 +3621,7 @@
3271
3621
  if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
3272
3622
  return false;
3273
3623
  }
3274
- return true;
3624
+ return x instanceof ReadableStream;
3275
3625
  }
3276
3626
  function IsReadableStreamLocked(stream) {
3277
3627
  if (stream._reader === undefined) {
@@ -3289,6 +3639,13 @@
3289
3639
  return promiseRejectedWith(stream._storedError);
3290
3640
  }
3291
3641
  ReadableStreamClose(stream);
3642
+ const reader = stream._reader;
3643
+ if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
3644
+ reader._readIntoRequests.forEach(readIntoRequest => {
3645
+ readIntoRequest._closeSteps(undefined);
3646
+ });
3647
+ reader._readIntoRequests = new SimpleQueue();
3648
+ }
3292
3649
  const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
3293
3650
  return transformPromiseWith(sourceCancelPromise, noop);
3294
3651
  }
@@ -3341,9 +3698,14 @@
3341
3698
  };
3342
3699
  }
3343
3700
 
3344
- const byteLengthSizeFunction = function size(chunk) {
3701
+ // The size function must not have a prototype property nor be a constructor
3702
+ const byteLengthSizeFunction = (chunk) => {
3345
3703
  return chunk.byteLength;
3346
3704
  };
3705
+ Object.defineProperty(byteLengthSizeFunction, 'name', {
3706
+ value: 'size',
3707
+ configurable: true
3708
+ });
3347
3709
  /**
3348
3710
  * A queuing strategy that counts the number of bytes in each chunk.
3349
3711
  *
@@ -3395,12 +3757,17 @@
3395
3757
  if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
3396
3758
  return false;
3397
3759
  }
3398
- return true;
3760
+ return x instanceof ByteLengthQueuingStrategy;
3399
3761
  }
3400
3762
 
3401
- const countSizeFunction = function size() {
3763
+ // The size function must not have a prototype property nor be a constructor
3764
+ const countSizeFunction = () => {
3402
3765
  return 1;
3403
3766
  };
3767
+ Object.defineProperty(countSizeFunction, 'name', {
3768
+ value: 'size',
3769
+ configurable: true
3770
+ });
3404
3771
  /**
3405
3772
  * A queuing strategy that counts the number of chunks.
3406
3773
  *
@@ -3453,7 +3820,7 @@
3453
3820
  if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
3454
3821
  return false;
3455
3822
  }
3456
- return true;
3823
+ return x instanceof CountQueuingStrategy;
3457
3824
  }
3458
3825
 
3459
3826
  function convertTransformer(original, context) {
@@ -3535,7 +3902,7 @@
3535
3902
  */
3536
3903
  get readable() {
3537
3904
  if (!IsTransformStream(this)) {
3538
- throw streamBrandCheckException$2('readable');
3905
+ throw streamBrandCheckException('readable');
3539
3906
  }
3540
3907
  return this._readable;
3541
3908
  }
@@ -3544,7 +3911,7 @@
3544
3911
  */
3545
3912
  get writable() {
3546
3913
  if (!IsTransformStream(this)) {
3547
- throw streamBrandCheckException$2('writable');
3914
+ throw streamBrandCheckException('writable');
3548
3915
  }
3549
3916
  return this._writable;
3550
3917
  }
@@ -3595,7 +3962,7 @@
3595
3962
  if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
3596
3963
  return false;
3597
3964
  }
3598
- return true;
3965
+ return x instanceof TransformStream;
3599
3966
  }
3600
3967
  // This is a no-op if both sides are already errored.
3601
3968
  function TransformStreamError(stream, e) {
@@ -3637,14 +4004,14 @@
3637
4004
  */
3638
4005
  get desiredSize() {
3639
4006
  if (!IsTransformStreamDefaultController(this)) {
3640
- throw defaultControllerBrandCheckException$1('desiredSize');
4007
+ throw defaultControllerBrandCheckException('desiredSize');
3641
4008
  }
3642
4009
  const readableController = this._controlledTransformStream._readable._readableStreamController;
3643
4010
  return ReadableStreamDefaultControllerGetDesiredSize(readableController);
3644
4011
  }
3645
4012
  enqueue(chunk = undefined) {
3646
4013
  if (!IsTransformStreamDefaultController(this)) {
3647
- throw defaultControllerBrandCheckException$1('enqueue');
4014
+ throw defaultControllerBrandCheckException('enqueue');
3648
4015
  }
3649
4016
  TransformStreamDefaultControllerEnqueue(this, chunk);
3650
4017
  }
@@ -3654,7 +4021,7 @@
3654
4021
  */
3655
4022
  error(reason = undefined) {
3656
4023
  if (!IsTransformStreamDefaultController(this)) {
3657
- throw defaultControllerBrandCheckException$1('error');
4024
+ throw defaultControllerBrandCheckException('error');
3658
4025
  }
3659
4026
  TransformStreamDefaultControllerError(this, reason);
3660
4027
  }
@@ -3664,7 +4031,7 @@
3664
4031
  */
3665
4032
  terminate() {
3666
4033
  if (!IsTransformStreamDefaultController(this)) {
3667
- throw defaultControllerBrandCheckException$1('terminate');
4034
+ throw defaultControllerBrandCheckException('terminate');
3668
4035
  }
3669
4036
  TransformStreamDefaultControllerTerminate(this);
3670
4037
  }
@@ -3689,7 +4056,7 @@
3689
4056
  if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
3690
4057
  return false;
3691
4058
  }
3692
- return true;
4059
+ return x instanceof TransformStreamDefaultController;
3693
4060
  }
3694
4061
  function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
3695
4062
  controller._controlledTransformStream = stream;
@@ -3806,11 +4173,11 @@
3806
4173
  return stream._backpressureChangePromise;
3807
4174
  }
3808
4175
  // Helper functions for the TransformStreamDefaultController.
3809
- function defaultControllerBrandCheckException$1(name) {
4176
+ function defaultControllerBrandCheckException(name) {
3810
4177
  return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
3811
4178
  }
3812
4179
  // Helper functions for the TransformStream.
3813
- function streamBrandCheckException$2(name) {
4180
+ function streamBrandCheckException(name) {
3814
4181
  return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
3815
4182
  }
3816
4183