[d24f17c] | 1 | /**
|
---|
| 2 | * @license
|
---|
| 3 | * web-streams-polyfill v3.3.2
|
---|
| 4 | * Copyright 2024 Mattias Buelens, Diwank Singh Tomer and other contributors.
|
---|
| 5 | * This code is released under the MIT license.
|
---|
| 6 | * SPDX-License-Identifier: MIT
|
---|
| 7 | */
|
---|
| 8 | /// <reference lib="es2015.symbol" />
|
---|
| 9 | var SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
|
---|
| 10 | Symbol :
|
---|
| 11 | function (description) { return "Symbol(".concat(description, ")"); };
|
---|
| 12 |
|
---|
| 13 | /******************************************************************************
|
---|
| 14 | Copyright (c) Microsoft Corporation.
|
---|
| 15 |
|
---|
| 16 | Permission to use, copy, modify, and/or distribute this software for any
|
---|
| 17 | purpose with or without fee is hereby granted.
|
---|
| 18 |
|
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
---|
| 20 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
---|
| 21 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
---|
| 22 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
---|
| 23 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
---|
| 24 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
---|
| 25 | PERFORMANCE OF THIS SOFTWARE.
|
---|
| 26 | ***************************************************************************** */
|
---|
| 27 | /* global Reflect, Promise, SuppressedError, Symbol */
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | function __generator(thisArg, body) {
|
---|
| 31 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
---|
| 32 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
---|
| 33 | function verb(n) { return function (v) { return step([n, v]); }; }
|
---|
| 34 | function step(op) {
|
---|
| 35 | if (f) throw new TypeError("Generator is already executing.");
|
---|
| 36 | while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
---|
| 37 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
---|
| 38 | if (y = 0, t) op = [op[0] & 2, t.value];
|
---|
| 39 | switch (op[0]) {
|
---|
| 40 | case 0: case 1: t = op; break;
|
---|
| 41 | case 4: _.label++; return { value: op[1], done: false };
|
---|
| 42 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
---|
| 43 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
---|
| 44 | default:
|
---|
| 45 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
---|
| 46 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
---|
| 47 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
---|
| 48 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
---|
| 49 | if (t[2]) _.ops.pop();
|
---|
| 50 | _.trys.pop(); continue;
|
---|
| 51 | }
|
---|
| 52 | op = body.call(thisArg, _);
|
---|
| 53 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
---|
| 54 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | function __values(o) {
|
---|
| 59 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
---|
| 60 | if (m) return m.call(o);
|
---|
| 61 | if (o && typeof o.length === "number") return {
|
---|
| 62 | next: function () {
|
---|
| 63 | if (o && i >= o.length) o = void 0;
|
---|
| 64 | return { value: o && o[i++], done: !o };
|
---|
| 65 | }
|
---|
| 66 | };
|
---|
| 67 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | function __await(v) {
|
---|
| 71 | return this instanceof __await ? (this.v = v, this) : new __await(v);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | function __asyncGenerator(thisArg, _arguments, generator) {
|
---|
| 75 | if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
---|
| 76 | var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
---|
| 77 | return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
---|
| 78 | function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
---|
| 79 | function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
---|
| 80 | function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
---|
| 81 | function fulfill(value) { resume("next", value); }
|
---|
| 82 | function reject(value) { resume("throw", value); }
|
---|
| 83 | function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | function __asyncDelegator(o) {
|
---|
| 87 | var i, p;
|
---|
| 88 | return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
|
---|
| 89 | function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | function __asyncValues(o) {
|
---|
| 93 | if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
---|
| 94 | var m = o[Symbol.asyncIterator], i;
|
---|
| 95 | return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
---|
| 96 | function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
---|
| 97 | function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
---|
| 101 | var e = new Error(message);
|
---|
| 102 | return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | function noop() {
|
---|
| 106 | return undefined;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | function typeIsObject(x) {
|
---|
| 110 | return (typeof x === 'object' && x !== null) || typeof x === 'function';
|
---|
| 111 | }
|
---|
| 112 | var rethrowAssertionErrorRejection = noop;
|
---|
| 113 | function setFunctionName(fn, name) {
|
---|
| 114 | try {
|
---|
| 115 | Object.defineProperty(fn, 'name', {
|
---|
| 116 | value: name,
|
---|
| 117 | configurable: true
|
---|
| 118 | });
|
---|
| 119 | }
|
---|
| 120 | catch (_a) {
|
---|
| 121 | // This property is non-configurable in older browsers, so ignore if this throws.
|
---|
| 122 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#browser_compatibility
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | var originalPromise = Promise;
|
---|
| 127 | var originalPromiseThen = Promise.prototype.then;
|
---|
| 128 | var originalPromiseReject = Promise.reject.bind(originalPromise);
|
---|
| 129 | // https://webidl.spec.whatwg.org/#a-new-promise
|
---|
| 130 | function newPromise(executor) {
|
---|
| 131 | return new originalPromise(executor);
|
---|
| 132 | }
|
---|
| 133 | // https://webidl.spec.whatwg.org/#a-promise-resolved-with
|
---|
| 134 | function promiseResolvedWith(value) {
|
---|
| 135 | return newPromise(function (resolve) { return resolve(value); });
|
---|
| 136 | }
|
---|
| 137 | // https://webidl.spec.whatwg.org/#a-promise-rejected-with
|
---|
| 138 | function promiseRejectedWith(reason) {
|
---|
| 139 | return originalPromiseReject(reason);
|
---|
| 140 | }
|
---|
| 141 | function PerformPromiseThen(promise, onFulfilled, onRejected) {
|
---|
| 142 | // There doesn't appear to be any way to correctly emulate the behaviour from JavaScript, so this is just an
|
---|
| 143 | // approximation.
|
---|
| 144 | return originalPromiseThen.call(promise, onFulfilled, onRejected);
|
---|
| 145 | }
|
---|
| 146 | // Bluebird logs a warning when a promise is created within a fulfillment handler, but then isn't returned
|
---|
| 147 | // from that handler. To prevent this, return null instead of void from all handlers.
|
---|
| 148 | // http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it
|
---|
| 149 | function uponPromise(promise, onFulfilled, onRejected) {
|
---|
| 150 | PerformPromiseThen(PerformPromiseThen(promise, onFulfilled, onRejected), undefined, rethrowAssertionErrorRejection);
|
---|
| 151 | }
|
---|
| 152 | function uponFulfillment(promise, onFulfilled) {
|
---|
| 153 | uponPromise(promise, onFulfilled);
|
---|
| 154 | }
|
---|
| 155 | function uponRejection(promise, onRejected) {
|
---|
| 156 | uponPromise(promise, undefined, onRejected);
|
---|
| 157 | }
|
---|
| 158 | function transformPromiseWith(promise, fulfillmentHandler, rejectionHandler) {
|
---|
| 159 | return PerformPromiseThen(promise, fulfillmentHandler, rejectionHandler);
|
---|
| 160 | }
|
---|
| 161 | function setPromiseIsHandledToTrue(promise) {
|
---|
| 162 | PerformPromiseThen(promise, undefined, rethrowAssertionErrorRejection);
|
---|
| 163 | }
|
---|
| 164 | var _queueMicrotask = function (callback) {
|
---|
| 165 | if (typeof queueMicrotask === 'function') {
|
---|
| 166 | _queueMicrotask = queueMicrotask;
|
---|
| 167 | }
|
---|
| 168 | else {
|
---|
| 169 | var resolvedPromise_1 = promiseResolvedWith(undefined);
|
---|
| 170 | _queueMicrotask = function (cb) { return PerformPromiseThen(resolvedPromise_1, cb); };
|
---|
| 171 | }
|
---|
| 172 | return _queueMicrotask(callback);
|
---|
| 173 | };
|
---|
| 174 | function reflectCall(F, V, args) {
|
---|
| 175 | if (typeof F !== 'function') {
|
---|
| 176 | throw new TypeError('Argument is not a function');
|
---|
| 177 | }
|
---|
| 178 | return Function.prototype.apply.call(F, V, args);
|
---|
| 179 | }
|
---|
| 180 | function promiseCall(F, V, args) {
|
---|
| 181 | try {
|
---|
| 182 | return promiseResolvedWith(reflectCall(F, V, args));
|
---|
| 183 | }
|
---|
| 184 | catch (value) {
|
---|
| 185 | return promiseRejectedWith(value);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | // Original from Chromium
|
---|
| 190 | // https://chromium.googlesource.com/chromium/src/+/0aee4434a4dba42a42abaea9bfbc0cd196a63bc1/third_party/blink/renderer/core/streams/SimpleQueue.js
|
---|
| 191 | var QUEUE_MAX_ARRAY_SIZE = 16384;
|
---|
| 192 | /**
|
---|
| 193 | * Simple queue structure.
|
---|
| 194 | *
|
---|
| 195 | * Avoids scalability issues with using a packed array directly by using
|
---|
| 196 | * multiple arrays in a linked list and keeping the array size bounded.
|
---|
| 197 | */
|
---|
| 198 | var SimpleQueue = /** @class */ (function () {
|
---|
| 199 | function SimpleQueue() {
|
---|
| 200 | this._cursor = 0;
|
---|
| 201 | this._size = 0;
|
---|
| 202 | // _front and _back are always defined.
|
---|
| 203 | this._front = {
|
---|
| 204 | _elements: [],
|
---|
| 205 | _next: undefined
|
---|
| 206 | };
|
---|
| 207 | this._back = this._front;
|
---|
| 208 | // The cursor is used to avoid calling Array.shift().
|
---|
| 209 | // It contains the index of the front element of the array inside the
|
---|
| 210 | // front-most node. It is always in the range [0, QUEUE_MAX_ARRAY_SIZE).
|
---|
| 211 | this._cursor = 0;
|
---|
| 212 | // When there is only one node, size === elements.length - cursor.
|
---|
| 213 | this._size = 0;
|
---|
| 214 | }
|
---|
| 215 | Object.defineProperty(SimpleQueue.prototype, "length", {
|
---|
| 216 | get: function () {
|
---|
| 217 | return this._size;
|
---|
| 218 | },
|
---|
| 219 | enumerable: false,
|
---|
| 220 | configurable: true
|
---|
| 221 | });
|
---|
| 222 | // For exception safety, this method is structured in order:
|
---|
| 223 | // 1. Read state
|
---|
| 224 | // 2. Calculate required state mutations
|
---|
| 225 | // 3. Perform state mutations
|
---|
| 226 | SimpleQueue.prototype.push = function (element) {
|
---|
| 227 | var oldBack = this._back;
|
---|
| 228 | var newBack = oldBack;
|
---|
| 229 | if (oldBack._elements.length === QUEUE_MAX_ARRAY_SIZE - 1) {
|
---|
| 230 | newBack = {
|
---|
| 231 | _elements: [],
|
---|
| 232 | _next: undefined
|
---|
| 233 | };
|
---|
| 234 | }
|
---|
| 235 | // push() is the mutation most likely to throw an exception, so it
|
---|
| 236 | // goes first.
|
---|
| 237 | oldBack._elements.push(element);
|
---|
| 238 | if (newBack !== oldBack) {
|
---|
| 239 | this._back = newBack;
|
---|
| 240 | oldBack._next = newBack;
|
---|
| 241 | }
|
---|
| 242 | ++this._size;
|
---|
| 243 | };
|
---|
| 244 | // Like push(), shift() follows the read -> calculate -> mutate pattern for
|
---|
| 245 | // exception safety.
|
---|
| 246 | SimpleQueue.prototype.shift = function () { // must not be called on an empty queue
|
---|
| 247 | var oldFront = this._front;
|
---|
| 248 | var newFront = oldFront;
|
---|
| 249 | var oldCursor = this._cursor;
|
---|
| 250 | var newCursor = oldCursor + 1;
|
---|
| 251 | var elements = oldFront._elements;
|
---|
| 252 | var element = elements[oldCursor];
|
---|
| 253 | if (newCursor === QUEUE_MAX_ARRAY_SIZE) {
|
---|
| 254 | newFront = oldFront._next;
|
---|
| 255 | newCursor = 0;
|
---|
| 256 | }
|
---|
| 257 | // No mutations before this point.
|
---|
| 258 | --this._size;
|
---|
| 259 | this._cursor = newCursor;
|
---|
| 260 | if (oldFront !== newFront) {
|
---|
| 261 | this._front = newFront;
|
---|
| 262 | }
|
---|
| 263 | // Permit shifted element to be garbage collected.
|
---|
| 264 | elements[oldCursor] = undefined;
|
---|
| 265 | return element;
|
---|
| 266 | };
|
---|
| 267 | // The tricky thing about forEach() is that it can be called
|
---|
| 268 | // re-entrantly. The queue may be mutated inside the callback. It is easy to
|
---|
| 269 | // see that push() within the callback has no negative effects since the end
|
---|
| 270 | // of the queue is checked for on every iteration. If shift() is called
|
---|
| 271 | // repeatedly within the callback then the next iteration may return an
|
---|
| 272 | // element that has been removed. In this case the callback will be called
|
---|
| 273 | // with undefined values until we either "catch up" with elements that still
|
---|
| 274 | // exist or reach the back of the queue.
|
---|
| 275 | SimpleQueue.prototype.forEach = function (callback) {
|
---|
| 276 | var i = this._cursor;
|
---|
| 277 | var node = this._front;
|
---|
| 278 | var elements = node._elements;
|
---|
| 279 | while (i !== elements.length || node._next !== undefined) {
|
---|
| 280 | if (i === elements.length) {
|
---|
| 281 | node = node._next;
|
---|
| 282 | elements = node._elements;
|
---|
| 283 | i = 0;
|
---|
| 284 | if (elements.length === 0) {
|
---|
| 285 | break;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 | callback(elements[i]);
|
---|
| 289 | ++i;
|
---|
| 290 | }
|
---|
| 291 | };
|
---|
| 292 | // Return the element that would be returned if shift() was called now,
|
---|
| 293 | // without modifying the queue.
|
---|
| 294 | SimpleQueue.prototype.peek = function () { // must not be called on an empty queue
|
---|
| 295 | var front = this._front;
|
---|
| 296 | var cursor = this._cursor;
|
---|
| 297 | return front._elements[cursor];
|
---|
| 298 | };
|
---|
| 299 | return SimpleQueue;
|
---|
| 300 | }());
|
---|
| 301 |
|
---|
| 302 | var AbortSteps = SymbolPolyfill('[[AbortSteps]]');
|
---|
| 303 | var ErrorSteps = SymbolPolyfill('[[ErrorSteps]]');
|
---|
| 304 | var CancelSteps = SymbolPolyfill('[[CancelSteps]]');
|
---|
| 305 | var PullSteps = SymbolPolyfill('[[PullSteps]]');
|
---|
| 306 | var ReleaseSteps = SymbolPolyfill('[[ReleaseSteps]]');
|
---|
| 307 |
|
---|
| 308 | function ReadableStreamReaderGenericInitialize(reader, stream) {
|
---|
| 309 | reader._ownerReadableStream = stream;
|
---|
| 310 | stream._reader = reader;
|
---|
| 311 | if (stream._state === 'readable') {
|
---|
| 312 | defaultReaderClosedPromiseInitialize(reader);
|
---|
| 313 | }
|
---|
| 314 | else if (stream._state === 'closed') {
|
---|
| 315 | defaultReaderClosedPromiseInitializeAsResolved(reader);
|
---|
| 316 | }
|
---|
| 317 | else {
|
---|
| 318 | defaultReaderClosedPromiseInitializeAsRejected(reader, stream._storedError);
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 | // A client of ReadableStreamDefaultReader and ReadableStreamBYOBReader may use these functions directly to bypass state
|
---|
| 322 | // check.
|
---|
| 323 | function ReadableStreamReaderGenericCancel(reader, reason) {
|
---|
| 324 | var stream = reader._ownerReadableStream;
|
---|
| 325 | return ReadableStreamCancel(stream, reason);
|
---|
| 326 | }
|
---|
| 327 | function ReadableStreamReaderGenericRelease(reader) {
|
---|
| 328 | var stream = reader._ownerReadableStream;
|
---|
| 329 | if (stream._state === 'readable') {
|
---|
| 330 | defaultReaderClosedPromiseReject(reader, new TypeError("Reader was released and can no longer be used to monitor the stream's closedness"));
|
---|
| 331 | }
|
---|
| 332 | else {
|
---|
| 333 | defaultReaderClosedPromiseResetToRejected(reader, new TypeError("Reader was released and can no longer be used to monitor the stream's closedness"));
|
---|
| 334 | }
|
---|
| 335 | stream._readableStreamController[ReleaseSteps]();
|
---|
| 336 | stream._reader = undefined;
|
---|
| 337 | reader._ownerReadableStream = undefined;
|
---|
| 338 | }
|
---|
| 339 | // Helper functions for the readers.
|
---|
| 340 | function readerLockException(name) {
|
---|
| 341 | return new TypeError('Cannot ' + name + ' a stream using a released reader');
|
---|
| 342 | }
|
---|
| 343 | // Helper functions for the ReadableStreamDefaultReader.
|
---|
| 344 | function defaultReaderClosedPromiseInitialize(reader) {
|
---|
| 345 | reader._closedPromise = newPromise(function (resolve, reject) {
|
---|
| 346 | reader._closedPromise_resolve = resolve;
|
---|
| 347 | reader._closedPromise_reject = reject;
|
---|
| 348 | });
|
---|
| 349 | }
|
---|
| 350 | function defaultReaderClosedPromiseInitializeAsRejected(reader, reason) {
|
---|
| 351 | defaultReaderClosedPromiseInitialize(reader);
|
---|
| 352 | defaultReaderClosedPromiseReject(reader, reason);
|
---|
| 353 | }
|
---|
| 354 | function defaultReaderClosedPromiseInitializeAsResolved(reader) {
|
---|
| 355 | defaultReaderClosedPromiseInitialize(reader);
|
---|
| 356 | defaultReaderClosedPromiseResolve(reader);
|
---|
| 357 | }
|
---|
| 358 | function defaultReaderClosedPromiseReject(reader, reason) {
|
---|
| 359 | if (reader._closedPromise_reject === undefined) {
|
---|
| 360 | return;
|
---|
| 361 | }
|
---|
| 362 | setPromiseIsHandledToTrue(reader._closedPromise);
|
---|
| 363 | reader._closedPromise_reject(reason);
|
---|
| 364 | reader._closedPromise_resolve = undefined;
|
---|
| 365 | reader._closedPromise_reject = undefined;
|
---|
| 366 | }
|
---|
| 367 | function defaultReaderClosedPromiseResetToRejected(reader, reason) {
|
---|
| 368 | defaultReaderClosedPromiseInitializeAsRejected(reader, reason);
|
---|
| 369 | }
|
---|
| 370 | function defaultReaderClosedPromiseResolve(reader) {
|
---|
| 371 | if (reader._closedPromise_resolve === undefined) {
|
---|
| 372 | return;
|
---|
| 373 | }
|
---|
| 374 | reader._closedPromise_resolve(undefined);
|
---|
| 375 | reader._closedPromise_resolve = undefined;
|
---|
| 376 | reader._closedPromise_reject = undefined;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /// <reference lib="es2015.core" />
|
---|
| 380 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite#Polyfill
|
---|
| 381 | var NumberIsFinite = Number.isFinite || function (x) {
|
---|
| 382 | return typeof x === 'number' && isFinite(x);
|
---|
| 383 | };
|
---|
| 384 |
|
---|
| 385 | /// <reference lib="es2015.core" />
|
---|
| 386 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc#Polyfill
|
---|
| 387 | var MathTrunc = Math.trunc || function (v) {
|
---|
| 388 | return v < 0 ? Math.ceil(v) : Math.floor(v);
|
---|
| 389 | };
|
---|
| 390 |
|
---|
| 391 | // https://heycam.github.io/webidl/#idl-dictionaries
|
---|
| 392 | function isDictionary(x) {
|
---|
| 393 | return typeof x === 'object' || typeof x === 'function';
|
---|
| 394 | }
|
---|
| 395 | function assertDictionary(obj, context) {
|
---|
| 396 | if (obj !== undefined && !isDictionary(obj)) {
|
---|
| 397 | throw new TypeError("".concat(context, " is not an object."));
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 | // https://heycam.github.io/webidl/#idl-callback-functions
|
---|
| 401 | function assertFunction(x, context) {
|
---|
| 402 | if (typeof x !== 'function') {
|
---|
| 403 | throw new TypeError("".concat(context, " is not a function."));
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 | // https://heycam.github.io/webidl/#idl-object
|
---|
| 407 | function isObject(x) {
|
---|
| 408 | return (typeof x === 'object' && x !== null) || typeof x === 'function';
|
---|
| 409 | }
|
---|
| 410 | function assertObject(x, context) {
|
---|
| 411 | if (!isObject(x)) {
|
---|
| 412 | throw new TypeError("".concat(context, " is not an object."));
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 | function assertRequiredArgument(x, position, context) {
|
---|
| 416 | if (x === undefined) {
|
---|
| 417 | throw new TypeError("Parameter ".concat(position, " is required in '").concat(context, "'."));
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | function assertRequiredField(x, field, context) {
|
---|
| 421 | if (x === undefined) {
|
---|
| 422 | throw new TypeError("".concat(field, " is required in '").concat(context, "'."));
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | // https://heycam.github.io/webidl/#idl-unrestricted-double
|
---|
| 426 | function convertUnrestrictedDouble(value) {
|
---|
| 427 | return Number(value);
|
---|
| 428 | }
|
---|
| 429 | function censorNegativeZero(x) {
|
---|
| 430 | return x === 0 ? 0 : x;
|
---|
| 431 | }
|
---|
| 432 | function integerPart(x) {
|
---|
| 433 | return censorNegativeZero(MathTrunc(x));
|
---|
| 434 | }
|
---|
| 435 | // https://heycam.github.io/webidl/#idl-unsigned-long-long
|
---|
| 436 | function convertUnsignedLongLongWithEnforceRange(value, context) {
|
---|
| 437 | var lowerBound = 0;
|
---|
| 438 | var upperBound = Number.MAX_SAFE_INTEGER;
|
---|
| 439 | var x = Number(value);
|
---|
| 440 | x = censorNegativeZero(x);
|
---|
| 441 | if (!NumberIsFinite(x)) {
|
---|
| 442 | throw new TypeError("".concat(context, " is not a finite number"));
|
---|
| 443 | }
|
---|
| 444 | x = integerPart(x);
|
---|
| 445 | if (x < lowerBound || x > upperBound) {
|
---|
| 446 | throw new TypeError("".concat(context, " is outside the accepted range of ").concat(lowerBound, " to ").concat(upperBound, ", inclusive"));
|
---|
| 447 | }
|
---|
| 448 | if (!NumberIsFinite(x) || x === 0) {
|
---|
| 449 | return 0;
|
---|
| 450 | }
|
---|
| 451 | // TODO Use BigInt if supported?
|
---|
| 452 | // let xBigInt = BigInt(integerPart(x));
|
---|
| 453 | // xBigInt = BigInt.asUintN(64, xBigInt);
|
---|
| 454 | // return Number(xBigInt);
|
---|
| 455 | return x;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | function assertReadableStream(x, context) {
|
---|
| 459 | if (!IsReadableStream(x)) {
|
---|
| 460 | throw new TypeError("".concat(context, " is not a ReadableStream."));
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | // Abstract operations for the ReadableStream.
|
---|
| 465 | function AcquireReadableStreamDefaultReader(stream) {
|
---|
| 466 | return new ReadableStreamDefaultReader(stream);
|
---|
| 467 | }
|
---|
| 468 | // ReadableStream API exposed for controllers.
|
---|
| 469 | function ReadableStreamAddReadRequest(stream, readRequest) {
|
---|
| 470 | stream._reader._readRequests.push(readRequest);
|
---|
| 471 | }
|
---|
| 472 | function ReadableStreamFulfillReadRequest(stream, chunk, done) {
|
---|
| 473 | var reader = stream._reader;
|
---|
| 474 | var readRequest = reader._readRequests.shift();
|
---|
| 475 | if (done) {
|
---|
| 476 | readRequest._closeSteps();
|
---|
| 477 | }
|
---|
| 478 | else {
|
---|
| 479 | readRequest._chunkSteps(chunk);
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | function ReadableStreamGetNumReadRequests(stream) {
|
---|
| 483 | return stream._reader._readRequests.length;
|
---|
| 484 | }
|
---|
| 485 | function ReadableStreamHasDefaultReader(stream) {
|
---|
| 486 | var reader = stream._reader;
|
---|
| 487 | if (reader === undefined) {
|
---|
| 488 | return false;
|
---|
| 489 | }
|
---|
| 490 | if (!IsReadableStreamDefaultReader(reader)) {
|
---|
| 491 | return false;
|
---|
| 492 | }
|
---|
| 493 | return true;
|
---|
| 494 | }
|
---|
| 495 | /**
|
---|
| 496 | * A default reader vended by a {@link ReadableStream}.
|
---|
| 497 | *
|
---|
| 498 | * @public
|
---|
| 499 | */
|
---|
| 500 | var ReadableStreamDefaultReader = /** @class */ (function () {
|
---|
| 501 | function ReadableStreamDefaultReader(stream) {
|
---|
| 502 | assertRequiredArgument(stream, 1, 'ReadableStreamDefaultReader');
|
---|
| 503 | assertReadableStream(stream, 'First parameter');
|
---|
| 504 | if (IsReadableStreamLocked(stream)) {
|
---|
| 505 | throw new TypeError('This stream has already been locked for exclusive reading by another reader');
|
---|
| 506 | }
|
---|
| 507 | ReadableStreamReaderGenericInitialize(this, stream);
|
---|
| 508 | this._readRequests = new SimpleQueue();
|
---|
| 509 | }
|
---|
| 510 | Object.defineProperty(ReadableStreamDefaultReader.prototype, "closed", {
|
---|
| 511 | /**
|
---|
| 512 | * Returns a promise that will be fulfilled when the stream becomes closed,
|
---|
| 513 | * or rejected if the stream ever errors or the reader's lock is released before the stream finishes closing.
|
---|
| 514 | */
|
---|
| 515 | get: function () {
|
---|
| 516 | if (!IsReadableStreamDefaultReader(this)) {
|
---|
| 517 | return promiseRejectedWith(defaultReaderBrandCheckException('closed'));
|
---|
| 518 | }
|
---|
| 519 | return this._closedPromise;
|
---|
| 520 | },
|
---|
| 521 | enumerable: false,
|
---|
| 522 | configurable: true
|
---|
| 523 | });
|
---|
| 524 | /**
|
---|
| 525 | * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
|
---|
| 526 | */
|
---|
| 527 | ReadableStreamDefaultReader.prototype.cancel = function (reason) {
|
---|
| 528 | if (reason === void 0) { reason = undefined; }
|
---|
| 529 | if (!IsReadableStreamDefaultReader(this)) {
|
---|
| 530 | return promiseRejectedWith(defaultReaderBrandCheckException('cancel'));
|
---|
| 531 | }
|
---|
| 532 | if (this._ownerReadableStream === undefined) {
|
---|
| 533 | return promiseRejectedWith(readerLockException('cancel'));
|
---|
| 534 | }
|
---|
| 535 | return ReadableStreamReaderGenericCancel(this, reason);
|
---|
| 536 | };
|
---|
| 537 | /**
|
---|
| 538 | * Returns a promise that allows access to the next chunk from the stream's internal queue, if available.
|
---|
| 539 | *
|
---|
| 540 | * If reading a chunk causes the queue to become empty, more data will be pulled from the underlying source.
|
---|
| 541 | */
|
---|
| 542 | ReadableStreamDefaultReader.prototype.read = function () {
|
---|
| 543 | if (!IsReadableStreamDefaultReader(this)) {
|
---|
| 544 | return promiseRejectedWith(defaultReaderBrandCheckException('read'));
|
---|
| 545 | }
|
---|
| 546 | if (this._ownerReadableStream === undefined) {
|
---|
| 547 | return promiseRejectedWith(readerLockException('read from'));
|
---|
| 548 | }
|
---|
| 549 | var resolvePromise;
|
---|
| 550 | var rejectPromise;
|
---|
| 551 | var promise = newPromise(function (resolve, reject) {
|
---|
| 552 | resolvePromise = resolve;
|
---|
| 553 | rejectPromise = reject;
|
---|
| 554 | });
|
---|
| 555 | var readRequest = {
|
---|
| 556 | _chunkSteps: function (chunk) { return resolvePromise({ value: chunk, done: false }); },
|
---|
| 557 | _closeSteps: function () { return resolvePromise({ value: undefined, done: true }); },
|
---|
| 558 | _errorSteps: function (e) { return rejectPromise(e); }
|
---|
| 559 | };
|
---|
| 560 | ReadableStreamDefaultReaderRead(this, readRequest);
|
---|
| 561 | return promise;
|
---|
| 562 | };
|
---|
| 563 | /**
|
---|
| 564 | * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
|
---|
| 565 | * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
|
---|
| 566 | * from now on; otherwise, the reader will appear closed.
|
---|
| 567 | *
|
---|
| 568 | * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
|
---|
| 569 | * the reader's {@link ReadableStreamDefaultReader.read | read()} method has not yet been settled. Attempting to
|
---|
| 570 | * do so will throw a `TypeError` and leave the reader locked to the stream.
|
---|
| 571 | */
|
---|
| 572 | ReadableStreamDefaultReader.prototype.releaseLock = function () {
|
---|
| 573 | if (!IsReadableStreamDefaultReader(this)) {
|
---|
| 574 | throw defaultReaderBrandCheckException('releaseLock');
|
---|
| 575 | }
|
---|
| 576 | if (this._ownerReadableStream === undefined) {
|
---|
| 577 | return;
|
---|
| 578 | }
|
---|
| 579 | ReadableStreamDefaultReaderRelease(this);
|
---|
| 580 | };
|
---|
| 581 | return ReadableStreamDefaultReader;
|
---|
| 582 | }());
|
---|
| 583 | Object.defineProperties(ReadableStreamDefaultReader.prototype, {
|
---|
| 584 | cancel: { enumerable: true },
|
---|
| 585 | read: { enumerable: true },
|
---|
| 586 | releaseLock: { enumerable: true },
|
---|
| 587 | closed: { enumerable: true }
|
---|
| 588 | });
|
---|
| 589 | setFunctionName(ReadableStreamDefaultReader.prototype.cancel, 'cancel');
|
---|
| 590 | setFunctionName(ReadableStreamDefaultReader.prototype.read, 'read');
|
---|
| 591 | setFunctionName(ReadableStreamDefaultReader.prototype.releaseLock, 'releaseLock');
|
---|
| 592 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 593 | Object.defineProperty(ReadableStreamDefaultReader.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 594 | value: 'ReadableStreamDefaultReader',
|
---|
| 595 | configurable: true
|
---|
| 596 | });
|
---|
| 597 | }
|
---|
| 598 | // Abstract operations for the readers.
|
---|
| 599 | function IsReadableStreamDefaultReader(x) {
|
---|
| 600 | if (!typeIsObject(x)) {
|
---|
| 601 | return false;
|
---|
| 602 | }
|
---|
| 603 | if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
|
---|
| 604 | return false;
|
---|
| 605 | }
|
---|
| 606 | return x instanceof ReadableStreamDefaultReader;
|
---|
| 607 | }
|
---|
| 608 | function ReadableStreamDefaultReaderRead(reader, readRequest) {
|
---|
| 609 | var stream = reader._ownerReadableStream;
|
---|
| 610 | stream._disturbed = true;
|
---|
| 611 | if (stream._state === 'closed') {
|
---|
| 612 | readRequest._closeSteps();
|
---|
| 613 | }
|
---|
| 614 | else if (stream._state === 'errored') {
|
---|
| 615 | readRequest._errorSteps(stream._storedError);
|
---|
| 616 | }
|
---|
| 617 | else {
|
---|
| 618 | stream._readableStreamController[PullSteps](readRequest);
|
---|
| 619 | }
|
---|
| 620 | }
|
---|
| 621 | function ReadableStreamDefaultReaderRelease(reader) {
|
---|
| 622 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 623 | var e = new TypeError('Reader was released');
|
---|
| 624 | ReadableStreamDefaultReaderErrorReadRequests(reader, e);
|
---|
| 625 | }
|
---|
| 626 | function ReadableStreamDefaultReaderErrorReadRequests(reader, e) {
|
---|
| 627 | var readRequests = reader._readRequests;
|
---|
| 628 | reader._readRequests = new SimpleQueue();
|
---|
| 629 | readRequests.forEach(function (readRequest) {
|
---|
| 630 | readRequest._errorSteps(e);
|
---|
| 631 | });
|
---|
| 632 | }
|
---|
| 633 | // Helper functions for the ReadableStreamDefaultReader.
|
---|
| 634 | function defaultReaderBrandCheckException(name) {
|
---|
| 635 | return new TypeError("ReadableStreamDefaultReader.prototype.".concat(name, " can only be used on a ReadableStreamDefaultReader"));
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | /// <reference lib="es2018.asynciterable" />
|
---|
| 639 | var _a;
|
---|
| 640 | var AsyncIteratorPrototype;
|
---|
| 641 | if (typeof SymbolPolyfill.asyncIterator === 'symbol') {
|
---|
| 642 | // We're running inside a ES2018+ environment, but we're compiling to an older syntax.
|
---|
| 643 | // We cannot access %AsyncIteratorPrototype% without non-ES2018 syntax, but we can re-create it.
|
---|
| 644 | AsyncIteratorPrototype = (_a = {},
|
---|
| 645 | // 25.1.3.1 %AsyncIteratorPrototype% [ @@asyncIterator ] ( )
|
---|
| 646 | // https://tc39.github.io/ecma262/#sec-asynciteratorprototype-asynciterator
|
---|
| 647 | _a[SymbolPolyfill.asyncIterator] = function () {
|
---|
| 648 | return this;
|
---|
| 649 | },
|
---|
| 650 | _a);
|
---|
| 651 | Object.defineProperty(AsyncIteratorPrototype, SymbolPolyfill.asyncIterator, { enumerable: false });
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | /// <reference lib="es2018.asynciterable" />
|
---|
| 655 | var ReadableStreamAsyncIteratorImpl = /** @class */ (function () {
|
---|
| 656 | function ReadableStreamAsyncIteratorImpl(reader, preventCancel) {
|
---|
| 657 | this._ongoingPromise = undefined;
|
---|
| 658 | this._isFinished = false;
|
---|
| 659 | this._reader = reader;
|
---|
| 660 | this._preventCancel = preventCancel;
|
---|
| 661 | }
|
---|
| 662 | ReadableStreamAsyncIteratorImpl.prototype.next = function () {
|
---|
| 663 | var _this = this;
|
---|
| 664 | var nextSteps = function () { return _this._nextSteps(); };
|
---|
| 665 | this._ongoingPromise = this._ongoingPromise ?
|
---|
| 666 | transformPromiseWith(this._ongoingPromise, nextSteps, nextSteps) :
|
---|
| 667 | nextSteps();
|
---|
| 668 | return this._ongoingPromise;
|
---|
| 669 | };
|
---|
| 670 | ReadableStreamAsyncIteratorImpl.prototype.return = function (value) {
|
---|
| 671 | var _this = this;
|
---|
| 672 | var returnSteps = function () { return _this._returnSteps(value); };
|
---|
| 673 | return this._ongoingPromise ?
|
---|
| 674 | transformPromiseWith(this._ongoingPromise, returnSteps, returnSteps) :
|
---|
| 675 | returnSteps();
|
---|
| 676 | };
|
---|
| 677 | ReadableStreamAsyncIteratorImpl.prototype._nextSteps = function () {
|
---|
| 678 | var _this = this;
|
---|
| 679 | if (this._isFinished) {
|
---|
| 680 | return Promise.resolve({ value: undefined, done: true });
|
---|
| 681 | }
|
---|
| 682 | var reader = this._reader;
|
---|
| 683 | var resolvePromise;
|
---|
| 684 | var rejectPromise;
|
---|
| 685 | var promise = newPromise(function (resolve, reject) {
|
---|
| 686 | resolvePromise = resolve;
|
---|
| 687 | rejectPromise = reject;
|
---|
| 688 | });
|
---|
| 689 | var readRequest = {
|
---|
| 690 | _chunkSteps: function (chunk) {
|
---|
| 691 | _this._ongoingPromise = undefined;
|
---|
| 692 | // This needs to be delayed by one microtask, otherwise we stop pulling too early which breaks a test.
|
---|
| 693 | // FIXME Is this a bug in the specification, or in the test?
|
---|
| 694 | _queueMicrotask(function () { return resolvePromise({ value: chunk, done: false }); });
|
---|
| 695 | },
|
---|
| 696 | _closeSteps: function () {
|
---|
| 697 | _this._ongoingPromise = undefined;
|
---|
| 698 | _this._isFinished = true;
|
---|
| 699 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 700 | resolvePromise({ value: undefined, done: true });
|
---|
| 701 | },
|
---|
| 702 | _errorSteps: function (reason) {
|
---|
| 703 | _this._ongoingPromise = undefined;
|
---|
| 704 | _this._isFinished = true;
|
---|
| 705 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 706 | rejectPromise(reason);
|
---|
| 707 | }
|
---|
| 708 | };
|
---|
| 709 | ReadableStreamDefaultReaderRead(reader, readRequest);
|
---|
| 710 | return promise;
|
---|
| 711 | };
|
---|
| 712 | ReadableStreamAsyncIteratorImpl.prototype._returnSteps = function (value) {
|
---|
| 713 | if (this._isFinished) {
|
---|
| 714 | return Promise.resolve({ value: value, done: true });
|
---|
| 715 | }
|
---|
| 716 | this._isFinished = true;
|
---|
| 717 | var reader = this._reader;
|
---|
| 718 | if (!this._preventCancel) {
|
---|
| 719 | var result = ReadableStreamReaderGenericCancel(reader, value);
|
---|
| 720 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 721 | return transformPromiseWith(result, function () { return ({ value: value, done: true }); });
|
---|
| 722 | }
|
---|
| 723 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 724 | return promiseResolvedWith({ value: value, done: true });
|
---|
| 725 | };
|
---|
| 726 | return ReadableStreamAsyncIteratorImpl;
|
---|
| 727 | }());
|
---|
| 728 | var ReadableStreamAsyncIteratorPrototype = {
|
---|
| 729 | next: function () {
|
---|
| 730 | if (!IsReadableStreamAsyncIterator(this)) {
|
---|
| 731 | return promiseRejectedWith(streamAsyncIteratorBrandCheckException('next'));
|
---|
| 732 | }
|
---|
| 733 | return this._asyncIteratorImpl.next();
|
---|
| 734 | },
|
---|
| 735 | return: function (value) {
|
---|
| 736 | if (!IsReadableStreamAsyncIterator(this)) {
|
---|
| 737 | return promiseRejectedWith(streamAsyncIteratorBrandCheckException('return'));
|
---|
| 738 | }
|
---|
| 739 | return this._asyncIteratorImpl.return(value);
|
---|
| 740 | }
|
---|
| 741 | };
|
---|
| 742 | if (AsyncIteratorPrototype !== undefined) {
|
---|
| 743 | Object.setPrototypeOf(ReadableStreamAsyncIteratorPrototype, AsyncIteratorPrototype);
|
---|
| 744 | }
|
---|
| 745 | // Abstract operations for the ReadableStream.
|
---|
| 746 | function AcquireReadableStreamAsyncIterator(stream, preventCancel) {
|
---|
| 747 | var reader = AcquireReadableStreamDefaultReader(stream);
|
---|
| 748 | var impl = new ReadableStreamAsyncIteratorImpl(reader, preventCancel);
|
---|
| 749 | var iterator = Object.create(ReadableStreamAsyncIteratorPrototype);
|
---|
| 750 | iterator._asyncIteratorImpl = impl;
|
---|
| 751 | return iterator;
|
---|
| 752 | }
|
---|
| 753 | function IsReadableStreamAsyncIterator(x) {
|
---|
| 754 | if (!typeIsObject(x)) {
|
---|
| 755 | return false;
|
---|
| 756 | }
|
---|
| 757 | if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorImpl')) {
|
---|
| 758 | return false;
|
---|
| 759 | }
|
---|
| 760 | try {
|
---|
| 761 | // noinspection SuspiciousTypeOfGuard
|
---|
| 762 | return x._asyncIteratorImpl instanceof
|
---|
| 763 | ReadableStreamAsyncIteratorImpl;
|
---|
| 764 | }
|
---|
| 765 | catch (_a) {
|
---|
| 766 | return false;
|
---|
| 767 | }
|
---|
| 768 | }
|
---|
| 769 | // Helper functions for the ReadableStream.
|
---|
| 770 | function streamAsyncIteratorBrandCheckException(name) {
|
---|
| 771 | return new TypeError("ReadableStreamAsyncIterator.".concat(name, " can only be used on a ReadableSteamAsyncIterator"));
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | /// <reference lib="es2015.core" />
|
---|
| 775 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Polyfill
|
---|
| 776 | var NumberIsNaN = Number.isNaN || function (x) {
|
---|
| 777 | // eslint-disable-next-line no-self-compare
|
---|
| 778 | return x !== x;
|
---|
| 779 | };
|
---|
| 780 |
|
---|
| 781 | function CreateArrayFromList(elements) {
|
---|
| 782 | // We use arrays to represent lists, so this is basically a no-op.
|
---|
| 783 | // Do a slice though just in case we happen to depend on the unique-ness.
|
---|
| 784 | return elements.slice();
|
---|
| 785 | }
|
---|
| 786 | function CopyDataBlockBytes(dest, destOffset, src, srcOffset, n) {
|
---|
| 787 | new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
|
---|
| 788 | }
|
---|
| 789 | var TransferArrayBuffer = function (O) {
|
---|
| 790 | if (typeof O.transfer === 'function') {
|
---|
| 791 | TransferArrayBuffer = function (buffer) { return buffer.transfer(); };
|
---|
| 792 | }
|
---|
| 793 | else if (typeof structuredClone === 'function') {
|
---|
| 794 | TransferArrayBuffer = function (buffer) { return structuredClone(buffer, { transfer: [buffer] }); };
|
---|
| 795 | }
|
---|
| 796 | else {
|
---|
| 797 | // Not implemented correctly
|
---|
| 798 | TransferArrayBuffer = function (buffer) { return buffer; };
|
---|
| 799 | }
|
---|
| 800 | return TransferArrayBuffer(O);
|
---|
| 801 | };
|
---|
| 802 | var IsDetachedBuffer = function (O) {
|
---|
| 803 | if (typeof O.detached === 'boolean') {
|
---|
| 804 | IsDetachedBuffer = function (buffer) { return buffer.detached; };
|
---|
| 805 | }
|
---|
| 806 | else {
|
---|
| 807 | // Not implemented correctly
|
---|
| 808 | IsDetachedBuffer = function (buffer) { return buffer.byteLength === 0; };
|
---|
| 809 | }
|
---|
| 810 | return IsDetachedBuffer(O);
|
---|
| 811 | };
|
---|
| 812 | function ArrayBufferSlice(buffer, begin, end) {
|
---|
| 813 | // ArrayBuffer.prototype.slice is not available on IE10
|
---|
| 814 | // https://www.caniuse.com/mdn-javascript_builtins_arraybuffer_slice
|
---|
| 815 | if (buffer.slice) {
|
---|
| 816 | return buffer.slice(begin, end);
|
---|
| 817 | }
|
---|
| 818 | var length = end - begin;
|
---|
| 819 | var slice = new ArrayBuffer(length);
|
---|
| 820 | CopyDataBlockBytes(slice, 0, buffer, begin, length);
|
---|
| 821 | return slice;
|
---|
| 822 | }
|
---|
| 823 | function GetMethod(receiver, prop) {
|
---|
| 824 | var func = receiver[prop];
|
---|
| 825 | if (func === undefined || func === null) {
|
---|
| 826 | return undefined;
|
---|
| 827 | }
|
---|
| 828 | if (typeof func !== 'function') {
|
---|
| 829 | throw new TypeError("".concat(String(prop), " is not a function"));
|
---|
| 830 | }
|
---|
| 831 | return func;
|
---|
| 832 | }
|
---|
| 833 | function CreateAsyncFromSyncIterator(syncIteratorRecord) {
|
---|
| 834 | // Instead of re-implementing CreateAsyncFromSyncIterator and %AsyncFromSyncIteratorPrototype%,
|
---|
| 835 | // we use yield* inside an async generator function to achieve the same result.
|
---|
| 836 | var _a;
|
---|
| 837 | // Wrap the sync iterator inside a sync iterable, so we can use it with yield*.
|
---|
| 838 | var syncIterable = (_a = {},
|
---|
| 839 | _a[SymbolPolyfill.iterator] = function () { return syncIteratorRecord.iterator; },
|
---|
| 840 | _a);
|
---|
| 841 | // Create an async generator function and immediately invoke it.
|
---|
| 842 | var asyncIterator = (function () {
|
---|
| 843 | return __asyncGenerator(this, arguments, function () {
|
---|
| 844 | return __generator(this, function (_a) {
|
---|
| 845 | switch (_a.label) {
|
---|
| 846 | case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues(syncIterable)))];
|
---|
| 847 | case 1: return [4 /*yield*/, __await.apply(void 0, [_a.sent()])];
|
---|
| 848 | case 2: return [4 /*yield*/, __await.apply(void 0, [_a.sent()])];
|
---|
| 849 | case 3: return [2 /*return*/, _a.sent()];
|
---|
| 850 | }
|
---|
| 851 | });
|
---|
| 852 | });
|
---|
| 853 | }());
|
---|
| 854 | // Return as an async iterator record.
|
---|
| 855 | var nextMethod = asyncIterator.next;
|
---|
| 856 | return { iterator: asyncIterator, nextMethod: nextMethod, done: false };
|
---|
| 857 | }
|
---|
| 858 | function GetIterator(obj, hint, method) {
|
---|
| 859 | if (hint === void 0) { hint = 'sync'; }
|
---|
| 860 | if (method === undefined) {
|
---|
| 861 | if (hint === 'async') {
|
---|
| 862 | method = GetMethod(obj, SymbolPolyfill.asyncIterator);
|
---|
| 863 | if (method === undefined) {
|
---|
| 864 | var syncMethod = GetMethod(obj, SymbolPolyfill.iterator);
|
---|
| 865 | var syncIteratorRecord = GetIterator(obj, 'sync', syncMethod);
|
---|
| 866 | return CreateAsyncFromSyncIterator(syncIteratorRecord);
|
---|
| 867 | }
|
---|
| 868 | }
|
---|
| 869 | else {
|
---|
| 870 | method = GetMethod(obj, SymbolPolyfill.iterator);
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 | if (method === undefined) {
|
---|
| 874 | throw new TypeError('The object is not iterable');
|
---|
| 875 | }
|
---|
| 876 | var iterator = reflectCall(method, obj, []);
|
---|
| 877 | if (!typeIsObject(iterator)) {
|
---|
| 878 | throw new TypeError('The iterator method must return an object');
|
---|
| 879 | }
|
---|
| 880 | var nextMethod = iterator.next;
|
---|
| 881 | return { iterator: iterator, nextMethod: nextMethod, done: false };
|
---|
| 882 | }
|
---|
| 883 | function IteratorNext(iteratorRecord) {
|
---|
| 884 | var result = reflectCall(iteratorRecord.nextMethod, iteratorRecord.iterator, []);
|
---|
| 885 | if (!typeIsObject(result)) {
|
---|
| 886 | throw new TypeError('The iterator.next() method must return an object');
|
---|
| 887 | }
|
---|
| 888 | return result;
|
---|
| 889 | }
|
---|
| 890 | function IteratorComplete(iterResult) {
|
---|
| 891 | return Boolean(iterResult.done);
|
---|
| 892 | }
|
---|
| 893 | function IteratorValue(iterResult) {
|
---|
| 894 | return iterResult.value;
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | function IsNonNegativeNumber(v) {
|
---|
| 898 | if (typeof v !== 'number') {
|
---|
| 899 | return false;
|
---|
| 900 | }
|
---|
| 901 | if (NumberIsNaN(v)) {
|
---|
| 902 | return false;
|
---|
| 903 | }
|
---|
| 904 | if (v < 0) {
|
---|
| 905 | return false;
|
---|
| 906 | }
|
---|
| 907 | return true;
|
---|
| 908 | }
|
---|
| 909 | function CloneAsUint8Array(O) {
|
---|
| 910 | var buffer = ArrayBufferSlice(O.buffer, O.byteOffset, O.byteOffset + O.byteLength);
|
---|
| 911 | return new Uint8Array(buffer);
|
---|
| 912 | }
|
---|
| 913 |
|
---|
| 914 | function DequeueValue(container) {
|
---|
| 915 | var pair = container._queue.shift();
|
---|
| 916 | container._queueTotalSize -= pair.size;
|
---|
| 917 | if (container._queueTotalSize < 0) {
|
---|
| 918 | container._queueTotalSize = 0;
|
---|
| 919 | }
|
---|
| 920 | return pair.value;
|
---|
| 921 | }
|
---|
| 922 | function EnqueueValueWithSize(container, value, size) {
|
---|
| 923 | if (!IsNonNegativeNumber(size) || size === Infinity) {
|
---|
| 924 | throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
|
---|
| 925 | }
|
---|
| 926 | container._queue.push({ value: value, size: size });
|
---|
| 927 | container._queueTotalSize += size;
|
---|
| 928 | }
|
---|
| 929 | function PeekQueueValue(container) {
|
---|
| 930 | var pair = container._queue.peek();
|
---|
| 931 | return pair.value;
|
---|
| 932 | }
|
---|
| 933 | function ResetQueue(container) {
|
---|
| 934 | container._queue = new SimpleQueue();
|
---|
| 935 | container._queueTotalSize = 0;
|
---|
| 936 | }
|
---|
| 937 |
|
---|
| 938 | function isDataViewConstructor(ctor) {
|
---|
| 939 | return ctor === DataView;
|
---|
| 940 | }
|
---|
| 941 | function isDataView(view) {
|
---|
| 942 | return isDataViewConstructor(view.constructor);
|
---|
| 943 | }
|
---|
| 944 | function arrayBufferViewElementSize(ctor) {
|
---|
| 945 | if (isDataViewConstructor(ctor)) {
|
---|
| 946 | return 1;
|
---|
| 947 | }
|
---|
| 948 | return ctor.BYTES_PER_ELEMENT;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | /**
|
---|
| 952 | * A pull-into request in a {@link ReadableByteStreamController}.
|
---|
| 953 | *
|
---|
| 954 | * @public
|
---|
| 955 | */
|
---|
| 956 | var ReadableStreamBYOBRequest = /** @class */ (function () {
|
---|
| 957 | function ReadableStreamBYOBRequest() {
|
---|
| 958 | throw new TypeError('Illegal constructor');
|
---|
| 959 | }
|
---|
| 960 | Object.defineProperty(ReadableStreamBYOBRequest.prototype, "view", {
|
---|
| 961 | /**
|
---|
| 962 | * Returns the view for writing in to, or `null` if the BYOB request has already been responded to.
|
---|
| 963 | */
|
---|
| 964 | get: function () {
|
---|
| 965 | if (!IsReadableStreamBYOBRequest(this)) {
|
---|
| 966 | throw byobRequestBrandCheckException('view');
|
---|
| 967 | }
|
---|
| 968 | return this._view;
|
---|
| 969 | },
|
---|
| 970 | enumerable: false,
|
---|
| 971 | configurable: true
|
---|
| 972 | });
|
---|
| 973 | ReadableStreamBYOBRequest.prototype.respond = function (bytesWritten) {
|
---|
| 974 | if (!IsReadableStreamBYOBRequest(this)) {
|
---|
| 975 | throw byobRequestBrandCheckException('respond');
|
---|
| 976 | }
|
---|
| 977 | assertRequiredArgument(bytesWritten, 1, 'respond');
|
---|
| 978 | bytesWritten = convertUnsignedLongLongWithEnforceRange(bytesWritten, 'First parameter');
|
---|
| 979 | if (this._associatedReadableByteStreamController === undefined) {
|
---|
| 980 | throw new TypeError('This BYOB request has been invalidated');
|
---|
| 981 | }
|
---|
| 982 | if (IsDetachedBuffer(this._view.buffer)) {
|
---|
| 983 | throw new TypeError("The BYOB request's buffer has been detached and so cannot be used as a response");
|
---|
| 984 | }
|
---|
| 985 | ReadableByteStreamControllerRespond(this._associatedReadableByteStreamController, bytesWritten);
|
---|
| 986 | };
|
---|
| 987 | ReadableStreamBYOBRequest.prototype.respondWithNewView = function (view) {
|
---|
| 988 | if (!IsReadableStreamBYOBRequest(this)) {
|
---|
| 989 | throw byobRequestBrandCheckException('respondWithNewView');
|
---|
| 990 | }
|
---|
| 991 | assertRequiredArgument(view, 1, 'respondWithNewView');
|
---|
| 992 | if (!ArrayBuffer.isView(view)) {
|
---|
| 993 | throw new TypeError('You can only respond with array buffer views');
|
---|
| 994 | }
|
---|
| 995 | if (this._associatedReadableByteStreamController === undefined) {
|
---|
| 996 | throw new TypeError('This BYOB request has been invalidated');
|
---|
| 997 | }
|
---|
| 998 | if (IsDetachedBuffer(view.buffer)) {
|
---|
| 999 | throw new TypeError('The given view\'s buffer has been detached and so cannot be used as a response');
|
---|
| 1000 | }
|
---|
| 1001 | ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
|
---|
| 1002 | };
|
---|
| 1003 | return ReadableStreamBYOBRequest;
|
---|
| 1004 | }());
|
---|
| 1005 | Object.defineProperties(ReadableStreamBYOBRequest.prototype, {
|
---|
| 1006 | respond: { enumerable: true },
|
---|
| 1007 | respondWithNewView: { enumerable: true },
|
---|
| 1008 | view: { enumerable: true }
|
---|
| 1009 | });
|
---|
| 1010 | setFunctionName(ReadableStreamBYOBRequest.prototype.respond, 'respond');
|
---|
| 1011 | setFunctionName(ReadableStreamBYOBRequest.prototype.respondWithNewView, 'respondWithNewView');
|
---|
| 1012 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 1013 | Object.defineProperty(ReadableStreamBYOBRequest.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 1014 | value: 'ReadableStreamBYOBRequest',
|
---|
| 1015 | configurable: true
|
---|
| 1016 | });
|
---|
| 1017 | }
|
---|
| 1018 | /**
|
---|
| 1019 | * Allows control of a {@link ReadableStream | readable byte stream}'s state and internal queue.
|
---|
| 1020 | *
|
---|
| 1021 | * @public
|
---|
| 1022 | */
|
---|
| 1023 | var ReadableByteStreamController = /** @class */ (function () {
|
---|
| 1024 | function ReadableByteStreamController() {
|
---|
| 1025 | throw new TypeError('Illegal constructor');
|
---|
| 1026 | }
|
---|
| 1027 | Object.defineProperty(ReadableByteStreamController.prototype, "byobRequest", {
|
---|
| 1028 | /**
|
---|
| 1029 | * Returns the current BYOB pull request, or `null` if there isn't one.
|
---|
| 1030 | */
|
---|
| 1031 | get: function () {
|
---|
| 1032 | if (!IsReadableByteStreamController(this)) {
|
---|
| 1033 | throw byteStreamControllerBrandCheckException('byobRequest');
|
---|
| 1034 | }
|
---|
| 1035 | return ReadableByteStreamControllerGetBYOBRequest(this);
|
---|
| 1036 | },
|
---|
| 1037 | enumerable: false,
|
---|
| 1038 | configurable: true
|
---|
| 1039 | });
|
---|
| 1040 | Object.defineProperty(ReadableByteStreamController.prototype, "desiredSize", {
|
---|
| 1041 | /**
|
---|
| 1042 | * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
|
---|
| 1043 | * over-full. An underlying byte source ought to use this information to determine when and how to apply backpressure.
|
---|
| 1044 | */
|
---|
| 1045 | get: function () {
|
---|
| 1046 | if (!IsReadableByteStreamController(this)) {
|
---|
| 1047 | throw byteStreamControllerBrandCheckException('desiredSize');
|
---|
| 1048 | }
|
---|
| 1049 | return ReadableByteStreamControllerGetDesiredSize(this);
|
---|
| 1050 | },
|
---|
| 1051 | enumerable: false,
|
---|
| 1052 | configurable: true
|
---|
| 1053 | });
|
---|
| 1054 | /**
|
---|
| 1055 | * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
|
---|
| 1056 | * the stream, but once those are read, the stream will become closed.
|
---|
| 1057 | */
|
---|
| 1058 | ReadableByteStreamController.prototype.close = function () {
|
---|
| 1059 | if (!IsReadableByteStreamController(this)) {
|
---|
| 1060 | throw byteStreamControllerBrandCheckException('close');
|
---|
| 1061 | }
|
---|
| 1062 | if (this._closeRequested) {
|
---|
| 1063 | throw new TypeError('The stream has already been closed; do not close it again!');
|
---|
| 1064 | }
|
---|
| 1065 | var state = this._controlledReadableByteStream._state;
|
---|
| 1066 | if (state !== 'readable') {
|
---|
| 1067 | throw new TypeError("The stream (in ".concat(state, " state) is not in the readable state and cannot be closed"));
|
---|
| 1068 | }
|
---|
| 1069 | ReadableByteStreamControllerClose(this);
|
---|
| 1070 | };
|
---|
| 1071 | ReadableByteStreamController.prototype.enqueue = function (chunk) {
|
---|
| 1072 | if (!IsReadableByteStreamController(this)) {
|
---|
| 1073 | throw byteStreamControllerBrandCheckException('enqueue');
|
---|
| 1074 | }
|
---|
| 1075 | assertRequiredArgument(chunk, 1, 'enqueue');
|
---|
| 1076 | if (!ArrayBuffer.isView(chunk)) {
|
---|
| 1077 | throw new TypeError('chunk must be an array buffer view');
|
---|
| 1078 | }
|
---|
| 1079 | if (chunk.byteLength === 0) {
|
---|
| 1080 | throw new TypeError('chunk must have non-zero byteLength');
|
---|
| 1081 | }
|
---|
| 1082 | if (chunk.buffer.byteLength === 0) {
|
---|
| 1083 | throw new TypeError("chunk's buffer must have non-zero byteLength");
|
---|
| 1084 | }
|
---|
| 1085 | if (this._closeRequested) {
|
---|
| 1086 | throw new TypeError('stream is closed or draining');
|
---|
| 1087 | }
|
---|
| 1088 | var state = this._controlledReadableByteStream._state;
|
---|
| 1089 | if (state !== 'readable') {
|
---|
| 1090 | throw new TypeError("The stream (in ".concat(state, " state) is not in the readable state and cannot be enqueued to"));
|
---|
| 1091 | }
|
---|
| 1092 | ReadableByteStreamControllerEnqueue(this, chunk);
|
---|
| 1093 | };
|
---|
| 1094 | /**
|
---|
| 1095 | * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
|
---|
| 1096 | */
|
---|
| 1097 | ReadableByteStreamController.prototype.error = function (e) {
|
---|
| 1098 | if (e === void 0) { e = undefined; }
|
---|
| 1099 | if (!IsReadableByteStreamController(this)) {
|
---|
| 1100 | throw byteStreamControllerBrandCheckException('error');
|
---|
| 1101 | }
|
---|
| 1102 | ReadableByteStreamControllerError(this, e);
|
---|
| 1103 | };
|
---|
| 1104 | /** @internal */
|
---|
| 1105 | ReadableByteStreamController.prototype[CancelSteps] = function (reason) {
|
---|
| 1106 | ReadableByteStreamControllerClearPendingPullIntos(this);
|
---|
| 1107 | ResetQueue(this);
|
---|
| 1108 | var result = this._cancelAlgorithm(reason);
|
---|
| 1109 | ReadableByteStreamControllerClearAlgorithms(this);
|
---|
| 1110 | return result;
|
---|
| 1111 | };
|
---|
| 1112 | /** @internal */
|
---|
| 1113 | ReadableByteStreamController.prototype[PullSteps] = function (readRequest) {
|
---|
| 1114 | var stream = this._controlledReadableByteStream;
|
---|
| 1115 | if (this._queueTotalSize > 0) {
|
---|
| 1116 | ReadableByteStreamControllerFillReadRequestFromQueue(this, readRequest);
|
---|
| 1117 | return;
|
---|
| 1118 | }
|
---|
| 1119 | var autoAllocateChunkSize = this._autoAllocateChunkSize;
|
---|
| 1120 | if (autoAllocateChunkSize !== undefined) {
|
---|
| 1121 | var buffer = void 0;
|
---|
| 1122 | try {
|
---|
| 1123 | buffer = new ArrayBuffer(autoAllocateChunkSize);
|
---|
| 1124 | }
|
---|
| 1125 | catch (bufferE) {
|
---|
| 1126 | readRequest._errorSteps(bufferE);
|
---|
| 1127 | return;
|
---|
| 1128 | }
|
---|
| 1129 | var pullIntoDescriptor = {
|
---|
| 1130 | buffer: buffer,
|
---|
| 1131 | bufferByteLength: autoAllocateChunkSize,
|
---|
| 1132 | byteOffset: 0,
|
---|
| 1133 | byteLength: autoAllocateChunkSize,
|
---|
| 1134 | bytesFilled: 0,
|
---|
| 1135 | minimumFill: 1,
|
---|
| 1136 | elementSize: 1,
|
---|
| 1137 | viewConstructor: Uint8Array,
|
---|
| 1138 | readerType: 'default'
|
---|
| 1139 | };
|
---|
| 1140 | this._pendingPullIntos.push(pullIntoDescriptor);
|
---|
| 1141 | }
|
---|
| 1142 | ReadableStreamAddReadRequest(stream, readRequest);
|
---|
| 1143 | ReadableByteStreamControllerCallPullIfNeeded(this);
|
---|
| 1144 | };
|
---|
| 1145 | /** @internal */
|
---|
| 1146 | ReadableByteStreamController.prototype[ReleaseSteps] = function () {
|
---|
| 1147 | if (this._pendingPullIntos.length > 0) {
|
---|
| 1148 | var firstPullInto = this._pendingPullIntos.peek();
|
---|
| 1149 | firstPullInto.readerType = 'none';
|
---|
| 1150 | this._pendingPullIntos = new SimpleQueue();
|
---|
| 1151 | this._pendingPullIntos.push(firstPullInto);
|
---|
| 1152 | }
|
---|
| 1153 | };
|
---|
| 1154 | return ReadableByteStreamController;
|
---|
| 1155 | }());
|
---|
| 1156 | Object.defineProperties(ReadableByteStreamController.prototype, {
|
---|
| 1157 | close: { enumerable: true },
|
---|
| 1158 | enqueue: { enumerable: true },
|
---|
| 1159 | error: { enumerable: true },
|
---|
| 1160 | byobRequest: { enumerable: true },
|
---|
| 1161 | desiredSize: { enumerable: true }
|
---|
| 1162 | });
|
---|
| 1163 | setFunctionName(ReadableByteStreamController.prototype.close, 'close');
|
---|
| 1164 | setFunctionName(ReadableByteStreamController.prototype.enqueue, 'enqueue');
|
---|
| 1165 | setFunctionName(ReadableByteStreamController.prototype.error, 'error');
|
---|
| 1166 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 1167 | Object.defineProperty(ReadableByteStreamController.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 1168 | value: 'ReadableByteStreamController',
|
---|
| 1169 | configurable: true
|
---|
| 1170 | });
|
---|
| 1171 | }
|
---|
| 1172 | // Abstract operations for the ReadableByteStreamController.
|
---|
| 1173 | function IsReadableByteStreamController(x) {
|
---|
| 1174 | if (!typeIsObject(x)) {
|
---|
| 1175 | return false;
|
---|
| 1176 | }
|
---|
| 1177 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
|
---|
| 1178 | return false;
|
---|
| 1179 | }
|
---|
| 1180 | return x instanceof ReadableByteStreamController;
|
---|
| 1181 | }
|
---|
| 1182 | function IsReadableStreamBYOBRequest(x) {
|
---|
| 1183 | if (!typeIsObject(x)) {
|
---|
| 1184 | return false;
|
---|
| 1185 | }
|
---|
| 1186 | if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
|
---|
| 1187 | return false;
|
---|
| 1188 | }
|
---|
| 1189 | return x instanceof ReadableStreamBYOBRequest;
|
---|
| 1190 | }
|
---|
| 1191 | function ReadableByteStreamControllerCallPullIfNeeded(controller) {
|
---|
| 1192 | var shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
|
---|
| 1193 | if (!shouldPull) {
|
---|
| 1194 | return;
|
---|
| 1195 | }
|
---|
| 1196 | if (controller._pulling) {
|
---|
| 1197 | controller._pullAgain = true;
|
---|
| 1198 | return;
|
---|
| 1199 | }
|
---|
| 1200 | controller._pulling = true;
|
---|
| 1201 | // TODO: Test controller argument
|
---|
| 1202 | var pullPromise = controller._pullAlgorithm();
|
---|
| 1203 | uponPromise(pullPromise, function () {
|
---|
| 1204 | controller._pulling = false;
|
---|
| 1205 | if (controller._pullAgain) {
|
---|
| 1206 | controller._pullAgain = false;
|
---|
| 1207 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1208 | }
|
---|
| 1209 | return null;
|
---|
| 1210 | }, function (e) {
|
---|
| 1211 | ReadableByteStreamControllerError(controller, e);
|
---|
| 1212 | return null;
|
---|
| 1213 | });
|
---|
| 1214 | }
|
---|
| 1215 | function ReadableByteStreamControllerClearPendingPullIntos(controller) {
|
---|
| 1216 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
---|
| 1217 | controller._pendingPullIntos = new SimpleQueue();
|
---|
| 1218 | }
|
---|
| 1219 | function ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor) {
|
---|
| 1220 | var done = false;
|
---|
| 1221 | if (stream._state === 'closed') {
|
---|
| 1222 | done = true;
|
---|
| 1223 | }
|
---|
| 1224 | var filledView = ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor);
|
---|
| 1225 | if (pullIntoDescriptor.readerType === 'default') {
|
---|
| 1226 | ReadableStreamFulfillReadRequest(stream, filledView, done);
|
---|
| 1227 | }
|
---|
| 1228 | else {
|
---|
| 1229 | ReadableStreamFulfillReadIntoRequest(stream, filledView, done);
|
---|
| 1230 | }
|
---|
| 1231 | }
|
---|
| 1232 | function ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor) {
|
---|
| 1233 | var bytesFilled = pullIntoDescriptor.bytesFilled;
|
---|
| 1234 | var elementSize = pullIntoDescriptor.elementSize;
|
---|
| 1235 | return new pullIntoDescriptor.viewConstructor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, bytesFilled / elementSize);
|
---|
| 1236 | }
|
---|
| 1237 | function ReadableByteStreamControllerEnqueueChunkToQueue(controller, buffer, byteOffset, byteLength) {
|
---|
| 1238 | controller._queue.push({ buffer: buffer, byteOffset: byteOffset, byteLength: byteLength });
|
---|
| 1239 | controller._queueTotalSize += byteLength;
|
---|
| 1240 | }
|
---|
| 1241 | function ReadableByteStreamControllerEnqueueClonedChunkToQueue(controller, buffer, byteOffset, byteLength) {
|
---|
| 1242 | var clonedChunk;
|
---|
| 1243 | try {
|
---|
| 1244 | clonedChunk = ArrayBufferSlice(buffer, byteOffset, byteOffset + byteLength);
|
---|
| 1245 | }
|
---|
| 1246 | catch (cloneE) {
|
---|
| 1247 | ReadableByteStreamControllerError(controller, cloneE);
|
---|
| 1248 | throw cloneE;
|
---|
| 1249 | }
|
---|
| 1250 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, clonedChunk, 0, byteLength);
|
---|
| 1251 | }
|
---|
| 1252 | function ReadableByteStreamControllerEnqueueDetachedPullIntoToQueue(controller, firstDescriptor) {
|
---|
| 1253 | if (firstDescriptor.bytesFilled > 0) {
|
---|
| 1254 | ReadableByteStreamControllerEnqueueClonedChunkToQueue(controller, firstDescriptor.buffer, firstDescriptor.byteOffset, firstDescriptor.bytesFilled);
|
---|
| 1255 | }
|
---|
| 1256 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1257 | }
|
---|
| 1258 | function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor) {
|
---|
| 1259 | var maxBytesToCopy = Math.min(controller._queueTotalSize, pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled);
|
---|
| 1260 | var maxBytesFilled = pullIntoDescriptor.bytesFilled + maxBytesToCopy;
|
---|
| 1261 | var totalBytesToCopyRemaining = maxBytesToCopy;
|
---|
| 1262 | var ready = false;
|
---|
| 1263 | var remainderBytes = maxBytesFilled % pullIntoDescriptor.elementSize;
|
---|
| 1264 | var maxAlignedBytes = maxBytesFilled - remainderBytes;
|
---|
| 1265 | // A descriptor for a read() request that is not yet filled up to its minimum length will stay at the head
|
---|
| 1266 | // of the queue, so the underlying source can keep filling it.
|
---|
| 1267 | if (maxAlignedBytes >= pullIntoDescriptor.minimumFill) {
|
---|
| 1268 | totalBytesToCopyRemaining = maxAlignedBytes - pullIntoDescriptor.bytesFilled;
|
---|
| 1269 | ready = true;
|
---|
| 1270 | }
|
---|
| 1271 | var queue = controller._queue;
|
---|
| 1272 | while (totalBytesToCopyRemaining > 0) {
|
---|
| 1273 | var headOfQueue = queue.peek();
|
---|
| 1274 | var bytesToCopy = Math.min(totalBytesToCopyRemaining, headOfQueue.byteLength);
|
---|
| 1275 | var destStart = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
|
---|
| 1276 | CopyDataBlockBytes(pullIntoDescriptor.buffer, destStart, headOfQueue.buffer, headOfQueue.byteOffset, bytesToCopy);
|
---|
| 1277 | if (headOfQueue.byteLength === bytesToCopy) {
|
---|
| 1278 | queue.shift();
|
---|
| 1279 | }
|
---|
| 1280 | else {
|
---|
| 1281 | headOfQueue.byteOffset += bytesToCopy;
|
---|
| 1282 | headOfQueue.byteLength -= bytesToCopy;
|
---|
| 1283 | }
|
---|
| 1284 | controller._queueTotalSize -= bytesToCopy;
|
---|
| 1285 | ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesToCopy, pullIntoDescriptor);
|
---|
| 1286 | totalBytesToCopyRemaining -= bytesToCopy;
|
---|
| 1287 | }
|
---|
| 1288 | return ready;
|
---|
| 1289 | }
|
---|
| 1290 | function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
|
---|
| 1291 | pullIntoDescriptor.bytesFilled += size;
|
---|
| 1292 | }
|
---|
| 1293 | function ReadableByteStreamControllerHandleQueueDrain(controller) {
|
---|
| 1294 | if (controller._queueTotalSize === 0 && controller._closeRequested) {
|
---|
| 1295 | ReadableByteStreamControllerClearAlgorithms(controller);
|
---|
| 1296 | ReadableStreamClose(controller._controlledReadableByteStream);
|
---|
| 1297 | }
|
---|
| 1298 | else {
|
---|
| 1299 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1300 | }
|
---|
| 1301 | }
|
---|
| 1302 | function ReadableByteStreamControllerInvalidateBYOBRequest(controller) {
|
---|
| 1303 | if (controller._byobRequest === null) {
|
---|
| 1304 | return;
|
---|
| 1305 | }
|
---|
| 1306 | controller._byobRequest._associatedReadableByteStreamController = undefined;
|
---|
| 1307 | controller._byobRequest._view = null;
|
---|
| 1308 | controller._byobRequest = null;
|
---|
| 1309 | }
|
---|
| 1310 | function ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller) {
|
---|
| 1311 | while (controller._pendingPullIntos.length > 0) {
|
---|
| 1312 | if (controller._queueTotalSize === 0) {
|
---|
| 1313 | return;
|
---|
| 1314 | }
|
---|
| 1315 | var pullIntoDescriptor = controller._pendingPullIntos.peek();
|
---|
| 1316 | if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor)) {
|
---|
| 1317 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1318 | ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
|
---|
| 1319 | }
|
---|
| 1320 | }
|
---|
| 1321 | }
|
---|
| 1322 | function ReadableByteStreamControllerProcessReadRequestsUsingQueue(controller) {
|
---|
| 1323 | var reader = controller._controlledReadableByteStream._reader;
|
---|
| 1324 | while (reader._readRequests.length > 0) {
|
---|
| 1325 | if (controller._queueTotalSize === 0) {
|
---|
| 1326 | return;
|
---|
| 1327 | }
|
---|
| 1328 | var readRequest = reader._readRequests.shift();
|
---|
| 1329 | ReadableByteStreamControllerFillReadRequestFromQueue(controller, readRequest);
|
---|
| 1330 | }
|
---|
| 1331 | }
|
---|
| 1332 | function ReadableByteStreamControllerPullInto(controller, view, min, readIntoRequest) {
|
---|
| 1333 | var stream = controller._controlledReadableByteStream;
|
---|
| 1334 | var ctor = view.constructor;
|
---|
| 1335 | var elementSize = arrayBufferViewElementSize(ctor);
|
---|
| 1336 | var byteOffset = view.byteOffset, byteLength = view.byteLength;
|
---|
| 1337 | var minimumFill = min * elementSize;
|
---|
| 1338 | var buffer;
|
---|
| 1339 | try {
|
---|
| 1340 | buffer = TransferArrayBuffer(view.buffer);
|
---|
| 1341 | }
|
---|
| 1342 | catch (e) {
|
---|
| 1343 | readIntoRequest._errorSteps(e);
|
---|
| 1344 | return;
|
---|
| 1345 | }
|
---|
| 1346 | var pullIntoDescriptor = {
|
---|
| 1347 | buffer: buffer,
|
---|
| 1348 | bufferByteLength: buffer.byteLength,
|
---|
| 1349 | byteOffset: byteOffset,
|
---|
| 1350 | byteLength: byteLength,
|
---|
| 1351 | bytesFilled: 0,
|
---|
| 1352 | minimumFill: minimumFill,
|
---|
| 1353 | elementSize: elementSize,
|
---|
| 1354 | viewConstructor: ctor,
|
---|
| 1355 | readerType: 'byob'
|
---|
| 1356 | };
|
---|
| 1357 | if (controller._pendingPullIntos.length > 0) {
|
---|
| 1358 | controller._pendingPullIntos.push(pullIntoDescriptor);
|
---|
| 1359 | // No ReadableByteStreamControllerCallPullIfNeeded() call since:
|
---|
| 1360 | // - No change happens on desiredSize
|
---|
| 1361 | // - The source has already been notified of that there's at least 1 pending read(view)
|
---|
| 1362 | ReadableStreamAddReadIntoRequest(stream, readIntoRequest);
|
---|
| 1363 | return;
|
---|
| 1364 | }
|
---|
| 1365 | if (stream._state === 'closed') {
|
---|
| 1366 | var emptyView = new ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, 0);
|
---|
| 1367 | readIntoRequest._closeSteps(emptyView);
|
---|
| 1368 | return;
|
---|
| 1369 | }
|
---|
| 1370 | if (controller._queueTotalSize > 0) {
|
---|
| 1371 | if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor)) {
|
---|
| 1372 | var filledView = ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor);
|
---|
| 1373 | ReadableByteStreamControllerHandleQueueDrain(controller);
|
---|
| 1374 | readIntoRequest._chunkSteps(filledView);
|
---|
| 1375 | return;
|
---|
| 1376 | }
|
---|
| 1377 | if (controller._closeRequested) {
|
---|
| 1378 | var e = new TypeError('Insufficient bytes to fill elements in the given buffer');
|
---|
| 1379 | ReadableByteStreamControllerError(controller, e);
|
---|
| 1380 | readIntoRequest._errorSteps(e);
|
---|
| 1381 | return;
|
---|
| 1382 | }
|
---|
| 1383 | }
|
---|
| 1384 | controller._pendingPullIntos.push(pullIntoDescriptor);
|
---|
| 1385 | ReadableStreamAddReadIntoRequest(stream, readIntoRequest);
|
---|
| 1386 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1387 | }
|
---|
| 1388 | function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
|
---|
| 1389 | if (firstDescriptor.readerType === 'none') {
|
---|
| 1390 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1391 | }
|
---|
| 1392 | var stream = controller._controlledReadableByteStream;
|
---|
| 1393 | if (ReadableStreamHasBYOBReader(stream)) {
|
---|
| 1394 | while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
|
---|
| 1395 | var pullIntoDescriptor = ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1396 | ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor);
|
---|
| 1397 | }
|
---|
| 1398 | }
|
---|
| 1399 | }
|
---|
| 1400 | function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
|
---|
| 1401 | ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
|
---|
| 1402 | if (pullIntoDescriptor.readerType === 'none') {
|
---|
| 1403 | ReadableByteStreamControllerEnqueueDetachedPullIntoToQueue(controller, pullIntoDescriptor);
|
---|
| 1404 | ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
|
---|
| 1405 | return;
|
---|
| 1406 | }
|
---|
| 1407 | if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.minimumFill) {
|
---|
| 1408 | // A descriptor for a read() request that is not yet filled up to its minimum length will stay at the head
|
---|
| 1409 | // of the queue, so the underlying source can keep filling it.
|
---|
| 1410 | return;
|
---|
| 1411 | }
|
---|
| 1412 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1413 | var remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
|
---|
| 1414 | if (remainderSize > 0) {
|
---|
| 1415 | var end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
|
---|
| 1416 | ReadableByteStreamControllerEnqueueClonedChunkToQueue(controller, pullIntoDescriptor.buffer, end - remainderSize, remainderSize);
|
---|
| 1417 | }
|
---|
| 1418 | pullIntoDescriptor.bytesFilled -= remainderSize;
|
---|
| 1419 | ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
|
---|
| 1420 | ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
|
---|
| 1421 | }
|
---|
| 1422 | function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
|
---|
| 1423 | var firstDescriptor = controller._pendingPullIntos.peek();
|
---|
| 1424 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
---|
| 1425 | var state = controller._controlledReadableByteStream._state;
|
---|
| 1426 | if (state === 'closed') {
|
---|
| 1427 | ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
|
---|
| 1428 | }
|
---|
| 1429 | else {
|
---|
| 1430 | ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
|
---|
| 1431 | }
|
---|
| 1432 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1433 | }
|
---|
| 1434 | function ReadableByteStreamControllerShiftPendingPullInto(controller) {
|
---|
| 1435 | var descriptor = controller._pendingPullIntos.shift();
|
---|
| 1436 | return descriptor;
|
---|
| 1437 | }
|
---|
| 1438 | function ReadableByteStreamControllerShouldCallPull(controller) {
|
---|
| 1439 | var stream = controller._controlledReadableByteStream;
|
---|
| 1440 | if (stream._state !== 'readable') {
|
---|
| 1441 | return false;
|
---|
| 1442 | }
|
---|
| 1443 | if (controller._closeRequested) {
|
---|
| 1444 | return false;
|
---|
| 1445 | }
|
---|
| 1446 | if (!controller._started) {
|
---|
| 1447 | return false;
|
---|
| 1448 | }
|
---|
| 1449 | if (ReadableStreamHasDefaultReader(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {
|
---|
| 1450 | return true;
|
---|
| 1451 | }
|
---|
| 1452 | if (ReadableStreamHasBYOBReader(stream) && ReadableStreamGetNumReadIntoRequests(stream) > 0) {
|
---|
| 1453 | return true;
|
---|
| 1454 | }
|
---|
| 1455 | var desiredSize = ReadableByteStreamControllerGetDesiredSize(controller);
|
---|
| 1456 | if (desiredSize > 0) {
|
---|
| 1457 | return true;
|
---|
| 1458 | }
|
---|
| 1459 | return false;
|
---|
| 1460 | }
|
---|
| 1461 | function ReadableByteStreamControllerClearAlgorithms(controller) {
|
---|
| 1462 | controller._pullAlgorithm = undefined;
|
---|
| 1463 | controller._cancelAlgorithm = undefined;
|
---|
| 1464 | }
|
---|
| 1465 | // A client of ReadableByteStreamController may use these functions directly to bypass state check.
|
---|
| 1466 | function ReadableByteStreamControllerClose(controller) {
|
---|
| 1467 | var stream = controller._controlledReadableByteStream;
|
---|
| 1468 | if (controller._closeRequested || stream._state !== 'readable') {
|
---|
| 1469 | return;
|
---|
| 1470 | }
|
---|
| 1471 | if (controller._queueTotalSize > 0) {
|
---|
| 1472 | controller._closeRequested = true;
|
---|
| 1473 | return;
|
---|
| 1474 | }
|
---|
| 1475 | if (controller._pendingPullIntos.length > 0) {
|
---|
| 1476 | var firstPendingPullInto = controller._pendingPullIntos.peek();
|
---|
| 1477 | if (firstPendingPullInto.bytesFilled % firstPendingPullInto.elementSize !== 0) {
|
---|
| 1478 | var e = new TypeError('Insufficient bytes to fill elements in the given buffer');
|
---|
| 1479 | ReadableByteStreamControllerError(controller, e);
|
---|
| 1480 | throw e;
|
---|
| 1481 | }
|
---|
| 1482 | }
|
---|
| 1483 | ReadableByteStreamControllerClearAlgorithms(controller);
|
---|
| 1484 | ReadableStreamClose(stream);
|
---|
| 1485 | }
|
---|
| 1486 | function ReadableByteStreamControllerEnqueue(controller, chunk) {
|
---|
| 1487 | var stream = controller._controlledReadableByteStream;
|
---|
| 1488 | if (controller._closeRequested || stream._state !== 'readable') {
|
---|
| 1489 | return;
|
---|
| 1490 | }
|
---|
| 1491 | var buffer = chunk.buffer, byteOffset = chunk.byteOffset, byteLength = chunk.byteLength;
|
---|
| 1492 | if (IsDetachedBuffer(buffer)) {
|
---|
| 1493 | throw new TypeError('chunk\'s buffer is detached and so cannot be enqueued');
|
---|
| 1494 | }
|
---|
| 1495 | var transferredBuffer = TransferArrayBuffer(buffer);
|
---|
| 1496 | if (controller._pendingPullIntos.length > 0) {
|
---|
| 1497 | var firstPendingPullInto = controller._pendingPullIntos.peek();
|
---|
| 1498 | if (IsDetachedBuffer(firstPendingPullInto.buffer)) {
|
---|
| 1499 | throw new TypeError('The BYOB request\'s buffer has been detached and so cannot be filled with an enqueued chunk');
|
---|
| 1500 | }
|
---|
| 1501 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
---|
| 1502 | firstPendingPullInto.buffer = TransferArrayBuffer(firstPendingPullInto.buffer);
|
---|
| 1503 | if (firstPendingPullInto.readerType === 'none') {
|
---|
| 1504 | ReadableByteStreamControllerEnqueueDetachedPullIntoToQueue(controller, firstPendingPullInto);
|
---|
| 1505 | }
|
---|
| 1506 | }
|
---|
| 1507 | if (ReadableStreamHasDefaultReader(stream)) {
|
---|
| 1508 | ReadableByteStreamControllerProcessReadRequestsUsingQueue(controller);
|
---|
| 1509 | if (ReadableStreamGetNumReadRequests(stream) === 0) {
|
---|
| 1510 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
---|
| 1511 | }
|
---|
| 1512 | else {
|
---|
| 1513 | if (controller._pendingPullIntos.length > 0) {
|
---|
| 1514 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
---|
| 1515 | }
|
---|
| 1516 | var transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
|
---|
| 1517 | ReadableStreamFulfillReadRequest(stream, transferredView, false);
|
---|
| 1518 | }
|
---|
| 1519 | }
|
---|
| 1520 | else if (ReadableStreamHasBYOBReader(stream)) {
|
---|
| 1521 | // TODO: Ideally in this branch detaching should happen only if the buffer is not consumed fully.
|
---|
| 1522 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
---|
| 1523 | ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
|
---|
| 1524 | }
|
---|
| 1525 | else {
|
---|
| 1526 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
---|
| 1527 | }
|
---|
| 1528 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1529 | }
|
---|
| 1530 | function ReadableByteStreamControllerError(controller, e) {
|
---|
| 1531 | var stream = controller._controlledReadableByteStream;
|
---|
| 1532 | if (stream._state !== 'readable') {
|
---|
| 1533 | return;
|
---|
| 1534 | }
|
---|
| 1535 | ReadableByteStreamControllerClearPendingPullIntos(controller);
|
---|
| 1536 | ResetQueue(controller);
|
---|
| 1537 | ReadableByteStreamControllerClearAlgorithms(controller);
|
---|
| 1538 | ReadableStreamError(stream, e);
|
---|
| 1539 | }
|
---|
| 1540 | function ReadableByteStreamControllerFillReadRequestFromQueue(controller, readRequest) {
|
---|
| 1541 | var entry = controller._queue.shift();
|
---|
| 1542 | controller._queueTotalSize -= entry.byteLength;
|
---|
| 1543 | ReadableByteStreamControllerHandleQueueDrain(controller);
|
---|
| 1544 | var view = new Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);
|
---|
| 1545 | readRequest._chunkSteps(view);
|
---|
| 1546 | }
|
---|
| 1547 | function ReadableByteStreamControllerGetBYOBRequest(controller) {
|
---|
| 1548 | if (controller._byobRequest === null && controller._pendingPullIntos.length > 0) {
|
---|
| 1549 | var firstDescriptor = controller._pendingPullIntos.peek();
|
---|
| 1550 | var view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
|
---|
| 1551 | var byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
|
---|
| 1552 | SetUpReadableStreamBYOBRequest(byobRequest, controller, view);
|
---|
| 1553 | controller._byobRequest = byobRequest;
|
---|
| 1554 | }
|
---|
| 1555 | return controller._byobRequest;
|
---|
| 1556 | }
|
---|
| 1557 | function ReadableByteStreamControllerGetDesiredSize(controller) {
|
---|
| 1558 | var state = controller._controlledReadableByteStream._state;
|
---|
| 1559 | if (state === 'errored') {
|
---|
| 1560 | return null;
|
---|
| 1561 | }
|
---|
| 1562 | if (state === 'closed') {
|
---|
| 1563 | return 0;
|
---|
| 1564 | }
|
---|
| 1565 | return controller._strategyHWM - controller._queueTotalSize;
|
---|
| 1566 | }
|
---|
| 1567 | function ReadableByteStreamControllerRespond(controller, bytesWritten) {
|
---|
| 1568 | var firstDescriptor = controller._pendingPullIntos.peek();
|
---|
| 1569 | var state = controller._controlledReadableByteStream._state;
|
---|
| 1570 | if (state === 'closed') {
|
---|
| 1571 | if (bytesWritten !== 0) {
|
---|
| 1572 | throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
|
---|
| 1573 | }
|
---|
| 1574 | }
|
---|
| 1575 | else {
|
---|
| 1576 | if (bytesWritten === 0) {
|
---|
| 1577 | throw new TypeError('bytesWritten must be greater than 0 when calling respond() on a readable stream');
|
---|
| 1578 | }
|
---|
| 1579 | if (firstDescriptor.bytesFilled + bytesWritten > firstDescriptor.byteLength) {
|
---|
| 1580 | throw new RangeError('bytesWritten out of range');
|
---|
| 1581 | }
|
---|
| 1582 | }
|
---|
| 1583 | firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
|
---|
| 1584 | ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
|
---|
| 1585 | }
|
---|
| 1586 | function ReadableByteStreamControllerRespondWithNewView(controller, view) {
|
---|
| 1587 | var firstDescriptor = controller._pendingPullIntos.peek();
|
---|
| 1588 | var state = controller._controlledReadableByteStream._state;
|
---|
| 1589 | if (state === 'closed') {
|
---|
| 1590 | if (view.byteLength !== 0) {
|
---|
| 1591 | throw new TypeError('The view\'s length must be 0 when calling respondWithNewView() on a closed stream');
|
---|
| 1592 | }
|
---|
| 1593 | }
|
---|
| 1594 | else {
|
---|
| 1595 | if (view.byteLength === 0) {
|
---|
| 1596 | throw new TypeError('The view\'s length must be greater than 0 when calling respondWithNewView() on a readable stream');
|
---|
| 1597 | }
|
---|
| 1598 | }
|
---|
| 1599 | if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
|
---|
| 1600 | throw new RangeError('The region specified by view does not match byobRequest');
|
---|
| 1601 | }
|
---|
| 1602 | if (firstDescriptor.bufferByteLength !== view.buffer.byteLength) {
|
---|
| 1603 | throw new RangeError('The buffer of view has different capacity than byobRequest');
|
---|
| 1604 | }
|
---|
| 1605 | if (firstDescriptor.bytesFilled + view.byteLength > firstDescriptor.byteLength) {
|
---|
| 1606 | throw new RangeError('The region specified by view is larger than byobRequest');
|
---|
| 1607 | }
|
---|
| 1608 | var viewByteLength = view.byteLength;
|
---|
| 1609 | firstDescriptor.buffer = TransferArrayBuffer(view.buffer);
|
---|
| 1610 | ReadableByteStreamControllerRespondInternal(controller, viewByteLength);
|
---|
| 1611 | }
|
---|
| 1612 | function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
|
---|
| 1613 | controller._controlledReadableByteStream = stream;
|
---|
| 1614 | controller._pullAgain = false;
|
---|
| 1615 | controller._pulling = false;
|
---|
| 1616 | controller._byobRequest = null;
|
---|
| 1617 | // Need to set the slots so that the assert doesn't fire. In the spec the slots already exist implicitly.
|
---|
| 1618 | controller._queue = controller._queueTotalSize = undefined;
|
---|
| 1619 | ResetQueue(controller);
|
---|
| 1620 | controller._closeRequested = false;
|
---|
| 1621 | controller._started = false;
|
---|
| 1622 | controller._strategyHWM = highWaterMark;
|
---|
| 1623 | controller._pullAlgorithm = pullAlgorithm;
|
---|
| 1624 | controller._cancelAlgorithm = cancelAlgorithm;
|
---|
| 1625 | controller._autoAllocateChunkSize = autoAllocateChunkSize;
|
---|
| 1626 | controller._pendingPullIntos = new SimpleQueue();
|
---|
| 1627 | stream._readableStreamController = controller;
|
---|
| 1628 | var startResult = startAlgorithm();
|
---|
| 1629 | uponPromise(promiseResolvedWith(startResult), function () {
|
---|
| 1630 | controller._started = true;
|
---|
| 1631 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
---|
| 1632 | return null;
|
---|
| 1633 | }, function (r) {
|
---|
| 1634 | ReadableByteStreamControllerError(controller, r);
|
---|
| 1635 | return null;
|
---|
| 1636 | });
|
---|
| 1637 | }
|
---|
| 1638 | function SetUpReadableByteStreamControllerFromUnderlyingSource(stream, underlyingByteSource, highWaterMark) {
|
---|
| 1639 | var controller = Object.create(ReadableByteStreamController.prototype);
|
---|
| 1640 | var startAlgorithm;
|
---|
| 1641 | var pullAlgorithm;
|
---|
| 1642 | var cancelAlgorithm;
|
---|
| 1643 | if (underlyingByteSource.start !== undefined) {
|
---|
| 1644 | startAlgorithm = function () { return underlyingByteSource.start(controller); };
|
---|
| 1645 | }
|
---|
| 1646 | else {
|
---|
| 1647 | startAlgorithm = function () { return undefined; };
|
---|
| 1648 | }
|
---|
| 1649 | if (underlyingByteSource.pull !== undefined) {
|
---|
| 1650 | pullAlgorithm = function () { return underlyingByteSource.pull(controller); };
|
---|
| 1651 | }
|
---|
| 1652 | else {
|
---|
| 1653 | pullAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 1654 | }
|
---|
| 1655 | if (underlyingByteSource.cancel !== undefined) {
|
---|
| 1656 | cancelAlgorithm = function (reason) { return underlyingByteSource.cancel(reason); };
|
---|
| 1657 | }
|
---|
| 1658 | else {
|
---|
| 1659 | cancelAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 1660 | }
|
---|
| 1661 | var autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
|
---|
| 1662 | if (autoAllocateChunkSize === 0) {
|
---|
| 1663 | throw new TypeError('autoAllocateChunkSize must be greater than 0');
|
---|
| 1664 | }
|
---|
| 1665 | SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
|
---|
| 1666 | }
|
---|
| 1667 | function SetUpReadableStreamBYOBRequest(request, controller, view) {
|
---|
| 1668 | request._associatedReadableByteStreamController = controller;
|
---|
| 1669 | request._view = view;
|
---|
| 1670 | }
|
---|
| 1671 | // Helper functions for the ReadableStreamBYOBRequest.
|
---|
| 1672 | function byobRequestBrandCheckException(name) {
|
---|
| 1673 | return new TypeError("ReadableStreamBYOBRequest.prototype.".concat(name, " can only be used on a ReadableStreamBYOBRequest"));
|
---|
| 1674 | }
|
---|
| 1675 | // Helper functions for the ReadableByteStreamController.
|
---|
| 1676 | function byteStreamControllerBrandCheckException(name) {
|
---|
| 1677 | return new TypeError("ReadableByteStreamController.prototype.".concat(name, " can only be used on a ReadableByteStreamController"));
|
---|
| 1678 | }
|
---|
| 1679 |
|
---|
| 1680 | function convertReaderOptions(options, context) {
|
---|
| 1681 | assertDictionary(options, context);
|
---|
| 1682 | var mode = options === null || options === void 0 ? void 0 : options.mode;
|
---|
| 1683 | return {
|
---|
| 1684 | mode: mode === undefined ? undefined : convertReadableStreamReaderMode(mode, "".concat(context, " has member 'mode' that"))
|
---|
| 1685 | };
|
---|
| 1686 | }
|
---|
| 1687 | function convertReadableStreamReaderMode(mode, context) {
|
---|
| 1688 | mode = "".concat(mode);
|
---|
| 1689 | if (mode !== 'byob') {
|
---|
| 1690 | throw new TypeError("".concat(context, " '").concat(mode, "' is not a valid enumeration value for ReadableStreamReaderMode"));
|
---|
| 1691 | }
|
---|
| 1692 | return mode;
|
---|
| 1693 | }
|
---|
| 1694 | function convertByobReadOptions(options, context) {
|
---|
| 1695 | var _a;
|
---|
| 1696 | assertDictionary(options, context);
|
---|
| 1697 | var min = (_a = options === null || options === void 0 ? void 0 : options.min) !== null && _a !== void 0 ? _a : 1;
|
---|
| 1698 | return {
|
---|
| 1699 | min: convertUnsignedLongLongWithEnforceRange(min, "".concat(context, " has member 'min' that"))
|
---|
| 1700 | };
|
---|
| 1701 | }
|
---|
| 1702 |
|
---|
| 1703 | // Abstract operations for the ReadableStream.
|
---|
| 1704 | function AcquireReadableStreamBYOBReader(stream) {
|
---|
| 1705 | return new ReadableStreamBYOBReader(stream);
|
---|
| 1706 | }
|
---|
| 1707 | // ReadableStream API exposed for controllers.
|
---|
| 1708 | function ReadableStreamAddReadIntoRequest(stream, readIntoRequest) {
|
---|
| 1709 | stream._reader._readIntoRequests.push(readIntoRequest);
|
---|
| 1710 | }
|
---|
| 1711 | function ReadableStreamFulfillReadIntoRequest(stream, chunk, done) {
|
---|
| 1712 | var reader = stream._reader;
|
---|
| 1713 | var readIntoRequest = reader._readIntoRequests.shift();
|
---|
| 1714 | if (done) {
|
---|
| 1715 | readIntoRequest._closeSteps(chunk);
|
---|
| 1716 | }
|
---|
| 1717 | else {
|
---|
| 1718 | readIntoRequest._chunkSteps(chunk);
|
---|
| 1719 | }
|
---|
| 1720 | }
|
---|
| 1721 | function ReadableStreamGetNumReadIntoRequests(stream) {
|
---|
| 1722 | return stream._reader._readIntoRequests.length;
|
---|
| 1723 | }
|
---|
| 1724 | function ReadableStreamHasBYOBReader(stream) {
|
---|
| 1725 | var reader = stream._reader;
|
---|
| 1726 | if (reader === undefined) {
|
---|
| 1727 | return false;
|
---|
| 1728 | }
|
---|
| 1729 | if (!IsReadableStreamBYOBReader(reader)) {
|
---|
| 1730 | return false;
|
---|
| 1731 | }
|
---|
| 1732 | return true;
|
---|
| 1733 | }
|
---|
| 1734 | /**
|
---|
| 1735 | * A BYOB reader vended by a {@link ReadableStream}.
|
---|
| 1736 | *
|
---|
| 1737 | * @public
|
---|
| 1738 | */
|
---|
| 1739 | var ReadableStreamBYOBReader = /** @class */ (function () {
|
---|
| 1740 | function ReadableStreamBYOBReader(stream) {
|
---|
| 1741 | assertRequiredArgument(stream, 1, 'ReadableStreamBYOBReader');
|
---|
| 1742 | assertReadableStream(stream, 'First parameter');
|
---|
| 1743 | if (IsReadableStreamLocked(stream)) {
|
---|
| 1744 | throw new TypeError('This stream has already been locked for exclusive reading by another reader');
|
---|
| 1745 | }
|
---|
| 1746 | if (!IsReadableByteStreamController(stream._readableStreamController)) {
|
---|
| 1747 | throw new TypeError('Cannot construct a ReadableStreamBYOBReader for a stream not constructed with a byte ' +
|
---|
| 1748 | 'source');
|
---|
| 1749 | }
|
---|
| 1750 | ReadableStreamReaderGenericInitialize(this, stream);
|
---|
| 1751 | this._readIntoRequests = new SimpleQueue();
|
---|
| 1752 | }
|
---|
| 1753 | Object.defineProperty(ReadableStreamBYOBReader.prototype, "closed", {
|
---|
| 1754 | /**
|
---|
| 1755 | * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
|
---|
| 1756 | * the reader's lock is released before the stream finishes closing.
|
---|
| 1757 | */
|
---|
| 1758 | get: function () {
|
---|
| 1759 | if (!IsReadableStreamBYOBReader(this)) {
|
---|
| 1760 | return promiseRejectedWith(byobReaderBrandCheckException('closed'));
|
---|
| 1761 | }
|
---|
| 1762 | return this._closedPromise;
|
---|
| 1763 | },
|
---|
| 1764 | enumerable: false,
|
---|
| 1765 | configurable: true
|
---|
| 1766 | });
|
---|
| 1767 | /**
|
---|
| 1768 | * If the reader is active, behaves the same as {@link ReadableStream.cancel | stream.cancel(reason)}.
|
---|
| 1769 | */
|
---|
| 1770 | ReadableStreamBYOBReader.prototype.cancel = function (reason) {
|
---|
| 1771 | if (reason === void 0) { reason = undefined; }
|
---|
| 1772 | if (!IsReadableStreamBYOBReader(this)) {
|
---|
| 1773 | return promiseRejectedWith(byobReaderBrandCheckException('cancel'));
|
---|
| 1774 | }
|
---|
| 1775 | if (this._ownerReadableStream === undefined) {
|
---|
| 1776 | return promiseRejectedWith(readerLockException('cancel'));
|
---|
| 1777 | }
|
---|
| 1778 | return ReadableStreamReaderGenericCancel(this, reason);
|
---|
| 1779 | };
|
---|
| 1780 | ReadableStreamBYOBReader.prototype.read = function (view, rawOptions) {
|
---|
| 1781 | if (rawOptions === void 0) { rawOptions = {}; }
|
---|
| 1782 | if (!IsReadableStreamBYOBReader(this)) {
|
---|
| 1783 | return promiseRejectedWith(byobReaderBrandCheckException('read'));
|
---|
| 1784 | }
|
---|
| 1785 | if (!ArrayBuffer.isView(view)) {
|
---|
| 1786 | return promiseRejectedWith(new TypeError('view must be an array buffer view'));
|
---|
| 1787 | }
|
---|
| 1788 | if (view.byteLength === 0) {
|
---|
| 1789 | return promiseRejectedWith(new TypeError('view must have non-zero byteLength'));
|
---|
| 1790 | }
|
---|
| 1791 | if (view.buffer.byteLength === 0) {
|
---|
| 1792 | return promiseRejectedWith(new TypeError("view's buffer must have non-zero byteLength"));
|
---|
| 1793 | }
|
---|
| 1794 | if (IsDetachedBuffer(view.buffer)) {
|
---|
| 1795 | return promiseRejectedWith(new TypeError('view\'s buffer has been detached'));
|
---|
| 1796 | }
|
---|
| 1797 | var options;
|
---|
| 1798 | try {
|
---|
| 1799 | options = convertByobReadOptions(rawOptions, 'options');
|
---|
| 1800 | }
|
---|
| 1801 | catch (e) {
|
---|
| 1802 | return promiseRejectedWith(e);
|
---|
| 1803 | }
|
---|
| 1804 | var min = options.min;
|
---|
| 1805 | if (min === 0) {
|
---|
| 1806 | return promiseRejectedWith(new TypeError('options.min must be greater than 0'));
|
---|
| 1807 | }
|
---|
| 1808 | if (!isDataView(view)) {
|
---|
| 1809 | if (min > view.length) {
|
---|
| 1810 | return promiseRejectedWith(new RangeError('options.min must be less than or equal to view\'s length'));
|
---|
| 1811 | }
|
---|
| 1812 | }
|
---|
| 1813 | else if (min > view.byteLength) {
|
---|
| 1814 | return promiseRejectedWith(new RangeError('options.min must be less than or equal to view\'s byteLength'));
|
---|
| 1815 | }
|
---|
| 1816 | if (this._ownerReadableStream === undefined) {
|
---|
| 1817 | return promiseRejectedWith(readerLockException('read from'));
|
---|
| 1818 | }
|
---|
| 1819 | var resolvePromise;
|
---|
| 1820 | var rejectPromise;
|
---|
| 1821 | var promise = newPromise(function (resolve, reject) {
|
---|
| 1822 | resolvePromise = resolve;
|
---|
| 1823 | rejectPromise = reject;
|
---|
| 1824 | });
|
---|
| 1825 | var readIntoRequest = {
|
---|
| 1826 | _chunkSteps: function (chunk) { return resolvePromise({ value: chunk, done: false }); },
|
---|
| 1827 | _closeSteps: function (chunk) { return resolvePromise({ value: chunk, done: true }); },
|
---|
| 1828 | _errorSteps: function (e) { return rejectPromise(e); }
|
---|
| 1829 | };
|
---|
| 1830 | ReadableStreamBYOBReaderRead(this, view, min, readIntoRequest);
|
---|
| 1831 | return promise;
|
---|
| 1832 | };
|
---|
| 1833 | /**
|
---|
| 1834 | * Releases the reader's lock on the corresponding stream. After the lock is released, the reader is no longer active.
|
---|
| 1835 | * If the associated stream is errored when the lock is released, the reader will appear errored in the same way
|
---|
| 1836 | * from now on; otherwise, the reader will appear closed.
|
---|
| 1837 | *
|
---|
| 1838 | * A reader's lock cannot be released while it still has a pending read request, i.e., if a promise returned by
|
---|
| 1839 | * the reader's {@link ReadableStreamBYOBReader.read | read()} method has not yet been settled. Attempting to
|
---|
| 1840 | * do so will throw a `TypeError` and leave the reader locked to the stream.
|
---|
| 1841 | */
|
---|
| 1842 | ReadableStreamBYOBReader.prototype.releaseLock = function () {
|
---|
| 1843 | if (!IsReadableStreamBYOBReader(this)) {
|
---|
| 1844 | throw byobReaderBrandCheckException('releaseLock');
|
---|
| 1845 | }
|
---|
| 1846 | if (this._ownerReadableStream === undefined) {
|
---|
| 1847 | return;
|
---|
| 1848 | }
|
---|
| 1849 | ReadableStreamBYOBReaderRelease(this);
|
---|
| 1850 | };
|
---|
| 1851 | return ReadableStreamBYOBReader;
|
---|
| 1852 | }());
|
---|
| 1853 | Object.defineProperties(ReadableStreamBYOBReader.prototype, {
|
---|
| 1854 | cancel: { enumerable: true },
|
---|
| 1855 | read: { enumerable: true },
|
---|
| 1856 | releaseLock: { enumerable: true },
|
---|
| 1857 | closed: { enumerable: true }
|
---|
| 1858 | });
|
---|
| 1859 | setFunctionName(ReadableStreamBYOBReader.prototype.cancel, 'cancel');
|
---|
| 1860 | setFunctionName(ReadableStreamBYOBReader.prototype.read, 'read');
|
---|
| 1861 | setFunctionName(ReadableStreamBYOBReader.prototype.releaseLock, 'releaseLock');
|
---|
| 1862 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 1863 | Object.defineProperty(ReadableStreamBYOBReader.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 1864 | value: 'ReadableStreamBYOBReader',
|
---|
| 1865 | configurable: true
|
---|
| 1866 | });
|
---|
| 1867 | }
|
---|
| 1868 | // Abstract operations for the readers.
|
---|
| 1869 | function IsReadableStreamBYOBReader(x) {
|
---|
| 1870 | if (!typeIsObject(x)) {
|
---|
| 1871 | return false;
|
---|
| 1872 | }
|
---|
| 1873 | if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
|
---|
| 1874 | return false;
|
---|
| 1875 | }
|
---|
| 1876 | return x instanceof ReadableStreamBYOBReader;
|
---|
| 1877 | }
|
---|
| 1878 | function ReadableStreamBYOBReaderRead(reader, view, min, readIntoRequest) {
|
---|
| 1879 | var stream = reader._ownerReadableStream;
|
---|
| 1880 | stream._disturbed = true;
|
---|
| 1881 | if (stream._state === 'errored') {
|
---|
| 1882 | readIntoRequest._errorSteps(stream._storedError);
|
---|
| 1883 | }
|
---|
| 1884 | else {
|
---|
| 1885 | ReadableByteStreamControllerPullInto(stream._readableStreamController, view, min, readIntoRequest);
|
---|
| 1886 | }
|
---|
| 1887 | }
|
---|
| 1888 | function ReadableStreamBYOBReaderRelease(reader) {
|
---|
| 1889 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 1890 | var e = new TypeError('Reader was released');
|
---|
| 1891 | ReadableStreamBYOBReaderErrorReadIntoRequests(reader, e);
|
---|
| 1892 | }
|
---|
| 1893 | function ReadableStreamBYOBReaderErrorReadIntoRequests(reader, e) {
|
---|
| 1894 | var readIntoRequests = reader._readIntoRequests;
|
---|
| 1895 | reader._readIntoRequests = new SimpleQueue();
|
---|
| 1896 | readIntoRequests.forEach(function (readIntoRequest) {
|
---|
| 1897 | readIntoRequest._errorSteps(e);
|
---|
| 1898 | });
|
---|
| 1899 | }
|
---|
| 1900 | // Helper functions for the ReadableStreamBYOBReader.
|
---|
| 1901 | function byobReaderBrandCheckException(name) {
|
---|
| 1902 | return new TypeError("ReadableStreamBYOBReader.prototype.".concat(name, " can only be used on a ReadableStreamBYOBReader"));
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
| 1905 | function ExtractHighWaterMark(strategy, defaultHWM) {
|
---|
| 1906 | var highWaterMark = strategy.highWaterMark;
|
---|
| 1907 | if (highWaterMark === undefined) {
|
---|
| 1908 | return defaultHWM;
|
---|
| 1909 | }
|
---|
| 1910 | if (NumberIsNaN(highWaterMark) || highWaterMark < 0) {
|
---|
| 1911 | throw new RangeError('Invalid highWaterMark');
|
---|
| 1912 | }
|
---|
| 1913 | return highWaterMark;
|
---|
| 1914 | }
|
---|
| 1915 | function ExtractSizeAlgorithm(strategy) {
|
---|
| 1916 | var size = strategy.size;
|
---|
| 1917 | if (!size) {
|
---|
| 1918 | return function () { return 1; };
|
---|
| 1919 | }
|
---|
| 1920 | return size;
|
---|
| 1921 | }
|
---|
| 1922 |
|
---|
| 1923 | function convertQueuingStrategy(init, context) {
|
---|
| 1924 | assertDictionary(init, context);
|
---|
| 1925 | var highWaterMark = init === null || init === void 0 ? void 0 : init.highWaterMark;
|
---|
| 1926 | var size = init === null || init === void 0 ? void 0 : init.size;
|
---|
| 1927 | return {
|
---|
| 1928 | highWaterMark: highWaterMark === undefined ? undefined : convertUnrestrictedDouble(highWaterMark),
|
---|
| 1929 | size: size === undefined ? undefined : convertQueuingStrategySize(size, "".concat(context, " has member 'size' that"))
|
---|
| 1930 | };
|
---|
| 1931 | }
|
---|
| 1932 | function convertQueuingStrategySize(fn, context) {
|
---|
| 1933 | assertFunction(fn, context);
|
---|
| 1934 | return function (chunk) { return convertUnrestrictedDouble(fn(chunk)); };
|
---|
| 1935 | }
|
---|
| 1936 |
|
---|
| 1937 | function convertUnderlyingSink(original, context) {
|
---|
| 1938 | assertDictionary(original, context);
|
---|
| 1939 | var abort = original === null || original === void 0 ? void 0 : original.abort;
|
---|
| 1940 | var close = original === null || original === void 0 ? void 0 : original.close;
|
---|
| 1941 | var start = original === null || original === void 0 ? void 0 : original.start;
|
---|
| 1942 | var type = original === null || original === void 0 ? void 0 : original.type;
|
---|
| 1943 | var write = original === null || original === void 0 ? void 0 : original.write;
|
---|
| 1944 | return {
|
---|
| 1945 | abort: abort === undefined ?
|
---|
| 1946 | undefined :
|
---|
| 1947 | convertUnderlyingSinkAbortCallback(abort, original, "".concat(context, " has member 'abort' that")),
|
---|
| 1948 | close: close === undefined ?
|
---|
| 1949 | undefined :
|
---|
| 1950 | convertUnderlyingSinkCloseCallback(close, original, "".concat(context, " has member 'close' that")),
|
---|
| 1951 | start: start === undefined ?
|
---|
| 1952 | undefined :
|
---|
| 1953 | convertUnderlyingSinkStartCallback(start, original, "".concat(context, " has member 'start' that")),
|
---|
| 1954 | write: write === undefined ?
|
---|
| 1955 | undefined :
|
---|
| 1956 | convertUnderlyingSinkWriteCallback(write, original, "".concat(context, " has member 'write' that")),
|
---|
| 1957 | type: type
|
---|
| 1958 | };
|
---|
| 1959 | }
|
---|
| 1960 | function convertUnderlyingSinkAbortCallback(fn, original, context) {
|
---|
| 1961 | assertFunction(fn, context);
|
---|
| 1962 | return function (reason) { return promiseCall(fn, original, [reason]); };
|
---|
| 1963 | }
|
---|
| 1964 | function convertUnderlyingSinkCloseCallback(fn, original, context) {
|
---|
| 1965 | assertFunction(fn, context);
|
---|
| 1966 | return function () { return promiseCall(fn, original, []); };
|
---|
| 1967 | }
|
---|
| 1968 | function convertUnderlyingSinkStartCallback(fn, original, context) {
|
---|
| 1969 | assertFunction(fn, context);
|
---|
| 1970 | return function (controller) { return reflectCall(fn, original, [controller]); };
|
---|
| 1971 | }
|
---|
| 1972 | function convertUnderlyingSinkWriteCallback(fn, original, context) {
|
---|
| 1973 | assertFunction(fn, context);
|
---|
| 1974 | return function (chunk, controller) { return promiseCall(fn, original, [chunk, controller]); };
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
| 1977 | function assertWritableStream(x, context) {
|
---|
| 1978 | if (!IsWritableStream(x)) {
|
---|
| 1979 | throw new TypeError("".concat(context, " is not a WritableStream."));
|
---|
| 1980 | }
|
---|
| 1981 | }
|
---|
| 1982 |
|
---|
| 1983 | function isAbortSignal(value) {
|
---|
| 1984 | if (typeof value !== 'object' || value === null) {
|
---|
| 1985 | return false;
|
---|
| 1986 | }
|
---|
| 1987 | try {
|
---|
| 1988 | return typeof value.aborted === 'boolean';
|
---|
| 1989 | }
|
---|
| 1990 | catch (_a) {
|
---|
| 1991 | // AbortSignal.prototype.aborted throws if its brand check fails
|
---|
| 1992 | return false;
|
---|
| 1993 | }
|
---|
| 1994 | }
|
---|
| 1995 | var supportsAbortController = typeof AbortController === 'function';
|
---|
| 1996 | /**
|
---|
| 1997 | * Construct a new AbortController, if supported by the platform.
|
---|
| 1998 | *
|
---|
| 1999 | * @internal
|
---|
| 2000 | */
|
---|
| 2001 | function createAbortController() {
|
---|
| 2002 | if (supportsAbortController) {
|
---|
| 2003 | return new AbortController();
|
---|
| 2004 | }
|
---|
| 2005 | return undefined;
|
---|
| 2006 | }
|
---|
| 2007 |
|
---|
| 2008 | /**
|
---|
| 2009 | * A writable stream represents a destination for data, into which you can write.
|
---|
| 2010 | *
|
---|
| 2011 | * @public
|
---|
| 2012 | */
|
---|
| 2013 | var WritableStream = /** @class */ (function () {
|
---|
| 2014 | function WritableStream(rawUnderlyingSink, rawStrategy) {
|
---|
| 2015 | if (rawUnderlyingSink === void 0) { rawUnderlyingSink = {}; }
|
---|
| 2016 | if (rawStrategy === void 0) { rawStrategy = {}; }
|
---|
| 2017 | if (rawUnderlyingSink === undefined) {
|
---|
| 2018 | rawUnderlyingSink = null;
|
---|
| 2019 | }
|
---|
| 2020 | else {
|
---|
| 2021 | assertObject(rawUnderlyingSink, 'First parameter');
|
---|
| 2022 | }
|
---|
| 2023 | var strategy = convertQueuingStrategy(rawStrategy, 'Second parameter');
|
---|
| 2024 | var underlyingSink = convertUnderlyingSink(rawUnderlyingSink, 'First parameter');
|
---|
| 2025 | InitializeWritableStream(this);
|
---|
| 2026 | var type = underlyingSink.type;
|
---|
| 2027 | if (type !== undefined) {
|
---|
| 2028 | throw new RangeError('Invalid type is specified');
|
---|
| 2029 | }
|
---|
| 2030 | var sizeAlgorithm = ExtractSizeAlgorithm(strategy);
|
---|
| 2031 | var highWaterMark = ExtractHighWaterMark(strategy, 1);
|
---|
| 2032 | SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, highWaterMark, sizeAlgorithm);
|
---|
| 2033 | }
|
---|
| 2034 | Object.defineProperty(WritableStream.prototype, "locked", {
|
---|
| 2035 | /**
|
---|
| 2036 | * Returns whether or not the writable stream is locked to a writer.
|
---|
| 2037 | */
|
---|
| 2038 | get: function () {
|
---|
| 2039 | if (!IsWritableStream(this)) {
|
---|
| 2040 | throw streamBrandCheckException$2('locked');
|
---|
| 2041 | }
|
---|
| 2042 | return IsWritableStreamLocked(this);
|
---|
| 2043 | },
|
---|
| 2044 | enumerable: false,
|
---|
| 2045 | configurable: true
|
---|
| 2046 | });
|
---|
| 2047 | /**
|
---|
| 2048 | * Aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be
|
---|
| 2049 | * immediately moved to an errored state, with any queued-up writes discarded. This will also execute any abort
|
---|
| 2050 | * mechanism of the underlying sink.
|
---|
| 2051 | *
|
---|
| 2052 | * The returned promise will fulfill if the stream shuts down successfully, or reject if the underlying sink signaled
|
---|
| 2053 | * that there was an error doing so. Additionally, it will reject with a `TypeError` (without attempting to cancel
|
---|
| 2054 | * the stream) if the stream is currently locked.
|
---|
| 2055 | */
|
---|
| 2056 | WritableStream.prototype.abort = function (reason) {
|
---|
| 2057 | if (reason === void 0) { reason = undefined; }
|
---|
| 2058 | if (!IsWritableStream(this)) {
|
---|
| 2059 | return promiseRejectedWith(streamBrandCheckException$2('abort'));
|
---|
| 2060 | }
|
---|
| 2061 | if (IsWritableStreamLocked(this)) {
|
---|
| 2062 | return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
|
---|
| 2063 | }
|
---|
| 2064 | return WritableStreamAbort(this, reason);
|
---|
| 2065 | };
|
---|
| 2066 | /**
|
---|
| 2067 | * Closes the stream. The underlying sink will finish processing any previously-written chunks, before invoking its
|
---|
| 2068 | * close behavior. During this time any further attempts to write will fail (without erroring the stream).
|
---|
| 2069 | *
|
---|
| 2070 | * The method returns a promise that will fulfill if all remaining chunks are successfully written and the stream
|
---|
| 2071 | * successfully closes, or rejects if an error is encountered during this process. Additionally, it will reject with
|
---|
| 2072 | * a `TypeError` (without attempting to cancel the stream) if the stream is currently locked.
|
---|
| 2073 | */
|
---|
| 2074 | WritableStream.prototype.close = function () {
|
---|
| 2075 | if (!IsWritableStream(this)) {
|
---|
| 2076 | return promiseRejectedWith(streamBrandCheckException$2('close'));
|
---|
| 2077 | }
|
---|
| 2078 | if (IsWritableStreamLocked(this)) {
|
---|
| 2079 | return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
|
---|
| 2080 | }
|
---|
| 2081 | if (WritableStreamCloseQueuedOrInFlight(this)) {
|
---|
| 2082 | return promiseRejectedWith(new TypeError('Cannot close an already-closing stream'));
|
---|
| 2083 | }
|
---|
| 2084 | return WritableStreamClose(this);
|
---|
| 2085 | };
|
---|
| 2086 | /**
|
---|
| 2087 | * Creates a {@link WritableStreamDefaultWriter | writer} and locks the stream to the new writer. While the stream
|
---|
| 2088 | * is locked, no other writer can be acquired until this one is released.
|
---|
| 2089 | *
|
---|
| 2090 | * This functionality is especially useful for creating abstractions that desire the ability to write to a stream
|
---|
| 2091 | * without interruption or interleaving. By getting a writer for the stream, you can ensure nobody else can write at
|
---|
| 2092 | * the same time, which would cause the resulting written data to be unpredictable and probably useless.
|
---|
| 2093 | */
|
---|
| 2094 | WritableStream.prototype.getWriter = function () {
|
---|
| 2095 | if (!IsWritableStream(this)) {
|
---|
| 2096 | throw streamBrandCheckException$2('getWriter');
|
---|
| 2097 | }
|
---|
| 2098 | return AcquireWritableStreamDefaultWriter(this);
|
---|
| 2099 | };
|
---|
| 2100 | return WritableStream;
|
---|
| 2101 | }());
|
---|
| 2102 | Object.defineProperties(WritableStream.prototype, {
|
---|
| 2103 | abort: { enumerable: true },
|
---|
| 2104 | close: { enumerable: true },
|
---|
| 2105 | getWriter: { enumerable: true },
|
---|
| 2106 | locked: { enumerable: true }
|
---|
| 2107 | });
|
---|
| 2108 | setFunctionName(WritableStream.prototype.abort, 'abort');
|
---|
| 2109 | setFunctionName(WritableStream.prototype.close, 'close');
|
---|
| 2110 | setFunctionName(WritableStream.prototype.getWriter, 'getWriter');
|
---|
| 2111 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 2112 | Object.defineProperty(WritableStream.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 2113 | value: 'WritableStream',
|
---|
| 2114 | configurable: true
|
---|
| 2115 | });
|
---|
| 2116 | }
|
---|
| 2117 | // Abstract operations for the WritableStream.
|
---|
| 2118 | function AcquireWritableStreamDefaultWriter(stream) {
|
---|
| 2119 | return new WritableStreamDefaultWriter(stream);
|
---|
| 2120 | }
|
---|
| 2121 | // Throws if and only if startAlgorithm throws.
|
---|
| 2122 | function CreateWritableStream(startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
|
---|
| 2123 | if (highWaterMark === void 0) { highWaterMark = 1; }
|
---|
| 2124 | if (sizeAlgorithm === void 0) { sizeAlgorithm = function () { return 1; }; }
|
---|
| 2125 | var stream = Object.create(WritableStream.prototype);
|
---|
| 2126 | InitializeWritableStream(stream);
|
---|
| 2127 | var controller = Object.create(WritableStreamDefaultController.prototype);
|
---|
| 2128 | SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm);
|
---|
| 2129 | return stream;
|
---|
| 2130 | }
|
---|
| 2131 | function InitializeWritableStream(stream) {
|
---|
| 2132 | stream._state = 'writable';
|
---|
| 2133 | // The error that will be reported by new method calls once the state becomes errored. Only set when [[state]] is
|
---|
| 2134 | // 'erroring' or 'errored'. May be set to an undefined value.
|
---|
| 2135 | stream._storedError = undefined;
|
---|
| 2136 | stream._writer = undefined;
|
---|
| 2137 | // Initialize to undefined first because the constructor of the controller checks this
|
---|
| 2138 | // variable to validate the caller.
|
---|
| 2139 | stream._writableStreamController = undefined;
|
---|
| 2140 | // This queue is placed here instead of the writer class in order to allow for passing a writer to the next data
|
---|
| 2141 | // producer without waiting for the queued writes to finish.
|
---|
| 2142 | stream._writeRequests = new SimpleQueue();
|
---|
| 2143 | // Write requests are removed from _writeRequests when write() is called on the underlying sink. This prevents
|
---|
| 2144 | // them from being erroneously rejected on error. If a write() call is in-flight, the request is stored here.
|
---|
| 2145 | stream._inFlightWriteRequest = undefined;
|
---|
| 2146 | // The promise that was returned from writer.close(). Stored here because it may be fulfilled after the writer
|
---|
| 2147 | // has been detached.
|
---|
| 2148 | stream._closeRequest = undefined;
|
---|
| 2149 | // Close request is removed from _closeRequest when close() is called on the underlying sink. This prevents it
|
---|
| 2150 | // from being erroneously rejected on error. If a close() call is in-flight, the request is stored here.
|
---|
| 2151 | stream._inFlightCloseRequest = undefined;
|
---|
| 2152 | // The promise that was returned from writer.abort(). This may also be fulfilled after the writer has detached.
|
---|
| 2153 | stream._pendingAbortRequest = undefined;
|
---|
| 2154 | // The backpressure signal set by the controller.
|
---|
| 2155 | stream._backpressure = false;
|
---|
| 2156 | }
|
---|
| 2157 | function IsWritableStream(x) {
|
---|
| 2158 | if (!typeIsObject(x)) {
|
---|
| 2159 | return false;
|
---|
| 2160 | }
|
---|
| 2161 | if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
|
---|
| 2162 | return false;
|
---|
| 2163 | }
|
---|
| 2164 | return x instanceof WritableStream;
|
---|
| 2165 | }
|
---|
| 2166 | function IsWritableStreamLocked(stream) {
|
---|
| 2167 | if (stream._writer === undefined) {
|
---|
| 2168 | return false;
|
---|
| 2169 | }
|
---|
| 2170 | return true;
|
---|
| 2171 | }
|
---|
| 2172 | function WritableStreamAbort(stream, reason) {
|
---|
| 2173 | var _a;
|
---|
| 2174 | if (stream._state === 'closed' || stream._state === 'errored') {
|
---|
| 2175 | return promiseResolvedWith(undefined);
|
---|
| 2176 | }
|
---|
| 2177 | stream._writableStreamController._abortReason = reason;
|
---|
| 2178 | (_a = stream._writableStreamController._abortController) === null || _a === void 0 ? void 0 : _a.abort(reason);
|
---|
| 2179 | // TypeScript narrows the type of `stream._state` down to 'writable' | 'erroring',
|
---|
| 2180 | // but it doesn't know that signaling abort runs author code that might have changed the state.
|
---|
| 2181 | // Widen the type again by casting to WritableStreamState.
|
---|
| 2182 | var state = stream._state;
|
---|
| 2183 | if (state === 'closed' || state === 'errored') {
|
---|
| 2184 | return promiseResolvedWith(undefined);
|
---|
| 2185 | }
|
---|
| 2186 | if (stream._pendingAbortRequest !== undefined) {
|
---|
| 2187 | return stream._pendingAbortRequest._promise;
|
---|
| 2188 | }
|
---|
| 2189 | var wasAlreadyErroring = false;
|
---|
| 2190 | if (state === 'erroring') {
|
---|
| 2191 | wasAlreadyErroring = true;
|
---|
| 2192 | // reason will not be used, so don't keep a reference to it.
|
---|
| 2193 | reason = undefined;
|
---|
| 2194 | }
|
---|
| 2195 | var promise = newPromise(function (resolve, reject) {
|
---|
| 2196 | stream._pendingAbortRequest = {
|
---|
| 2197 | _promise: undefined,
|
---|
| 2198 | _resolve: resolve,
|
---|
| 2199 | _reject: reject,
|
---|
| 2200 | _reason: reason,
|
---|
| 2201 | _wasAlreadyErroring: wasAlreadyErroring
|
---|
| 2202 | };
|
---|
| 2203 | });
|
---|
| 2204 | stream._pendingAbortRequest._promise = promise;
|
---|
| 2205 | if (!wasAlreadyErroring) {
|
---|
| 2206 | WritableStreamStartErroring(stream, reason);
|
---|
| 2207 | }
|
---|
| 2208 | return promise;
|
---|
| 2209 | }
|
---|
| 2210 | function WritableStreamClose(stream) {
|
---|
| 2211 | var state = stream._state;
|
---|
| 2212 | if (state === 'closed' || state === 'errored') {
|
---|
| 2213 | return promiseRejectedWith(new TypeError("The stream (in ".concat(state, " state) is not in the writable state and cannot be closed")));
|
---|
| 2214 | }
|
---|
| 2215 | var promise = newPromise(function (resolve, reject) {
|
---|
| 2216 | var closeRequest = {
|
---|
| 2217 | _resolve: resolve,
|
---|
| 2218 | _reject: reject
|
---|
| 2219 | };
|
---|
| 2220 | stream._closeRequest = closeRequest;
|
---|
| 2221 | });
|
---|
| 2222 | var writer = stream._writer;
|
---|
| 2223 | if (writer !== undefined && stream._backpressure && state === 'writable') {
|
---|
| 2224 | defaultWriterReadyPromiseResolve(writer);
|
---|
| 2225 | }
|
---|
| 2226 | WritableStreamDefaultControllerClose(stream._writableStreamController);
|
---|
| 2227 | return promise;
|
---|
| 2228 | }
|
---|
| 2229 | // WritableStream API exposed for controllers.
|
---|
| 2230 | function WritableStreamAddWriteRequest(stream) {
|
---|
| 2231 | var promise = newPromise(function (resolve, reject) {
|
---|
| 2232 | var writeRequest = {
|
---|
| 2233 | _resolve: resolve,
|
---|
| 2234 | _reject: reject
|
---|
| 2235 | };
|
---|
| 2236 | stream._writeRequests.push(writeRequest);
|
---|
| 2237 | });
|
---|
| 2238 | return promise;
|
---|
| 2239 | }
|
---|
| 2240 | function WritableStreamDealWithRejection(stream, error) {
|
---|
| 2241 | var state = stream._state;
|
---|
| 2242 | if (state === 'writable') {
|
---|
| 2243 | WritableStreamStartErroring(stream, error);
|
---|
| 2244 | return;
|
---|
| 2245 | }
|
---|
| 2246 | WritableStreamFinishErroring(stream);
|
---|
| 2247 | }
|
---|
| 2248 | function WritableStreamStartErroring(stream, reason) {
|
---|
| 2249 | var controller = stream._writableStreamController;
|
---|
| 2250 | stream._state = 'erroring';
|
---|
| 2251 | stream._storedError = reason;
|
---|
| 2252 | var writer = stream._writer;
|
---|
| 2253 | if (writer !== undefined) {
|
---|
| 2254 | WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason);
|
---|
| 2255 | }
|
---|
| 2256 | if (!WritableStreamHasOperationMarkedInFlight(stream) && controller._started) {
|
---|
| 2257 | WritableStreamFinishErroring(stream);
|
---|
| 2258 | }
|
---|
| 2259 | }
|
---|
| 2260 | function WritableStreamFinishErroring(stream) {
|
---|
| 2261 | stream._state = 'errored';
|
---|
| 2262 | stream._writableStreamController[ErrorSteps]();
|
---|
| 2263 | var storedError = stream._storedError;
|
---|
| 2264 | stream._writeRequests.forEach(function (writeRequest) {
|
---|
| 2265 | writeRequest._reject(storedError);
|
---|
| 2266 | });
|
---|
| 2267 | stream._writeRequests = new SimpleQueue();
|
---|
| 2268 | if (stream._pendingAbortRequest === undefined) {
|
---|
| 2269 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
---|
| 2270 | return;
|
---|
| 2271 | }
|
---|
| 2272 | var abortRequest = stream._pendingAbortRequest;
|
---|
| 2273 | stream._pendingAbortRequest = undefined;
|
---|
| 2274 | if (abortRequest._wasAlreadyErroring) {
|
---|
| 2275 | abortRequest._reject(storedError);
|
---|
| 2276 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
---|
| 2277 | return;
|
---|
| 2278 | }
|
---|
| 2279 | var promise = stream._writableStreamController[AbortSteps](abortRequest._reason);
|
---|
| 2280 | uponPromise(promise, function () {
|
---|
| 2281 | abortRequest._resolve();
|
---|
| 2282 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
---|
| 2283 | return null;
|
---|
| 2284 | }, function (reason) {
|
---|
| 2285 | abortRequest._reject(reason);
|
---|
| 2286 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
---|
| 2287 | return null;
|
---|
| 2288 | });
|
---|
| 2289 | }
|
---|
| 2290 | function WritableStreamFinishInFlightWrite(stream) {
|
---|
| 2291 | stream._inFlightWriteRequest._resolve(undefined);
|
---|
| 2292 | stream._inFlightWriteRequest = undefined;
|
---|
| 2293 | }
|
---|
| 2294 | function WritableStreamFinishInFlightWriteWithError(stream, error) {
|
---|
| 2295 | stream._inFlightWriteRequest._reject(error);
|
---|
| 2296 | stream._inFlightWriteRequest = undefined;
|
---|
| 2297 | WritableStreamDealWithRejection(stream, error);
|
---|
| 2298 | }
|
---|
| 2299 | function WritableStreamFinishInFlightClose(stream) {
|
---|
| 2300 | stream._inFlightCloseRequest._resolve(undefined);
|
---|
| 2301 | stream._inFlightCloseRequest = undefined;
|
---|
| 2302 | var state = stream._state;
|
---|
| 2303 | if (state === 'erroring') {
|
---|
| 2304 | // The error was too late to do anything, so it is ignored.
|
---|
| 2305 | stream._storedError = undefined;
|
---|
| 2306 | if (stream._pendingAbortRequest !== undefined) {
|
---|
| 2307 | stream._pendingAbortRequest._resolve();
|
---|
| 2308 | stream._pendingAbortRequest = undefined;
|
---|
| 2309 | }
|
---|
| 2310 | }
|
---|
| 2311 | stream._state = 'closed';
|
---|
| 2312 | var writer = stream._writer;
|
---|
| 2313 | if (writer !== undefined) {
|
---|
| 2314 | defaultWriterClosedPromiseResolve(writer);
|
---|
| 2315 | }
|
---|
| 2316 | }
|
---|
| 2317 | function WritableStreamFinishInFlightCloseWithError(stream, error) {
|
---|
| 2318 | stream._inFlightCloseRequest._reject(error);
|
---|
| 2319 | stream._inFlightCloseRequest = undefined;
|
---|
| 2320 | // Never execute sink abort() after sink close().
|
---|
| 2321 | if (stream._pendingAbortRequest !== undefined) {
|
---|
| 2322 | stream._pendingAbortRequest._reject(error);
|
---|
| 2323 | stream._pendingAbortRequest = undefined;
|
---|
| 2324 | }
|
---|
| 2325 | WritableStreamDealWithRejection(stream, error);
|
---|
| 2326 | }
|
---|
| 2327 | // TODO(ricea): Fix alphabetical order.
|
---|
| 2328 | function WritableStreamCloseQueuedOrInFlight(stream) {
|
---|
| 2329 | if (stream._closeRequest === undefined && stream._inFlightCloseRequest === undefined) {
|
---|
| 2330 | return false;
|
---|
| 2331 | }
|
---|
| 2332 | return true;
|
---|
| 2333 | }
|
---|
| 2334 | function WritableStreamHasOperationMarkedInFlight(stream) {
|
---|
| 2335 | if (stream._inFlightWriteRequest === undefined && stream._inFlightCloseRequest === undefined) {
|
---|
| 2336 | return false;
|
---|
| 2337 | }
|
---|
| 2338 | return true;
|
---|
| 2339 | }
|
---|
| 2340 | function WritableStreamMarkCloseRequestInFlight(stream) {
|
---|
| 2341 | stream._inFlightCloseRequest = stream._closeRequest;
|
---|
| 2342 | stream._closeRequest = undefined;
|
---|
| 2343 | }
|
---|
| 2344 | function WritableStreamMarkFirstWriteRequestInFlight(stream) {
|
---|
| 2345 | stream._inFlightWriteRequest = stream._writeRequests.shift();
|
---|
| 2346 | }
|
---|
| 2347 | function WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream) {
|
---|
| 2348 | if (stream._closeRequest !== undefined) {
|
---|
| 2349 | stream._closeRequest._reject(stream._storedError);
|
---|
| 2350 | stream._closeRequest = undefined;
|
---|
| 2351 | }
|
---|
| 2352 | var writer = stream._writer;
|
---|
| 2353 | if (writer !== undefined) {
|
---|
| 2354 | defaultWriterClosedPromiseReject(writer, stream._storedError);
|
---|
| 2355 | }
|
---|
| 2356 | }
|
---|
| 2357 | function WritableStreamUpdateBackpressure(stream, backpressure) {
|
---|
| 2358 | var writer = stream._writer;
|
---|
| 2359 | if (writer !== undefined && backpressure !== stream._backpressure) {
|
---|
| 2360 | if (backpressure) {
|
---|
| 2361 | defaultWriterReadyPromiseReset(writer);
|
---|
| 2362 | }
|
---|
| 2363 | else {
|
---|
| 2364 | defaultWriterReadyPromiseResolve(writer);
|
---|
| 2365 | }
|
---|
| 2366 | }
|
---|
| 2367 | stream._backpressure = backpressure;
|
---|
| 2368 | }
|
---|
| 2369 | /**
|
---|
| 2370 | * A default writer vended by a {@link WritableStream}.
|
---|
| 2371 | *
|
---|
| 2372 | * @public
|
---|
| 2373 | */
|
---|
| 2374 | var WritableStreamDefaultWriter = /** @class */ (function () {
|
---|
| 2375 | function WritableStreamDefaultWriter(stream) {
|
---|
| 2376 | assertRequiredArgument(stream, 1, 'WritableStreamDefaultWriter');
|
---|
| 2377 | assertWritableStream(stream, 'First parameter');
|
---|
| 2378 | if (IsWritableStreamLocked(stream)) {
|
---|
| 2379 | throw new TypeError('This stream has already been locked for exclusive writing by another writer');
|
---|
| 2380 | }
|
---|
| 2381 | this._ownerWritableStream = stream;
|
---|
| 2382 | stream._writer = this;
|
---|
| 2383 | var state = stream._state;
|
---|
| 2384 | if (state === 'writable') {
|
---|
| 2385 | if (!WritableStreamCloseQueuedOrInFlight(stream) && stream._backpressure) {
|
---|
| 2386 | defaultWriterReadyPromiseInitialize(this);
|
---|
| 2387 | }
|
---|
| 2388 | else {
|
---|
| 2389 | defaultWriterReadyPromiseInitializeAsResolved(this);
|
---|
| 2390 | }
|
---|
| 2391 | defaultWriterClosedPromiseInitialize(this);
|
---|
| 2392 | }
|
---|
| 2393 | else if (state === 'erroring') {
|
---|
| 2394 | defaultWriterReadyPromiseInitializeAsRejected(this, stream._storedError);
|
---|
| 2395 | defaultWriterClosedPromiseInitialize(this);
|
---|
| 2396 | }
|
---|
| 2397 | else if (state === 'closed') {
|
---|
| 2398 | defaultWriterReadyPromiseInitializeAsResolved(this);
|
---|
| 2399 | defaultWriterClosedPromiseInitializeAsResolved(this);
|
---|
| 2400 | }
|
---|
| 2401 | else {
|
---|
| 2402 | var storedError = stream._storedError;
|
---|
| 2403 | defaultWriterReadyPromiseInitializeAsRejected(this, storedError);
|
---|
| 2404 | defaultWriterClosedPromiseInitializeAsRejected(this, storedError);
|
---|
| 2405 | }
|
---|
| 2406 | }
|
---|
| 2407 | Object.defineProperty(WritableStreamDefaultWriter.prototype, "closed", {
|
---|
| 2408 | /**
|
---|
| 2409 | * Returns a promise that will be fulfilled when the stream becomes closed, or rejected if the stream ever errors or
|
---|
| 2410 | * the writer’s lock is released before the stream finishes closing.
|
---|
| 2411 | */
|
---|
| 2412 | get: function () {
|
---|
| 2413 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2414 | return promiseRejectedWith(defaultWriterBrandCheckException('closed'));
|
---|
| 2415 | }
|
---|
| 2416 | return this._closedPromise;
|
---|
| 2417 | },
|
---|
| 2418 | enumerable: false,
|
---|
| 2419 | configurable: true
|
---|
| 2420 | });
|
---|
| 2421 | Object.defineProperty(WritableStreamDefaultWriter.prototype, "desiredSize", {
|
---|
| 2422 | /**
|
---|
| 2423 | * Returns the desired size to fill the stream’s internal queue. It can be negative, if the queue is over-full.
|
---|
| 2424 | * A producer can use this information to determine the right amount of data to write.
|
---|
| 2425 | *
|
---|
| 2426 | * It will be `null` if the stream cannot be successfully written to (due to either being errored, or having an abort
|
---|
| 2427 | * queued up). It will return zero if the stream is closed. And the getter will throw an exception if invoked when
|
---|
| 2428 | * the writer’s lock is released.
|
---|
| 2429 | */
|
---|
| 2430 | get: function () {
|
---|
| 2431 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2432 | throw defaultWriterBrandCheckException('desiredSize');
|
---|
| 2433 | }
|
---|
| 2434 | if (this._ownerWritableStream === undefined) {
|
---|
| 2435 | throw defaultWriterLockException('desiredSize');
|
---|
| 2436 | }
|
---|
| 2437 | return WritableStreamDefaultWriterGetDesiredSize(this);
|
---|
| 2438 | },
|
---|
| 2439 | enumerable: false,
|
---|
| 2440 | configurable: true
|
---|
| 2441 | });
|
---|
| 2442 | Object.defineProperty(WritableStreamDefaultWriter.prototype, "ready", {
|
---|
| 2443 | /**
|
---|
| 2444 | * Returns a promise that will be fulfilled when the desired size to fill the stream’s internal queue transitions
|
---|
| 2445 | * from non-positive to positive, signaling that it is no longer applying backpressure. Once the desired size dips
|
---|
| 2446 | * back to zero or below, the getter will return a new promise that stays pending until the next transition.
|
---|
| 2447 | *
|
---|
| 2448 | * If the stream becomes errored or aborted, or the writer’s lock is released, the returned promise will become
|
---|
| 2449 | * rejected.
|
---|
| 2450 | */
|
---|
| 2451 | get: function () {
|
---|
| 2452 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2453 | return promiseRejectedWith(defaultWriterBrandCheckException('ready'));
|
---|
| 2454 | }
|
---|
| 2455 | return this._readyPromise;
|
---|
| 2456 | },
|
---|
| 2457 | enumerable: false,
|
---|
| 2458 | configurable: true
|
---|
| 2459 | });
|
---|
| 2460 | /**
|
---|
| 2461 | * If the reader is active, behaves the same as {@link WritableStream.abort | stream.abort(reason)}.
|
---|
| 2462 | */
|
---|
| 2463 | WritableStreamDefaultWriter.prototype.abort = function (reason) {
|
---|
| 2464 | if (reason === void 0) { reason = undefined; }
|
---|
| 2465 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2466 | return promiseRejectedWith(defaultWriterBrandCheckException('abort'));
|
---|
| 2467 | }
|
---|
| 2468 | if (this._ownerWritableStream === undefined) {
|
---|
| 2469 | return promiseRejectedWith(defaultWriterLockException('abort'));
|
---|
| 2470 | }
|
---|
| 2471 | return WritableStreamDefaultWriterAbort(this, reason);
|
---|
| 2472 | };
|
---|
| 2473 | /**
|
---|
| 2474 | * If the reader is active, behaves the same as {@link WritableStream.close | stream.close()}.
|
---|
| 2475 | */
|
---|
| 2476 | WritableStreamDefaultWriter.prototype.close = function () {
|
---|
| 2477 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2478 | return promiseRejectedWith(defaultWriterBrandCheckException('close'));
|
---|
| 2479 | }
|
---|
| 2480 | var stream = this._ownerWritableStream;
|
---|
| 2481 | if (stream === undefined) {
|
---|
| 2482 | return promiseRejectedWith(defaultWriterLockException('close'));
|
---|
| 2483 | }
|
---|
| 2484 | if (WritableStreamCloseQueuedOrInFlight(stream)) {
|
---|
| 2485 | return promiseRejectedWith(new TypeError('Cannot close an already-closing stream'));
|
---|
| 2486 | }
|
---|
| 2487 | return WritableStreamDefaultWriterClose(this);
|
---|
| 2488 | };
|
---|
| 2489 | /**
|
---|
| 2490 | * Releases the writer’s lock on the corresponding stream. After the lock is released, the writer is no longer active.
|
---|
| 2491 | * If the associated stream is errored when the lock is released, the writer will appear errored in the same way from
|
---|
| 2492 | * now on; otherwise, the writer will appear closed.
|
---|
| 2493 | *
|
---|
| 2494 | * Note that the lock can still be released even if some ongoing writes have not yet finished (i.e. even if the
|
---|
| 2495 | * promises returned from previous calls to {@link WritableStreamDefaultWriter.write | write()} have not yet settled).
|
---|
| 2496 | * It’s not necessary to hold the lock on the writer for the duration of the write; the lock instead simply prevents
|
---|
| 2497 | * other producers from writing in an interleaved manner.
|
---|
| 2498 | */
|
---|
| 2499 | WritableStreamDefaultWriter.prototype.releaseLock = function () {
|
---|
| 2500 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2501 | throw defaultWriterBrandCheckException('releaseLock');
|
---|
| 2502 | }
|
---|
| 2503 | var stream = this._ownerWritableStream;
|
---|
| 2504 | if (stream === undefined) {
|
---|
| 2505 | return;
|
---|
| 2506 | }
|
---|
| 2507 | WritableStreamDefaultWriterRelease(this);
|
---|
| 2508 | };
|
---|
| 2509 | WritableStreamDefaultWriter.prototype.write = function (chunk) {
|
---|
| 2510 | if (chunk === void 0) { chunk = undefined; }
|
---|
| 2511 | if (!IsWritableStreamDefaultWriter(this)) {
|
---|
| 2512 | return promiseRejectedWith(defaultWriterBrandCheckException('write'));
|
---|
| 2513 | }
|
---|
| 2514 | if (this._ownerWritableStream === undefined) {
|
---|
| 2515 | return promiseRejectedWith(defaultWriterLockException('write to'));
|
---|
| 2516 | }
|
---|
| 2517 | return WritableStreamDefaultWriterWrite(this, chunk);
|
---|
| 2518 | };
|
---|
| 2519 | return WritableStreamDefaultWriter;
|
---|
| 2520 | }());
|
---|
| 2521 | Object.defineProperties(WritableStreamDefaultWriter.prototype, {
|
---|
| 2522 | abort: { enumerable: true },
|
---|
| 2523 | close: { enumerable: true },
|
---|
| 2524 | releaseLock: { enumerable: true },
|
---|
| 2525 | write: { enumerable: true },
|
---|
| 2526 | closed: { enumerable: true },
|
---|
| 2527 | desiredSize: { enumerable: true },
|
---|
| 2528 | ready: { enumerable: true }
|
---|
| 2529 | });
|
---|
| 2530 | setFunctionName(WritableStreamDefaultWriter.prototype.abort, 'abort');
|
---|
| 2531 | setFunctionName(WritableStreamDefaultWriter.prototype.close, 'close');
|
---|
| 2532 | setFunctionName(WritableStreamDefaultWriter.prototype.releaseLock, 'releaseLock');
|
---|
| 2533 | setFunctionName(WritableStreamDefaultWriter.prototype.write, 'write');
|
---|
| 2534 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 2535 | Object.defineProperty(WritableStreamDefaultWriter.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 2536 | value: 'WritableStreamDefaultWriter',
|
---|
| 2537 | configurable: true
|
---|
| 2538 | });
|
---|
| 2539 | }
|
---|
| 2540 | // Abstract operations for the WritableStreamDefaultWriter.
|
---|
| 2541 | function IsWritableStreamDefaultWriter(x) {
|
---|
| 2542 | if (!typeIsObject(x)) {
|
---|
| 2543 | return false;
|
---|
| 2544 | }
|
---|
| 2545 | if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
|
---|
| 2546 | return false;
|
---|
| 2547 | }
|
---|
| 2548 | return x instanceof WritableStreamDefaultWriter;
|
---|
| 2549 | }
|
---|
| 2550 | // A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.
|
---|
| 2551 | function WritableStreamDefaultWriterAbort(writer, reason) {
|
---|
| 2552 | var stream = writer._ownerWritableStream;
|
---|
| 2553 | return WritableStreamAbort(stream, reason);
|
---|
| 2554 | }
|
---|
| 2555 | function WritableStreamDefaultWriterClose(writer) {
|
---|
| 2556 | var stream = writer._ownerWritableStream;
|
---|
| 2557 | return WritableStreamClose(stream);
|
---|
| 2558 | }
|
---|
| 2559 | function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {
|
---|
| 2560 | var stream = writer._ownerWritableStream;
|
---|
| 2561 | var state = stream._state;
|
---|
| 2562 | if (WritableStreamCloseQueuedOrInFlight(stream) || state === 'closed') {
|
---|
| 2563 | return promiseResolvedWith(undefined);
|
---|
| 2564 | }
|
---|
| 2565 | if (state === 'errored') {
|
---|
| 2566 | return promiseRejectedWith(stream._storedError);
|
---|
| 2567 | }
|
---|
| 2568 | return WritableStreamDefaultWriterClose(writer);
|
---|
| 2569 | }
|
---|
| 2570 | function WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) {
|
---|
| 2571 | if (writer._closedPromiseState === 'pending') {
|
---|
| 2572 | defaultWriterClosedPromiseReject(writer, error);
|
---|
| 2573 | }
|
---|
| 2574 | else {
|
---|
| 2575 | defaultWriterClosedPromiseResetToRejected(writer, error);
|
---|
| 2576 | }
|
---|
| 2577 | }
|
---|
| 2578 | function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) {
|
---|
| 2579 | if (writer._readyPromiseState === 'pending') {
|
---|
| 2580 | defaultWriterReadyPromiseReject(writer, error);
|
---|
| 2581 | }
|
---|
| 2582 | else {
|
---|
| 2583 | defaultWriterReadyPromiseResetToRejected(writer, error);
|
---|
| 2584 | }
|
---|
| 2585 | }
|
---|
| 2586 | function WritableStreamDefaultWriterGetDesiredSize(writer) {
|
---|
| 2587 | var stream = writer._ownerWritableStream;
|
---|
| 2588 | var state = stream._state;
|
---|
| 2589 | if (state === 'errored' || state === 'erroring') {
|
---|
| 2590 | return null;
|
---|
| 2591 | }
|
---|
| 2592 | if (state === 'closed') {
|
---|
| 2593 | return 0;
|
---|
| 2594 | }
|
---|
| 2595 | return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);
|
---|
| 2596 | }
|
---|
| 2597 | function WritableStreamDefaultWriterRelease(writer) {
|
---|
| 2598 | var stream = writer._ownerWritableStream;
|
---|
| 2599 | var releasedError = new TypeError("Writer was released and can no longer be used to monitor the stream's closedness");
|
---|
| 2600 | WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError);
|
---|
| 2601 | // The state transitions to "errored" before the sink abort() method runs, but the writer.closed promise is not
|
---|
| 2602 | // rejected until afterwards. This means that simply testing state will not work.
|
---|
| 2603 | WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError);
|
---|
| 2604 | stream._writer = undefined;
|
---|
| 2605 | writer._ownerWritableStream = undefined;
|
---|
| 2606 | }
|
---|
| 2607 | function WritableStreamDefaultWriterWrite(writer, chunk) {
|
---|
| 2608 | var stream = writer._ownerWritableStream;
|
---|
| 2609 | var controller = stream._writableStreamController;
|
---|
| 2610 | var chunkSize = WritableStreamDefaultControllerGetChunkSize(controller, chunk);
|
---|
| 2611 | if (stream !== writer._ownerWritableStream) {
|
---|
| 2612 | return promiseRejectedWith(defaultWriterLockException('write to'));
|
---|
| 2613 | }
|
---|
| 2614 | var state = stream._state;
|
---|
| 2615 | if (state === 'errored') {
|
---|
| 2616 | return promiseRejectedWith(stream._storedError);
|
---|
| 2617 | }
|
---|
| 2618 | if (WritableStreamCloseQueuedOrInFlight(stream) || state === 'closed') {
|
---|
| 2619 | return promiseRejectedWith(new TypeError('The stream is closing or closed and cannot be written to'));
|
---|
| 2620 | }
|
---|
| 2621 | if (state === 'erroring') {
|
---|
| 2622 | return promiseRejectedWith(stream._storedError);
|
---|
| 2623 | }
|
---|
| 2624 | var promise = WritableStreamAddWriteRequest(stream);
|
---|
| 2625 | WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);
|
---|
| 2626 | return promise;
|
---|
| 2627 | }
|
---|
| 2628 | var closeSentinel = {};
|
---|
| 2629 | /**
|
---|
| 2630 | * Allows control of a {@link WritableStream | writable stream}'s state and internal queue.
|
---|
| 2631 | *
|
---|
| 2632 | * @public
|
---|
| 2633 | */
|
---|
| 2634 | var WritableStreamDefaultController = /** @class */ (function () {
|
---|
| 2635 | function WritableStreamDefaultController() {
|
---|
| 2636 | throw new TypeError('Illegal constructor');
|
---|
| 2637 | }
|
---|
| 2638 | Object.defineProperty(WritableStreamDefaultController.prototype, "abortReason", {
|
---|
| 2639 | /**
|
---|
| 2640 | * The reason which was passed to `WritableStream.abort(reason)` when the stream was aborted.
|
---|
| 2641 | *
|
---|
| 2642 | * @deprecated
|
---|
| 2643 | * This property has been removed from the specification, see https://github.com/whatwg/streams/pull/1177.
|
---|
| 2644 | * Use {@link WritableStreamDefaultController.signal}'s `reason` instead.
|
---|
| 2645 | */
|
---|
| 2646 | get: function () {
|
---|
| 2647 | if (!IsWritableStreamDefaultController(this)) {
|
---|
| 2648 | throw defaultControllerBrandCheckException$2('abortReason');
|
---|
| 2649 | }
|
---|
| 2650 | return this._abortReason;
|
---|
| 2651 | },
|
---|
| 2652 | enumerable: false,
|
---|
| 2653 | configurable: true
|
---|
| 2654 | });
|
---|
| 2655 | Object.defineProperty(WritableStreamDefaultController.prototype, "signal", {
|
---|
| 2656 | /**
|
---|
| 2657 | * An `AbortSignal` that can be used to abort the pending write or close operation when the stream is aborted.
|
---|
| 2658 | */
|
---|
| 2659 | get: function () {
|
---|
| 2660 | if (!IsWritableStreamDefaultController(this)) {
|
---|
| 2661 | throw defaultControllerBrandCheckException$2('signal');
|
---|
| 2662 | }
|
---|
| 2663 | if (this._abortController === undefined) {
|
---|
| 2664 | // Older browsers or older Node versions may not support `AbortController` or `AbortSignal`.
|
---|
| 2665 | // We don't want to bundle and ship an `AbortController` polyfill together with our polyfill,
|
---|
| 2666 | // so instead we only implement support for `signal` if we find a global `AbortController` constructor.
|
---|
| 2667 | throw new TypeError('WritableStreamDefaultController.prototype.signal is not supported');
|
---|
| 2668 | }
|
---|
| 2669 | return this._abortController.signal;
|
---|
| 2670 | },
|
---|
| 2671 | enumerable: false,
|
---|
| 2672 | configurable: true
|
---|
| 2673 | });
|
---|
| 2674 | /**
|
---|
| 2675 | * Closes the controlled writable stream, making all future interactions with it fail with the given error `e`.
|
---|
| 2676 | *
|
---|
| 2677 | * This method is rarely used, since usually it suffices to return a rejected promise from one of the underlying
|
---|
| 2678 | * sink's methods. However, it can be useful for suddenly shutting down a stream in response to an event outside the
|
---|
| 2679 | * normal lifecycle of interactions with the underlying sink.
|
---|
| 2680 | */
|
---|
| 2681 | WritableStreamDefaultController.prototype.error = function (e) {
|
---|
| 2682 | if (e === void 0) { e = undefined; }
|
---|
| 2683 | if (!IsWritableStreamDefaultController(this)) {
|
---|
| 2684 | throw defaultControllerBrandCheckException$2('error');
|
---|
| 2685 | }
|
---|
| 2686 | var state = this._controlledWritableStream._state;
|
---|
| 2687 | if (state !== 'writable') {
|
---|
| 2688 | // The stream is closed, errored or will be soon. The sink can't do anything useful if it gets an error here, so
|
---|
| 2689 | // just treat it as a no-op.
|
---|
| 2690 | return;
|
---|
| 2691 | }
|
---|
| 2692 | WritableStreamDefaultControllerError(this, e);
|
---|
| 2693 | };
|
---|
| 2694 | /** @internal */
|
---|
| 2695 | WritableStreamDefaultController.prototype[AbortSteps] = function (reason) {
|
---|
| 2696 | var result = this._abortAlgorithm(reason);
|
---|
| 2697 | WritableStreamDefaultControllerClearAlgorithms(this);
|
---|
| 2698 | return result;
|
---|
| 2699 | };
|
---|
| 2700 | /** @internal */
|
---|
| 2701 | WritableStreamDefaultController.prototype[ErrorSteps] = function () {
|
---|
| 2702 | ResetQueue(this);
|
---|
| 2703 | };
|
---|
| 2704 | return WritableStreamDefaultController;
|
---|
| 2705 | }());
|
---|
| 2706 | Object.defineProperties(WritableStreamDefaultController.prototype, {
|
---|
| 2707 | abortReason: { enumerable: true },
|
---|
| 2708 | signal: { enumerable: true },
|
---|
| 2709 | error: { enumerable: true }
|
---|
| 2710 | });
|
---|
| 2711 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 2712 | Object.defineProperty(WritableStreamDefaultController.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 2713 | value: 'WritableStreamDefaultController',
|
---|
| 2714 | configurable: true
|
---|
| 2715 | });
|
---|
| 2716 | }
|
---|
| 2717 | // Abstract operations implementing interface required by the WritableStream.
|
---|
| 2718 | function IsWritableStreamDefaultController(x) {
|
---|
| 2719 | if (!typeIsObject(x)) {
|
---|
| 2720 | return false;
|
---|
| 2721 | }
|
---|
| 2722 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
|
---|
| 2723 | return false;
|
---|
| 2724 | }
|
---|
| 2725 | return x instanceof WritableStreamDefaultController;
|
---|
| 2726 | }
|
---|
| 2727 | function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
|
---|
| 2728 | controller._controlledWritableStream = stream;
|
---|
| 2729 | stream._writableStreamController = controller;
|
---|
| 2730 | // Need to set the slots so that the assert doesn't fire. In the spec the slots already exist implicitly.
|
---|
| 2731 | controller._queue = undefined;
|
---|
| 2732 | controller._queueTotalSize = undefined;
|
---|
| 2733 | ResetQueue(controller);
|
---|
| 2734 | controller._abortReason = undefined;
|
---|
| 2735 | controller._abortController = createAbortController();
|
---|
| 2736 | controller._started = false;
|
---|
| 2737 | controller._strategySizeAlgorithm = sizeAlgorithm;
|
---|
| 2738 | controller._strategyHWM = highWaterMark;
|
---|
| 2739 | controller._writeAlgorithm = writeAlgorithm;
|
---|
| 2740 | controller._closeAlgorithm = closeAlgorithm;
|
---|
| 2741 | controller._abortAlgorithm = abortAlgorithm;
|
---|
| 2742 | var backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
---|
| 2743 | WritableStreamUpdateBackpressure(stream, backpressure);
|
---|
| 2744 | var startResult = startAlgorithm();
|
---|
| 2745 | var startPromise = promiseResolvedWith(startResult);
|
---|
| 2746 | uponPromise(startPromise, function () {
|
---|
| 2747 | controller._started = true;
|
---|
| 2748 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
---|
| 2749 | return null;
|
---|
| 2750 | }, function (r) {
|
---|
| 2751 | controller._started = true;
|
---|
| 2752 | WritableStreamDealWithRejection(stream, r);
|
---|
| 2753 | return null;
|
---|
| 2754 | });
|
---|
| 2755 | }
|
---|
| 2756 | function SetUpWritableStreamDefaultControllerFromUnderlyingSink(stream, underlyingSink, highWaterMark, sizeAlgorithm) {
|
---|
| 2757 | var controller = Object.create(WritableStreamDefaultController.prototype);
|
---|
| 2758 | var startAlgorithm;
|
---|
| 2759 | var writeAlgorithm;
|
---|
| 2760 | var closeAlgorithm;
|
---|
| 2761 | var abortAlgorithm;
|
---|
| 2762 | if (underlyingSink.start !== undefined) {
|
---|
| 2763 | startAlgorithm = function () { return underlyingSink.start(controller); };
|
---|
| 2764 | }
|
---|
| 2765 | else {
|
---|
| 2766 | startAlgorithm = function () { return undefined; };
|
---|
| 2767 | }
|
---|
| 2768 | if (underlyingSink.write !== undefined) {
|
---|
| 2769 | writeAlgorithm = function (chunk) { return underlyingSink.write(chunk, controller); };
|
---|
| 2770 | }
|
---|
| 2771 | else {
|
---|
| 2772 | writeAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 2773 | }
|
---|
| 2774 | if (underlyingSink.close !== undefined) {
|
---|
| 2775 | closeAlgorithm = function () { return underlyingSink.close(); };
|
---|
| 2776 | }
|
---|
| 2777 | else {
|
---|
| 2778 | closeAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 2779 | }
|
---|
| 2780 | if (underlyingSink.abort !== undefined) {
|
---|
| 2781 | abortAlgorithm = function (reason) { return underlyingSink.abort(reason); };
|
---|
| 2782 | }
|
---|
| 2783 | else {
|
---|
| 2784 | abortAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 2785 | }
|
---|
| 2786 | SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm);
|
---|
| 2787 | }
|
---|
| 2788 | // ClearAlgorithms may be called twice. Erroring the same stream in multiple ways will often result in redundant calls.
|
---|
| 2789 | function WritableStreamDefaultControllerClearAlgorithms(controller) {
|
---|
| 2790 | controller._writeAlgorithm = undefined;
|
---|
| 2791 | controller._closeAlgorithm = undefined;
|
---|
| 2792 | controller._abortAlgorithm = undefined;
|
---|
| 2793 | controller._strategySizeAlgorithm = undefined;
|
---|
| 2794 | }
|
---|
| 2795 | function WritableStreamDefaultControllerClose(controller) {
|
---|
| 2796 | EnqueueValueWithSize(controller, closeSentinel, 0);
|
---|
| 2797 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
---|
| 2798 | }
|
---|
| 2799 | function WritableStreamDefaultControllerGetChunkSize(controller, chunk) {
|
---|
| 2800 | try {
|
---|
| 2801 | return controller._strategySizeAlgorithm(chunk);
|
---|
| 2802 | }
|
---|
| 2803 | catch (chunkSizeE) {
|
---|
| 2804 | WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);
|
---|
| 2805 | return 1;
|
---|
| 2806 | }
|
---|
| 2807 | }
|
---|
| 2808 | function WritableStreamDefaultControllerGetDesiredSize(controller) {
|
---|
| 2809 | return controller._strategyHWM - controller._queueTotalSize;
|
---|
| 2810 | }
|
---|
| 2811 | function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) {
|
---|
| 2812 | try {
|
---|
| 2813 | EnqueueValueWithSize(controller, chunk, chunkSize);
|
---|
| 2814 | }
|
---|
| 2815 | catch (enqueueE) {
|
---|
| 2816 | WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);
|
---|
| 2817 | return;
|
---|
| 2818 | }
|
---|
| 2819 | var stream = controller._controlledWritableStream;
|
---|
| 2820 | if (!WritableStreamCloseQueuedOrInFlight(stream) && stream._state === 'writable') {
|
---|
| 2821 | var backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
---|
| 2822 | WritableStreamUpdateBackpressure(stream, backpressure);
|
---|
| 2823 | }
|
---|
| 2824 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
---|
| 2825 | }
|
---|
| 2826 | // Abstract operations for the WritableStreamDefaultController.
|
---|
| 2827 | function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {
|
---|
| 2828 | var stream = controller._controlledWritableStream;
|
---|
| 2829 | if (!controller._started) {
|
---|
| 2830 | return;
|
---|
| 2831 | }
|
---|
| 2832 | if (stream._inFlightWriteRequest !== undefined) {
|
---|
| 2833 | return;
|
---|
| 2834 | }
|
---|
| 2835 | var state = stream._state;
|
---|
| 2836 | if (state === 'erroring') {
|
---|
| 2837 | WritableStreamFinishErroring(stream);
|
---|
| 2838 | return;
|
---|
| 2839 | }
|
---|
| 2840 | if (controller._queue.length === 0) {
|
---|
| 2841 | return;
|
---|
| 2842 | }
|
---|
| 2843 | var value = PeekQueueValue(controller);
|
---|
| 2844 | if (value === closeSentinel) {
|
---|
| 2845 | WritableStreamDefaultControllerProcessClose(controller);
|
---|
| 2846 | }
|
---|
| 2847 | else {
|
---|
| 2848 | WritableStreamDefaultControllerProcessWrite(controller, value);
|
---|
| 2849 | }
|
---|
| 2850 | }
|
---|
| 2851 | function WritableStreamDefaultControllerErrorIfNeeded(controller, error) {
|
---|
| 2852 | if (controller._controlledWritableStream._state === 'writable') {
|
---|
| 2853 | WritableStreamDefaultControllerError(controller, error);
|
---|
| 2854 | }
|
---|
| 2855 | }
|
---|
| 2856 | function WritableStreamDefaultControllerProcessClose(controller) {
|
---|
| 2857 | var stream = controller._controlledWritableStream;
|
---|
| 2858 | WritableStreamMarkCloseRequestInFlight(stream);
|
---|
| 2859 | DequeueValue(controller);
|
---|
| 2860 | var sinkClosePromise = controller._closeAlgorithm();
|
---|
| 2861 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 2862 | uponPromise(sinkClosePromise, function () {
|
---|
| 2863 | WritableStreamFinishInFlightClose(stream);
|
---|
| 2864 | return null;
|
---|
| 2865 | }, function (reason) {
|
---|
| 2866 | WritableStreamFinishInFlightCloseWithError(stream, reason);
|
---|
| 2867 | return null;
|
---|
| 2868 | });
|
---|
| 2869 | }
|
---|
| 2870 | function WritableStreamDefaultControllerProcessWrite(controller, chunk) {
|
---|
| 2871 | var stream = controller._controlledWritableStream;
|
---|
| 2872 | WritableStreamMarkFirstWriteRequestInFlight(stream);
|
---|
| 2873 | var sinkWritePromise = controller._writeAlgorithm(chunk);
|
---|
| 2874 | uponPromise(sinkWritePromise, function () {
|
---|
| 2875 | WritableStreamFinishInFlightWrite(stream);
|
---|
| 2876 | var state = stream._state;
|
---|
| 2877 | DequeueValue(controller);
|
---|
| 2878 | if (!WritableStreamCloseQueuedOrInFlight(stream) && state === 'writable') {
|
---|
| 2879 | var backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
---|
| 2880 | WritableStreamUpdateBackpressure(stream, backpressure);
|
---|
| 2881 | }
|
---|
| 2882 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
---|
| 2883 | return null;
|
---|
| 2884 | }, function (reason) {
|
---|
| 2885 | if (stream._state === 'writable') {
|
---|
| 2886 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 2887 | }
|
---|
| 2888 | WritableStreamFinishInFlightWriteWithError(stream, reason);
|
---|
| 2889 | return null;
|
---|
| 2890 | });
|
---|
| 2891 | }
|
---|
| 2892 | function WritableStreamDefaultControllerGetBackpressure(controller) {
|
---|
| 2893 | var desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);
|
---|
| 2894 | return desiredSize <= 0;
|
---|
| 2895 | }
|
---|
| 2896 | // A client of WritableStreamDefaultController may use these functions directly to bypass state check.
|
---|
| 2897 | function WritableStreamDefaultControllerError(controller, error) {
|
---|
| 2898 | var stream = controller._controlledWritableStream;
|
---|
| 2899 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 2900 | WritableStreamStartErroring(stream, error);
|
---|
| 2901 | }
|
---|
| 2902 | // Helper functions for the WritableStream.
|
---|
| 2903 | function streamBrandCheckException$2(name) {
|
---|
| 2904 | return new TypeError("WritableStream.prototype.".concat(name, " can only be used on a WritableStream"));
|
---|
| 2905 | }
|
---|
| 2906 | // Helper functions for the WritableStreamDefaultController.
|
---|
| 2907 | function defaultControllerBrandCheckException$2(name) {
|
---|
| 2908 | return new TypeError("WritableStreamDefaultController.prototype.".concat(name, " can only be used on a WritableStreamDefaultController"));
|
---|
| 2909 | }
|
---|
| 2910 | // Helper functions for the WritableStreamDefaultWriter.
|
---|
| 2911 | function defaultWriterBrandCheckException(name) {
|
---|
| 2912 | return new TypeError("WritableStreamDefaultWriter.prototype.".concat(name, " can only be used on a WritableStreamDefaultWriter"));
|
---|
| 2913 | }
|
---|
| 2914 | function defaultWriterLockException(name) {
|
---|
| 2915 | return new TypeError('Cannot ' + name + ' a stream using a released writer');
|
---|
| 2916 | }
|
---|
| 2917 | function defaultWriterClosedPromiseInitialize(writer) {
|
---|
| 2918 | writer._closedPromise = newPromise(function (resolve, reject) {
|
---|
| 2919 | writer._closedPromise_resolve = resolve;
|
---|
| 2920 | writer._closedPromise_reject = reject;
|
---|
| 2921 | writer._closedPromiseState = 'pending';
|
---|
| 2922 | });
|
---|
| 2923 | }
|
---|
| 2924 | function defaultWriterClosedPromiseInitializeAsRejected(writer, reason) {
|
---|
| 2925 | defaultWriterClosedPromiseInitialize(writer);
|
---|
| 2926 | defaultWriterClosedPromiseReject(writer, reason);
|
---|
| 2927 | }
|
---|
| 2928 | function defaultWriterClosedPromiseInitializeAsResolved(writer) {
|
---|
| 2929 | defaultWriterClosedPromiseInitialize(writer);
|
---|
| 2930 | defaultWriterClosedPromiseResolve(writer);
|
---|
| 2931 | }
|
---|
| 2932 | function defaultWriterClosedPromiseReject(writer, reason) {
|
---|
| 2933 | if (writer._closedPromise_reject === undefined) {
|
---|
| 2934 | return;
|
---|
| 2935 | }
|
---|
| 2936 | setPromiseIsHandledToTrue(writer._closedPromise);
|
---|
| 2937 | writer._closedPromise_reject(reason);
|
---|
| 2938 | writer._closedPromise_resolve = undefined;
|
---|
| 2939 | writer._closedPromise_reject = undefined;
|
---|
| 2940 | writer._closedPromiseState = 'rejected';
|
---|
| 2941 | }
|
---|
| 2942 | function defaultWriterClosedPromiseResetToRejected(writer, reason) {
|
---|
| 2943 | defaultWriterClosedPromiseInitializeAsRejected(writer, reason);
|
---|
| 2944 | }
|
---|
| 2945 | function defaultWriterClosedPromiseResolve(writer) {
|
---|
| 2946 | if (writer._closedPromise_resolve === undefined) {
|
---|
| 2947 | return;
|
---|
| 2948 | }
|
---|
| 2949 | writer._closedPromise_resolve(undefined);
|
---|
| 2950 | writer._closedPromise_resolve = undefined;
|
---|
| 2951 | writer._closedPromise_reject = undefined;
|
---|
| 2952 | writer._closedPromiseState = 'resolved';
|
---|
| 2953 | }
|
---|
| 2954 | function defaultWriterReadyPromiseInitialize(writer) {
|
---|
| 2955 | writer._readyPromise = newPromise(function (resolve, reject) {
|
---|
| 2956 | writer._readyPromise_resolve = resolve;
|
---|
| 2957 | writer._readyPromise_reject = reject;
|
---|
| 2958 | });
|
---|
| 2959 | writer._readyPromiseState = 'pending';
|
---|
| 2960 | }
|
---|
| 2961 | function defaultWriterReadyPromiseInitializeAsRejected(writer, reason) {
|
---|
| 2962 | defaultWriterReadyPromiseInitialize(writer);
|
---|
| 2963 | defaultWriterReadyPromiseReject(writer, reason);
|
---|
| 2964 | }
|
---|
| 2965 | function defaultWriterReadyPromiseInitializeAsResolved(writer) {
|
---|
| 2966 | defaultWriterReadyPromiseInitialize(writer);
|
---|
| 2967 | defaultWriterReadyPromiseResolve(writer);
|
---|
| 2968 | }
|
---|
| 2969 | function defaultWriterReadyPromiseReject(writer, reason) {
|
---|
| 2970 | if (writer._readyPromise_reject === undefined) {
|
---|
| 2971 | return;
|
---|
| 2972 | }
|
---|
| 2973 | setPromiseIsHandledToTrue(writer._readyPromise);
|
---|
| 2974 | writer._readyPromise_reject(reason);
|
---|
| 2975 | writer._readyPromise_resolve = undefined;
|
---|
| 2976 | writer._readyPromise_reject = undefined;
|
---|
| 2977 | writer._readyPromiseState = 'rejected';
|
---|
| 2978 | }
|
---|
| 2979 | function defaultWriterReadyPromiseReset(writer) {
|
---|
| 2980 | defaultWriterReadyPromiseInitialize(writer);
|
---|
| 2981 | }
|
---|
| 2982 | function defaultWriterReadyPromiseResetToRejected(writer, reason) {
|
---|
| 2983 | defaultWriterReadyPromiseInitializeAsRejected(writer, reason);
|
---|
| 2984 | }
|
---|
| 2985 | function defaultWriterReadyPromiseResolve(writer) {
|
---|
| 2986 | if (writer._readyPromise_resolve === undefined) {
|
---|
| 2987 | return;
|
---|
| 2988 | }
|
---|
| 2989 | writer._readyPromise_resolve(undefined);
|
---|
| 2990 | writer._readyPromise_resolve = undefined;
|
---|
| 2991 | writer._readyPromise_reject = undefined;
|
---|
| 2992 | writer._readyPromiseState = 'fulfilled';
|
---|
| 2993 | }
|
---|
| 2994 |
|
---|
| 2995 | /// <reference lib="dom" />
|
---|
| 2996 | function getGlobals() {
|
---|
| 2997 | if (typeof globalThis !== 'undefined') {
|
---|
| 2998 | return globalThis;
|
---|
| 2999 | }
|
---|
| 3000 | else if (typeof self !== 'undefined') {
|
---|
| 3001 | return self;
|
---|
| 3002 | }
|
---|
| 3003 | else if (typeof global !== 'undefined') {
|
---|
| 3004 | return global;
|
---|
| 3005 | }
|
---|
| 3006 | return undefined;
|
---|
| 3007 | }
|
---|
| 3008 | var globals = getGlobals();
|
---|
| 3009 |
|
---|
| 3010 | /// <reference types="node" />
|
---|
| 3011 | function isDOMExceptionConstructor(ctor) {
|
---|
| 3012 | if (!(typeof ctor === 'function' || typeof ctor === 'object')) {
|
---|
| 3013 | return false;
|
---|
| 3014 | }
|
---|
| 3015 | if (ctor.name !== 'DOMException') {
|
---|
| 3016 | return false;
|
---|
| 3017 | }
|
---|
| 3018 | try {
|
---|
| 3019 | new ctor();
|
---|
| 3020 | return true;
|
---|
| 3021 | }
|
---|
| 3022 | catch (_a) {
|
---|
| 3023 | return false;
|
---|
| 3024 | }
|
---|
| 3025 | }
|
---|
| 3026 | /**
|
---|
| 3027 | * Support:
|
---|
| 3028 | * - Web browsers
|
---|
| 3029 | * - Node 18 and higher (https://github.com/nodejs/node/commit/e4b1fb5e6422c1ff151234bb9de792d45dd88d87)
|
---|
| 3030 | */
|
---|
| 3031 | function getFromGlobal() {
|
---|
| 3032 | var ctor = globals === null || globals === void 0 ? void 0 : globals.DOMException;
|
---|
| 3033 | return isDOMExceptionConstructor(ctor) ? ctor : undefined;
|
---|
| 3034 | }
|
---|
| 3035 | /**
|
---|
| 3036 | * Support:
|
---|
| 3037 | * - All platforms
|
---|
| 3038 | */
|
---|
| 3039 | function createPolyfill() {
|
---|
| 3040 | // eslint-disable-next-line @typescript-eslint/no-shadow
|
---|
| 3041 | var ctor = function DOMException(message, name) {
|
---|
| 3042 | this.message = message || '';
|
---|
| 3043 | this.name = name || 'Error';
|
---|
| 3044 | if (Error.captureStackTrace) {
|
---|
| 3045 | Error.captureStackTrace(this, this.constructor);
|
---|
| 3046 | }
|
---|
| 3047 | };
|
---|
| 3048 | setFunctionName(ctor, 'DOMException');
|
---|
| 3049 | ctor.prototype = Object.create(Error.prototype);
|
---|
| 3050 | Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
|
---|
| 3051 | return ctor;
|
---|
| 3052 | }
|
---|
| 3053 | // eslint-disable-next-line @typescript-eslint/no-redeclare
|
---|
| 3054 | var DOMException = getFromGlobal() || createPolyfill();
|
---|
| 3055 |
|
---|
| 3056 | function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
|
---|
| 3057 | var reader = AcquireReadableStreamDefaultReader(source);
|
---|
| 3058 | var writer = AcquireWritableStreamDefaultWriter(dest);
|
---|
| 3059 | source._disturbed = true;
|
---|
| 3060 | var shuttingDown = false;
|
---|
| 3061 | // This is used to keep track of the spec's requirement that we wait for ongoing writes during shutdown.
|
---|
| 3062 | var currentWrite = promiseResolvedWith(undefined);
|
---|
| 3063 | return newPromise(function (resolve, reject) {
|
---|
| 3064 | var abortAlgorithm;
|
---|
| 3065 | if (signal !== undefined) {
|
---|
| 3066 | abortAlgorithm = function () {
|
---|
| 3067 | var error = signal.reason !== undefined ? signal.reason : new DOMException('Aborted', 'AbortError');
|
---|
| 3068 | var actions = [];
|
---|
| 3069 | if (!preventAbort) {
|
---|
| 3070 | actions.push(function () {
|
---|
| 3071 | if (dest._state === 'writable') {
|
---|
| 3072 | return WritableStreamAbort(dest, error);
|
---|
| 3073 | }
|
---|
| 3074 | return promiseResolvedWith(undefined);
|
---|
| 3075 | });
|
---|
| 3076 | }
|
---|
| 3077 | if (!preventCancel) {
|
---|
| 3078 | actions.push(function () {
|
---|
| 3079 | if (source._state === 'readable') {
|
---|
| 3080 | return ReadableStreamCancel(source, error);
|
---|
| 3081 | }
|
---|
| 3082 | return promiseResolvedWith(undefined);
|
---|
| 3083 | });
|
---|
| 3084 | }
|
---|
| 3085 | shutdownWithAction(function () { return Promise.all(actions.map(function (action) { return action(); })); }, true, error);
|
---|
| 3086 | };
|
---|
| 3087 | if (signal.aborted) {
|
---|
| 3088 | abortAlgorithm();
|
---|
| 3089 | return;
|
---|
| 3090 | }
|
---|
| 3091 | signal.addEventListener('abort', abortAlgorithm);
|
---|
| 3092 | }
|
---|
| 3093 | // Using reader and writer, read all chunks from this and write them to dest
|
---|
| 3094 | // - Backpressure must be enforced
|
---|
| 3095 | // - Shutdown must stop all activity
|
---|
| 3096 | function pipeLoop() {
|
---|
| 3097 | return newPromise(function (resolveLoop, rejectLoop) {
|
---|
| 3098 | function next(done) {
|
---|
| 3099 | if (done) {
|
---|
| 3100 | resolveLoop();
|
---|
| 3101 | }
|
---|
| 3102 | else {
|
---|
| 3103 | // Use `PerformPromiseThen` instead of `uponPromise` to avoid
|
---|
| 3104 | // adding unnecessary `.catch(rethrowAssertionErrorRejection)` handlers
|
---|
| 3105 | PerformPromiseThen(pipeStep(), next, rejectLoop);
|
---|
| 3106 | }
|
---|
| 3107 | }
|
---|
| 3108 | next(false);
|
---|
| 3109 | });
|
---|
| 3110 | }
|
---|
| 3111 | function pipeStep() {
|
---|
| 3112 | if (shuttingDown) {
|
---|
| 3113 | return promiseResolvedWith(true);
|
---|
| 3114 | }
|
---|
| 3115 | return PerformPromiseThen(writer._readyPromise, function () {
|
---|
| 3116 | return newPromise(function (resolveRead, rejectRead) {
|
---|
| 3117 | ReadableStreamDefaultReaderRead(reader, {
|
---|
| 3118 | _chunkSteps: function (chunk) {
|
---|
| 3119 | currentWrite = PerformPromiseThen(WritableStreamDefaultWriterWrite(writer, chunk), undefined, noop);
|
---|
| 3120 | resolveRead(false);
|
---|
| 3121 | },
|
---|
| 3122 | _closeSteps: function () { return resolveRead(true); },
|
---|
| 3123 | _errorSteps: rejectRead
|
---|
| 3124 | });
|
---|
| 3125 | });
|
---|
| 3126 | });
|
---|
| 3127 | }
|
---|
| 3128 | // Errors must be propagated forward
|
---|
| 3129 | isOrBecomesErrored(source, reader._closedPromise, function (storedError) {
|
---|
| 3130 | if (!preventAbort) {
|
---|
| 3131 | shutdownWithAction(function () { return WritableStreamAbort(dest, storedError); }, true, storedError);
|
---|
| 3132 | }
|
---|
| 3133 | else {
|
---|
| 3134 | shutdown(true, storedError);
|
---|
| 3135 | }
|
---|
| 3136 | return null;
|
---|
| 3137 | });
|
---|
| 3138 | // Errors must be propagated backward
|
---|
| 3139 | isOrBecomesErrored(dest, writer._closedPromise, function (storedError) {
|
---|
| 3140 | if (!preventCancel) {
|
---|
| 3141 | shutdownWithAction(function () { return ReadableStreamCancel(source, storedError); }, true, storedError);
|
---|
| 3142 | }
|
---|
| 3143 | else {
|
---|
| 3144 | shutdown(true, storedError);
|
---|
| 3145 | }
|
---|
| 3146 | return null;
|
---|
| 3147 | });
|
---|
| 3148 | // Closing must be propagated forward
|
---|
| 3149 | isOrBecomesClosed(source, reader._closedPromise, function () {
|
---|
| 3150 | if (!preventClose) {
|
---|
| 3151 | shutdownWithAction(function () { return WritableStreamDefaultWriterCloseWithErrorPropagation(writer); });
|
---|
| 3152 | }
|
---|
| 3153 | else {
|
---|
| 3154 | shutdown();
|
---|
| 3155 | }
|
---|
| 3156 | return null;
|
---|
| 3157 | });
|
---|
| 3158 | // Closing must be propagated backward
|
---|
| 3159 | if (WritableStreamCloseQueuedOrInFlight(dest) || dest._state === 'closed') {
|
---|
| 3160 | var destClosed_1 = new TypeError('the destination writable stream closed before all data could be piped to it');
|
---|
| 3161 | if (!preventCancel) {
|
---|
| 3162 | shutdownWithAction(function () { return ReadableStreamCancel(source, destClosed_1); }, true, destClosed_1);
|
---|
| 3163 | }
|
---|
| 3164 | else {
|
---|
| 3165 | shutdown(true, destClosed_1);
|
---|
| 3166 | }
|
---|
| 3167 | }
|
---|
| 3168 | setPromiseIsHandledToTrue(pipeLoop());
|
---|
| 3169 | function waitForWritesToFinish() {
|
---|
| 3170 | // Another write may have started while we were waiting on this currentWrite, so we have to be sure to wait
|
---|
| 3171 | // for that too.
|
---|
| 3172 | var oldCurrentWrite = currentWrite;
|
---|
| 3173 | return PerformPromiseThen(currentWrite, function () { return oldCurrentWrite !== currentWrite ? waitForWritesToFinish() : undefined; });
|
---|
| 3174 | }
|
---|
| 3175 | function isOrBecomesErrored(stream, promise, action) {
|
---|
| 3176 | if (stream._state === 'errored') {
|
---|
| 3177 | action(stream._storedError);
|
---|
| 3178 | }
|
---|
| 3179 | else {
|
---|
| 3180 | uponRejection(promise, action);
|
---|
| 3181 | }
|
---|
| 3182 | }
|
---|
| 3183 | function isOrBecomesClosed(stream, promise, action) {
|
---|
| 3184 | if (stream._state === 'closed') {
|
---|
| 3185 | action();
|
---|
| 3186 | }
|
---|
| 3187 | else {
|
---|
| 3188 | uponFulfillment(promise, action);
|
---|
| 3189 | }
|
---|
| 3190 | }
|
---|
| 3191 | function shutdownWithAction(action, originalIsError, originalError) {
|
---|
| 3192 | if (shuttingDown) {
|
---|
| 3193 | return;
|
---|
| 3194 | }
|
---|
| 3195 | shuttingDown = true;
|
---|
| 3196 | if (dest._state === 'writable' && !WritableStreamCloseQueuedOrInFlight(dest)) {
|
---|
| 3197 | uponFulfillment(waitForWritesToFinish(), doTheRest);
|
---|
| 3198 | }
|
---|
| 3199 | else {
|
---|
| 3200 | doTheRest();
|
---|
| 3201 | }
|
---|
| 3202 | function doTheRest() {
|
---|
| 3203 | uponPromise(action(), function () { return finalize(originalIsError, originalError); }, function (newError) { return finalize(true, newError); });
|
---|
| 3204 | return null;
|
---|
| 3205 | }
|
---|
| 3206 | }
|
---|
| 3207 | function shutdown(isError, error) {
|
---|
| 3208 | if (shuttingDown) {
|
---|
| 3209 | return;
|
---|
| 3210 | }
|
---|
| 3211 | shuttingDown = true;
|
---|
| 3212 | if (dest._state === 'writable' && !WritableStreamCloseQueuedOrInFlight(dest)) {
|
---|
| 3213 | uponFulfillment(waitForWritesToFinish(), function () { return finalize(isError, error); });
|
---|
| 3214 | }
|
---|
| 3215 | else {
|
---|
| 3216 | finalize(isError, error);
|
---|
| 3217 | }
|
---|
| 3218 | }
|
---|
| 3219 | function finalize(isError, error) {
|
---|
| 3220 | WritableStreamDefaultWriterRelease(writer);
|
---|
| 3221 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 3222 | if (signal !== undefined) {
|
---|
| 3223 | signal.removeEventListener('abort', abortAlgorithm);
|
---|
| 3224 | }
|
---|
| 3225 | if (isError) {
|
---|
| 3226 | reject(error);
|
---|
| 3227 | }
|
---|
| 3228 | else {
|
---|
| 3229 | resolve(undefined);
|
---|
| 3230 | }
|
---|
| 3231 | return null;
|
---|
| 3232 | }
|
---|
| 3233 | });
|
---|
| 3234 | }
|
---|
| 3235 |
|
---|
| 3236 | /**
|
---|
| 3237 | * Allows control of a {@link ReadableStream | readable stream}'s state and internal queue.
|
---|
| 3238 | *
|
---|
| 3239 | * @public
|
---|
| 3240 | */
|
---|
| 3241 | var ReadableStreamDefaultController = /** @class */ (function () {
|
---|
| 3242 | function ReadableStreamDefaultController() {
|
---|
| 3243 | throw new TypeError('Illegal constructor');
|
---|
| 3244 | }
|
---|
| 3245 | Object.defineProperty(ReadableStreamDefaultController.prototype, "desiredSize", {
|
---|
| 3246 | /**
|
---|
| 3247 | * Returns the desired size to fill the controlled stream's internal queue. It can be negative, if the queue is
|
---|
| 3248 | * over-full. An underlying source ought to use this information to determine when and how to apply backpressure.
|
---|
| 3249 | */
|
---|
| 3250 | get: function () {
|
---|
| 3251 | if (!IsReadableStreamDefaultController(this)) {
|
---|
| 3252 | throw defaultControllerBrandCheckException$1('desiredSize');
|
---|
| 3253 | }
|
---|
| 3254 | return ReadableStreamDefaultControllerGetDesiredSize(this);
|
---|
| 3255 | },
|
---|
| 3256 | enumerable: false,
|
---|
| 3257 | configurable: true
|
---|
| 3258 | });
|
---|
| 3259 | /**
|
---|
| 3260 | * Closes the controlled readable stream. Consumers will still be able to read any previously-enqueued chunks from
|
---|
| 3261 | * the stream, but once those are read, the stream will become closed.
|
---|
| 3262 | */
|
---|
| 3263 | ReadableStreamDefaultController.prototype.close = function () {
|
---|
| 3264 | if (!IsReadableStreamDefaultController(this)) {
|
---|
| 3265 | throw defaultControllerBrandCheckException$1('close');
|
---|
| 3266 | }
|
---|
| 3267 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
|
---|
| 3268 | throw new TypeError('The stream is not in a state that permits close');
|
---|
| 3269 | }
|
---|
| 3270 | ReadableStreamDefaultControllerClose(this);
|
---|
| 3271 | };
|
---|
| 3272 | ReadableStreamDefaultController.prototype.enqueue = function (chunk) {
|
---|
| 3273 | if (chunk === void 0) { chunk = undefined; }
|
---|
| 3274 | if (!IsReadableStreamDefaultController(this)) {
|
---|
| 3275 | throw defaultControllerBrandCheckException$1('enqueue');
|
---|
| 3276 | }
|
---|
| 3277 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(this)) {
|
---|
| 3278 | throw new TypeError('The stream is not in a state that permits enqueue');
|
---|
| 3279 | }
|
---|
| 3280 | return ReadableStreamDefaultControllerEnqueue(this, chunk);
|
---|
| 3281 | };
|
---|
| 3282 | /**
|
---|
| 3283 | * Errors the controlled readable stream, making all future interactions with it fail with the given error `e`.
|
---|
| 3284 | */
|
---|
| 3285 | ReadableStreamDefaultController.prototype.error = function (e) {
|
---|
| 3286 | if (e === void 0) { e = undefined; }
|
---|
| 3287 | if (!IsReadableStreamDefaultController(this)) {
|
---|
| 3288 | throw defaultControllerBrandCheckException$1('error');
|
---|
| 3289 | }
|
---|
| 3290 | ReadableStreamDefaultControllerError(this, e);
|
---|
| 3291 | };
|
---|
| 3292 | /** @internal */
|
---|
| 3293 | ReadableStreamDefaultController.prototype[CancelSteps] = function (reason) {
|
---|
| 3294 | ResetQueue(this);
|
---|
| 3295 | var result = this._cancelAlgorithm(reason);
|
---|
| 3296 | ReadableStreamDefaultControllerClearAlgorithms(this);
|
---|
| 3297 | return result;
|
---|
| 3298 | };
|
---|
| 3299 | /** @internal */
|
---|
| 3300 | ReadableStreamDefaultController.prototype[PullSteps] = function (readRequest) {
|
---|
| 3301 | var stream = this._controlledReadableStream;
|
---|
| 3302 | if (this._queue.length > 0) {
|
---|
| 3303 | var chunk = DequeueValue(this);
|
---|
| 3304 | if (this._closeRequested && this._queue.length === 0) {
|
---|
| 3305 | ReadableStreamDefaultControllerClearAlgorithms(this);
|
---|
| 3306 | ReadableStreamClose(stream);
|
---|
| 3307 | }
|
---|
| 3308 | else {
|
---|
| 3309 | ReadableStreamDefaultControllerCallPullIfNeeded(this);
|
---|
| 3310 | }
|
---|
| 3311 | readRequest._chunkSteps(chunk);
|
---|
| 3312 | }
|
---|
| 3313 | else {
|
---|
| 3314 | ReadableStreamAddReadRequest(stream, readRequest);
|
---|
| 3315 | ReadableStreamDefaultControllerCallPullIfNeeded(this);
|
---|
| 3316 | }
|
---|
| 3317 | };
|
---|
| 3318 | /** @internal */
|
---|
| 3319 | ReadableStreamDefaultController.prototype[ReleaseSteps] = function () {
|
---|
| 3320 | // Do nothing.
|
---|
| 3321 | };
|
---|
| 3322 | return ReadableStreamDefaultController;
|
---|
| 3323 | }());
|
---|
| 3324 | Object.defineProperties(ReadableStreamDefaultController.prototype, {
|
---|
| 3325 | close: { enumerable: true },
|
---|
| 3326 | enqueue: { enumerable: true },
|
---|
| 3327 | error: { enumerable: true },
|
---|
| 3328 | desiredSize: { enumerable: true }
|
---|
| 3329 | });
|
---|
| 3330 | setFunctionName(ReadableStreamDefaultController.prototype.close, 'close');
|
---|
| 3331 | setFunctionName(ReadableStreamDefaultController.prototype.enqueue, 'enqueue');
|
---|
| 3332 | setFunctionName(ReadableStreamDefaultController.prototype.error, 'error');
|
---|
| 3333 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 3334 | Object.defineProperty(ReadableStreamDefaultController.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 3335 | value: 'ReadableStreamDefaultController',
|
---|
| 3336 | configurable: true
|
---|
| 3337 | });
|
---|
| 3338 | }
|
---|
| 3339 | // Abstract operations for the ReadableStreamDefaultController.
|
---|
| 3340 | function IsReadableStreamDefaultController(x) {
|
---|
| 3341 | if (!typeIsObject(x)) {
|
---|
| 3342 | return false;
|
---|
| 3343 | }
|
---|
| 3344 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
|
---|
| 3345 | return false;
|
---|
| 3346 | }
|
---|
| 3347 | return x instanceof ReadableStreamDefaultController;
|
---|
| 3348 | }
|
---|
| 3349 | function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
|
---|
| 3350 | var shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
|
---|
| 3351 | if (!shouldPull) {
|
---|
| 3352 | return;
|
---|
| 3353 | }
|
---|
| 3354 | if (controller._pulling) {
|
---|
| 3355 | controller._pullAgain = true;
|
---|
| 3356 | return;
|
---|
| 3357 | }
|
---|
| 3358 | controller._pulling = true;
|
---|
| 3359 | var pullPromise = controller._pullAlgorithm();
|
---|
| 3360 | uponPromise(pullPromise, function () {
|
---|
| 3361 | controller._pulling = false;
|
---|
| 3362 | if (controller._pullAgain) {
|
---|
| 3363 | controller._pullAgain = false;
|
---|
| 3364 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
---|
| 3365 | }
|
---|
| 3366 | return null;
|
---|
| 3367 | }, function (e) {
|
---|
| 3368 | ReadableStreamDefaultControllerError(controller, e);
|
---|
| 3369 | return null;
|
---|
| 3370 | });
|
---|
| 3371 | }
|
---|
| 3372 | function ReadableStreamDefaultControllerShouldCallPull(controller) {
|
---|
| 3373 | var stream = controller._controlledReadableStream;
|
---|
| 3374 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {
|
---|
| 3375 | return false;
|
---|
| 3376 | }
|
---|
| 3377 | if (!controller._started) {
|
---|
| 3378 | return false;
|
---|
| 3379 | }
|
---|
| 3380 | if (IsReadableStreamLocked(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {
|
---|
| 3381 | return true;
|
---|
| 3382 | }
|
---|
| 3383 | var desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);
|
---|
| 3384 | if (desiredSize > 0) {
|
---|
| 3385 | return true;
|
---|
| 3386 | }
|
---|
| 3387 | return false;
|
---|
| 3388 | }
|
---|
| 3389 | function ReadableStreamDefaultControllerClearAlgorithms(controller) {
|
---|
| 3390 | controller._pullAlgorithm = undefined;
|
---|
| 3391 | controller._cancelAlgorithm = undefined;
|
---|
| 3392 | controller._strategySizeAlgorithm = undefined;
|
---|
| 3393 | }
|
---|
| 3394 | // A client of ReadableStreamDefaultController may use these functions directly to bypass state check.
|
---|
| 3395 | function ReadableStreamDefaultControllerClose(controller) {
|
---|
| 3396 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {
|
---|
| 3397 | return;
|
---|
| 3398 | }
|
---|
| 3399 | var stream = controller._controlledReadableStream;
|
---|
| 3400 | controller._closeRequested = true;
|
---|
| 3401 | if (controller._queue.length === 0) {
|
---|
| 3402 | ReadableStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 3403 | ReadableStreamClose(stream);
|
---|
| 3404 | }
|
---|
| 3405 | }
|
---|
| 3406 | function ReadableStreamDefaultControllerEnqueue(controller, chunk) {
|
---|
| 3407 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {
|
---|
| 3408 | return;
|
---|
| 3409 | }
|
---|
| 3410 | var stream = controller._controlledReadableStream;
|
---|
| 3411 | if (IsReadableStreamLocked(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {
|
---|
| 3412 | ReadableStreamFulfillReadRequest(stream, chunk, false);
|
---|
| 3413 | }
|
---|
| 3414 | else {
|
---|
| 3415 | var chunkSize = void 0;
|
---|
| 3416 | try {
|
---|
| 3417 | chunkSize = controller._strategySizeAlgorithm(chunk);
|
---|
| 3418 | }
|
---|
| 3419 | catch (chunkSizeE) {
|
---|
| 3420 | ReadableStreamDefaultControllerError(controller, chunkSizeE);
|
---|
| 3421 | throw chunkSizeE;
|
---|
| 3422 | }
|
---|
| 3423 | try {
|
---|
| 3424 | EnqueueValueWithSize(controller, chunk, chunkSize);
|
---|
| 3425 | }
|
---|
| 3426 | catch (enqueueE) {
|
---|
| 3427 | ReadableStreamDefaultControllerError(controller, enqueueE);
|
---|
| 3428 | throw enqueueE;
|
---|
| 3429 | }
|
---|
| 3430 | }
|
---|
| 3431 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
---|
| 3432 | }
|
---|
| 3433 | function ReadableStreamDefaultControllerError(controller, e) {
|
---|
| 3434 | var stream = controller._controlledReadableStream;
|
---|
| 3435 | if (stream._state !== 'readable') {
|
---|
| 3436 | return;
|
---|
| 3437 | }
|
---|
| 3438 | ResetQueue(controller);
|
---|
| 3439 | ReadableStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 3440 | ReadableStreamError(stream, e);
|
---|
| 3441 | }
|
---|
| 3442 | function ReadableStreamDefaultControllerGetDesiredSize(controller) {
|
---|
| 3443 | var state = controller._controlledReadableStream._state;
|
---|
| 3444 | if (state === 'errored') {
|
---|
| 3445 | return null;
|
---|
| 3446 | }
|
---|
| 3447 | if (state === 'closed') {
|
---|
| 3448 | return 0;
|
---|
| 3449 | }
|
---|
| 3450 | return controller._strategyHWM - controller._queueTotalSize;
|
---|
| 3451 | }
|
---|
| 3452 | // This is used in the implementation of TransformStream.
|
---|
| 3453 | function ReadableStreamDefaultControllerHasBackpressure(controller) {
|
---|
| 3454 | if (ReadableStreamDefaultControllerShouldCallPull(controller)) {
|
---|
| 3455 | return false;
|
---|
| 3456 | }
|
---|
| 3457 | return true;
|
---|
| 3458 | }
|
---|
| 3459 | function ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) {
|
---|
| 3460 | var state = controller._controlledReadableStream._state;
|
---|
| 3461 | if (!controller._closeRequested && state === 'readable') {
|
---|
| 3462 | return true;
|
---|
| 3463 | }
|
---|
| 3464 | return false;
|
---|
| 3465 | }
|
---|
| 3466 | function SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm) {
|
---|
| 3467 | controller._controlledReadableStream = stream;
|
---|
| 3468 | controller._queue = undefined;
|
---|
| 3469 | controller._queueTotalSize = undefined;
|
---|
| 3470 | ResetQueue(controller);
|
---|
| 3471 | controller._started = false;
|
---|
| 3472 | controller._closeRequested = false;
|
---|
| 3473 | controller._pullAgain = false;
|
---|
| 3474 | controller._pulling = false;
|
---|
| 3475 | controller._strategySizeAlgorithm = sizeAlgorithm;
|
---|
| 3476 | controller._strategyHWM = highWaterMark;
|
---|
| 3477 | controller._pullAlgorithm = pullAlgorithm;
|
---|
| 3478 | controller._cancelAlgorithm = cancelAlgorithm;
|
---|
| 3479 | stream._readableStreamController = controller;
|
---|
| 3480 | var startResult = startAlgorithm();
|
---|
| 3481 | uponPromise(promiseResolvedWith(startResult), function () {
|
---|
| 3482 | controller._started = true;
|
---|
| 3483 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
---|
| 3484 | return null;
|
---|
| 3485 | }, function (r) {
|
---|
| 3486 | ReadableStreamDefaultControllerError(controller, r);
|
---|
| 3487 | return null;
|
---|
| 3488 | });
|
---|
| 3489 | }
|
---|
| 3490 | function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underlyingSource, highWaterMark, sizeAlgorithm) {
|
---|
| 3491 | var controller = Object.create(ReadableStreamDefaultController.prototype);
|
---|
| 3492 | var startAlgorithm;
|
---|
| 3493 | var pullAlgorithm;
|
---|
| 3494 | var cancelAlgorithm;
|
---|
| 3495 | if (underlyingSource.start !== undefined) {
|
---|
| 3496 | startAlgorithm = function () { return underlyingSource.start(controller); };
|
---|
| 3497 | }
|
---|
| 3498 | else {
|
---|
| 3499 | startAlgorithm = function () { return undefined; };
|
---|
| 3500 | }
|
---|
| 3501 | if (underlyingSource.pull !== undefined) {
|
---|
| 3502 | pullAlgorithm = function () { return underlyingSource.pull(controller); };
|
---|
| 3503 | }
|
---|
| 3504 | else {
|
---|
| 3505 | pullAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 3506 | }
|
---|
| 3507 | if (underlyingSource.cancel !== undefined) {
|
---|
| 3508 | cancelAlgorithm = function (reason) { return underlyingSource.cancel(reason); };
|
---|
| 3509 | }
|
---|
| 3510 | else {
|
---|
| 3511 | cancelAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 3512 | }
|
---|
| 3513 | SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
|
---|
| 3514 | }
|
---|
| 3515 | // Helper functions for the ReadableStreamDefaultController.
|
---|
| 3516 | function defaultControllerBrandCheckException$1(name) {
|
---|
| 3517 | return new TypeError("ReadableStreamDefaultController.prototype.".concat(name, " can only be used on a ReadableStreamDefaultController"));
|
---|
| 3518 | }
|
---|
| 3519 |
|
---|
| 3520 | function ReadableStreamTee(stream, cloneForBranch2) {
|
---|
| 3521 | if (IsReadableByteStreamController(stream._readableStreamController)) {
|
---|
| 3522 | return ReadableByteStreamTee(stream);
|
---|
| 3523 | }
|
---|
| 3524 | return ReadableStreamDefaultTee(stream);
|
---|
| 3525 | }
|
---|
| 3526 | function ReadableStreamDefaultTee(stream, cloneForBranch2) {
|
---|
| 3527 | var reader = AcquireReadableStreamDefaultReader(stream);
|
---|
| 3528 | var reading = false;
|
---|
| 3529 | var readAgain = false;
|
---|
| 3530 | var canceled1 = false;
|
---|
| 3531 | var canceled2 = false;
|
---|
| 3532 | var reason1;
|
---|
| 3533 | var reason2;
|
---|
| 3534 | var branch1;
|
---|
| 3535 | var branch2;
|
---|
| 3536 | var resolveCancelPromise;
|
---|
| 3537 | var cancelPromise = newPromise(function (resolve) {
|
---|
| 3538 | resolveCancelPromise = resolve;
|
---|
| 3539 | });
|
---|
| 3540 | function pullAlgorithm() {
|
---|
| 3541 | if (reading) {
|
---|
| 3542 | readAgain = true;
|
---|
| 3543 | return promiseResolvedWith(undefined);
|
---|
| 3544 | }
|
---|
| 3545 | reading = true;
|
---|
| 3546 | var readRequest = {
|
---|
| 3547 | _chunkSteps: function (chunk) {
|
---|
| 3548 | // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
|
---|
| 3549 | // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
|
---|
| 3550 | // successful synchronously-available reads get ahead of asynchronously-available errors.
|
---|
| 3551 | _queueMicrotask(function () {
|
---|
| 3552 | readAgain = false;
|
---|
| 3553 | var chunk1 = chunk;
|
---|
| 3554 | var chunk2 = chunk;
|
---|
| 3555 | // There is no way to access the cloning code right now in the reference implementation.
|
---|
| 3556 | // If we add one then we'll need an implementation for serializable objects.
|
---|
| 3557 | // if (!canceled2 && cloneForBranch2) {
|
---|
| 3558 | // chunk2 = StructuredDeserialize(StructuredSerialize(chunk2));
|
---|
| 3559 | // }
|
---|
| 3560 | if (!canceled1) {
|
---|
| 3561 | ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, chunk1);
|
---|
| 3562 | }
|
---|
| 3563 | if (!canceled2) {
|
---|
| 3564 | ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, chunk2);
|
---|
| 3565 | }
|
---|
| 3566 | reading = false;
|
---|
| 3567 | if (readAgain) {
|
---|
| 3568 | pullAlgorithm();
|
---|
| 3569 | }
|
---|
| 3570 | });
|
---|
| 3571 | },
|
---|
| 3572 | _closeSteps: function () {
|
---|
| 3573 | reading = false;
|
---|
| 3574 | if (!canceled1) {
|
---|
| 3575 | ReadableStreamDefaultControllerClose(branch1._readableStreamController);
|
---|
| 3576 | }
|
---|
| 3577 | if (!canceled2) {
|
---|
| 3578 | ReadableStreamDefaultControllerClose(branch2._readableStreamController);
|
---|
| 3579 | }
|
---|
| 3580 | if (!canceled1 || !canceled2) {
|
---|
| 3581 | resolveCancelPromise(undefined);
|
---|
| 3582 | }
|
---|
| 3583 | },
|
---|
| 3584 | _errorSteps: function () {
|
---|
| 3585 | reading = false;
|
---|
| 3586 | }
|
---|
| 3587 | };
|
---|
| 3588 | ReadableStreamDefaultReaderRead(reader, readRequest);
|
---|
| 3589 | return promiseResolvedWith(undefined);
|
---|
| 3590 | }
|
---|
| 3591 | function cancel1Algorithm(reason) {
|
---|
| 3592 | canceled1 = true;
|
---|
| 3593 | reason1 = reason;
|
---|
| 3594 | if (canceled2) {
|
---|
| 3595 | var compositeReason = CreateArrayFromList([reason1, reason2]);
|
---|
| 3596 | var cancelResult = ReadableStreamCancel(stream, compositeReason);
|
---|
| 3597 | resolveCancelPromise(cancelResult);
|
---|
| 3598 | }
|
---|
| 3599 | return cancelPromise;
|
---|
| 3600 | }
|
---|
| 3601 | function cancel2Algorithm(reason) {
|
---|
| 3602 | canceled2 = true;
|
---|
| 3603 | reason2 = reason;
|
---|
| 3604 | if (canceled1) {
|
---|
| 3605 | var compositeReason = CreateArrayFromList([reason1, reason2]);
|
---|
| 3606 | var cancelResult = ReadableStreamCancel(stream, compositeReason);
|
---|
| 3607 | resolveCancelPromise(cancelResult);
|
---|
| 3608 | }
|
---|
| 3609 | return cancelPromise;
|
---|
| 3610 | }
|
---|
| 3611 | function startAlgorithm() {
|
---|
| 3612 | // do nothing
|
---|
| 3613 | }
|
---|
| 3614 | branch1 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel1Algorithm);
|
---|
| 3615 | branch2 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel2Algorithm);
|
---|
| 3616 | uponRejection(reader._closedPromise, function (r) {
|
---|
| 3617 | ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
|
---|
| 3618 | ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
|
---|
| 3619 | if (!canceled1 || !canceled2) {
|
---|
| 3620 | resolveCancelPromise(undefined);
|
---|
| 3621 | }
|
---|
| 3622 | return null;
|
---|
| 3623 | });
|
---|
| 3624 | return [branch1, branch2];
|
---|
| 3625 | }
|
---|
| 3626 | function ReadableByteStreamTee(stream) {
|
---|
| 3627 | var reader = AcquireReadableStreamDefaultReader(stream);
|
---|
| 3628 | var reading = false;
|
---|
| 3629 | var readAgainForBranch1 = false;
|
---|
| 3630 | var readAgainForBranch2 = false;
|
---|
| 3631 | var canceled1 = false;
|
---|
| 3632 | var canceled2 = false;
|
---|
| 3633 | var reason1;
|
---|
| 3634 | var reason2;
|
---|
| 3635 | var branch1;
|
---|
| 3636 | var branch2;
|
---|
| 3637 | var resolveCancelPromise;
|
---|
| 3638 | var cancelPromise = newPromise(function (resolve) {
|
---|
| 3639 | resolveCancelPromise = resolve;
|
---|
| 3640 | });
|
---|
| 3641 | function forwardReaderError(thisReader) {
|
---|
| 3642 | uponRejection(thisReader._closedPromise, function (r) {
|
---|
| 3643 | if (thisReader !== reader) {
|
---|
| 3644 | return null;
|
---|
| 3645 | }
|
---|
| 3646 | ReadableByteStreamControllerError(branch1._readableStreamController, r);
|
---|
| 3647 | ReadableByteStreamControllerError(branch2._readableStreamController, r);
|
---|
| 3648 | if (!canceled1 || !canceled2) {
|
---|
| 3649 | resolveCancelPromise(undefined);
|
---|
| 3650 | }
|
---|
| 3651 | return null;
|
---|
| 3652 | });
|
---|
| 3653 | }
|
---|
| 3654 | function pullWithDefaultReader() {
|
---|
| 3655 | if (IsReadableStreamBYOBReader(reader)) {
|
---|
| 3656 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 3657 | reader = AcquireReadableStreamDefaultReader(stream);
|
---|
| 3658 | forwardReaderError(reader);
|
---|
| 3659 | }
|
---|
| 3660 | var readRequest = {
|
---|
| 3661 | _chunkSteps: function (chunk) {
|
---|
| 3662 | // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
|
---|
| 3663 | // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
|
---|
| 3664 | // successful synchronously-available reads get ahead of asynchronously-available errors.
|
---|
| 3665 | _queueMicrotask(function () {
|
---|
| 3666 | readAgainForBranch1 = false;
|
---|
| 3667 | readAgainForBranch2 = false;
|
---|
| 3668 | var chunk1 = chunk;
|
---|
| 3669 | var chunk2 = chunk;
|
---|
| 3670 | if (!canceled1 && !canceled2) {
|
---|
| 3671 | try {
|
---|
| 3672 | chunk2 = CloneAsUint8Array(chunk);
|
---|
| 3673 | }
|
---|
| 3674 | catch (cloneE) {
|
---|
| 3675 | ReadableByteStreamControllerError(branch1._readableStreamController, cloneE);
|
---|
| 3676 | ReadableByteStreamControllerError(branch2._readableStreamController, cloneE);
|
---|
| 3677 | resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
|
---|
| 3678 | return;
|
---|
| 3679 | }
|
---|
| 3680 | }
|
---|
| 3681 | if (!canceled1) {
|
---|
| 3682 | ReadableByteStreamControllerEnqueue(branch1._readableStreamController, chunk1);
|
---|
| 3683 | }
|
---|
| 3684 | if (!canceled2) {
|
---|
| 3685 | ReadableByteStreamControllerEnqueue(branch2._readableStreamController, chunk2);
|
---|
| 3686 | }
|
---|
| 3687 | reading = false;
|
---|
| 3688 | if (readAgainForBranch1) {
|
---|
| 3689 | pull1Algorithm();
|
---|
| 3690 | }
|
---|
| 3691 | else if (readAgainForBranch2) {
|
---|
| 3692 | pull2Algorithm();
|
---|
| 3693 | }
|
---|
| 3694 | });
|
---|
| 3695 | },
|
---|
| 3696 | _closeSteps: function () {
|
---|
| 3697 | reading = false;
|
---|
| 3698 | if (!canceled1) {
|
---|
| 3699 | ReadableByteStreamControllerClose(branch1._readableStreamController);
|
---|
| 3700 | }
|
---|
| 3701 | if (!canceled2) {
|
---|
| 3702 | ReadableByteStreamControllerClose(branch2._readableStreamController);
|
---|
| 3703 | }
|
---|
| 3704 | if (branch1._readableStreamController._pendingPullIntos.length > 0) {
|
---|
| 3705 | ReadableByteStreamControllerRespond(branch1._readableStreamController, 0);
|
---|
| 3706 | }
|
---|
| 3707 | if (branch2._readableStreamController._pendingPullIntos.length > 0) {
|
---|
| 3708 | ReadableByteStreamControllerRespond(branch2._readableStreamController, 0);
|
---|
| 3709 | }
|
---|
| 3710 | if (!canceled1 || !canceled2) {
|
---|
| 3711 | resolveCancelPromise(undefined);
|
---|
| 3712 | }
|
---|
| 3713 | },
|
---|
| 3714 | _errorSteps: function () {
|
---|
| 3715 | reading = false;
|
---|
| 3716 | }
|
---|
| 3717 | };
|
---|
| 3718 | ReadableStreamDefaultReaderRead(reader, readRequest);
|
---|
| 3719 | }
|
---|
| 3720 | function pullWithBYOBReader(view, forBranch2) {
|
---|
| 3721 | if (IsReadableStreamDefaultReader(reader)) {
|
---|
| 3722 | ReadableStreamReaderGenericRelease(reader);
|
---|
| 3723 | reader = AcquireReadableStreamBYOBReader(stream);
|
---|
| 3724 | forwardReaderError(reader);
|
---|
| 3725 | }
|
---|
| 3726 | var byobBranch = forBranch2 ? branch2 : branch1;
|
---|
| 3727 | var otherBranch = forBranch2 ? branch1 : branch2;
|
---|
| 3728 | var readIntoRequest = {
|
---|
| 3729 | _chunkSteps: function (chunk) {
|
---|
| 3730 | // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using
|
---|
| 3731 | // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let
|
---|
| 3732 | // successful synchronously-available reads get ahead of asynchronously-available errors.
|
---|
| 3733 | _queueMicrotask(function () {
|
---|
| 3734 | readAgainForBranch1 = false;
|
---|
| 3735 | readAgainForBranch2 = false;
|
---|
| 3736 | var byobCanceled = forBranch2 ? canceled2 : canceled1;
|
---|
| 3737 | var otherCanceled = forBranch2 ? canceled1 : canceled2;
|
---|
| 3738 | if (!otherCanceled) {
|
---|
| 3739 | var clonedChunk = void 0;
|
---|
| 3740 | try {
|
---|
| 3741 | clonedChunk = CloneAsUint8Array(chunk);
|
---|
| 3742 | }
|
---|
| 3743 | catch (cloneE) {
|
---|
| 3744 | ReadableByteStreamControllerError(byobBranch._readableStreamController, cloneE);
|
---|
| 3745 | ReadableByteStreamControllerError(otherBranch._readableStreamController, cloneE);
|
---|
| 3746 | resolveCancelPromise(ReadableStreamCancel(stream, cloneE));
|
---|
| 3747 | return;
|
---|
| 3748 | }
|
---|
| 3749 | if (!byobCanceled) {
|
---|
| 3750 | ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
|
---|
| 3751 | }
|
---|
| 3752 | ReadableByteStreamControllerEnqueue(otherBranch._readableStreamController, clonedChunk);
|
---|
| 3753 | }
|
---|
| 3754 | else if (!byobCanceled) {
|
---|
| 3755 | ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
|
---|
| 3756 | }
|
---|
| 3757 | reading = false;
|
---|
| 3758 | if (readAgainForBranch1) {
|
---|
| 3759 | pull1Algorithm();
|
---|
| 3760 | }
|
---|
| 3761 | else if (readAgainForBranch2) {
|
---|
| 3762 | pull2Algorithm();
|
---|
| 3763 | }
|
---|
| 3764 | });
|
---|
| 3765 | },
|
---|
| 3766 | _closeSteps: function (chunk) {
|
---|
| 3767 | reading = false;
|
---|
| 3768 | var byobCanceled = forBranch2 ? canceled2 : canceled1;
|
---|
| 3769 | var otherCanceled = forBranch2 ? canceled1 : canceled2;
|
---|
| 3770 | if (!byobCanceled) {
|
---|
| 3771 | ReadableByteStreamControllerClose(byobBranch._readableStreamController);
|
---|
| 3772 | }
|
---|
| 3773 | if (!otherCanceled) {
|
---|
| 3774 | ReadableByteStreamControllerClose(otherBranch._readableStreamController);
|
---|
| 3775 | }
|
---|
| 3776 | if (chunk !== undefined) {
|
---|
| 3777 | if (!byobCanceled) {
|
---|
| 3778 | ReadableByteStreamControllerRespondWithNewView(byobBranch._readableStreamController, chunk);
|
---|
| 3779 | }
|
---|
| 3780 | if (!otherCanceled && otherBranch._readableStreamController._pendingPullIntos.length > 0) {
|
---|
| 3781 | ReadableByteStreamControllerRespond(otherBranch._readableStreamController, 0);
|
---|
| 3782 | }
|
---|
| 3783 | }
|
---|
| 3784 | if (!byobCanceled || !otherCanceled) {
|
---|
| 3785 | resolveCancelPromise(undefined);
|
---|
| 3786 | }
|
---|
| 3787 | },
|
---|
| 3788 | _errorSteps: function () {
|
---|
| 3789 | reading = false;
|
---|
| 3790 | }
|
---|
| 3791 | };
|
---|
| 3792 | ReadableStreamBYOBReaderRead(reader, view, 1, readIntoRequest);
|
---|
| 3793 | }
|
---|
| 3794 | function pull1Algorithm() {
|
---|
| 3795 | if (reading) {
|
---|
| 3796 | readAgainForBranch1 = true;
|
---|
| 3797 | return promiseResolvedWith(undefined);
|
---|
| 3798 | }
|
---|
| 3799 | reading = true;
|
---|
| 3800 | var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch1._readableStreamController);
|
---|
| 3801 | if (byobRequest === null) {
|
---|
| 3802 | pullWithDefaultReader();
|
---|
| 3803 | }
|
---|
| 3804 | else {
|
---|
| 3805 | pullWithBYOBReader(byobRequest._view, false);
|
---|
| 3806 | }
|
---|
| 3807 | return promiseResolvedWith(undefined);
|
---|
| 3808 | }
|
---|
| 3809 | function pull2Algorithm() {
|
---|
| 3810 | if (reading) {
|
---|
| 3811 | readAgainForBranch2 = true;
|
---|
| 3812 | return promiseResolvedWith(undefined);
|
---|
| 3813 | }
|
---|
| 3814 | reading = true;
|
---|
| 3815 | var byobRequest = ReadableByteStreamControllerGetBYOBRequest(branch2._readableStreamController);
|
---|
| 3816 | if (byobRequest === null) {
|
---|
| 3817 | pullWithDefaultReader();
|
---|
| 3818 | }
|
---|
| 3819 | else {
|
---|
| 3820 | pullWithBYOBReader(byobRequest._view, true);
|
---|
| 3821 | }
|
---|
| 3822 | return promiseResolvedWith(undefined);
|
---|
| 3823 | }
|
---|
| 3824 | function cancel1Algorithm(reason) {
|
---|
| 3825 | canceled1 = true;
|
---|
| 3826 | reason1 = reason;
|
---|
| 3827 | if (canceled2) {
|
---|
| 3828 | var compositeReason = CreateArrayFromList([reason1, reason2]);
|
---|
| 3829 | var cancelResult = ReadableStreamCancel(stream, compositeReason);
|
---|
| 3830 | resolveCancelPromise(cancelResult);
|
---|
| 3831 | }
|
---|
| 3832 | return cancelPromise;
|
---|
| 3833 | }
|
---|
| 3834 | function cancel2Algorithm(reason) {
|
---|
| 3835 | canceled2 = true;
|
---|
| 3836 | reason2 = reason;
|
---|
| 3837 | if (canceled1) {
|
---|
| 3838 | var compositeReason = CreateArrayFromList([reason1, reason2]);
|
---|
| 3839 | var cancelResult = ReadableStreamCancel(stream, compositeReason);
|
---|
| 3840 | resolveCancelPromise(cancelResult);
|
---|
| 3841 | }
|
---|
| 3842 | return cancelPromise;
|
---|
| 3843 | }
|
---|
| 3844 | function startAlgorithm() {
|
---|
| 3845 | return;
|
---|
| 3846 | }
|
---|
| 3847 | branch1 = CreateReadableByteStream(startAlgorithm, pull1Algorithm, cancel1Algorithm);
|
---|
| 3848 | branch2 = CreateReadableByteStream(startAlgorithm, pull2Algorithm, cancel2Algorithm);
|
---|
| 3849 | forwardReaderError(reader);
|
---|
| 3850 | return [branch1, branch2];
|
---|
| 3851 | }
|
---|
| 3852 |
|
---|
| 3853 | function isReadableStreamLike(stream) {
|
---|
| 3854 | return typeIsObject(stream) && typeof stream.getReader !== 'undefined';
|
---|
| 3855 | }
|
---|
| 3856 |
|
---|
| 3857 | function ReadableStreamFrom(source) {
|
---|
| 3858 | if (isReadableStreamLike(source)) {
|
---|
| 3859 | return ReadableStreamFromDefaultReader(source.getReader());
|
---|
| 3860 | }
|
---|
| 3861 | return ReadableStreamFromIterable(source);
|
---|
| 3862 | }
|
---|
| 3863 | function ReadableStreamFromIterable(asyncIterable) {
|
---|
| 3864 | var stream;
|
---|
| 3865 | var iteratorRecord = GetIterator(asyncIterable, 'async');
|
---|
| 3866 | var startAlgorithm = noop;
|
---|
| 3867 | function pullAlgorithm() {
|
---|
| 3868 | var nextResult;
|
---|
| 3869 | try {
|
---|
| 3870 | nextResult = IteratorNext(iteratorRecord);
|
---|
| 3871 | }
|
---|
| 3872 | catch (e) {
|
---|
| 3873 | return promiseRejectedWith(e);
|
---|
| 3874 | }
|
---|
| 3875 | var nextPromise = promiseResolvedWith(nextResult);
|
---|
| 3876 | return transformPromiseWith(nextPromise, function (iterResult) {
|
---|
| 3877 | if (!typeIsObject(iterResult)) {
|
---|
| 3878 | throw new TypeError('The promise returned by the iterator.next() method must fulfill with an object');
|
---|
| 3879 | }
|
---|
| 3880 | var done = IteratorComplete(iterResult);
|
---|
| 3881 | if (done) {
|
---|
| 3882 | ReadableStreamDefaultControllerClose(stream._readableStreamController);
|
---|
| 3883 | }
|
---|
| 3884 | else {
|
---|
| 3885 | var value = IteratorValue(iterResult);
|
---|
| 3886 | ReadableStreamDefaultControllerEnqueue(stream._readableStreamController, value);
|
---|
| 3887 | }
|
---|
| 3888 | });
|
---|
| 3889 | }
|
---|
| 3890 | function cancelAlgorithm(reason) {
|
---|
| 3891 | var iterator = iteratorRecord.iterator;
|
---|
| 3892 | var returnMethod;
|
---|
| 3893 | try {
|
---|
| 3894 | returnMethod = GetMethod(iterator, 'return');
|
---|
| 3895 | }
|
---|
| 3896 | catch (e) {
|
---|
| 3897 | return promiseRejectedWith(e);
|
---|
| 3898 | }
|
---|
| 3899 | if (returnMethod === undefined) {
|
---|
| 3900 | return promiseResolvedWith(undefined);
|
---|
| 3901 | }
|
---|
| 3902 | var returnResult;
|
---|
| 3903 | try {
|
---|
| 3904 | returnResult = reflectCall(returnMethod, iterator, [reason]);
|
---|
| 3905 | }
|
---|
| 3906 | catch (e) {
|
---|
| 3907 | return promiseRejectedWith(e);
|
---|
| 3908 | }
|
---|
| 3909 | var returnPromise = promiseResolvedWith(returnResult);
|
---|
| 3910 | return transformPromiseWith(returnPromise, function (iterResult) {
|
---|
| 3911 | if (!typeIsObject(iterResult)) {
|
---|
| 3912 | throw new TypeError('The promise returned by the iterator.return() method must fulfill with an object');
|
---|
| 3913 | }
|
---|
| 3914 | return undefined;
|
---|
| 3915 | });
|
---|
| 3916 | }
|
---|
| 3917 | stream = CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, 0);
|
---|
| 3918 | return stream;
|
---|
| 3919 | }
|
---|
| 3920 | function ReadableStreamFromDefaultReader(reader) {
|
---|
| 3921 | var stream;
|
---|
| 3922 | var startAlgorithm = noop;
|
---|
| 3923 | function pullAlgorithm() {
|
---|
| 3924 | var readPromise;
|
---|
| 3925 | try {
|
---|
| 3926 | readPromise = reader.read();
|
---|
| 3927 | }
|
---|
| 3928 | catch (e) {
|
---|
| 3929 | return promiseRejectedWith(e);
|
---|
| 3930 | }
|
---|
| 3931 | return transformPromiseWith(readPromise, function (readResult) {
|
---|
| 3932 | if (!typeIsObject(readResult)) {
|
---|
| 3933 | throw new TypeError('The promise returned by the reader.read() method must fulfill with an object');
|
---|
| 3934 | }
|
---|
| 3935 | if (readResult.done) {
|
---|
| 3936 | ReadableStreamDefaultControllerClose(stream._readableStreamController);
|
---|
| 3937 | }
|
---|
| 3938 | else {
|
---|
| 3939 | var value = readResult.value;
|
---|
| 3940 | ReadableStreamDefaultControllerEnqueue(stream._readableStreamController, value);
|
---|
| 3941 | }
|
---|
| 3942 | });
|
---|
| 3943 | }
|
---|
| 3944 | function cancelAlgorithm(reason) {
|
---|
| 3945 | try {
|
---|
| 3946 | return promiseResolvedWith(reader.cancel(reason));
|
---|
| 3947 | }
|
---|
| 3948 | catch (e) {
|
---|
| 3949 | return promiseRejectedWith(e);
|
---|
| 3950 | }
|
---|
| 3951 | }
|
---|
| 3952 | stream = CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, 0);
|
---|
| 3953 | return stream;
|
---|
| 3954 | }
|
---|
| 3955 |
|
---|
| 3956 | function convertUnderlyingDefaultOrByteSource(source, context) {
|
---|
| 3957 | assertDictionary(source, context);
|
---|
| 3958 | var original = source;
|
---|
| 3959 | var autoAllocateChunkSize = original === null || original === void 0 ? void 0 : original.autoAllocateChunkSize;
|
---|
| 3960 | var cancel = original === null || original === void 0 ? void 0 : original.cancel;
|
---|
| 3961 | var pull = original === null || original === void 0 ? void 0 : original.pull;
|
---|
| 3962 | var start = original === null || original === void 0 ? void 0 : original.start;
|
---|
| 3963 | var type = original === null || original === void 0 ? void 0 : original.type;
|
---|
| 3964 | return {
|
---|
| 3965 | autoAllocateChunkSize: autoAllocateChunkSize === undefined ?
|
---|
| 3966 | undefined :
|
---|
| 3967 | convertUnsignedLongLongWithEnforceRange(autoAllocateChunkSize, "".concat(context, " has member 'autoAllocateChunkSize' that")),
|
---|
| 3968 | cancel: cancel === undefined ?
|
---|
| 3969 | undefined :
|
---|
| 3970 | convertUnderlyingSourceCancelCallback(cancel, original, "".concat(context, " has member 'cancel' that")),
|
---|
| 3971 | pull: pull === undefined ?
|
---|
| 3972 | undefined :
|
---|
| 3973 | convertUnderlyingSourcePullCallback(pull, original, "".concat(context, " has member 'pull' that")),
|
---|
| 3974 | start: start === undefined ?
|
---|
| 3975 | undefined :
|
---|
| 3976 | convertUnderlyingSourceStartCallback(start, original, "".concat(context, " has member 'start' that")),
|
---|
| 3977 | type: type === undefined ? undefined : convertReadableStreamType(type, "".concat(context, " has member 'type' that"))
|
---|
| 3978 | };
|
---|
| 3979 | }
|
---|
| 3980 | function convertUnderlyingSourceCancelCallback(fn, original, context) {
|
---|
| 3981 | assertFunction(fn, context);
|
---|
| 3982 | return function (reason) { return promiseCall(fn, original, [reason]); };
|
---|
| 3983 | }
|
---|
| 3984 | function convertUnderlyingSourcePullCallback(fn, original, context) {
|
---|
| 3985 | assertFunction(fn, context);
|
---|
| 3986 | return function (controller) { return promiseCall(fn, original, [controller]); };
|
---|
| 3987 | }
|
---|
| 3988 | function convertUnderlyingSourceStartCallback(fn, original, context) {
|
---|
| 3989 | assertFunction(fn, context);
|
---|
| 3990 | return function (controller) { return reflectCall(fn, original, [controller]); };
|
---|
| 3991 | }
|
---|
| 3992 | function convertReadableStreamType(type, context) {
|
---|
| 3993 | type = "".concat(type);
|
---|
| 3994 | if (type !== 'bytes') {
|
---|
| 3995 | throw new TypeError("".concat(context, " '").concat(type, "' is not a valid enumeration value for ReadableStreamType"));
|
---|
| 3996 | }
|
---|
| 3997 | return type;
|
---|
| 3998 | }
|
---|
| 3999 |
|
---|
| 4000 | function convertIteratorOptions(options, context) {
|
---|
| 4001 | assertDictionary(options, context);
|
---|
| 4002 | var preventCancel = options === null || options === void 0 ? void 0 : options.preventCancel;
|
---|
| 4003 | return { preventCancel: Boolean(preventCancel) };
|
---|
| 4004 | }
|
---|
| 4005 |
|
---|
| 4006 | function convertPipeOptions(options, context) {
|
---|
| 4007 | assertDictionary(options, context);
|
---|
| 4008 | var preventAbort = options === null || options === void 0 ? void 0 : options.preventAbort;
|
---|
| 4009 | var preventCancel = options === null || options === void 0 ? void 0 : options.preventCancel;
|
---|
| 4010 | var preventClose = options === null || options === void 0 ? void 0 : options.preventClose;
|
---|
| 4011 | var signal = options === null || options === void 0 ? void 0 : options.signal;
|
---|
| 4012 | if (signal !== undefined) {
|
---|
| 4013 | assertAbortSignal(signal, "".concat(context, " has member 'signal' that"));
|
---|
| 4014 | }
|
---|
| 4015 | return {
|
---|
| 4016 | preventAbort: Boolean(preventAbort),
|
---|
| 4017 | preventCancel: Boolean(preventCancel),
|
---|
| 4018 | preventClose: Boolean(preventClose),
|
---|
| 4019 | signal: signal
|
---|
| 4020 | };
|
---|
| 4021 | }
|
---|
| 4022 | function assertAbortSignal(signal, context) {
|
---|
| 4023 | if (!isAbortSignal(signal)) {
|
---|
| 4024 | throw new TypeError("".concat(context, " is not an AbortSignal."));
|
---|
| 4025 | }
|
---|
| 4026 | }
|
---|
| 4027 |
|
---|
| 4028 | function convertReadableWritablePair(pair, context) {
|
---|
| 4029 | assertDictionary(pair, context);
|
---|
| 4030 | var readable = pair === null || pair === void 0 ? void 0 : pair.readable;
|
---|
| 4031 | assertRequiredField(readable, 'readable', 'ReadableWritablePair');
|
---|
| 4032 | assertReadableStream(readable, "".concat(context, " has member 'readable' that"));
|
---|
| 4033 | var writable = pair === null || pair === void 0 ? void 0 : pair.writable;
|
---|
| 4034 | assertRequiredField(writable, 'writable', 'ReadableWritablePair');
|
---|
| 4035 | assertWritableStream(writable, "".concat(context, " has member 'writable' that"));
|
---|
| 4036 | return { readable: readable, writable: writable };
|
---|
| 4037 | }
|
---|
| 4038 |
|
---|
| 4039 | /**
|
---|
| 4040 | * A readable stream represents a source of data, from which you can read.
|
---|
| 4041 | *
|
---|
| 4042 | * @public
|
---|
| 4043 | */
|
---|
| 4044 | var ReadableStream = /** @class */ (function () {
|
---|
| 4045 | function ReadableStream(rawUnderlyingSource, rawStrategy) {
|
---|
| 4046 | if (rawUnderlyingSource === void 0) { rawUnderlyingSource = {}; }
|
---|
| 4047 | if (rawStrategy === void 0) { rawStrategy = {}; }
|
---|
| 4048 | if (rawUnderlyingSource === undefined) {
|
---|
| 4049 | rawUnderlyingSource = null;
|
---|
| 4050 | }
|
---|
| 4051 | else {
|
---|
| 4052 | assertObject(rawUnderlyingSource, 'First parameter');
|
---|
| 4053 | }
|
---|
| 4054 | var strategy = convertQueuingStrategy(rawStrategy, 'Second parameter');
|
---|
| 4055 | var underlyingSource = convertUnderlyingDefaultOrByteSource(rawUnderlyingSource, 'First parameter');
|
---|
| 4056 | InitializeReadableStream(this);
|
---|
| 4057 | if (underlyingSource.type === 'bytes') {
|
---|
| 4058 | if (strategy.size !== undefined) {
|
---|
| 4059 | throw new RangeError('The strategy for a byte stream cannot have a size function');
|
---|
| 4060 | }
|
---|
| 4061 | var highWaterMark = ExtractHighWaterMark(strategy, 0);
|
---|
| 4062 | SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, highWaterMark);
|
---|
| 4063 | }
|
---|
| 4064 | else {
|
---|
| 4065 | var sizeAlgorithm = ExtractSizeAlgorithm(strategy);
|
---|
| 4066 | var highWaterMark = ExtractHighWaterMark(strategy, 1);
|
---|
| 4067 | SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, highWaterMark, sizeAlgorithm);
|
---|
| 4068 | }
|
---|
| 4069 | }
|
---|
| 4070 | Object.defineProperty(ReadableStream.prototype, "locked", {
|
---|
| 4071 | /**
|
---|
| 4072 | * Whether or not the readable stream is locked to a {@link ReadableStreamDefaultReader | reader}.
|
---|
| 4073 | */
|
---|
| 4074 | get: function () {
|
---|
| 4075 | if (!IsReadableStream(this)) {
|
---|
| 4076 | throw streamBrandCheckException$1('locked');
|
---|
| 4077 | }
|
---|
| 4078 | return IsReadableStreamLocked(this);
|
---|
| 4079 | },
|
---|
| 4080 | enumerable: false,
|
---|
| 4081 | configurable: true
|
---|
| 4082 | });
|
---|
| 4083 | /**
|
---|
| 4084 | * Cancels the stream, signaling a loss of interest in the stream by a consumer.
|
---|
| 4085 | *
|
---|
| 4086 | * The supplied `reason` argument will be given to the underlying source's {@link UnderlyingSource.cancel | cancel()}
|
---|
| 4087 | * method, which might or might not use it.
|
---|
| 4088 | */
|
---|
| 4089 | ReadableStream.prototype.cancel = function (reason) {
|
---|
| 4090 | if (reason === void 0) { reason = undefined; }
|
---|
| 4091 | if (!IsReadableStream(this)) {
|
---|
| 4092 | return promiseRejectedWith(streamBrandCheckException$1('cancel'));
|
---|
| 4093 | }
|
---|
| 4094 | if (IsReadableStreamLocked(this)) {
|
---|
| 4095 | return promiseRejectedWith(new TypeError('Cannot cancel a stream that already has a reader'));
|
---|
| 4096 | }
|
---|
| 4097 | return ReadableStreamCancel(this, reason);
|
---|
| 4098 | };
|
---|
| 4099 | ReadableStream.prototype.getReader = function (rawOptions) {
|
---|
| 4100 | if (rawOptions === void 0) { rawOptions = undefined; }
|
---|
| 4101 | if (!IsReadableStream(this)) {
|
---|
| 4102 | throw streamBrandCheckException$1('getReader');
|
---|
| 4103 | }
|
---|
| 4104 | var options = convertReaderOptions(rawOptions, 'First parameter');
|
---|
| 4105 | if (options.mode === undefined) {
|
---|
| 4106 | return AcquireReadableStreamDefaultReader(this);
|
---|
| 4107 | }
|
---|
| 4108 | return AcquireReadableStreamBYOBReader(this);
|
---|
| 4109 | };
|
---|
| 4110 | ReadableStream.prototype.pipeThrough = function (rawTransform, rawOptions) {
|
---|
| 4111 | if (rawOptions === void 0) { rawOptions = {}; }
|
---|
| 4112 | if (!IsReadableStream(this)) {
|
---|
| 4113 | throw streamBrandCheckException$1('pipeThrough');
|
---|
| 4114 | }
|
---|
| 4115 | assertRequiredArgument(rawTransform, 1, 'pipeThrough');
|
---|
| 4116 | var transform = convertReadableWritablePair(rawTransform, 'First parameter');
|
---|
| 4117 | var options = convertPipeOptions(rawOptions, 'Second parameter');
|
---|
| 4118 | if (IsReadableStreamLocked(this)) {
|
---|
| 4119 | throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked ReadableStream');
|
---|
| 4120 | }
|
---|
| 4121 | if (IsWritableStreamLocked(transform.writable)) {
|
---|
| 4122 | throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked WritableStream');
|
---|
| 4123 | }
|
---|
| 4124 | var promise = ReadableStreamPipeTo(this, transform.writable, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
|
---|
| 4125 | setPromiseIsHandledToTrue(promise);
|
---|
| 4126 | return transform.readable;
|
---|
| 4127 | };
|
---|
| 4128 | ReadableStream.prototype.pipeTo = function (destination, rawOptions) {
|
---|
| 4129 | if (rawOptions === void 0) { rawOptions = {}; }
|
---|
| 4130 | if (!IsReadableStream(this)) {
|
---|
| 4131 | return promiseRejectedWith(streamBrandCheckException$1('pipeTo'));
|
---|
| 4132 | }
|
---|
| 4133 | if (destination === undefined) {
|
---|
| 4134 | return promiseRejectedWith("Parameter 1 is required in 'pipeTo'.");
|
---|
| 4135 | }
|
---|
| 4136 | if (!IsWritableStream(destination)) {
|
---|
| 4137 | return promiseRejectedWith(new TypeError("ReadableStream.prototype.pipeTo's first argument must be a WritableStream"));
|
---|
| 4138 | }
|
---|
| 4139 | var options;
|
---|
| 4140 | try {
|
---|
| 4141 | options = convertPipeOptions(rawOptions, 'Second parameter');
|
---|
| 4142 | }
|
---|
| 4143 | catch (e) {
|
---|
| 4144 | return promiseRejectedWith(e);
|
---|
| 4145 | }
|
---|
| 4146 | if (IsReadableStreamLocked(this)) {
|
---|
| 4147 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked ReadableStream'));
|
---|
| 4148 | }
|
---|
| 4149 | if (IsWritableStreamLocked(destination)) {
|
---|
| 4150 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked WritableStream'));
|
---|
| 4151 | }
|
---|
| 4152 | return ReadableStreamPipeTo(this, destination, options.preventClose, options.preventAbort, options.preventCancel, options.signal);
|
---|
| 4153 | };
|
---|
| 4154 | /**
|
---|
| 4155 | * Tees this readable stream, returning a two-element array containing the two resulting branches as
|
---|
| 4156 | * new {@link ReadableStream} instances.
|
---|
| 4157 | *
|
---|
| 4158 | * Teeing a stream will lock it, preventing any other consumer from acquiring a reader.
|
---|
| 4159 | * To cancel the stream, cancel both of the resulting branches; a composite cancellation reason will then be
|
---|
| 4160 | * propagated to the stream's underlying source.
|
---|
| 4161 | *
|
---|
| 4162 | * Note that the chunks seen in each branch will be the same object. If the chunks are not immutable,
|
---|
| 4163 | * this could allow interference between the two branches.
|
---|
| 4164 | */
|
---|
| 4165 | ReadableStream.prototype.tee = function () {
|
---|
| 4166 | if (!IsReadableStream(this)) {
|
---|
| 4167 | throw streamBrandCheckException$1('tee');
|
---|
| 4168 | }
|
---|
| 4169 | var branches = ReadableStreamTee(this);
|
---|
| 4170 | return CreateArrayFromList(branches);
|
---|
| 4171 | };
|
---|
| 4172 | ReadableStream.prototype.values = function (rawOptions) {
|
---|
| 4173 | if (rawOptions === void 0) { rawOptions = undefined; }
|
---|
| 4174 | if (!IsReadableStream(this)) {
|
---|
| 4175 | throw streamBrandCheckException$1('values');
|
---|
| 4176 | }
|
---|
| 4177 | var options = convertIteratorOptions(rawOptions, 'First parameter');
|
---|
| 4178 | return AcquireReadableStreamAsyncIterator(this, options.preventCancel);
|
---|
| 4179 | };
|
---|
| 4180 | /**
|
---|
| 4181 | * Creates a new ReadableStream wrapping the provided iterable or async iterable.
|
---|
| 4182 | *
|
---|
| 4183 | * This can be used to adapt various kinds of objects into a readable stream,
|
---|
| 4184 | * such as an array, an async generator, or a Node.js readable stream.
|
---|
| 4185 | */
|
---|
| 4186 | ReadableStream.from = function (asyncIterable) {
|
---|
| 4187 | return ReadableStreamFrom(asyncIterable);
|
---|
| 4188 | };
|
---|
| 4189 | return ReadableStream;
|
---|
| 4190 | }());
|
---|
| 4191 | Object.defineProperties(ReadableStream, {
|
---|
| 4192 | from: { enumerable: true }
|
---|
| 4193 | });
|
---|
| 4194 | Object.defineProperties(ReadableStream.prototype, {
|
---|
| 4195 | cancel: { enumerable: true },
|
---|
| 4196 | getReader: { enumerable: true },
|
---|
| 4197 | pipeThrough: { enumerable: true },
|
---|
| 4198 | pipeTo: { enumerable: true },
|
---|
| 4199 | tee: { enumerable: true },
|
---|
| 4200 | values: { enumerable: true },
|
---|
| 4201 | locked: { enumerable: true }
|
---|
| 4202 | });
|
---|
| 4203 | setFunctionName(ReadableStream.from, 'from');
|
---|
| 4204 | setFunctionName(ReadableStream.prototype.cancel, 'cancel');
|
---|
| 4205 | setFunctionName(ReadableStream.prototype.getReader, 'getReader');
|
---|
| 4206 | setFunctionName(ReadableStream.prototype.pipeThrough, 'pipeThrough');
|
---|
| 4207 | setFunctionName(ReadableStream.prototype.pipeTo, 'pipeTo');
|
---|
| 4208 | setFunctionName(ReadableStream.prototype.tee, 'tee');
|
---|
| 4209 | setFunctionName(ReadableStream.prototype.values, 'values');
|
---|
| 4210 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 4211 | Object.defineProperty(ReadableStream.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 4212 | value: 'ReadableStream',
|
---|
| 4213 | configurable: true
|
---|
| 4214 | });
|
---|
| 4215 | }
|
---|
| 4216 | if (typeof SymbolPolyfill.asyncIterator === 'symbol') {
|
---|
| 4217 | Object.defineProperty(ReadableStream.prototype, SymbolPolyfill.asyncIterator, {
|
---|
| 4218 | value: ReadableStream.prototype.values,
|
---|
| 4219 | writable: true,
|
---|
| 4220 | configurable: true
|
---|
| 4221 | });
|
---|
| 4222 | }
|
---|
| 4223 | // Abstract operations for the ReadableStream.
|
---|
| 4224 | // Throws if and only if startAlgorithm throws.
|
---|
| 4225 | function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm) {
|
---|
| 4226 | if (highWaterMark === void 0) { highWaterMark = 1; }
|
---|
| 4227 | if (sizeAlgorithm === void 0) { sizeAlgorithm = function () { return 1; }; }
|
---|
| 4228 | var stream = Object.create(ReadableStream.prototype);
|
---|
| 4229 | InitializeReadableStream(stream);
|
---|
| 4230 | var controller = Object.create(ReadableStreamDefaultController.prototype);
|
---|
| 4231 | SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
|
---|
| 4232 | return stream;
|
---|
| 4233 | }
|
---|
| 4234 | // Throws if and only if startAlgorithm throws.
|
---|
| 4235 | function CreateReadableByteStream(startAlgorithm, pullAlgorithm, cancelAlgorithm) {
|
---|
| 4236 | var stream = Object.create(ReadableStream.prototype);
|
---|
| 4237 | InitializeReadableStream(stream);
|
---|
| 4238 | var controller = Object.create(ReadableByteStreamController.prototype);
|
---|
| 4239 | SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, 0, undefined);
|
---|
| 4240 | return stream;
|
---|
| 4241 | }
|
---|
| 4242 | function InitializeReadableStream(stream) {
|
---|
| 4243 | stream._state = 'readable';
|
---|
| 4244 | stream._reader = undefined;
|
---|
| 4245 | stream._storedError = undefined;
|
---|
| 4246 | stream._disturbed = false;
|
---|
| 4247 | }
|
---|
| 4248 | function IsReadableStream(x) {
|
---|
| 4249 | if (!typeIsObject(x)) {
|
---|
| 4250 | return false;
|
---|
| 4251 | }
|
---|
| 4252 | if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
|
---|
| 4253 | return false;
|
---|
| 4254 | }
|
---|
| 4255 | return x instanceof ReadableStream;
|
---|
| 4256 | }
|
---|
| 4257 | function IsReadableStreamLocked(stream) {
|
---|
| 4258 | if (stream._reader === undefined) {
|
---|
| 4259 | return false;
|
---|
| 4260 | }
|
---|
| 4261 | return true;
|
---|
| 4262 | }
|
---|
| 4263 | // ReadableStream API exposed for controllers.
|
---|
| 4264 | function ReadableStreamCancel(stream, reason) {
|
---|
| 4265 | stream._disturbed = true;
|
---|
| 4266 | if (stream._state === 'closed') {
|
---|
| 4267 | return promiseResolvedWith(undefined);
|
---|
| 4268 | }
|
---|
| 4269 | if (stream._state === 'errored') {
|
---|
| 4270 | return promiseRejectedWith(stream._storedError);
|
---|
| 4271 | }
|
---|
| 4272 | ReadableStreamClose(stream);
|
---|
| 4273 | var reader = stream._reader;
|
---|
| 4274 | if (reader !== undefined && IsReadableStreamBYOBReader(reader)) {
|
---|
| 4275 | var readIntoRequests = reader._readIntoRequests;
|
---|
| 4276 | reader._readIntoRequests = new SimpleQueue();
|
---|
| 4277 | readIntoRequests.forEach(function (readIntoRequest) {
|
---|
| 4278 | readIntoRequest._closeSteps(undefined);
|
---|
| 4279 | });
|
---|
| 4280 | }
|
---|
| 4281 | var sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
|
---|
| 4282 | return transformPromiseWith(sourceCancelPromise, noop);
|
---|
| 4283 | }
|
---|
| 4284 | function ReadableStreamClose(stream) {
|
---|
| 4285 | stream._state = 'closed';
|
---|
| 4286 | var reader = stream._reader;
|
---|
| 4287 | if (reader === undefined) {
|
---|
| 4288 | return;
|
---|
| 4289 | }
|
---|
| 4290 | defaultReaderClosedPromiseResolve(reader);
|
---|
| 4291 | if (IsReadableStreamDefaultReader(reader)) {
|
---|
| 4292 | var readRequests = reader._readRequests;
|
---|
| 4293 | reader._readRequests = new SimpleQueue();
|
---|
| 4294 | readRequests.forEach(function (readRequest) {
|
---|
| 4295 | readRequest._closeSteps();
|
---|
| 4296 | });
|
---|
| 4297 | }
|
---|
| 4298 | }
|
---|
| 4299 | function ReadableStreamError(stream, e) {
|
---|
| 4300 | stream._state = 'errored';
|
---|
| 4301 | stream._storedError = e;
|
---|
| 4302 | var reader = stream._reader;
|
---|
| 4303 | if (reader === undefined) {
|
---|
| 4304 | return;
|
---|
| 4305 | }
|
---|
| 4306 | defaultReaderClosedPromiseReject(reader, e);
|
---|
| 4307 | if (IsReadableStreamDefaultReader(reader)) {
|
---|
| 4308 | ReadableStreamDefaultReaderErrorReadRequests(reader, e);
|
---|
| 4309 | }
|
---|
| 4310 | else {
|
---|
| 4311 | ReadableStreamBYOBReaderErrorReadIntoRequests(reader, e);
|
---|
| 4312 | }
|
---|
| 4313 | }
|
---|
| 4314 | // Helper functions for the ReadableStream.
|
---|
| 4315 | function streamBrandCheckException$1(name) {
|
---|
| 4316 | return new TypeError("ReadableStream.prototype.".concat(name, " can only be used on a ReadableStream"));
|
---|
| 4317 | }
|
---|
| 4318 |
|
---|
| 4319 | function convertQueuingStrategyInit(init, context) {
|
---|
| 4320 | assertDictionary(init, context);
|
---|
| 4321 | var highWaterMark = init === null || init === void 0 ? void 0 : init.highWaterMark;
|
---|
| 4322 | assertRequiredField(highWaterMark, 'highWaterMark', 'QueuingStrategyInit');
|
---|
| 4323 | return {
|
---|
| 4324 | highWaterMark: convertUnrestrictedDouble(highWaterMark)
|
---|
| 4325 | };
|
---|
| 4326 | }
|
---|
| 4327 |
|
---|
| 4328 | // The size function must not have a prototype property nor be a constructor
|
---|
| 4329 | var byteLengthSizeFunction = function (chunk) {
|
---|
| 4330 | return chunk.byteLength;
|
---|
| 4331 | };
|
---|
| 4332 | setFunctionName(byteLengthSizeFunction, 'size');
|
---|
| 4333 | /**
|
---|
| 4334 | * A queuing strategy that counts the number of bytes in each chunk.
|
---|
| 4335 | *
|
---|
| 4336 | * @public
|
---|
| 4337 | */
|
---|
| 4338 | var ByteLengthQueuingStrategy = /** @class */ (function () {
|
---|
| 4339 | function ByteLengthQueuingStrategy(options) {
|
---|
| 4340 | assertRequiredArgument(options, 1, 'ByteLengthQueuingStrategy');
|
---|
| 4341 | options = convertQueuingStrategyInit(options, 'First parameter');
|
---|
| 4342 | this._byteLengthQueuingStrategyHighWaterMark = options.highWaterMark;
|
---|
| 4343 | }
|
---|
| 4344 | Object.defineProperty(ByteLengthQueuingStrategy.prototype, "highWaterMark", {
|
---|
| 4345 | /**
|
---|
| 4346 | * Returns the high water mark provided to the constructor.
|
---|
| 4347 | */
|
---|
| 4348 | get: function () {
|
---|
| 4349 | if (!IsByteLengthQueuingStrategy(this)) {
|
---|
| 4350 | throw byteLengthBrandCheckException('highWaterMark');
|
---|
| 4351 | }
|
---|
| 4352 | return this._byteLengthQueuingStrategyHighWaterMark;
|
---|
| 4353 | },
|
---|
| 4354 | enumerable: false,
|
---|
| 4355 | configurable: true
|
---|
| 4356 | });
|
---|
| 4357 | Object.defineProperty(ByteLengthQueuingStrategy.prototype, "size", {
|
---|
| 4358 | /**
|
---|
| 4359 | * Measures the size of `chunk` by returning the value of its `byteLength` property.
|
---|
| 4360 | */
|
---|
| 4361 | get: function () {
|
---|
| 4362 | if (!IsByteLengthQueuingStrategy(this)) {
|
---|
| 4363 | throw byteLengthBrandCheckException('size');
|
---|
| 4364 | }
|
---|
| 4365 | return byteLengthSizeFunction;
|
---|
| 4366 | },
|
---|
| 4367 | enumerable: false,
|
---|
| 4368 | configurable: true
|
---|
| 4369 | });
|
---|
| 4370 | return ByteLengthQueuingStrategy;
|
---|
| 4371 | }());
|
---|
| 4372 | Object.defineProperties(ByteLengthQueuingStrategy.prototype, {
|
---|
| 4373 | highWaterMark: { enumerable: true },
|
---|
| 4374 | size: { enumerable: true }
|
---|
| 4375 | });
|
---|
| 4376 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 4377 | Object.defineProperty(ByteLengthQueuingStrategy.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 4378 | value: 'ByteLengthQueuingStrategy',
|
---|
| 4379 | configurable: true
|
---|
| 4380 | });
|
---|
| 4381 | }
|
---|
| 4382 | // Helper functions for the ByteLengthQueuingStrategy.
|
---|
| 4383 | function byteLengthBrandCheckException(name) {
|
---|
| 4384 | return new TypeError("ByteLengthQueuingStrategy.prototype.".concat(name, " can only be used on a ByteLengthQueuingStrategy"));
|
---|
| 4385 | }
|
---|
| 4386 | function IsByteLengthQueuingStrategy(x) {
|
---|
| 4387 | if (!typeIsObject(x)) {
|
---|
| 4388 | return false;
|
---|
| 4389 | }
|
---|
| 4390 | if (!Object.prototype.hasOwnProperty.call(x, '_byteLengthQueuingStrategyHighWaterMark')) {
|
---|
| 4391 | return false;
|
---|
| 4392 | }
|
---|
| 4393 | return x instanceof ByteLengthQueuingStrategy;
|
---|
| 4394 | }
|
---|
| 4395 |
|
---|
| 4396 | // The size function must not have a prototype property nor be a constructor
|
---|
| 4397 | var countSizeFunction = function () {
|
---|
| 4398 | return 1;
|
---|
| 4399 | };
|
---|
| 4400 | setFunctionName(countSizeFunction, 'size');
|
---|
| 4401 | /**
|
---|
| 4402 | * A queuing strategy that counts the number of chunks.
|
---|
| 4403 | *
|
---|
| 4404 | * @public
|
---|
| 4405 | */
|
---|
| 4406 | var CountQueuingStrategy = /** @class */ (function () {
|
---|
| 4407 | function CountQueuingStrategy(options) {
|
---|
| 4408 | assertRequiredArgument(options, 1, 'CountQueuingStrategy');
|
---|
| 4409 | options = convertQueuingStrategyInit(options, 'First parameter');
|
---|
| 4410 | this._countQueuingStrategyHighWaterMark = options.highWaterMark;
|
---|
| 4411 | }
|
---|
| 4412 | Object.defineProperty(CountQueuingStrategy.prototype, "highWaterMark", {
|
---|
| 4413 | /**
|
---|
| 4414 | * Returns the high water mark provided to the constructor.
|
---|
| 4415 | */
|
---|
| 4416 | get: function () {
|
---|
| 4417 | if (!IsCountQueuingStrategy(this)) {
|
---|
| 4418 | throw countBrandCheckException('highWaterMark');
|
---|
| 4419 | }
|
---|
| 4420 | return this._countQueuingStrategyHighWaterMark;
|
---|
| 4421 | },
|
---|
| 4422 | enumerable: false,
|
---|
| 4423 | configurable: true
|
---|
| 4424 | });
|
---|
| 4425 | Object.defineProperty(CountQueuingStrategy.prototype, "size", {
|
---|
| 4426 | /**
|
---|
| 4427 | * Measures the size of `chunk` by always returning 1.
|
---|
| 4428 | * This ensures that the total queue size is a count of the number of chunks in the queue.
|
---|
| 4429 | */
|
---|
| 4430 | get: function () {
|
---|
| 4431 | if (!IsCountQueuingStrategy(this)) {
|
---|
| 4432 | throw countBrandCheckException('size');
|
---|
| 4433 | }
|
---|
| 4434 | return countSizeFunction;
|
---|
| 4435 | },
|
---|
| 4436 | enumerable: false,
|
---|
| 4437 | configurable: true
|
---|
| 4438 | });
|
---|
| 4439 | return CountQueuingStrategy;
|
---|
| 4440 | }());
|
---|
| 4441 | Object.defineProperties(CountQueuingStrategy.prototype, {
|
---|
| 4442 | highWaterMark: { enumerable: true },
|
---|
| 4443 | size: { enumerable: true }
|
---|
| 4444 | });
|
---|
| 4445 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 4446 | Object.defineProperty(CountQueuingStrategy.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 4447 | value: 'CountQueuingStrategy',
|
---|
| 4448 | configurable: true
|
---|
| 4449 | });
|
---|
| 4450 | }
|
---|
| 4451 | // Helper functions for the CountQueuingStrategy.
|
---|
| 4452 | function countBrandCheckException(name) {
|
---|
| 4453 | return new TypeError("CountQueuingStrategy.prototype.".concat(name, " can only be used on a CountQueuingStrategy"));
|
---|
| 4454 | }
|
---|
| 4455 | function IsCountQueuingStrategy(x) {
|
---|
| 4456 | if (!typeIsObject(x)) {
|
---|
| 4457 | return false;
|
---|
| 4458 | }
|
---|
| 4459 | if (!Object.prototype.hasOwnProperty.call(x, '_countQueuingStrategyHighWaterMark')) {
|
---|
| 4460 | return false;
|
---|
| 4461 | }
|
---|
| 4462 | return x instanceof CountQueuingStrategy;
|
---|
| 4463 | }
|
---|
| 4464 |
|
---|
| 4465 | function convertTransformer(original, context) {
|
---|
| 4466 | assertDictionary(original, context);
|
---|
| 4467 | var cancel = original === null || original === void 0 ? void 0 : original.cancel;
|
---|
| 4468 | var flush = original === null || original === void 0 ? void 0 : original.flush;
|
---|
| 4469 | var readableType = original === null || original === void 0 ? void 0 : original.readableType;
|
---|
| 4470 | var start = original === null || original === void 0 ? void 0 : original.start;
|
---|
| 4471 | var transform = original === null || original === void 0 ? void 0 : original.transform;
|
---|
| 4472 | var writableType = original === null || original === void 0 ? void 0 : original.writableType;
|
---|
| 4473 | return {
|
---|
| 4474 | cancel: cancel === undefined ?
|
---|
| 4475 | undefined :
|
---|
| 4476 | convertTransformerCancelCallback(cancel, original, "".concat(context, " has member 'cancel' that")),
|
---|
| 4477 | flush: flush === undefined ?
|
---|
| 4478 | undefined :
|
---|
| 4479 | convertTransformerFlushCallback(flush, original, "".concat(context, " has member 'flush' that")),
|
---|
| 4480 | readableType: readableType,
|
---|
| 4481 | start: start === undefined ?
|
---|
| 4482 | undefined :
|
---|
| 4483 | convertTransformerStartCallback(start, original, "".concat(context, " has member 'start' that")),
|
---|
| 4484 | transform: transform === undefined ?
|
---|
| 4485 | undefined :
|
---|
| 4486 | convertTransformerTransformCallback(transform, original, "".concat(context, " has member 'transform' that")),
|
---|
| 4487 | writableType: writableType
|
---|
| 4488 | };
|
---|
| 4489 | }
|
---|
| 4490 | function convertTransformerFlushCallback(fn, original, context) {
|
---|
| 4491 | assertFunction(fn, context);
|
---|
| 4492 | return function (controller) { return promiseCall(fn, original, [controller]); };
|
---|
| 4493 | }
|
---|
| 4494 | function convertTransformerStartCallback(fn, original, context) {
|
---|
| 4495 | assertFunction(fn, context);
|
---|
| 4496 | return function (controller) { return reflectCall(fn, original, [controller]); };
|
---|
| 4497 | }
|
---|
| 4498 | function convertTransformerTransformCallback(fn, original, context) {
|
---|
| 4499 | assertFunction(fn, context);
|
---|
| 4500 | return function (chunk, controller) { return promiseCall(fn, original, [chunk, controller]); };
|
---|
| 4501 | }
|
---|
| 4502 | function convertTransformerCancelCallback(fn, original, context) {
|
---|
| 4503 | assertFunction(fn, context);
|
---|
| 4504 | return function (reason) { return promiseCall(fn, original, [reason]); };
|
---|
| 4505 | }
|
---|
| 4506 |
|
---|
| 4507 | // Class TransformStream
|
---|
| 4508 | /**
|
---|
| 4509 | * A transform stream consists of a pair of streams: a {@link WritableStream | writable stream},
|
---|
| 4510 | * known as its writable side, and a {@link ReadableStream | readable stream}, known as its readable side.
|
---|
| 4511 | * In a manner specific to the transform stream in question, writes to the writable side result in new data being
|
---|
| 4512 | * made available for reading from the readable side.
|
---|
| 4513 | *
|
---|
| 4514 | * @public
|
---|
| 4515 | */
|
---|
| 4516 | var TransformStream = /** @class */ (function () {
|
---|
| 4517 | function TransformStream(rawTransformer, rawWritableStrategy, rawReadableStrategy) {
|
---|
| 4518 | if (rawTransformer === void 0) { rawTransformer = {}; }
|
---|
| 4519 | if (rawWritableStrategy === void 0) { rawWritableStrategy = {}; }
|
---|
| 4520 | if (rawReadableStrategy === void 0) { rawReadableStrategy = {}; }
|
---|
| 4521 | if (rawTransformer === undefined) {
|
---|
| 4522 | rawTransformer = null;
|
---|
| 4523 | }
|
---|
| 4524 | var writableStrategy = convertQueuingStrategy(rawWritableStrategy, 'Second parameter');
|
---|
| 4525 | var readableStrategy = convertQueuingStrategy(rawReadableStrategy, 'Third parameter');
|
---|
| 4526 | var transformer = convertTransformer(rawTransformer, 'First parameter');
|
---|
| 4527 | if (transformer.readableType !== undefined) {
|
---|
| 4528 | throw new RangeError('Invalid readableType specified');
|
---|
| 4529 | }
|
---|
| 4530 | if (transformer.writableType !== undefined) {
|
---|
| 4531 | throw new RangeError('Invalid writableType specified');
|
---|
| 4532 | }
|
---|
| 4533 | var readableHighWaterMark = ExtractHighWaterMark(readableStrategy, 0);
|
---|
| 4534 | var readableSizeAlgorithm = ExtractSizeAlgorithm(readableStrategy);
|
---|
| 4535 | var writableHighWaterMark = ExtractHighWaterMark(writableStrategy, 1);
|
---|
| 4536 | var writableSizeAlgorithm = ExtractSizeAlgorithm(writableStrategy);
|
---|
| 4537 | var startPromise_resolve;
|
---|
| 4538 | var startPromise = newPromise(function (resolve) {
|
---|
| 4539 | startPromise_resolve = resolve;
|
---|
| 4540 | });
|
---|
| 4541 | InitializeTransformStream(this, startPromise, writableHighWaterMark, writableSizeAlgorithm, readableHighWaterMark, readableSizeAlgorithm);
|
---|
| 4542 | SetUpTransformStreamDefaultControllerFromTransformer(this, transformer);
|
---|
| 4543 | if (transformer.start !== undefined) {
|
---|
| 4544 | startPromise_resolve(transformer.start(this._transformStreamController));
|
---|
| 4545 | }
|
---|
| 4546 | else {
|
---|
| 4547 | startPromise_resolve(undefined);
|
---|
| 4548 | }
|
---|
| 4549 | }
|
---|
| 4550 | Object.defineProperty(TransformStream.prototype, "readable", {
|
---|
| 4551 | /**
|
---|
| 4552 | * The readable side of the transform stream.
|
---|
| 4553 | */
|
---|
| 4554 | get: function () {
|
---|
| 4555 | if (!IsTransformStream(this)) {
|
---|
| 4556 | throw streamBrandCheckException('readable');
|
---|
| 4557 | }
|
---|
| 4558 | return this._readable;
|
---|
| 4559 | },
|
---|
| 4560 | enumerable: false,
|
---|
| 4561 | configurable: true
|
---|
| 4562 | });
|
---|
| 4563 | Object.defineProperty(TransformStream.prototype, "writable", {
|
---|
| 4564 | /**
|
---|
| 4565 | * The writable side of the transform stream.
|
---|
| 4566 | */
|
---|
| 4567 | get: function () {
|
---|
| 4568 | if (!IsTransformStream(this)) {
|
---|
| 4569 | throw streamBrandCheckException('writable');
|
---|
| 4570 | }
|
---|
| 4571 | return this._writable;
|
---|
| 4572 | },
|
---|
| 4573 | enumerable: false,
|
---|
| 4574 | configurable: true
|
---|
| 4575 | });
|
---|
| 4576 | return TransformStream;
|
---|
| 4577 | }());
|
---|
| 4578 | Object.defineProperties(TransformStream.prototype, {
|
---|
| 4579 | readable: { enumerable: true },
|
---|
| 4580 | writable: { enumerable: true }
|
---|
| 4581 | });
|
---|
| 4582 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 4583 | Object.defineProperty(TransformStream.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 4584 | value: 'TransformStream',
|
---|
| 4585 | configurable: true
|
---|
| 4586 | });
|
---|
| 4587 | }
|
---|
| 4588 | function InitializeTransformStream(stream, startPromise, writableHighWaterMark, writableSizeAlgorithm, readableHighWaterMark, readableSizeAlgorithm) {
|
---|
| 4589 | function startAlgorithm() {
|
---|
| 4590 | return startPromise;
|
---|
| 4591 | }
|
---|
| 4592 | function writeAlgorithm(chunk) {
|
---|
| 4593 | return TransformStreamDefaultSinkWriteAlgorithm(stream, chunk);
|
---|
| 4594 | }
|
---|
| 4595 | function abortAlgorithm(reason) {
|
---|
| 4596 | return TransformStreamDefaultSinkAbortAlgorithm(stream, reason);
|
---|
| 4597 | }
|
---|
| 4598 | function closeAlgorithm() {
|
---|
| 4599 | return TransformStreamDefaultSinkCloseAlgorithm(stream);
|
---|
| 4600 | }
|
---|
| 4601 | stream._writable = CreateWritableStream(startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, writableHighWaterMark, writableSizeAlgorithm);
|
---|
| 4602 | function pullAlgorithm() {
|
---|
| 4603 | return TransformStreamDefaultSourcePullAlgorithm(stream);
|
---|
| 4604 | }
|
---|
| 4605 | function cancelAlgorithm(reason) {
|
---|
| 4606 | return TransformStreamDefaultSourceCancelAlgorithm(stream, reason);
|
---|
| 4607 | }
|
---|
| 4608 | stream._readable = CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, readableHighWaterMark, readableSizeAlgorithm);
|
---|
| 4609 | // The [[backpressure]] slot is set to undefined so that it can be initialised by TransformStreamSetBackpressure.
|
---|
| 4610 | stream._backpressure = undefined;
|
---|
| 4611 | stream._backpressureChangePromise = undefined;
|
---|
| 4612 | stream._backpressureChangePromise_resolve = undefined;
|
---|
| 4613 | TransformStreamSetBackpressure(stream, true);
|
---|
| 4614 | stream._transformStreamController = undefined;
|
---|
| 4615 | }
|
---|
| 4616 | function IsTransformStream(x) {
|
---|
| 4617 | if (!typeIsObject(x)) {
|
---|
| 4618 | return false;
|
---|
| 4619 | }
|
---|
| 4620 | if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
|
---|
| 4621 | return false;
|
---|
| 4622 | }
|
---|
| 4623 | return x instanceof TransformStream;
|
---|
| 4624 | }
|
---|
| 4625 | // This is a no-op if both sides are already errored.
|
---|
| 4626 | function TransformStreamError(stream, e) {
|
---|
| 4627 | ReadableStreamDefaultControllerError(stream._readable._readableStreamController, e);
|
---|
| 4628 | TransformStreamErrorWritableAndUnblockWrite(stream, e);
|
---|
| 4629 | }
|
---|
| 4630 | function TransformStreamErrorWritableAndUnblockWrite(stream, e) {
|
---|
| 4631 | TransformStreamDefaultControllerClearAlgorithms(stream._transformStreamController);
|
---|
| 4632 | WritableStreamDefaultControllerErrorIfNeeded(stream._writable._writableStreamController, e);
|
---|
| 4633 | TransformStreamUnblockWrite(stream);
|
---|
| 4634 | }
|
---|
| 4635 | function TransformStreamUnblockWrite(stream) {
|
---|
| 4636 | if (stream._backpressure) {
|
---|
| 4637 | // Pretend that pull() was called to permit any pending write() calls to complete. TransformStreamSetBackpressure()
|
---|
| 4638 | // cannot be called from enqueue() or pull() once the ReadableStream is errored, so this will will be the final time
|
---|
| 4639 | // _backpressure is set.
|
---|
| 4640 | TransformStreamSetBackpressure(stream, false);
|
---|
| 4641 | }
|
---|
| 4642 | }
|
---|
| 4643 | function TransformStreamSetBackpressure(stream, backpressure) {
|
---|
| 4644 | // Passes also when called during construction.
|
---|
| 4645 | if (stream._backpressureChangePromise !== undefined) {
|
---|
| 4646 | stream._backpressureChangePromise_resolve();
|
---|
| 4647 | }
|
---|
| 4648 | stream._backpressureChangePromise = newPromise(function (resolve) {
|
---|
| 4649 | stream._backpressureChangePromise_resolve = resolve;
|
---|
| 4650 | });
|
---|
| 4651 | stream._backpressure = backpressure;
|
---|
| 4652 | }
|
---|
| 4653 | // Class TransformStreamDefaultController
|
---|
| 4654 | /**
|
---|
| 4655 | * Allows control of the {@link ReadableStream} and {@link WritableStream} of the associated {@link TransformStream}.
|
---|
| 4656 | *
|
---|
| 4657 | * @public
|
---|
| 4658 | */
|
---|
| 4659 | var TransformStreamDefaultController = /** @class */ (function () {
|
---|
| 4660 | function TransformStreamDefaultController() {
|
---|
| 4661 | throw new TypeError('Illegal constructor');
|
---|
| 4662 | }
|
---|
| 4663 | Object.defineProperty(TransformStreamDefaultController.prototype, "desiredSize", {
|
---|
| 4664 | /**
|
---|
| 4665 | * Returns the desired size to fill the readable side’s internal queue. It can be negative, if the queue is over-full.
|
---|
| 4666 | */
|
---|
| 4667 | get: function () {
|
---|
| 4668 | if (!IsTransformStreamDefaultController(this)) {
|
---|
| 4669 | throw defaultControllerBrandCheckException('desiredSize');
|
---|
| 4670 | }
|
---|
| 4671 | var readableController = this._controlledTransformStream._readable._readableStreamController;
|
---|
| 4672 | return ReadableStreamDefaultControllerGetDesiredSize(readableController);
|
---|
| 4673 | },
|
---|
| 4674 | enumerable: false,
|
---|
| 4675 | configurable: true
|
---|
| 4676 | });
|
---|
| 4677 | TransformStreamDefaultController.prototype.enqueue = function (chunk) {
|
---|
| 4678 | if (chunk === void 0) { chunk = undefined; }
|
---|
| 4679 | if (!IsTransformStreamDefaultController(this)) {
|
---|
| 4680 | throw defaultControllerBrandCheckException('enqueue');
|
---|
| 4681 | }
|
---|
| 4682 | TransformStreamDefaultControllerEnqueue(this, chunk);
|
---|
| 4683 | };
|
---|
| 4684 | /**
|
---|
| 4685 | * Errors both the readable side and the writable side of the controlled transform stream, making all future
|
---|
| 4686 | * interactions with it fail with the given error `e`. Any chunks queued for transformation will be discarded.
|
---|
| 4687 | */
|
---|
| 4688 | TransformStreamDefaultController.prototype.error = function (reason) {
|
---|
| 4689 | if (reason === void 0) { reason = undefined; }
|
---|
| 4690 | if (!IsTransformStreamDefaultController(this)) {
|
---|
| 4691 | throw defaultControllerBrandCheckException('error');
|
---|
| 4692 | }
|
---|
| 4693 | TransformStreamDefaultControllerError(this, reason);
|
---|
| 4694 | };
|
---|
| 4695 | /**
|
---|
| 4696 | * Closes the readable side and errors the writable side of the controlled transform stream. This is useful when the
|
---|
| 4697 | * transformer only needs to consume a portion of the chunks written to the writable side.
|
---|
| 4698 | */
|
---|
| 4699 | TransformStreamDefaultController.prototype.terminate = function () {
|
---|
| 4700 | if (!IsTransformStreamDefaultController(this)) {
|
---|
| 4701 | throw defaultControllerBrandCheckException('terminate');
|
---|
| 4702 | }
|
---|
| 4703 | TransformStreamDefaultControllerTerminate(this);
|
---|
| 4704 | };
|
---|
| 4705 | return TransformStreamDefaultController;
|
---|
| 4706 | }());
|
---|
| 4707 | Object.defineProperties(TransformStreamDefaultController.prototype, {
|
---|
| 4708 | enqueue: { enumerable: true },
|
---|
| 4709 | error: { enumerable: true },
|
---|
| 4710 | terminate: { enumerable: true },
|
---|
| 4711 | desiredSize: { enumerable: true }
|
---|
| 4712 | });
|
---|
| 4713 | setFunctionName(TransformStreamDefaultController.prototype.enqueue, 'enqueue');
|
---|
| 4714 | setFunctionName(TransformStreamDefaultController.prototype.error, 'error');
|
---|
| 4715 | setFunctionName(TransformStreamDefaultController.prototype.terminate, 'terminate');
|
---|
| 4716 | if (typeof SymbolPolyfill.toStringTag === 'symbol') {
|
---|
| 4717 | Object.defineProperty(TransformStreamDefaultController.prototype, SymbolPolyfill.toStringTag, {
|
---|
| 4718 | value: 'TransformStreamDefaultController',
|
---|
| 4719 | configurable: true
|
---|
| 4720 | });
|
---|
| 4721 | }
|
---|
| 4722 | // Transform Stream Default Controller Abstract Operations
|
---|
| 4723 | function IsTransformStreamDefaultController(x) {
|
---|
| 4724 | if (!typeIsObject(x)) {
|
---|
| 4725 | return false;
|
---|
| 4726 | }
|
---|
| 4727 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
|
---|
| 4728 | return false;
|
---|
| 4729 | }
|
---|
| 4730 | return x instanceof TransformStreamDefaultController;
|
---|
| 4731 | }
|
---|
| 4732 | function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm, cancelAlgorithm) {
|
---|
| 4733 | controller._controlledTransformStream = stream;
|
---|
| 4734 | stream._transformStreamController = controller;
|
---|
| 4735 | controller._transformAlgorithm = transformAlgorithm;
|
---|
| 4736 | controller._flushAlgorithm = flushAlgorithm;
|
---|
| 4737 | controller._cancelAlgorithm = cancelAlgorithm;
|
---|
| 4738 | controller._finishPromise = undefined;
|
---|
| 4739 | controller._finishPromise_resolve = undefined;
|
---|
| 4740 | controller._finishPromise_reject = undefined;
|
---|
| 4741 | }
|
---|
| 4742 | function SetUpTransformStreamDefaultControllerFromTransformer(stream, transformer) {
|
---|
| 4743 | var controller = Object.create(TransformStreamDefaultController.prototype);
|
---|
| 4744 | var transformAlgorithm;
|
---|
| 4745 | var flushAlgorithm;
|
---|
| 4746 | var cancelAlgorithm;
|
---|
| 4747 | if (transformer.transform !== undefined) {
|
---|
| 4748 | transformAlgorithm = function (chunk) { return transformer.transform(chunk, controller); };
|
---|
| 4749 | }
|
---|
| 4750 | else {
|
---|
| 4751 | transformAlgorithm = function (chunk) {
|
---|
| 4752 | try {
|
---|
| 4753 | TransformStreamDefaultControllerEnqueue(controller, chunk);
|
---|
| 4754 | return promiseResolvedWith(undefined);
|
---|
| 4755 | }
|
---|
| 4756 | catch (transformResultE) {
|
---|
| 4757 | return promiseRejectedWith(transformResultE);
|
---|
| 4758 | }
|
---|
| 4759 | };
|
---|
| 4760 | }
|
---|
| 4761 | if (transformer.flush !== undefined) {
|
---|
| 4762 | flushAlgorithm = function () { return transformer.flush(controller); };
|
---|
| 4763 | }
|
---|
| 4764 | else {
|
---|
| 4765 | flushAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 4766 | }
|
---|
| 4767 | if (transformer.cancel !== undefined) {
|
---|
| 4768 | cancelAlgorithm = function (reason) { return transformer.cancel(reason); };
|
---|
| 4769 | }
|
---|
| 4770 | else {
|
---|
| 4771 | cancelAlgorithm = function () { return promiseResolvedWith(undefined); };
|
---|
| 4772 | }
|
---|
| 4773 | SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm, cancelAlgorithm);
|
---|
| 4774 | }
|
---|
| 4775 | function TransformStreamDefaultControllerClearAlgorithms(controller) {
|
---|
| 4776 | controller._transformAlgorithm = undefined;
|
---|
| 4777 | controller._flushAlgorithm = undefined;
|
---|
| 4778 | controller._cancelAlgorithm = undefined;
|
---|
| 4779 | }
|
---|
| 4780 | function TransformStreamDefaultControllerEnqueue(controller, chunk) {
|
---|
| 4781 | var stream = controller._controlledTransformStream;
|
---|
| 4782 | var readableController = stream._readable._readableStreamController;
|
---|
| 4783 | if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(readableController)) {
|
---|
| 4784 | throw new TypeError('Readable side is not in a state that permits enqueue');
|
---|
| 4785 | }
|
---|
| 4786 | // We throttle transform invocations based on the backpressure of the ReadableStream, but we still
|
---|
| 4787 | // accept TransformStreamDefaultControllerEnqueue() calls.
|
---|
| 4788 | try {
|
---|
| 4789 | ReadableStreamDefaultControllerEnqueue(readableController, chunk);
|
---|
| 4790 | }
|
---|
| 4791 | catch (e) {
|
---|
| 4792 | // This happens when readableStrategy.size() throws.
|
---|
| 4793 | TransformStreamErrorWritableAndUnblockWrite(stream, e);
|
---|
| 4794 | throw stream._readable._storedError;
|
---|
| 4795 | }
|
---|
| 4796 | var backpressure = ReadableStreamDefaultControllerHasBackpressure(readableController);
|
---|
| 4797 | if (backpressure !== stream._backpressure) {
|
---|
| 4798 | TransformStreamSetBackpressure(stream, true);
|
---|
| 4799 | }
|
---|
| 4800 | }
|
---|
| 4801 | function TransformStreamDefaultControllerError(controller, e) {
|
---|
| 4802 | TransformStreamError(controller._controlledTransformStream, e);
|
---|
| 4803 | }
|
---|
| 4804 | function TransformStreamDefaultControllerPerformTransform(controller, chunk) {
|
---|
| 4805 | var transformPromise = controller._transformAlgorithm(chunk);
|
---|
| 4806 | return transformPromiseWith(transformPromise, undefined, function (r) {
|
---|
| 4807 | TransformStreamError(controller._controlledTransformStream, r);
|
---|
| 4808 | throw r;
|
---|
| 4809 | });
|
---|
| 4810 | }
|
---|
| 4811 | function TransformStreamDefaultControllerTerminate(controller) {
|
---|
| 4812 | var stream = controller._controlledTransformStream;
|
---|
| 4813 | var readableController = stream._readable._readableStreamController;
|
---|
| 4814 | ReadableStreamDefaultControllerClose(readableController);
|
---|
| 4815 | var error = new TypeError('TransformStream terminated');
|
---|
| 4816 | TransformStreamErrorWritableAndUnblockWrite(stream, error);
|
---|
| 4817 | }
|
---|
| 4818 | // TransformStreamDefaultSink Algorithms
|
---|
| 4819 | function TransformStreamDefaultSinkWriteAlgorithm(stream, chunk) {
|
---|
| 4820 | var controller = stream._transformStreamController;
|
---|
| 4821 | if (stream._backpressure) {
|
---|
| 4822 | var backpressureChangePromise = stream._backpressureChangePromise;
|
---|
| 4823 | return transformPromiseWith(backpressureChangePromise, function () {
|
---|
| 4824 | var writable = stream._writable;
|
---|
| 4825 | var state = writable._state;
|
---|
| 4826 | if (state === 'erroring') {
|
---|
| 4827 | throw writable._storedError;
|
---|
| 4828 | }
|
---|
| 4829 | return TransformStreamDefaultControllerPerformTransform(controller, chunk);
|
---|
| 4830 | });
|
---|
| 4831 | }
|
---|
| 4832 | return TransformStreamDefaultControllerPerformTransform(controller, chunk);
|
---|
| 4833 | }
|
---|
| 4834 | function TransformStreamDefaultSinkAbortAlgorithm(stream, reason) {
|
---|
| 4835 | var controller = stream._transformStreamController;
|
---|
| 4836 | if (controller._finishPromise !== undefined) {
|
---|
| 4837 | return controller._finishPromise;
|
---|
| 4838 | }
|
---|
| 4839 | // stream._readable cannot change after construction, so caching it across a call to user code is safe.
|
---|
| 4840 | var readable = stream._readable;
|
---|
| 4841 | // Assign the _finishPromise now so that if _cancelAlgorithm calls readable.cancel() internally,
|
---|
| 4842 | // we don't run the _cancelAlgorithm again.
|
---|
| 4843 | controller._finishPromise = newPromise(function (resolve, reject) {
|
---|
| 4844 | controller._finishPromise_resolve = resolve;
|
---|
| 4845 | controller._finishPromise_reject = reject;
|
---|
| 4846 | });
|
---|
| 4847 | var cancelPromise = controller._cancelAlgorithm(reason);
|
---|
| 4848 | TransformStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 4849 | uponPromise(cancelPromise, function () {
|
---|
| 4850 | if (readable._state === 'errored') {
|
---|
| 4851 | defaultControllerFinishPromiseReject(controller, readable._storedError);
|
---|
| 4852 | }
|
---|
| 4853 | else {
|
---|
| 4854 | ReadableStreamDefaultControllerError(readable._readableStreamController, reason);
|
---|
| 4855 | defaultControllerFinishPromiseResolve(controller);
|
---|
| 4856 | }
|
---|
| 4857 | return null;
|
---|
| 4858 | }, function (r) {
|
---|
| 4859 | ReadableStreamDefaultControllerError(readable._readableStreamController, r);
|
---|
| 4860 | defaultControllerFinishPromiseReject(controller, r);
|
---|
| 4861 | return null;
|
---|
| 4862 | });
|
---|
| 4863 | return controller._finishPromise;
|
---|
| 4864 | }
|
---|
| 4865 | function TransformStreamDefaultSinkCloseAlgorithm(stream) {
|
---|
| 4866 | var controller = stream._transformStreamController;
|
---|
| 4867 | if (controller._finishPromise !== undefined) {
|
---|
| 4868 | return controller._finishPromise;
|
---|
| 4869 | }
|
---|
| 4870 | // stream._readable cannot change after construction, so caching it across a call to user code is safe.
|
---|
| 4871 | var readable = stream._readable;
|
---|
| 4872 | // Assign the _finishPromise now so that if _flushAlgorithm calls readable.cancel() internally,
|
---|
| 4873 | // we don't also run the _cancelAlgorithm.
|
---|
| 4874 | controller._finishPromise = newPromise(function (resolve, reject) {
|
---|
| 4875 | controller._finishPromise_resolve = resolve;
|
---|
| 4876 | controller._finishPromise_reject = reject;
|
---|
| 4877 | });
|
---|
| 4878 | var flushPromise = controller._flushAlgorithm();
|
---|
| 4879 | TransformStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 4880 | uponPromise(flushPromise, function () {
|
---|
| 4881 | if (readable._state === 'errored') {
|
---|
| 4882 | defaultControllerFinishPromiseReject(controller, readable._storedError);
|
---|
| 4883 | }
|
---|
| 4884 | else {
|
---|
| 4885 | ReadableStreamDefaultControllerClose(readable._readableStreamController);
|
---|
| 4886 | defaultControllerFinishPromiseResolve(controller);
|
---|
| 4887 | }
|
---|
| 4888 | return null;
|
---|
| 4889 | }, function (r) {
|
---|
| 4890 | ReadableStreamDefaultControllerError(readable._readableStreamController, r);
|
---|
| 4891 | defaultControllerFinishPromiseReject(controller, r);
|
---|
| 4892 | return null;
|
---|
| 4893 | });
|
---|
| 4894 | return controller._finishPromise;
|
---|
| 4895 | }
|
---|
| 4896 | // TransformStreamDefaultSource Algorithms
|
---|
| 4897 | function TransformStreamDefaultSourcePullAlgorithm(stream) {
|
---|
| 4898 | // Invariant. Enforced by the promises returned by start() and pull().
|
---|
| 4899 | TransformStreamSetBackpressure(stream, false);
|
---|
| 4900 | // Prevent the next pull() call until there is backpressure.
|
---|
| 4901 | return stream._backpressureChangePromise;
|
---|
| 4902 | }
|
---|
| 4903 | function TransformStreamDefaultSourceCancelAlgorithm(stream, reason) {
|
---|
| 4904 | var controller = stream._transformStreamController;
|
---|
| 4905 | if (controller._finishPromise !== undefined) {
|
---|
| 4906 | return controller._finishPromise;
|
---|
| 4907 | }
|
---|
| 4908 | // stream._writable cannot change after construction, so caching it across a call to user code is safe.
|
---|
| 4909 | var writable = stream._writable;
|
---|
| 4910 | // Assign the _finishPromise now so that if _flushAlgorithm calls writable.abort() or
|
---|
| 4911 | // writable.cancel() internally, we don't run the _cancelAlgorithm again, or also run the
|
---|
| 4912 | // _flushAlgorithm.
|
---|
| 4913 | controller._finishPromise = newPromise(function (resolve, reject) {
|
---|
| 4914 | controller._finishPromise_resolve = resolve;
|
---|
| 4915 | controller._finishPromise_reject = reject;
|
---|
| 4916 | });
|
---|
| 4917 | var cancelPromise = controller._cancelAlgorithm(reason);
|
---|
| 4918 | TransformStreamDefaultControllerClearAlgorithms(controller);
|
---|
| 4919 | uponPromise(cancelPromise, function () {
|
---|
| 4920 | if (writable._state === 'errored') {
|
---|
| 4921 | defaultControllerFinishPromiseReject(controller, writable._storedError);
|
---|
| 4922 | }
|
---|
| 4923 | else {
|
---|
| 4924 | WritableStreamDefaultControllerErrorIfNeeded(writable._writableStreamController, reason);
|
---|
| 4925 | TransformStreamUnblockWrite(stream);
|
---|
| 4926 | defaultControllerFinishPromiseResolve(controller);
|
---|
| 4927 | }
|
---|
| 4928 | return null;
|
---|
| 4929 | }, function (r) {
|
---|
| 4930 | WritableStreamDefaultControllerErrorIfNeeded(writable._writableStreamController, r);
|
---|
| 4931 | TransformStreamUnblockWrite(stream);
|
---|
| 4932 | defaultControllerFinishPromiseReject(controller, r);
|
---|
| 4933 | return null;
|
---|
| 4934 | });
|
---|
| 4935 | return controller._finishPromise;
|
---|
| 4936 | }
|
---|
| 4937 | // Helper functions for the TransformStreamDefaultController.
|
---|
| 4938 | function defaultControllerBrandCheckException(name) {
|
---|
| 4939 | return new TypeError("TransformStreamDefaultController.prototype.".concat(name, " can only be used on a TransformStreamDefaultController"));
|
---|
| 4940 | }
|
---|
| 4941 | function defaultControllerFinishPromiseResolve(controller) {
|
---|
| 4942 | if (controller._finishPromise_resolve === undefined) {
|
---|
| 4943 | return;
|
---|
| 4944 | }
|
---|
| 4945 | controller._finishPromise_resolve();
|
---|
| 4946 | controller._finishPromise_resolve = undefined;
|
---|
| 4947 | controller._finishPromise_reject = undefined;
|
---|
| 4948 | }
|
---|
| 4949 | function defaultControllerFinishPromiseReject(controller, reason) {
|
---|
| 4950 | if (controller._finishPromise_reject === undefined) {
|
---|
| 4951 | return;
|
---|
| 4952 | }
|
---|
| 4953 | setPromiseIsHandledToTrue(controller._finishPromise);
|
---|
| 4954 | controller._finishPromise_reject(reason);
|
---|
| 4955 | controller._finishPromise_resolve = undefined;
|
---|
| 4956 | controller._finishPromise_reject = undefined;
|
---|
| 4957 | }
|
---|
| 4958 | // Helper functions for the TransformStream.
|
---|
| 4959 | function streamBrandCheckException(name) {
|
---|
| 4960 | return new TypeError("TransformStream.prototype.".concat(name, " can only be used on a TransformStream"));
|
---|
| 4961 | }
|
---|
| 4962 |
|
---|
| 4963 | export { ByteLengthQueuingStrategy, CountQueuingStrategy, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, TransformStream, TransformStreamDefaultController, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter };
|
---|
| 4964 | //# sourceMappingURL=ponyfill.mjs.map
|
---|