2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="./11_streams_types.d.ts" />
|
|
|
|
/// <reference path="./lib.deno_fetch.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-17 12:42:36 -04:00
|
|
|
const customInspect = Symbol.for("Deno.customInspect");
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-09-18 09:20:55 -04:00
|
|
|
class AssertionError extends Error {
|
|
|
|
constructor(msg) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {unknown} cond
|
|
|
|
* @param {string=} msg
|
|
|
|
* @returns {asserts cond}
|
|
|
|
*/
|
2020-09-18 09:20:55 -04:00
|
|
|
function assert(cond, msg = "Assertion failed.") {
|
|
|
|
if (!cond) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template T */
|
|
|
|
class Deferred {
|
|
|
|
/** @type {Promise<T>} */
|
|
|
|
#promise;
|
|
|
|
/** @type {(reject?: any) => void} */
|
|
|
|
#reject;
|
|
|
|
/** @type {(value: T | PromiseLike<T>) => void} */
|
|
|
|
#resolve;
|
|
|
|
/** @type {"pending" | "fulfilled"} */
|
|
|
|
#state = "pending";
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
constructor() {
|
2021-01-14 16:57:19 -05:00
|
|
|
this.#promise = new Promise((resolve, reject) => {
|
|
|
|
this.#resolve = resolve;
|
|
|
|
this.#reject = reject;
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {Promise<T>} */
|
|
|
|
get promise() {
|
|
|
|
return this.#promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {"pending" | "fulfilled"} */
|
|
|
|
get state() {
|
|
|
|
return this.#state;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {any=} reason */
|
|
|
|
reject(reason) {
|
|
|
|
// already settled promises are a no-op
|
|
|
|
if (this.#state !== "pending") {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
this.#state = "fulfilled";
|
|
|
|
this.#reject(reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {T | PromiseLike<T>} value */
|
|
|
|
resolve(value) {
|
|
|
|
// already settled promises are a no-op
|
|
|
|
if (this.#state !== "pending") {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
this.#state = "fulfilled";
|
|
|
|
this.#resolve(value);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {(...args: any[]) => any} fn
|
|
|
|
* @param {boolean} enforcePromise
|
|
|
|
* @returns {(...args: any[]) => any}
|
|
|
|
*/
|
|
|
|
function reflectApply(fn, enforcePromise) {
|
|
|
|
if (typeof fn !== "function") {
|
|
|
|
throw new TypeError("The property must be a function.");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return function (...args) {
|
|
|
|
if (enforcePromise) {
|
|
|
|
try {
|
|
|
|
return resolvePromiseWith(Reflect.apply(fn, this, args));
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Reflect.apply(fn, this, args);
|
|
|
|
};
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
* @param {Transformer<I, O>} transformer
|
|
|
|
* @returns {Transformer<I, O>}
|
|
|
|
*/
|
|
|
|
function convertTransformer(transformer) {
|
|
|
|
const transformerDict = Object.create(null);
|
|
|
|
if (transformer === null) {
|
|
|
|
return transformerDict;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if ("flush" in transformer) {
|
|
|
|
transformerDict.flush = reflectApply(transformer.flush, true);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if ("readableType" in transformer) {
|
|
|
|
transformerDict.readableType = transformer.readableType;
|
|
|
|
}
|
|
|
|
if ("start" in transformer) {
|
|
|
|
transformerDict.start = reflectApply(transformer.start, false);
|
|
|
|
}
|
|
|
|
if ("transform" in transformer) {
|
|
|
|
transformerDict.transform = reflectApply(transformer.transform, true);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if ("writableType" in transformer) {
|
|
|
|
transformerDict.writableType = transformer.writableType;
|
|
|
|
}
|
|
|
|
return transformerDict;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {UnderlyingSink<W>} underlyingSink
|
|
|
|
* @returns {UnderlyingSink<W>}
|
|
|
|
*/
|
|
|
|
function convertUnderlyingSink(underlyingSink) {
|
|
|
|
const underlyingSinkDict = Object.create(null);
|
|
|
|
if (underlyingSink === null) {
|
|
|
|
return underlyingSinkDict;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if ("abort" in underlyingSink) {
|
|
|
|
underlyingSinkDict.abort = reflectApply(underlyingSink.abort, true);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if ("close" in underlyingSink) {
|
|
|
|
underlyingSinkDict.close = reflectApply(underlyingSink.close, true);
|
|
|
|
}
|
|
|
|
if ("start" in underlyingSink) {
|
|
|
|
underlyingSinkDict.start = reflectApply(underlyingSink.start, false);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (underlyingSink.type) {
|
|
|
|
underlyingSinkDict.type = underlyingSink.type;
|
|
|
|
}
|
|
|
|
if ("write" in underlyingSink) {
|
|
|
|
underlyingSinkDict.write = reflectApply(underlyingSink.write, true);
|
|
|
|
}
|
|
|
|
return underlyingSinkDict;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {UnderlyingSource<R>} underlyingSource
|
|
|
|
* @returns {UnderlyingSource<R>}
|
|
|
|
*/
|
|
|
|
function convertUnderlyingSource(underlyingSource) {
|
|
|
|
const underlyingSourceDict = Object.create(null);
|
|
|
|
if (underlyingSource === null) {
|
|
|
|
throw new TypeError("Underlying source cannot be null");
|
|
|
|
}
|
|
|
|
if (underlyingSource === undefined) {
|
|
|
|
return underlyingSourceDict;
|
|
|
|
}
|
|
|
|
if ("cancel" in underlyingSource) {
|
|
|
|
underlyingSourceDict.cancel = reflectApply(underlyingSource.cancel, true);
|
|
|
|
}
|
|
|
|
if ("pull" in underlyingSource) {
|
|
|
|
underlyingSourceDict.pull = reflectApply(underlyingSource.pull, true);
|
|
|
|
}
|
|
|
|
if ("start" in underlyingSource) {
|
|
|
|
underlyingSourceDict.start = reflectApply(underlyingSource.start, false);
|
|
|
|
}
|
|
|
|
if (underlyingSource.type !== undefined) {
|
|
|
|
if (underlyingSourceDict.type === null) {
|
|
|
|
throw new TypeError("type cannot be null");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
const type = String(underlyingSource.type);
|
|
|
|
if (type !== "bytes") {
|
|
|
|
throw new TypeError("invalid underlying source type");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
underlyingSourceDict.type = type;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return underlyingSourceDict;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
const originalPromise = Promise;
|
|
|
|
const originalPromiseThen = Promise.prototype.then;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template TResult1
|
|
|
|
* @template TResult2
|
|
|
|
* @param {Promise<T>} promise
|
|
|
|
* @param {(value: T) => TResult1 | PromiseLike<TResult1>} onFulfilled
|
|
|
|
* @param {(reason: any) => TResult2 | PromiseLike<TResult2>=} onRejected
|
|
|
|
* @returns {Promise<TResult1 | TResult2>}
|
|
|
|
*/
|
|
|
|
function performPromiseThen(promise, onFulfilled, onRejected) {
|
|
|
|
return originalPromiseThen.call(promise, onFulfilled, onRejected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {T | PromiseLike<T>} value
|
|
|
|
* @returns {Promise<T>}
|
|
|
|
*/
|
|
|
|
function resolvePromiseWith(value) {
|
|
|
|
return new originalPromise((resolve) => resolve(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {any} e */
|
|
|
|
function rethrowAssertionErrorRejection(e) {
|
|
|
|
if (e && e instanceof AssertionError) {
|
|
|
|
queueMicrotask(() => {
|
|
|
|
console.error(`Internal Error: ${e.stack}`);
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {Promise<any>} promise */
|
|
|
|
function setPromiseIsHandledToTrue(promise) {
|
|
|
|
performPromiseThen(promise, undefined, rethrowAssertionErrorRejection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template TResult1
|
|
|
|
* @template TResult2
|
|
|
|
* @param {Promise<T>} promise
|
|
|
|
* @param {(value: T) => TResult1 | PromiseLike<TResult1>} fulfillmentHandler
|
|
|
|
* @param {(reason: any) => TResult2 | PromiseLike<TResult2>=} rejectionHandler
|
|
|
|
* @returns {Promise<TResult1 | TResult2>}
|
|
|
|
*/
|
|
|
|
function transformPromiseWith(promise, fulfillmentHandler, rejectionHandler) {
|
|
|
|
return performPromiseThen(promise, fulfillmentHandler, rejectionHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template TResult
|
|
|
|
* @param {Promise<T>} promise
|
|
|
|
* @param {(value: T) => TResult | PromiseLike<TResult>} onFulfilled
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function uponFulfillment(promise, onFulfilled) {
|
|
|
|
uponPromise(promise, onFulfilled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template TResult
|
|
|
|
* @param {Promise<T>} promise
|
|
|
|
* @param {(value: T) => TResult | PromiseLike<TResult>} onRejected
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function uponRejection(promise, onRejected) {
|
|
|
|
uponPromise(promise, undefined, onRejected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @template TResult1
|
|
|
|
* @template TResult2
|
|
|
|
* @param {Promise<T>} promise
|
|
|
|
* @param {(value: T) => TResult1 | PromiseLike<TResult1>} onFulfilled
|
|
|
|
* @param {(reason: any) => TResult2 | PromiseLike<TResult2>=} onRejected
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function uponPromise(promise, onFulfilled, onRejected) {
|
|
|
|
performPromiseThen(
|
|
|
|
performPromiseThen(promise, onFulfilled, onRejected),
|
|
|
|
undefined,
|
|
|
|
rethrowAssertionErrorRejection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isFakeDetached = Symbol("<<detached>>");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ArrayBufferLike} O
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isDetachedBuffer(O) {
|
|
|
|
return isFakeDetached in O;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ArrayBufferLike} O
|
|
|
|
* @returns {ArrayBufferLike}
|
|
|
|
*/
|
|
|
|
function transferArrayBuffer(O) {
|
|
|
|
assert(!isDetachedBuffer(O));
|
|
|
|
const transferredIshVersion = O.slice(0);
|
|
|
|
Object.defineProperty(O, "byteLength", {
|
|
|
|
get() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
O[isFakeDetached] = true;
|
|
|
|
return transferredIshVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
const _abortAlgorithm = Symbol("[[abortAlgorithm]]");
|
|
|
|
const _abortSteps = Symbol("[[AbortSteps]]");
|
|
|
|
const _autoAllocateChunkSize = Symbol("[[autoAllocateChunkSize]]");
|
|
|
|
const _backpressure = Symbol("[[backpressure]]");
|
|
|
|
const _backpressureChangePromise = Symbol("[[backpressureChangePromise]]");
|
|
|
|
const _byobRequest = Symbol("[[byobRequest]]");
|
|
|
|
const _cancelAlgorithm = Symbol("[[cancelAlgorithm]]");
|
|
|
|
const _cancelSteps = Symbol("[[CancelSteps]]");
|
|
|
|
const _close = Symbol("close sentinel");
|
|
|
|
const _closeAlgorithm = Symbol("[[closeAlgorithm]]");
|
|
|
|
const _closedPromise = Symbol("[[closedPromise]]");
|
|
|
|
const _closeRequest = Symbol("[[closeRequest]]");
|
|
|
|
const _closeRequested = Symbol("[[closeRequested]]");
|
|
|
|
const _controller = Symbol("[[controller]]");
|
|
|
|
const _detached = Symbol("[[Detached]]");
|
|
|
|
const _disturbed = Symbol("[[disturbed]]");
|
|
|
|
const _errorSteps = Symbol("[[ErrorSteps]]");
|
|
|
|
const _flushAlgorithm = Symbol("[[flushAlgorithm]]");
|
|
|
|
const _globalObject = Symbol("[[globalObject]]");
|
|
|
|
const _inFlightCloseRequest = Symbol("[[inFlightCloseRequest]]");
|
|
|
|
const _inFlightWriteRequest = Symbol("[[inFlightWriteRequest]]");
|
|
|
|
const _pendingAbortRequest = Symbol("[pendingAbortRequest]");
|
|
|
|
const _preventCancel = Symbol("[[preventCancel]]");
|
|
|
|
const _pullAgain = Symbol("[[pullAgain]]");
|
|
|
|
const _pullAlgorithm = Symbol("[[pullAlgorithm]]");
|
|
|
|
const _pulling = Symbol("[[pulling]]");
|
|
|
|
const _pullSteps = Symbol("[[PullSteps]]");
|
|
|
|
const _queue = Symbol("[[queue]]");
|
|
|
|
const _queueTotalSize = Symbol("[[queueTotalSize]]");
|
|
|
|
const _readable = Symbol("[[readable]]");
|
|
|
|
const _reader = Symbol("[[reader]]");
|
|
|
|
const _readRequests = Symbol("[[readRequests]]");
|
|
|
|
const _readyPromise = Symbol("[[readyPromise]]");
|
|
|
|
const _started = Symbol("[[started]]");
|
|
|
|
const _state = Symbol("[[state]]");
|
|
|
|
const _storedError = Symbol("[[storedError]]");
|
|
|
|
const _strategyHWM = Symbol("[[strategyHWM]]");
|
|
|
|
const _strategySizeAlgorithm = Symbol("[[strategySizeAlgorithm]]");
|
|
|
|
const _stream = Symbol("[[stream]]");
|
|
|
|
const _transformAlgorithm = Symbol("[[transformAlgorithm]]");
|
|
|
|
const _writable = Symbol("[[writable]]");
|
|
|
|
const _writeAlgorithm = Symbol("[[writeAlgorithm]]");
|
|
|
|
const _writer = Symbol("[[writer]]");
|
|
|
|
const _writeRequests = Symbol("[[writeRequests]]");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @returns {ReadableStreamDefaultReader<R>}
|
|
|
|
*/
|
|
|
|
function acquireReadableStreamDefaultReader(stream) {
|
|
|
|
return new ReadableStreamDefaultReader(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStream<W>} stream
|
|
|
|
* @returns {WritableStreamDefaultWriter<W>}
|
|
|
|
*/
|
|
|
|
function acquireWritableStreamDefaultWriter(stream) {
|
|
|
|
return new WritableStreamDefaultWriter(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {() => void} startAlgorithm
|
|
|
|
* @param {() => Promise<void>} pullAlgorithm
|
|
|
|
* @param {(reason: any) => Promise<void>} cancelAlgorithm
|
|
|
|
* @param {number=} highWaterMark
|
|
|
|
* @param {((chunk: R) => number)=} sizeAlgorithm
|
|
|
|
* @returns {ReadableStream<R>}
|
|
|
|
*/
|
|
|
|
function createReadableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark = 1,
|
|
|
|
sizeAlgorithm = () => 1,
|
|
|
|
) {
|
|
|
|
assert(isNonNegativeNumber(highWaterMark));
|
|
|
|
/** @type {ReadableStream} */
|
|
|
|
const stream = Object.create(ReadableStream.prototype);
|
|
|
|
initializeReadableStream(stream);
|
|
|
|
const controller = Object.create(ReadableStreamDefaultController.prototype);
|
|
|
|
setUpReadableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {(controller: WritableStreamDefaultController<W>) => Promise<void>} startAlgorithm
|
|
|
|
* @param {(chunk: W) => Promise<void>} writeAlgorithm
|
|
|
|
* @param {() => Promise<void>} closeAlgorithm
|
|
|
|
* @param {(reason: any) => Promise<void>} abortAlgorithm
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {(chunk: W) => number} sizeAlgorithm
|
|
|
|
* @returns {WritableStream<W>}
|
|
|
|
*/
|
|
|
|
function createWritableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
writeAlgorithm,
|
|
|
|
closeAlgorithm,
|
|
|
|
abortAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
) {
|
|
|
|
assert(isNonNegativeNumber(highWaterMark));
|
|
|
|
const stream = Object.create(WritableStream.prototype);
|
|
|
|
initializeWritableStream(stream);
|
|
|
|
const controller = Object.create(WritableStreamDefaultController.prototype);
|
|
|
|
setUpWritableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
writeAlgorithm,
|
|
|
|
closeAlgorithm,
|
|
|
|
abortAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {{ [_queue]: Array<ValueWithSize<T>>, [_queueTotalSize]: number }} container
|
|
|
|
* @returns {T}
|
|
|
|
*/
|
|
|
|
function dequeueValue(container) {
|
|
|
|
assert(_queue in container && _queueTotalSize in container);
|
|
|
|
assert(container[_queue].length);
|
|
|
|
const valueWithSize = container[_queue].shift();
|
|
|
|
container[_queueTotalSize] -= valueWithSize.size;
|
|
|
|
if (container[_queueTotalSize] < 0) {
|
|
|
|
container[_queueTotalSize] = 0;
|
|
|
|
}
|
|
|
|
return valueWithSize.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container
|
|
|
|
* @param {T} value
|
|
|
|
* @param {number} size
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function enqueueValueWithSize(container, value, size) {
|
|
|
|
assert(_queue in container && _queueTotalSize in container);
|
|
|
|
if (isNonNegativeNumber(size) === false) {
|
|
|
|
throw RangeError("chunk size isn't a positive number");
|
|
|
|
}
|
|
|
|
if (size === Infinity) {
|
|
|
|
throw RangeError("chunk size is invalid");
|
|
|
|
}
|
|
|
|
container[_queue].push({ value, size });
|
|
|
|
container[_queueTotalSize] += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {QueuingStrategy} strategy
|
|
|
|
* @param {number} defaultHWM
|
|
|
|
*/
|
|
|
|
function extractHighWaterMark(strategy, defaultHWM) {
|
|
|
|
if (!("highWaterMark" in strategy)) {
|
|
|
|
return defaultHWM;
|
|
|
|
}
|
|
|
|
const highWaterMark = Number(strategy.highWaterMark);
|
|
|
|
if (Number.isNaN(highWaterMark) || highWaterMark < 0) {
|
|
|
|
throw RangeError(
|
|
|
|
`Expected highWaterMark to be a positive number or Infinity, got "${highWaterMark}".`,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return highWaterMark;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {QueuingStrategy<T>} strategy
|
|
|
|
* @return {(chunk: T) => number}
|
|
|
|
*/
|
|
|
|
function extractSizeAlgorithm(strategy) {
|
|
|
|
const { size } = strategy;
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
return () => 1;
|
|
|
|
}
|
|
|
|
return (chunk) => size(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function initializeReadableStream(stream) {
|
|
|
|
stream[_state] = "readable";
|
|
|
|
stream[_reader] = stream[_storedError] = undefined;
|
|
|
|
stream[_disturbed] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStream<I, O>} stream
|
|
|
|
* @param {Deferred<void>} startPromise
|
|
|
|
* @param {number} writableHighWaterMark
|
|
|
|
* @param {(chunk: I) => number} writableSizeAlgorithm
|
|
|
|
* @param {number} readableHighWaterMark
|
|
|
|
* @param {(chunk: O) => number} readableSizeAlgorithm
|
|
|
|
*/
|
|
|
|
function initializeTransformStream(
|
|
|
|
stream,
|
|
|
|
startPromise,
|
|
|
|
writableHighWaterMark,
|
|
|
|
writableSizeAlgorithm,
|
|
|
|
readableHighWaterMark,
|
|
|
|
readableSizeAlgorithm,
|
|
|
|
) {
|
|
|
|
function startAlgorithm() {
|
|
|
|
return startPromise.promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function writeAlgorithm(chunk) {
|
|
|
|
return transformStreamDefaultSinkWriteAlgorithm(stream, chunk);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function abortAlgorithm(reason) {
|
|
|
|
return transformStreamDefaultSinkAbortAlgorithm(stream, reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function closeAlgorithm() {
|
|
|
|
return transformStreamDefaultSinkCloseAlgorithm(stream);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_writable] = createWritableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
writeAlgorithm,
|
|
|
|
closeAlgorithm,
|
|
|
|
abortAlgorithm,
|
|
|
|
writableHighWaterMark,
|
|
|
|
writableSizeAlgorithm,
|
|
|
|
);
|
|
|
|
|
|
|
|
function pullAlgorithm() {
|
|
|
|
return transformStreamDefaultSourcePullAlgorithm(stream);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function cancelAlgorithm(reason) {
|
|
|
|
transformStreamErrorWritableAndUnblockWrite(stream, reason);
|
|
|
|
return resolvePromiseWith(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_readable] = createReadableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
readableHighWaterMark,
|
|
|
|
readableSizeAlgorithm,
|
|
|
|
);
|
|
|
|
|
|
|
|
stream[_backpressure] = stream[_backpressureChangePromise] = undefined;
|
|
|
|
transformStreamSetBackpressure(stream, true);
|
|
|
|
stream[_controller] = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function initializeWritableStream(stream) {
|
|
|
|
stream[_state] = "writable";
|
|
|
|
stream[_storedError] = stream[_writer] = stream[_controller] =
|
|
|
|
stream[_inFlightWriteRequest] = stream[_closeRequest] =
|
|
|
|
stream[_inFlightCloseRequest] = stream[_pendingAbortRequest] =
|
|
|
|
undefined;
|
|
|
|
stream[_writeRequests] = [];
|
|
|
|
stream[_backpressure] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {unknown} v
|
|
|
|
* @returns {v is number}
|
|
|
|
*/
|
|
|
|
function isNonNegativeNumber(v) {
|
|
|
|
if (typeof v !== "number") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Number.isNaN(v)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (v < 0) {
|
|
|
|
return false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {unknown} value
|
|
|
|
* @returns {value is ReadableStream}
|
|
|
|
*/
|
|
|
|
function isReadableStream(value) {
|
|
|
|
return !(typeof value !== "object" || value === null ||
|
|
|
|
!(_controller in value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isReadableStreamLocked(stream) {
|
|
|
|
if (stream[_reader] === undefined) {
|
|
|
|
return false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {unknown} value
|
|
|
|
* @returns {value is ReadableStreamDefaultReader}
|
|
|
|
*/
|
|
|
|
function isReadableStreamDefaultReader(value) {
|
|
|
|
return !(typeof value !== "object" || value === null ||
|
|
|
|
!(_readRequests in value));
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function isReadableStreamDisturbed(stream) {
|
|
|
|
assert(isReadableStream(stream));
|
2021-01-14 16:57:19 -05:00
|
|
|
return stream[_disturbed];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {unknown} value
|
|
|
|
* @returns {value is WritableStream}
|
|
|
|
*/
|
|
|
|
function isWritableStream(value) {
|
|
|
|
return !(typeof value !== "object" || value === null ||
|
|
|
|
!(_controller in value));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function isWritableStreamLocked(stream) {
|
2021-01-14 16:57:19 -05:00
|
|
|
if (stream[_writer] === undefined) {
|
|
|
|
return false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {{ [_queue]: Array<ValueWithSize<T | _close>>, [_queueTotalSize]: number }} container
|
|
|
|
* @returns {T | _close}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function peekQueueValue(container) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(_queue in container && _queueTotalSize in container);
|
|
|
|
assert(container[_queue].length);
|
|
|
|
const valueWithSize = container[_queue][0];
|
|
|
|
return valueWithSize.value;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerCallPullIfNeeded(controller) {
|
2020-07-19 13:49:44 -04:00
|
|
|
const shouldPull = readableByteStreamControllerShouldCallPull(controller);
|
|
|
|
if (!shouldPull) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (controller[_pulling]) {
|
|
|
|
controller[_pullAgain] = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(controller[_pullAgain] === false);
|
|
|
|
controller[_pulling] = true;
|
|
|
|
/** @type {Promise<void>} */
|
|
|
|
const pullPromise = controller[_pullAlgorithm](controller);
|
2020-07-19 13:49:44 -04:00
|
|
|
setPromiseIsHandledToTrue(
|
|
|
|
pullPromise.then(
|
|
|
|
() => {
|
2021-01-14 16:57:19 -05:00
|
|
|
controller[_pulling] = false;
|
|
|
|
if (controller[_pullAgain]) {
|
|
|
|
controller[_pullAgain] = false;
|
2020-07-19 13:49:44 -04:00
|
|
|
readableByteStreamControllerCallPullIfNeeded(controller);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(e) => {
|
|
|
|
readableByteStreamControllerError(controller, e);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerClearAlgorithms(controller) {
|
|
|
|
controller[_pullAlgorithm] = undefined;
|
|
|
|
controller[_cancelAlgorithm] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @param {any} e
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerError(controller, e) {
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (stream[_state] !== "readable") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 3. Perform ! ReadableByteStreamControllerClearPendingPullIntos(controller).
|
|
|
|
resetQueue(controller);
|
|
|
|
readableByteStreamControllerClearAlgorithms(controller);
|
|
|
|
readableStreamError(stream, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerClose(controller) {
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (controller[_closeRequested] || stream[_state] !== "readable") {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (controller[_queueTotalSize] > 0) {
|
|
|
|
controller[_closeRequested] = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 3.13.6.4 If controller.[[pendingPullIntos]] is not empty, (BYOB Support)
|
|
|
|
readableByteStreamControllerClearAlgorithms(controller);
|
|
|
|
readableStreamClose(stream);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @param {ArrayBufferView} chunk
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerEnqueue(controller, chunk) {
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (
|
|
|
|
controller[_closeRequested] ||
|
|
|
|
controller[_stream][_state] !== "readable"
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const { buffer, byteOffset, byteLength } = chunk;
|
|
|
|
const transferredBuffer = transferArrayBuffer(buffer);
|
|
|
|
if (readableStreamHasDefaultReader(stream)) {
|
|
|
|
if (readableStreamGetNumReadRequests(stream) === 0) {
|
|
|
|
readableByteStreamControllerEnqueueChunkToQueue(
|
|
|
|
controller,
|
|
|
|
transferredBuffer,
|
|
|
|
byteOffset,
|
|
|
|
byteLength,
|
|
|
|
);
|
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(controller[_queue].length === 0);
|
2020-07-19 13:49:44 -04:00
|
|
|
const transferredView = new Uint8Array(
|
|
|
|
transferredBuffer,
|
|
|
|
byteOffset,
|
|
|
|
byteLength,
|
|
|
|
);
|
|
|
|
readableStreamFulfillReadRequest(stream, transferredView, false);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
// 8 Otherwise, if ! ReadableStreamHasBYOBReader(stream) is true,
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(isReadableStreamLocked(stream) === false);
|
2020-07-19 13:49:44 -04:00
|
|
|
readableByteStreamControllerEnqueueChunkToQueue(
|
|
|
|
controller,
|
|
|
|
transferredBuffer,
|
|
|
|
byteOffset,
|
|
|
|
byteLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
readableByteStreamControllerCallPullIfNeeded(controller);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @param {ArrayBufferLike} buffer
|
|
|
|
* @param {number} byteOffset
|
|
|
|
* @param {number} byteLength
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function readableByteStreamControllerEnqueueChunkToQueue(
|
|
|
|
controller,
|
|
|
|
buffer,
|
|
|
|
byteOffset,
|
|
|
|
byteLength,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
controller[_queue].push({ buffer, byteOffset, byteLength });
|
|
|
|
controller[_queueTotalSize] += byteLength;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {number | null}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerGetDesiredSize(controller) {
|
|
|
|
const state = controller[_stream][_state];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (state === "errored") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (state === "closed") {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return controller[_strategyHWM] - controller[_queueTotalSize];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {{ [_queue]: any[], [_queueTotalSize]: number }} container
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function resetQueue(container) {
|
|
|
|
container[_queue] = [];
|
|
|
|
container[_queueTotalSize] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerHandleQueueDrain(controller) {
|
|
|
|
assert(controller[_stream][_state] === "readable");
|
2020-07-19 13:49:44 -04:00
|
|
|
if (
|
2021-01-14 16:57:19 -05:00
|
|
|
controller[_queueTotalSize] === 0 && controller[_closeRequested]
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
|
|
|
readableByteStreamControllerClearAlgorithms(controller);
|
2021-01-14 16:57:19 -05:00
|
|
|
readableStreamClose(controller[_stream]);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
|
|
|
readableByteStreamControllerCallPullIfNeeded(controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function readableByteStreamControllerShouldCallPull(controller) {
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (
|
|
|
|
stream[_state] !== "readable" ||
|
|
|
|
controller[_closeRequested] ||
|
|
|
|
!controller[_started]
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
readableStreamHasDefaultReader(stream) &&
|
|
|
|
readableStreamGetNumReadRequests(stream) > 0
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// 3.13.25.6 If ! ReadableStreamHasBYOBReader(stream) is true and !
|
|
|
|
// ReadableStreamGetNumReadIntoRequests(stream) > 0, return true.
|
|
|
|
const desiredSize = readableByteStreamControllerGetDesiredSize(controller);
|
|
|
|
assert(desiredSize !== null);
|
|
|
|
return desiredSize > 0;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {ReadRequest<R>} readRequest
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableStreamAddReadRequest(stream, readRequest) {
|
|
|
|
assert(isReadableStreamDefaultReader(stream[_reader]));
|
|
|
|
assert(stream[_state] === "readable");
|
|
|
|
stream[_reader][_readRequests].push(readRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function readableStreamCancel(stream, reason) {
|
|
|
|
stream[_disturbed] = true;
|
|
|
|
if (stream[_state] === "closed") {
|
|
|
|
return resolvePromiseWith(undefined);
|
|
|
|
}
|
|
|
|
if (stream[_state] === "errored") {
|
|
|
|
return Promise.reject(stream[_storedError]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
readableStreamClose(stream);
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {Promise<void>} */
|
|
|
|
const sourceCancelPromise = stream[_controller][_cancelSteps](reason);
|
|
|
|
return sourceCancelPromise.then(() => undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function readableStreamClose(stream) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_state] === "readable");
|
|
|
|
stream[_state] = "closed";
|
|
|
|
/** @type {ReadableStreamDefaultReader<R> | undefined} */
|
|
|
|
const reader = stream[_reader];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (!reader) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isReadableStreamDefaultReader(reader)) {
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {Array<ReadRequest<R>>} */
|
|
|
|
const readRequests = reader[_readRequests];
|
|
|
|
for (const readRequest of readRequests) {
|
|
|
|
readRequest.closeSteps();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
reader[_readRequests] = [];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
// This promise can be double resolved.
|
|
|
|
// See: https://github.com/whatwg/streams/issues/1100
|
|
|
|
reader[_closedPromise].resolve(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {ReadableStreamDefaultController<any>} controller */
|
|
|
|
function readableStreamDefaultControllerCallPullIfNeeded(controller) {
|
|
|
|
const shouldPull = readableStreamDefaultcontrollerShouldCallPull(
|
2020-07-19 13:49:44 -04:00
|
|
|
controller,
|
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
if (shouldPull === false) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (controller[_pulling] === true) {
|
|
|
|
controller[_pullAgain] = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(controller[_pullAgain] === false);
|
|
|
|
controller[_pulling] = true;
|
|
|
|
const pullPromise = controller[_pullAlgorithm](controller);
|
|
|
|
uponFulfillment(pullPromise, () => {
|
|
|
|
controller[_pulling] = false;
|
|
|
|
if (controller[_pullAgain] === true) {
|
|
|
|
controller[_pullAgain] = false;
|
|
|
|
readableStreamDefaultControllerCallPullIfNeeded(controller);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
uponRejection(pullPromise, (e) => {
|
|
|
|
readableStreamDefaultControllerError(controller, e);
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStreamDefaultController<any>} controller
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultControllerCanCloseOrEnqueue(controller) {
|
|
|
|
const state = controller[_stream][_state];
|
|
|
|
if (controller[_closeRequested] === false && state === "readable") {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {ReadableStreamDefaultController<any>} controller */
|
|
|
|
function readableStreamDefaultControllerClearAlgorithms(controller) {
|
|
|
|
controller[_pullAlgorithm] = undefined;
|
|
|
|
controller[_cancelAlgorithm] = undefined;
|
|
|
|
controller[_strategySizeAlgorithm] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {ReadableStreamDefaultController<any>} controller */
|
|
|
|
function readableStreamDefaultControllerClose(controller) {
|
|
|
|
if (
|
|
|
|
readableStreamDefaultControllerCanCloseOrEnqueue(controller) === false
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
const stream = controller[_stream];
|
|
|
|
controller[_closeRequested] = true;
|
|
|
|
if (controller[_queue].length === 0) {
|
2020-07-19 13:49:44 -04:00
|
|
|
readableStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
readableStreamClose(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStreamDefaultController<R>} controller
|
|
|
|
* @param {R} chunk
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultControllerEnqueue(controller, chunk) {
|
|
|
|
if (
|
|
|
|
readableStreamDefaultControllerCanCloseOrEnqueue(controller) === false
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
const stream = controller[_stream];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (
|
2021-01-14 16:57:19 -05:00
|
|
|
isReadableStreamLocked(stream) === true &&
|
2020-07-19 13:49:44 -04:00
|
|
|
readableStreamGetNumReadRequests(stream) > 0
|
|
|
|
) {
|
|
|
|
readableStreamFulfillReadRequest(stream, chunk, false);
|
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
let chunkSize;
|
|
|
|
try {
|
|
|
|
chunkSize = controller[_strategySizeAlgorithm](chunk);
|
|
|
|
} catch (e) {
|
|
|
|
readableStreamDefaultControllerError(controller, e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
try {
|
|
|
|
enqueueValueWithSize(controller, chunk, chunkSize);
|
2021-01-14 16:57:19 -05:00
|
|
|
} catch (e) {
|
|
|
|
readableStreamDefaultControllerError(controller, e);
|
|
|
|
throw e;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
readableStreamDefaultControllerCallPullIfNeeded(controller);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStreamDefaultController<any>} controller
|
|
|
|
* @param {any} e
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultControllerError(controller, e) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (stream[_state] !== "readable") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resetQueue(controller);
|
|
|
|
readableStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
readableStreamError(stream, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableStreamDefaultController<any>} controller
|
|
|
|
* @returns {number | null}
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultControllerGetDesiredSize(controller) {
|
|
|
|
const state = controller[_stream][_state];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (state === "errored") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (state === "closed") {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return controller[_strategyHWM] - controller[_queueTotalSize];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {ReadableStreamDefaultController} controller */
|
|
|
|
function readableStreamDefaultcontrollerHasBackpressure(controller) {
|
|
|
|
if (readableStreamDefaultcontrollerShouldCallPull(controller) === true) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStreamDefaultController<any>} controller
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultcontrollerShouldCallPull(controller) {
|
|
|
|
const stream = controller[_stream];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (
|
2021-01-14 16:57:19 -05:00
|
|
|
readableStreamDefaultControllerCanCloseOrEnqueue(controller) === false
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (controller[_started] === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
if (
|
|
|
|
isReadableStreamLocked(stream) &&
|
|
|
|
readableStreamGetNumReadRequests(stream) > 0
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const desiredSize = readableStreamDefaultControllerGetDesiredSize(
|
|
|
|
controller,
|
|
|
|
);
|
|
|
|
assert(desiredSize !== null);
|
2021-01-14 16:57:19 -05:00
|
|
|
if (desiredSize > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStreamDefaultReader<R>} reader
|
|
|
|
* @param {ReadRequest<R>} readRequest
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function readableStreamDefaultReaderRead(reader, readRequest) {
|
|
|
|
const stream = reader[_stream];
|
2020-07-19 13:49:44 -04:00
|
|
|
assert(stream);
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_disturbed] = true;
|
|
|
|
if (stream[_state] === "closed") {
|
|
|
|
readRequest.closeSteps();
|
|
|
|
} else if (stream[_state] === "errored") {
|
|
|
|
readRequest.errorSteps(stream[_storedError]);
|
|
|
|
} else {
|
|
|
|
assert(stream[_state] === "readable");
|
|
|
|
stream[_controller][_pullSteps](readRequest);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {any} e
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function readableStreamError(stream, e) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_state] === "readable");
|
|
|
|
stream[_state] = "errored";
|
|
|
|
stream[_storedError] = e;
|
|
|
|
/** @type {ReadableStreamDefaultReader<R> | undefined} */
|
|
|
|
const reader = stream[_reader];
|
2020-07-19 13:49:44 -04:00
|
|
|
if (reader === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isReadableStreamDefaultReader(reader)) {
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {Array<ReadRequest<R>>} */
|
|
|
|
const readRequests = reader[_readRequests];
|
|
|
|
for (const readRequest of readRequests) {
|
|
|
|
readRequest.errorSteps(e);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
reader[_readRequests] = [];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
// 3.5.6.8 Otherwise, support BYOB Reader
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const closedPromise = reader[_closedPromise];
|
|
|
|
console.log("closedPromise rejected");
|
|
|
|
closedPromise.reject(e);
|
|
|
|
setPromiseIsHandledToTrue(closedPromise.promise);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {R} chunk
|
|
|
|
* @param {boolean} done
|
|
|
|
*/
|
|
|
|
function readableStreamFulfillReadRequest(stream, chunk, done) {
|
|
|
|
assert(readableStreamHasDefaultReader(stream) === true);
|
|
|
|
/** @type {ReadableStreamDefaultReader<R>} */
|
|
|
|
const reader = stream[_reader];
|
|
|
|
assert(reader[_readRequests].length);
|
|
|
|
/** @type {ReadRequest<R>} */
|
|
|
|
const readRequest = reader[_readRequests].shift();
|
|
|
|
if (done) {
|
|
|
|
readRequest.closeSteps();
|
|
|
|
} else {
|
|
|
|
readRequest.chunkSteps(chunk);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
function readableStreamGetNumReadRequests(stream) {
|
|
|
|
assert(readableStreamHasDefaultReader(stream) === true);
|
|
|
|
return stream[_reader][_readRequests].length;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function readableStreamHasDefaultReader(stream) {
|
|
|
|
const reader = stream[_reader];
|
|
|
|
if (reader === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isReadableStreamDefaultReader(reader)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {ReadableStream<T>} source
|
|
|
|
* @param {WritableStream<T>} dest
|
|
|
|
* @param {boolean} preventClose
|
|
|
|
* @param {boolean} preventAbort
|
|
|
|
* @param {boolean} preventCancel
|
|
|
|
* @param {AbortSignal=} signal
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function readableStreamPipeTo(
|
|
|
|
source,
|
|
|
|
dest,
|
|
|
|
preventClose,
|
|
|
|
preventAbort,
|
|
|
|
preventCancel,
|
|
|
|
signal,
|
|
|
|
) {
|
|
|
|
assert(isReadableStream(source));
|
|
|
|
assert(isWritableStream(dest));
|
|
|
|
assert(
|
2021-01-14 16:57:19 -05:00
|
|
|
typeof preventClose === "boolean" && typeof preventAbort === "boolean" &&
|
2020-07-19 13:49:44 -04:00
|
|
|
typeof preventCancel === "boolean",
|
|
|
|
);
|
|
|
|
assert(signal === undefined || signal instanceof AbortSignal);
|
|
|
|
assert(!isReadableStreamLocked(source));
|
|
|
|
assert(!isWritableStreamLocked(dest));
|
|
|
|
const reader = acquireReadableStreamDefaultReader(source);
|
|
|
|
const writer = acquireWritableStreamDefaultWriter(dest);
|
2021-01-14 16:57:19 -05:00
|
|
|
source[_disturbed] = true;
|
2020-07-19 13:49:44 -04:00
|
|
|
let shuttingDown = false;
|
2021-01-14 16:57:19 -05:00
|
|
|
let currentWrite = resolvePromiseWith(undefined);
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const promise = new Deferred();
|
|
|
|
/** @type {() => void} */
|
2020-07-19 13:49:44 -04:00
|
|
|
let abortAlgorithm;
|
|
|
|
if (signal) {
|
|
|
|
abortAlgorithm = () => {
|
2021-01-14 16:57:19 -05:00
|
|
|
const error = new DOMException("Aborted", "AbortError");
|
|
|
|
/** @type {Array<() => Promise<void>>} */
|
2020-07-19 13:49:44 -04:00
|
|
|
const actions = [];
|
2021-01-14 16:57:19 -05:00
|
|
|
if (preventAbort === false) {
|
2020-07-19 13:49:44 -04:00
|
|
|
actions.push(() => {
|
2021-01-14 16:57:19 -05:00
|
|
|
if (dest[_state] === "writable") {
|
2020-07-19 13:49:44 -04:00
|
|
|
return writableStreamAbort(dest, error);
|
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
return resolvePromiseWith(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (preventCancel === false) {
|
2020-07-19 13:49:44 -04:00
|
|
|
actions.push(() => {
|
2021-01-14 16:57:19 -05:00
|
|
|
if (source[_state] === "readable") {
|
2020-07-19 13:49:44 -04:00
|
|
|
return readableStreamCancel(source, error);
|
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
return resolvePromiseWith(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
shutdownWithAction(
|
|
|
|
() => Promise.all(actions.map((action) => action())),
|
|
|
|
true,
|
|
|
|
error,
|
|
|
|
);
|
|
|
|
};
|
2021-01-14 16:57:19 -05:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
if (signal.aborted) {
|
|
|
|
abortAlgorithm();
|
|
|
|
return promise.promise;
|
|
|
|
}
|
|
|
|
signal.addEventListener("abort", abortAlgorithm);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function pipeLoop() {
|
|
|
|
return new Promise((resolveLoop, rejectLoop) => {
|
|
|
|
/** @param {boolean} done */
|
|
|
|
function next(done) {
|
|
|
|
if (done) {
|
|
|
|
resolveLoop();
|
|
|
|
} else {
|
|
|
|
uponPromise(pipeStep(), next, rejectLoop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {Promise<boolean>} */
|
|
|
|
function pipeStep() {
|
|
|
|
if (shuttingDown === true) {
|
|
|
|
return resolvePromiseWith(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformPromiseWith(writer[_readyPromise].promise, () => {
|
|
|
|
return new Promise((resolveRead, rejectRead) => {
|
|
|
|
readableStreamDefaultReaderRead(
|
|
|
|
reader,
|
|
|
|
{
|
|
|
|
chunkSteps(chunk) {
|
|
|
|
currentWrite = transformPromiseWith(
|
|
|
|
writableStreamDefaultWriterWrite(writer, chunk),
|
|
|
|
undefined,
|
|
|
|
() => {},
|
|
|
|
);
|
|
|
|
resolveRead(false);
|
|
|
|
},
|
|
|
|
closeSteps() {
|
|
|
|
resolveRead(true);
|
|
|
|
},
|
|
|
|
errorSteps: rejectRead,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
isOrBecomesErrored(
|
|
|
|
source,
|
|
|
|
reader[_closedPromise].promise,
|
|
|
|
(storedError) => {
|
|
|
|
if (preventAbort === false) {
|
|
|
|
shutdownWithAction(
|
|
|
|
() => writableStreamAbort(dest, storedError),
|
|
|
|
true,
|
|
|
|
storedError,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
shutdown(true, storedError);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
isOrBecomesErrored(dest, writer[_closedPromise].promise, (storedError) => {
|
|
|
|
if (preventCancel === false) {
|
|
|
|
shutdownWithAction(
|
|
|
|
() => readableStreamCancel(source, storedError),
|
|
|
|
true,
|
|
|
|
storedError,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
shutdown(true, storedError);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
isOrBecomesClosed(source, reader[_closedPromise].promise, () => {
|
|
|
|
if (preventClose === false) {
|
|
|
|
shutdownWithAction(() =>
|
|
|
|
writableStreamDefaultWriterCloseWithErrorPropagation(writer)
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
shutdown();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(dest) === true ||
|
|
|
|
dest[_state] === "closed"
|
|
|
|
) {
|
|
|
|
const destClosed = new TypeError(
|
|
|
|
"The destination writable stream closed before all the data could be piped to it.",
|
|
|
|
);
|
|
|
|
if (preventCancel === false) {
|
|
|
|
shutdownWithAction(
|
|
|
|
() => readableStreamCancel(source, destClosed),
|
|
|
|
true,
|
|
|
|
destClosed,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
shutdown(true, destClosed);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
setPromiseIsHandledToTrue(pipeLoop());
|
|
|
|
|
|
|
|
return promise.promise;
|
|
|
|
|
|
|
|
/** @returns {Promise<void>} */
|
2020-07-19 13:49:44 -04:00
|
|
|
function waitForWritesToFinish() {
|
|
|
|
const oldCurrentWrite = currentWrite;
|
2021-01-14 16:57:19 -05:00
|
|
|
return transformPromiseWith(
|
|
|
|
currentWrite,
|
|
|
|
() =>
|
|
|
|
oldCurrentWrite !== currentWrite
|
|
|
|
? waitForWritesToFinish()
|
|
|
|
: undefined,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream | WritableStream} stream
|
|
|
|
* @param {Promise<any>} promise
|
|
|
|
* @param {(e: any) => void} action
|
|
|
|
*/
|
|
|
|
function isOrBecomesErrored(stream, promise, action) {
|
|
|
|
if (stream[_state] === "errored") {
|
|
|
|
action(stream[_storedError]);
|
|
|
|
} else {
|
|
|
|
uponRejection(promise, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableStream} stream
|
|
|
|
* @param {Promise<any>} promise
|
|
|
|
* @param {() => void} action
|
|
|
|
*/
|
|
|
|
function isOrBecomesClosed(stream, promise, action) {
|
|
|
|
if (stream[_state] === "closed") {
|
|
|
|
action();
|
|
|
|
} else {
|
|
|
|
uponFulfillment(promise, action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {() => Promise<void[] | void>} action
|
|
|
|
* @param {boolean=} originalIsError
|
|
|
|
* @param {any=} originalError
|
|
|
|
*/
|
|
|
|
function shutdownWithAction(action, originalIsError, originalError) {
|
2020-07-19 13:49:44 -04:00
|
|
|
function doTheRest() {
|
2021-01-14 16:57:19 -05:00
|
|
|
uponPromise(
|
|
|
|
action(),
|
|
|
|
() => finalize(originalIsError, originalError),
|
|
|
|
(newError) => finalize(true, newError),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
if (shuttingDown === true) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
shuttingDown = true;
|
|
|
|
|
|
|
|
if (
|
2021-01-14 16:57:19 -05:00
|
|
|
dest[_state] === "writable" &&
|
2020-07-19 13:49:44 -04:00
|
|
|
writableStreamCloseQueuedOrInFlight(dest) === false
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
uponFulfillment(waitForWritesToFinish(), doTheRest);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
|
|
|
doTheRest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {boolean=} isError
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function shutdown(isError, error) {
|
|
|
|
if (shuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shuttingDown = true;
|
|
|
|
if (
|
2021-01-14 16:57:19 -05:00
|
|
|
dest[_state] === "writable" &&
|
|
|
|
writableStreamCloseQueuedOrInFlight(dest) === false
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
uponFulfillment(
|
|
|
|
waitForWritesToFinish(),
|
|
|
|
() => finalize(isError, error),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
} else {
|
|
|
|
finalize(isError, error);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {boolean=} isError
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function finalize(isError, error) {
|
|
|
|
writableStreamDefaultWriterRelease(writer);
|
|
|
|
readableStreamReaderGenericRelease(reader);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
if (signal !== undefined) {
|
|
|
|
signal.removeEventListener("abort", abortAlgorithm);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (isError) {
|
|
|
|
promise.reject(error);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
promise.resolve(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStreamGenericReader<any>} reader
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function readableStreamReaderGenericCancel(reader, reason) {
|
|
|
|
const stream = reader[_stream];
|
|
|
|
assert(stream !== undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
return readableStreamCancel(stream, reason);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStreamDefaultReader<R>} reader
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
*/
|
|
|
|
function readableStreamReaderGenericInitialize(reader, stream) {
|
|
|
|
reader[_stream] = stream;
|
|
|
|
stream[_reader] = reader;
|
|
|
|
if (stream[_state] === "readable") {
|
|
|
|
reader[_closedPromise] = new Deferred();
|
|
|
|
} else if (stream[_state] === "closed") {
|
|
|
|
reader[_closedPromise] = new Deferred();
|
|
|
|
reader[_closedPromise].resolve(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_state] === "errored");
|
|
|
|
reader[_closedPromise] = new Deferred();
|
|
|
|
reader[_closedPromise].reject(stream[_storedError]);
|
|
|
|
setPromiseIsHandledToTrue(reader[_closedPromise].promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStreamGenericReader<R>} reader
|
|
|
|
*/
|
|
|
|
function readableStreamReaderGenericRelease(reader) {
|
|
|
|
assert(reader[_stream] !== undefined);
|
|
|
|
assert(reader[_stream][_reader] === reader);
|
|
|
|
if (reader[_stream][_state] === "readable") {
|
|
|
|
reader[_closedPromise].reject(
|
|
|
|
new TypeError(
|
|
|
|
"Reader was released and can no longer be used to monitor the stream's closedness.",
|
|
|
|
),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-01-14 16:57:19 -05:00
|
|
|
reader[_closedPromise] = new Deferred();
|
|
|
|
reader[_closedPromise].reject(
|
|
|
|
new TypeError(
|
|
|
|
"Reader was released and can no longer be used to monitor the stream's closedness.",
|
|
|
|
),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
setPromiseIsHandledToTrue(reader[_closedPromise].promise);
|
|
|
|
reader[_stream][_reader] = undefined;
|
|
|
|
reader[_stream] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {boolean} cloneForBranch2
|
|
|
|
* @returns {[ReadableStream<R>, ReadableStream<R>]}
|
|
|
|
*/
|
|
|
|
function readableStreamTee(stream, cloneForBranch2) {
|
2020-07-19 13:49:44 -04:00
|
|
|
assert(isReadableStream(stream));
|
|
|
|
assert(typeof cloneForBranch2 === "boolean");
|
|
|
|
const reader = acquireReadableStreamDefaultReader(stream);
|
|
|
|
let reading = false;
|
|
|
|
let canceled1 = false;
|
|
|
|
let canceled2 = false;
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {any} */
|
|
|
|
let reason1;
|
|
|
|
/** @type {any} */
|
|
|
|
let reason2;
|
|
|
|
/** @type {ReadableStream<R>} */
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore prefer-const
|
2020-07-19 13:49:44 -04:00
|
|
|
let branch1;
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {ReadableStream<R>} */
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore prefer-const
|
2020-07-19 13:49:44 -04:00
|
|
|
let branch2;
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const cancelPromise = new Deferred();
|
|
|
|
|
|
|
|
function pullAlgorithm() {
|
|
|
|
if (reading === true) {
|
|
|
|
return resolvePromiseWith(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
reading = true;
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {ReadRequest<R>} */
|
|
|
|
const readRequest = {
|
|
|
|
chunkSteps(value) {
|
|
|
|
queueMicrotask(() => {
|
|
|
|
reading = false;
|
|
|
|
const value1 = value;
|
|
|
|
const value2 = value;
|
|
|
|
|
|
|
|
if (canceled1 === false) {
|
|
|
|
readableStreamDefaultControllerEnqueue(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch1[
|
|
|
|
_controller
|
|
|
|
]),
|
|
|
|
value1,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (canceled2 === false) {
|
|
|
|
readableStreamDefaultControllerEnqueue(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch2[
|
|
|
|
_controller
|
|
|
|
]),
|
|
|
|
value2,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
closeSteps() {
|
|
|
|
reading = false;
|
|
|
|
if (canceled1 === false) {
|
|
|
|
readableStreamDefaultControllerClose(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch1[
|
|
|
|
_controller
|
|
|
|
]),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
if (canceled2 === false) {
|
|
|
|
readableStreamDefaultControllerClose(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch2[
|
|
|
|
_controller
|
|
|
|
]),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
cancelPromise.resolve(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
},
|
2021-01-14 16:57:19 -05:00
|
|
|
errorSteps() {
|
|
|
|
reading = false;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
readableStreamDefaultReaderRead(reader, readRequest);
|
|
|
|
return resolvePromiseWith(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function cancel1Algorithm(reason) {
|
2020-07-19 13:49:44 -04:00
|
|
|
canceled1 = true;
|
|
|
|
reason1 = reason;
|
2021-01-14 16:57:19 -05:00
|
|
|
if (canceled2 === true) {
|
2020-07-19 13:49:44 -04:00
|
|
|
const compositeReason = [reason1, reason2];
|
|
|
|
const cancelResult = readableStreamCancel(stream, compositeReason);
|
|
|
|
cancelPromise.resolve(cancelResult);
|
|
|
|
}
|
|
|
|
return cancelPromise.promise;
|
2021-01-14 16:57:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function cancel2Algorithm(reason) {
|
2020-07-19 13:49:44 -04:00
|
|
|
canceled2 = true;
|
|
|
|
reason2 = reason;
|
2021-01-14 16:57:19 -05:00
|
|
|
if (canceled1 === true) {
|
2020-07-19 13:49:44 -04:00
|
|
|
const compositeReason = [reason1, reason2];
|
|
|
|
const cancelResult = readableStreamCancel(stream, compositeReason);
|
|
|
|
cancelPromise.resolve(cancelResult);
|
|
|
|
}
|
|
|
|
return cancelPromise.promise;
|
2021-01-14 16:57:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function startAlgorithm() {}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
branch1 = createReadableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancel1Algorithm,
|
|
|
|
);
|
|
|
|
branch2 = createReadableStream(
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancel2Algorithm,
|
|
|
|
);
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
uponRejection(reader[_closedPromise].promise, (r) => {
|
|
|
|
readableStreamDefaultControllerError(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch1[
|
|
|
|
_controller
|
|
|
|
]),
|
|
|
|
r,
|
|
|
|
);
|
|
|
|
readableStreamDefaultControllerError(
|
|
|
|
/** @type {ReadableStreamDefaultController<any>} */ (branch2[
|
|
|
|
_controller
|
|
|
|
]),
|
|
|
|
r,
|
|
|
|
);
|
|
|
|
cancelPromise.resolve(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
return [branch1, branch2];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream<ArrayBuffer>} stream
|
|
|
|
* @param {ReadableByteStreamController} controller
|
|
|
|
* @param {() => void} startAlgorithm
|
|
|
|
* @param {() => Promise<void>} pullAlgorithm
|
|
|
|
* @param {(reason: any) => Promise<void>} cancelAlgorithm
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {number | undefined} autoAllocateChunkSize
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpReadableByteStreamController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
autoAllocateChunkSize,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_controller] === undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
if (autoAllocateChunkSize !== undefined) {
|
|
|
|
assert(Number.isInteger(autoAllocateChunkSize));
|
|
|
|
assert(autoAllocateChunkSize >= 0);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
controller[_stream] = stream;
|
|
|
|
controller[_pullAgain] = controller[_pulling] = false;
|
|
|
|
controller[_byobRequest] = undefined;
|
|
|
|
resetQueue(controller);
|
|
|
|
controller[_closeRequested] = controller[_started] = false;
|
|
|
|
controller[_strategyHWM] = highWaterMark;
|
|
|
|
controller[_pullAlgorithm] = pullAlgorithm;
|
|
|
|
controller[_cancelAlgorithm] = cancelAlgorithm;
|
|
|
|
controller[_autoAllocateChunkSize] = autoAllocateChunkSize;
|
|
|
|
// 12. Set controller.[[pendingPullIntos]] to a new empty list.
|
|
|
|
stream[_controller] = controller;
|
2020-07-19 13:49:44 -04:00
|
|
|
const startResult = startAlgorithm();
|
2021-01-14 16:57:19 -05:00
|
|
|
const startPromise = resolvePromiseWith(startResult);
|
2020-07-19 13:49:44 -04:00
|
|
|
setPromiseIsHandledToTrue(
|
|
|
|
startPromise.then(
|
|
|
|
() => {
|
2021-01-14 16:57:19 -05:00
|
|
|
controller[_started] = true;
|
|
|
|
assert(controller[_pulling] === false);
|
|
|
|
assert(controller[_pullAgain] === false);
|
2020-07-19 13:49:44 -04:00
|
|
|
readableByteStreamControllerCallPullIfNeeded(controller);
|
|
|
|
},
|
|
|
|
(r) => {
|
|
|
|
readableByteStreamControllerError(controller, r);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStream<ArrayBuffer>} stream
|
|
|
|
* @param {UnderlyingSource<ArrayBuffer>} underlyingSource
|
|
|
|
* @param {UnderlyingSource<ArrayBuffer>} underlyingSourceDict
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpReadableByteStreamControllerFromUnderlyingSource(
|
|
|
|
stream,
|
2021-01-14 16:57:19 -05:00
|
|
|
underlyingSource,
|
|
|
|
underlyingSourceDict,
|
2020-07-19 13:49:44 -04:00
|
|
|
highWaterMark,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
const controller = new ReadableByteStreamController();
|
|
|
|
/** @type {() => void} */
|
|
|
|
let startAlgorithm = () => undefined;
|
|
|
|
/** @type {() => Promise<void>} */
|
|
|
|
let pullAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
/** @type {(reason: any) => Promise<void>} */
|
|
|
|
let cancelAlgorithm = (_reason) => resolvePromiseWith(undefined);
|
|
|
|
if ("start" in underlyingSourceDict) {
|
|
|
|
startAlgorithm = () =>
|
|
|
|
underlyingSourceDict.start.call(underlyingSource, controller);
|
|
|
|
}
|
|
|
|
if ("pull" in underlyingSourceDict) {
|
|
|
|
pullAlgorithm = () =>
|
|
|
|
underlyingSourceDict.pull.call(underlyingSource, controller);
|
|
|
|
}
|
|
|
|
if ("cancel" in underlyingSourceDict) {
|
|
|
|
cancelAlgorithm = (reason) =>
|
|
|
|
underlyingSourceDict.cancel.call(underlyingSource, reason);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
// 3.13.27.6 Let autoAllocateChunkSize be ? GetV(underlyingByteSource, "autoAllocateChunkSize").
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {undefined} */
|
2020-07-19 13:49:44 -04:00
|
|
|
const autoAllocateChunkSize = undefined;
|
|
|
|
setUpReadableByteStreamController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
autoAllocateChunkSize,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {ReadableStreamDefaultController<R>} controller
|
|
|
|
* @param {(controller: ReadableStreamDefaultController<R>) => void | Promise<void>} startAlgorithm
|
|
|
|
* @param {(controller: ReadableStreamDefaultController<R>) => Promise<void>} pullAlgorithm
|
|
|
|
* @param {(reason: any) => Promise<void>} cancelAlgorithm
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {(chunk: R) => number} sizeAlgorithm
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpReadableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_controller] === undefined);
|
|
|
|
controller[_stream] = stream;
|
|
|
|
resetQueue(controller);
|
|
|
|
controller[_started] = controller[_closeRequested] =
|
|
|
|
controller[_pullAgain] = controller[_pulling] = false;
|
|
|
|
controller[_strategySizeAlgorithm] = sizeAlgorithm;
|
|
|
|
controller[_strategyHWM] = highWaterMark;
|
|
|
|
controller[_pullAlgorithm] = pullAlgorithm;
|
|
|
|
controller[_cancelAlgorithm] = cancelAlgorithm;
|
|
|
|
stream[_controller] = controller;
|
|
|
|
const startResult = startAlgorithm(controller);
|
|
|
|
const startPromise = resolvePromiseWith(startResult);
|
|
|
|
uponPromise(startPromise, () => {
|
|
|
|
controller[_started] = true;
|
|
|
|
assert(controller[_pulling] === false);
|
|
|
|
assert(controller[_pullAgain] === false);
|
|
|
|
readableStreamDefaultControllerCallPullIfNeeded(controller);
|
|
|
|
}, (r) => {
|
|
|
|
readableStreamDefaultControllerError(controller, r);
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
* @param {UnderlyingSource<R>} underlyingSource
|
|
|
|
* @param {UnderlyingSource<R>} underlyingSourceDict
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {(chunk: R) => number} sizeAlgorithm
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpReadableStreamDefaultControllerFromUnderlyingSource(
|
|
|
|
stream,
|
|
|
|
underlyingSource,
|
2021-01-14 16:57:19 -05:00
|
|
|
underlyingSourceDict,
|
2020-07-19 13:49:44 -04:00
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
const controller = new ReadableStreamDefaultController();
|
|
|
|
/** @type {(controller: ReadableStreamDefaultController<R>) => Promise<void>} */
|
|
|
|
let startAlgorithm = () => undefined;
|
|
|
|
/** @type {(controller: ReadableStreamDefaultController<R>) => Promise<void>} */
|
|
|
|
let pullAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
/** @type {(reason?: any) => Promise<void>} */
|
|
|
|
let cancelAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
if ("start" in underlyingSourceDict) {
|
|
|
|
startAlgorithm = () =>
|
|
|
|
underlyingSourceDict.start.call(underlyingSource, controller);
|
|
|
|
}
|
|
|
|
if ("pull" in underlyingSourceDict) {
|
|
|
|
pullAlgorithm = () =>
|
|
|
|
underlyingSourceDict.pull.call(underlyingSource, controller);
|
|
|
|
}
|
|
|
|
if ("cancel" in underlyingSourceDict) {
|
|
|
|
cancelAlgorithm = (reason) =>
|
|
|
|
underlyingSourceDict.cancel.call(underlyingSource, reason);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
setUpReadableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
pullAlgorithm,
|
|
|
|
cancelAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template R
|
|
|
|
* @param {ReadableStreamDefaultReader<R>} reader
|
|
|
|
* @param {ReadableStream<R>} stream
|
|
|
|
*/
|
|
|
|
function setUpReadableStreamDefaultReader(reader, stream) {
|
|
|
|
if (isReadableStreamLocked(stream)) {
|
|
|
|
throw new TypeError("ReadableStream is locked.");
|
|
|
|
}
|
|
|
|
readableStreamReaderGenericInitialize(reader, stream);
|
|
|
|
reader[_readRequests] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStream<any, O>} stream
|
|
|
|
* @param {TransformStreamDefaultController<O>} controller
|
|
|
|
* @param {(chunk: O, controller: TransformStreamDefaultController<O>) => Promise<void>} transformAlgorithm
|
|
|
|
* @param {(controller: TransformStreamDefaultController<O>) => Promise<void>} flushAlgorithm
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpTransformStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
transformAlgorithm,
|
|
|
|
flushAlgorithm,
|
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream instanceof TransformStream);
|
|
|
|
assert(stream[_controller] === undefined);
|
|
|
|
controller[_stream] = stream;
|
|
|
|
stream[_controller] = controller;
|
|
|
|
controller[_transformAlgorithm] = transformAlgorithm;
|
|
|
|
controller[_flushAlgorithm] = flushAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStream<I, O>} stream
|
|
|
|
* @param {Transformer<I, O>} transformer
|
|
|
|
* @param {Transformer<I, O>} transformerDict
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpTransformStreamDefaultControllerFromTransformer(
|
|
|
|
stream,
|
|
|
|
transformer,
|
2021-01-14 16:57:19 -05:00
|
|
|
transformerDict,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {TransformStreamDefaultController<O>} */
|
|
|
|
const controller = new TransformStreamDefaultController();
|
|
|
|
/** @type {(chunk: O, controller: TransformStreamDefaultController<O>) => Promise<void>} */
|
2020-07-19 13:49:44 -04:00
|
|
|
let transformAlgorithm = (chunk) => {
|
|
|
|
try {
|
2021-01-14 16:57:19 -05:00
|
|
|
transformStreamDefaultControllerEnqueue(controller, chunk);
|
2020-07-19 13:49:44 -04:00
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return resolvePromiseWith(undefined);
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {(controller: TransformStreamDefaultController<O>) => Promise<void>} */
|
|
|
|
let flushAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
if ("transform" in transformerDict) {
|
|
|
|
transformAlgorithm = (chunk, controller) =>
|
|
|
|
transformerDict.transform.call(transformer, chunk, controller);
|
|
|
|
}
|
|
|
|
if ("flush" in transformerDict) {
|
|
|
|
flushAlgorithm = (controller) =>
|
|
|
|
transformerDict.flush.call(transformer, controller);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
setUpTransformStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
transformAlgorithm,
|
|
|
|
flushAlgorithm,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStream<W>} stream
|
|
|
|
* @param {WritableStreamDefaultController<W>} controller
|
|
|
|
* @param {(controller: WritableStreamDefaultController<W>) => Promise<void>} startAlgorithm
|
|
|
|
* @param {(chunk: W, controller: WritableStreamDefaultController<W>) => Promise<void>} writeAlgorithm
|
|
|
|
* @param {() => Promise<void>} closeAlgorithm
|
|
|
|
* @param {(reason?: any) => Promise<void>} abortAlgorithm
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {(chunk: W) => number} sizeAlgorithm
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
function setUpWritableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
writeAlgorithm,
|
|
|
|
closeAlgorithm,
|
|
|
|
abortAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
) {
|
|
|
|
assert(isWritableStream(stream));
|
2021-01-14 16:57:19 -05:00
|
|
|
assert(stream[_controller] === undefined);
|
|
|
|
controller[_stream] = stream;
|
|
|
|
stream[_controller] = controller;
|
|
|
|
resetQueue(controller);
|
|
|
|
controller[_started] = false;
|
|
|
|
controller[_strategySizeAlgorithm] = sizeAlgorithm;
|
|
|
|
controller[_strategyHWM] = highWaterMark;
|
|
|
|
controller[_writeAlgorithm] = writeAlgorithm;
|
|
|
|
controller[_closeAlgorithm] = closeAlgorithm;
|
|
|
|
controller[_abortAlgorithm] = abortAlgorithm;
|
2020-07-19 13:49:44 -04:00
|
|
|
const backpressure = writableStreamDefaultControllerGetBackpressure(
|
|
|
|
controller,
|
|
|
|
);
|
|
|
|
writableStreamUpdateBackpressure(stream, backpressure);
|
2021-01-14 16:57:19 -05:00
|
|
|
const startResult = startAlgorithm(controller);
|
|
|
|
const startPromise = resolvePromiseWith(startResult);
|
|
|
|
uponPromise(startPromise, () => {
|
|
|
|
assert(stream[_state] === "writable" || stream[_state] === "erroring");
|
|
|
|
controller[_started] = true;
|
|
|
|
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
|
|
|
}, (r) => {
|
|
|
|
assert(stream[_state] === "writable" || stream[_state] === "erroring");
|
|
|
|
controller[_started] = true;
|
|
|
|
writableStreamDealWithRejection(stream, r);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStream<W>} stream
|
|
|
|
* @param {UnderlyingSink<W>} underlyingSink
|
|
|
|
* @param {UnderlyingSink<W>} underlyingSinkDict
|
|
|
|
* @param {number} highWaterMark
|
|
|
|
* @param {(chunk: W) => number} sizeAlgorithm
|
|
|
|
*/
|
|
|
|
function setUpWritableStreamDefaultControllerFromUnderlyingSink(
|
|
|
|
stream,
|
|
|
|
underlyingSink,
|
|
|
|
underlyingSinkDict,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
) {
|
|
|
|
const controller = new WritableStreamDefaultController();
|
|
|
|
let startAlgorithm = () => undefined;
|
|
|
|
/** @type {(chunk: W) => Promise<void>} */
|
|
|
|
let writeAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
let closeAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
/** @type {(reason?: any) => Promise<void>} */
|
|
|
|
let abortAlgorithm = () => resolvePromiseWith(undefined);
|
|
|
|
if ("start" in underlyingSinkDict) {
|
|
|
|
startAlgorithm = () =>
|
|
|
|
underlyingSinkDict.start.call(underlyingSink, controller);
|
|
|
|
}
|
|
|
|
if ("write" in underlyingSinkDict) {
|
|
|
|
writeAlgorithm = (chunk) =>
|
|
|
|
underlyingSinkDict.write.call(underlyingSink, chunk, controller);
|
|
|
|
}
|
|
|
|
if ("close" in underlyingSinkDict) {
|
|
|
|
closeAlgorithm = () => underlyingSinkDict.close.call(underlyingSink);
|
|
|
|
}
|
|
|
|
if ("abort" in underlyingSinkDict) {
|
|
|
|
abortAlgorithm = (reason) =>
|
|
|
|
underlyingSinkDict.abort.call(underlyingSink, reason);
|
|
|
|
}
|
|
|
|
setUpWritableStreamDefaultController(
|
|
|
|
stream,
|
|
|
|
controller,
|
|
|
|
startAlgorithm,
|
|
|
|
writeAlgorithm,
|
|
|
|
closeAlgorithm,
|
|
|
|
abortAlgorithm,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultWriter<W>} writer
|
|
|
|
* @param {WritableStream<W>} stream
|
|
|
|
*/
|
|
|
|
function setUpWritableStreamDefaultWriter(writer, stream) {
|
|
|
|
if (isWritableStreamLocked(stream) === true) {
|
|
|
|
throw new TypeError("The stream is already locked.");
|
|
|
|
}
|
|
|
|
writer[_stream] = stream;
|
|
|
|
stream[_writer] = writer;
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "writable") {
|
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(stream) === false &&
|
|
|
|
stream[_backpressure] === true
|
|
|
|
) {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
} else {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
writer[_readyPromise].resolve(undefined);
|
|
|
|
}
|
|
|
|
writer[_closedPromise] = new Deferred();
|
|
|
|
} else if (state === "erroring") {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
writer[_readyPromise].reject(stream[_storedError]);
|
|
|
|
setPromiseIsHandledToTrue(writer[_readyPromise].promise);
|
|
|
|
writer[_closedPromise] = new Deferred();
|
|
|
|
} else if (state === "closed") {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
writer[_readyPromise].resolve(undefined);
|
|
|
|
writer[_closedPromise] = new Deferred();
|
|
|
|
writer[_closedPromise].resolve(undefined);
|
|
|
|
} else {
|
|
|
|
assert(state === "errored");
|
|
|
|
const storedError = stream[_storedError];
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
writer[_readyPromise].reject(storedError);
|
|
|
|
setPromiseIsHandledToTrue(writer[_readyPromise].promise);
|
|
|
|
writer[_closedPromise] = new Deferred();
|
|
|
|
writer[_closedPromise].reject(storedError);
|
|
|
|
setPromiseIsHandledToTrue(writer[_closedPromise].promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {TransformStreamDefaultController} controller */
|
|
|
|
function transformStreamDefaultControllerClearAlgorithms(controller) {
|
|
|
|
controller[_transformAlgorithm] = undefined;
|
|
|
|
controller[_flushAlgorithm] = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStreamDefaultController<O>} controller
|
|
|
|
* @param {O} chunk
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultControllerEnqueue(controller, chunk) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
const readableController = stream[_readable][_controller];
|
|
|
|
if (
|
|
|
|
readableStreamDefaultControllerCanCloseOrEnqueue(
|
|
|
|
/** @type {ReadableStreamDefaultController<O>} */ (readableController),
|
|
|
|
) === false
|
|
|
|
) {
|
|
|
|
throw new TypeError("Readable stream is unavailable.");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
readableStreamDefaultControllerEnqueue(
|
|
|
|
/** @type {ReadableStreamDefaultController<O>} */ (readableController),
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
transformStreamErrorWritableAndUnblockWrite(stream, e);
|
|
|
|
throw stream[_readable][_storedError];
|
|
|
|
}
|
|
|
|
const backpressure = readableStreamDefaultcontrollerHasBackpressure(
|
|
|
|
/** @type {ReadableStreamDefaultController<O>} */ (readableController),
|
|
|
|
);
|
|
|
|
if (backpressure !== stream[_backpressure]) {
|
|
|
|
assert(backpressure === true);
|
|
|
|
transformStreamSetBackpressure(stream, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStreamDefaultController} controller
|
|
|
|
* @param {any=} e
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultControllerError(controller, e) {
|
|
|
|
transformStreamError(controller[_stream], e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStreamDefaultController<O>} controller
|
|
|
|
* @param {any} chunk
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultControllerPerformTransform(controller, chunk) {
|
|
|
|
const transformPromise = controller[_transformAlgorithm](chunk, controller);
|
|
|
|
return transformPromiseWith(transformPromise, undefined, (r) => {
|
|
|
|
transformStreamError(controller[_stream], r);
|
|
|
|
throw r;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {TransformStreamDefaultController} controller */
|
|
|
|
function transformStreamDefaultControllerTerminate(controller) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
const readableController = stream[_readable][_controller];
|
|
|
|
readableStreamDefaultControllerClose(
|
|
|
|
/** @type {ReadableStreamDefaultController} */ (readableController),
|
|
|
|
);
|
|
|
|
const error = new TypeError("The stream has been terminated.");
|
|
|
|
transformStreamErrorWritableAndUnblockWrite(stream, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStream} stream
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultSinkAbortAlgorithm(stream, reason) {
|
|
|
|
transformStreamError(stream, reason);
|
|
|
|
return resolvePromiseWith(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStream<I, O>} stream
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultSinkCloseAlgorithm(stream) {
|
|
|
|
const readable = stream[_readable];
|
|
|
|
const controller = stream[_controller];
|
|
|
|
const flushPromise = controller[_flushAlgorithm](controller);
|
|
|
|
transformStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
return transformPromiseWith(flushPromise, () => {
|
|
|
|
if (readable[_state] === "errored") {
|
|
|
|
throw readable[_storedError];
|
|
|
|
}
|
|
|
|
readableStreamDefaultControllerClose(
|
|
|
|
/** @type {ReadableStreamDefaultController} */ (readable[_controller]),
|
|
|
|
);
|
|
|
|
}, (r) => {
|
|
|
|
transformStreamError(stream, r);
|
|
|
|
throw readable[_storedError];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
* @param {TransformStream<I, O>} stream
|
|
|
|
* @param {I} chunk
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) {
|
|
|
|
assert(stream[_writable][_state] === "writable");
|
|
|
|
const controller = stream[_controller];
|
|
|
|
if (stream[_backpressure] === true) {
|
|
|
|
const backpressureChangePromise = stream[_backpressureChangePromise];
|
|
|
|
assert(backpressureChangePromise !== undefined);
|
|
|
|
return transformPromiseWith(backpressureChangePromise.promise, () => {
|
|
|
|
const writable = stream[_writable];
|
|
|
|
const state = writable[_state];
|
|
|
|
if (state === "erroring") {
|
|
|
|
throw writable[_storedError];
|
|
|
|
}
|
|
|
|
assert(state === "writable");
|
|
|
|
return transformStreamDefaultControllerPerformTransform(
|
|
|
|
controller,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return transformStreamDefaultControllerPerformTransform(controller, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStream} stream
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function transformStreamDefaultSourcePullAlgorithm(stream) {
|
|
|
|
assert(stream[_backpressure] === true);
|
|
|
|
assert(stream[_backpressureChangePromise] !== undefined);
|
|
|
|
transformStreamSetBackpressure(stream, false);
|
|
|
|
return stream[_backpressureChangePromise].promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStream} stream
|
|
|
|
* @param {any=} e
|
|
|
|
*/
|
|
|
|
function transformStreamError(stream, e) {
|
|
|
|
readableStreamDefaultControllerError(
|
|
|
|
/** @type {ReadableStreamDefaultController} */ (stream[_readable][
|
|
|
|
_controller
|
|
|
|
]),
|
|
|
|
e,
|
|
|
|
);
|
|
|
|
transformStreamErrorWritableAndUnblockWrite(stream, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStream} stream
|
|
|
|
* @param {any=} e
|
|
|
|
*/
|
|
|
|
function transformStreamErrorWritableAndUnblockWrite(stream, e) {
|
|
|
|
transformStreamDefaultControllerClearAlgorithms(stream[_controller]);
|
|
|
|
writableStreamDefaultControllerErrorIfNeeded(
|
|
|
|
stream[_writable][_controller],
|
|
|
|
e,
|
|
|
|
);
|
|
|
|
if (stream[_backpressure] === true) {
|
|
|
|
transformStreamSetBackpressure(stream, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransformStream} stream
|
|
|
|
* @param {boolean} backpressure
|
|
|
|
*/
|
|
|
|
function transformStreamSetBackpressure(stream, backpressure) {
|
|
|
|
assert(stream[_backpressure] !== backpressure);
|
|
|
|
if (stream[_backpressureChangePromise] !== undefined) {
|
|
|
|
stream[_backpressureChangePromise].resolve(undefined);
|
|
|
|
}
|
|
|
|
stream[_backpressureChangePromise] = new Deferred();
|
|
|
|
stream[_backpressure] = backpressure;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamAbort(stream, reason) {
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "closed" || state === "errored") {
|
|
|
|
return resolvePromiseWith(undefined);
|
|
|
|
}
|
|
|
|
if (stream[_pendingAbortRequest] !== undefined) {
|
|
|
|
return stream[_pendingAbortRequest].deferred.promise;
|
|
|
|
}
|
|
|
|
assert(state === "writable" || state === "erroring");
|
|
|
|
let wasAlreadyErroring = false;
|
|
|
|
if (state === "erroring") {
|
|
|
|
wasAlreadyErroring = true;
|
|
|
|
reason = undefined;
|
|
|
|
}
|
|
|
|
/** Deferred<void> */
|
|
|
|
const deferred = new Deferred();
|
|
|
|
stream[_pendingAbortRequest] = {
|
|
|
|
deferred,
|
|
|
|
reason,
|
|
|
|
wasAlreadyErroring,
|
|
|
|
};
|
|
|
|
if (wasAlreadyErroring === false) {
|
|
|
|
writableStreamStartErroring(stream, reason);
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamAddWriteRequest(stream) {
|
|
|
|
assert(isWritableStreamLocked(stream) === true);
|
|
|
|
assert(stream[_state] === "writable");
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const deferred = new Deferred();
|
|
|
|
stream[_writeRequests].push(deferred);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamClose(stream) {
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "closed" || state === "errored") {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("Writable stream is closed or errored."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(state === "writable" || state === "erroring");
|
|
|
|
assert(writableStreamCloseQueuedOrInFlight(stream) === false);
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const deferred = new Deferred();
|
|
|
|
stream[_closeRequest] = deferred;
|
|
|
|
const writer = stream[_writer];
|
|
|
|
if (
|
|
|
|
writer !== undefined && stream[_backpressure] === true &&
|
|
|
|
state === "writable"
|
|
|
|
) {
|
|
|
|
writer[_readyPromise].resolve(undefined);
|
|
|
|
}
|
|
|
|
writableStreamDefaultControllerClose(stream[_controller]);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function writableStreamCloseQueuedOrInFlight(stream) {
|
|
|
|
if (
|
|
|
|
stream[_closeRequest] === undefined &&
|
|
|
|
stream[_inFlightCloseRequest] === undefined
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function writableStreamDealWithRejection(stream, error) {
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "writable") {
|
|
|
|
writableStreamStartErroring(stream, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(state === "erroring");
|
|
|
|
writableStreamFinishErroring(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultController<W>} controller
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (controller[_started] === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stream[_inFlightWriteRequest] !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const state = stream[_state];
|
|
|
|
assert(state !== "closed" && state !== "errored");
|
|
|
|
if (state === "erroring") {
|
|
|
|
writableStreamFinishErroring(stream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (controller[_queue].length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const value = peekQueueValue(controller);
|
|
|
|
if (value === _close) {
|
|
|
|
writableStreamDefaultControllerProcessClose(controller);
|
|
|
|
} else {
|
|
|
|
writableStreamDefaultControllerProcessWrite(controller, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function writableStreamDefaultControllerClearAlgorithms(controller) {
|
|
|
|
controller[_writeAlgorithm] = undefined;
|
|
|
|
controller[_closeAlgorithm] = undefined;
|
|
|
|
controller[_abortAlgorithm] = undefined;
|
|
|
|
controller[_strategySizeAlgorithm] = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {WritableStreamDefaultController} controller */
|
|
|
|
function writableStreamDefaultControllerClose(controller) {
|
|
|
|
enqueueValueWithSize(controller, _close, 0);
|
|
|
|
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultController} controller
|
|
|
|
* @param {any} error
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerError(controller, error) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
assert(stream[_state] === "writable");
|
|
|
|
writableStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
writableStreamStartErroring(stream, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultController} controller
|
|
|
|
* @param {any} error
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerErrorIfNeeded(controller, error) {
|
|
|
|
if (controller[_stream][_state] === "writable") {
|
|
|
|
writableStreamDefaultControllerError(controller, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultController} controller
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerGetBackpressure(controller) {
|
|
|
|
const desiredSize = writableStreamDefaultControllerGetDesiredSize(
|
|
|
|
controller,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
return desiredSize <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultController<W>} controller
|
|
|
|
* @param {W} chunk
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerGetChunkSize(controller, chunk) {
|
|
|
|
let value;
|
|
|
|
try {
|
|
|
|
value = controller[_strategySizeAlgorithm](chunk);
|
|
|
|
} catch (e) {
|
|
|
|
writableStreamDefaultControllerErrorIfNeeded(controller, e);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultController} controller
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerGetDesiredSize(controller) {
|
|
|
|
return controller[_strategyHWM] - controller[_queueTotalSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {WritableStreamDefaultController} controller */
|
|
|
|
function writableStreamDefaultControllerProcessClose(controller) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
writableStreamMarkCloseRequestInFlight(stream);
|
|
|
|
dequeueValue(controller);
|
|
|
|
assert(controller[_queue].length === 0);
|
|
|
|
const sinkClosePromise = controller[_closeAlgorithm]();
|
|
|
|
writableStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
uponPromise(sinkClosePromise, () => {
|
|
|
|
writableStreamFinishInFlightClose(stream);
|
|
|
|
}, (reason) => {
|
|
|
|
writableStreamFinishInFlightCloseWithError(stream, reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultController<W>} controller
|
|
|
|
* @param {W} chunk
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerProcessWrite(controller, chunk) {
|
|
|
|
const stream = controller[_stream];
|
|
|
|
writableStreamMarkFirstWriteRequestInFlight(stream);
|
|
|
|
const sinkWritePromise = controller[_writeAlgorithm](chunk, controller);
|
|
|
|
uponPromise(sinkWritePromise, () => {
|
|
|
|
writableStreamFinishInFlightWrite(stream);
|
|
|
|
const state = stream[_state];
|
|
|
|
assert(state === "writable" || state === "erroring");
|
|
|
|
dequeueValue(controller);
|
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(stream) === false &&
|
|
|
|
state === "writable"
|
|
|
|
) {
|
|
|
|
const backpressure = writableStreamDefaultControllerGetBackpressure(
|
|
|
|
controller,
|
|
|
|
);
|
|
|
|
writableStreamUpdateBackpressure(stream, backpressure);
|
|
|
|
}
|
|
|
|
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
|
|
|
}, (reason) => {
|
|
|
|
if (stream[_state] === "writable") {
|
|
|
|
writableStreamDefaultControllerClearAlgorithms(controller);
|
|
|
|
}
|
|
|
|
writableStreamFinishInFlightWriteWithError(stream, reason);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultController<W>} controller
|
|
|
|
* @param {W} chunk
|
|
|
|
* @param {number} chunkSize
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultControllerWrite(controller, chunk, chunkSize) {
|
|
|
|
try {
|
|
|
|
enqueueValueWithSize(controller, chunk, chunkSize);
|
|
|
|
} catch (e) {
|
|
|
|
writableStreamDefaultControllerErrorIfNeeded(controller, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stream = controller[_stream];
|
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(stream) === false &&
|
|
|
|
stream[_state] === "writable"
|
|
|
|
) {
|
|
|
|
const backpressure = writableStreamDefaultControllerGetBackpressure(
|
|
|
|
controller,
|
|
|
|
);
|
|
|
|
writableStreamUpdateBackpressure(stream, backpressure);
|
|
|
|
}
|
|
|
|
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterAbort(writer, reason) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
assert(stream !== undefined);
|
|
|
|
return writableStreamAbort(stream, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterClose(writer) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
assert(stream !== undefined);
|
|
|
|
return writableStreamClose(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterCloseWithErrorPropagation(writer) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
assert(stream !== undefined);
|
|
|
|
const state = stream[_state];
|
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(stream) === true || state === "closed"
|
|
|
|
) {
|
|
|
|
return resolvePromiseWith(undefined);
|
|
|
|
}
|
|
|
|
if (state === "errored") {
|
|
|
|
return Promise.reject(stream[_storedError]);
|
|
|
|
}
|
|
|
|
assert(state === "writable" || state === "erroring");
|
|
|
|
return writableStreamDefaultWriterClose(writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterEnsureClosedPromiseRejected(
|
|
|
|
writer,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
if (writer[_closedPromise].state === "pending") {
|
|
|
|
writer[_closedPromise].reject(error);
|
|
|
|
} else {
|
|
|
|
writer[_closedPromise] = new Deferred();
|
|
|
|
writer[_closedPromise].reject(error);
|
|
|
|
}
|
|
|
|
setPromiseIsHandledToTrue(writer[_closedPromise].promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterEnsureReadyPromiseRejected(
|
|
|
|
writer,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
if (writer[_readyPromise].state === "pending") {
|
|
|
|
writer[_readyPromise].reject(error);
|
|
|
|
} else {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
writer[_readyPromise].reject(error);
|
|
|
|
}
|
|
|
|
setPromiseIsHandledToTrue(writer[_readyPromise].promise);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStreamDefaultWriter} writer
|
|
|
|
* @returns {number | null}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterGetDesiredSize(writer) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "errored" || state === "erroring") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (state === "closed") {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return writableStreamDefaultControllerGetDesiredSize(stream[_controller]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {WritableStreamDefaultWriter} writer */
|
|
|
|
function writableStreamDefaultWriterRelease(writer) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
assert(stream !== undefined);
|
|
|
|
assert(stream[_writer] === writer);
|
|
|
|
const releasedError = new TypeError(
|
|
|
|
"The writer has already been released.",
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
writableStreamDefaultWriterEnsureReadyPromiseRejected(
|
|
|
|
writer,
|
|
|
|
releasedError,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
writableStreamDefaultWriterEnsureClosedPromiseRejected(
|
|
|
|
writer,
|
|
|
|
releasedError,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_writer] = undefined;
|
|
|
|
writer[_stream] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStreamDefaultWriter<W>} writer
|
|
|
|
* @param {W} chunk
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function writableStreamDefaultWriterWrite(writer, chunk) {
|
|
|
|
const stream = writer[_stream];
|
|
|
|
assert(stream !== undefined);
|
|
|
|
const controller = stream[_controller];
|
|
|
|
const chunkSize = writableStreamDefaultControllerGetChunkSize(
|
|
|
|
controller,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
if (stream !== writer[_stream]) {
|
|
|
|
return Promise.reject(new TypeError("Writer's stream is unexpected."));
|
|
|
|
}
|
|
|
|
const state = stream[_state];
|
|
|
|
if (state === "errored") {
|
|
|
|
return Promise.reject(stream[_storedError]);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
writableStreamCloseQueuedOrInFlight(stream) === true || state === "closed"
|
|
|
|
) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("The stream is closing or is closed."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (state === "erroring") {
|
|
|
|
return Promise.reject(stream[_storedError]);
|
|
|
|
}
|
|
|
|
assert(state === "writable");
|
|
|
|
const promise = writableStreamAddWriteRequest(stream);
|
|
|
|
writableStreamDefaultControllerWrite(controller, chunk, chunkSize);
|
|
|
|
return promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function writableStreamFinishErroring(stream) {
|
|
|
|
assert(stream[_state] === "erroring");
|
|
|
|
assert(writableStreamHasOperationMarkedInFlight(stream) === false);
|
|
|
|
stream[_state] = "errored";
|
|
|
|
stream[_controller][_errorSteps]();
|
|
|
|
const storedError = stream[_storedError];
|
|
|
|
for (const writeRequest of stream[_writeRequests]) {
|
|
|
|
writeRequest.reject(storedError);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_writeRequests] = [];
|
|
|
|
if (stream[_pendingAbortRequest] === undefined) {
|
|
|
|
writableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
const abortRequest = stream[_pendingAbortRequest];
|
|
|
|
stream[_pendingAbortRequest] = undefined;
|
|
|
|
if (abortRequest.wasAlreadyErroring === true) {
|
|
|
|
abortRequest.deferred.reject(storedError);
|
|
|
|
writableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
const promise = stream[_controller][_abortSteps](abortRequest.reason);
|
|
|
|
uponPromise(promise, () => {
|
|
|
|
abortRequest.deferred.resolve(undefined);
|
|
|
|
writableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
|
|
|
}, (reason) => {
|
|
|
|
abortRequest.deferred.reject(reason);
|
|
|
|
writableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function writableStreamFinishInFlightClose(stream) {
|
|
|
|
assert(stream[_inFlightCloseRequest] !== undefined);
|
|
|
|
stream[_inFlightCloseRequest].resolve(undefined);
|
|
|
|
stream[_inFlightCloseRequest] = undefined;
|
|
|
|
const state = stream[_state];
|
|
|
|
assert(state === "writable" || state === "erroring");
|
|
|
|
if (state === "erroring") {
|
|
|
|
stream[_storedError] = undefined;
|
|
|
|
if (stream[_pendingAbortRequest] !== undefined) {
|
|
|
|
stream[_pendingAbortRequest].deferred.resolve(undefined);
|
|
|
|
stream[_pendingAbortRequest] = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream[_state] = "closed";
|
|
|
|
const writer = stream[_writer];
|
|
|
|
if (writer !== undefined) {
|
|
|
|
writer[_closedPromise].resolve(undefined);
|
|
|
|
}
|
|
|
|
assert(stream[_pendingAbortRequest] === undefined);
|
|
|
|
assert(stream[_storedError] === undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function writableStreamFinishInFlightCloseWithError(stream, error) {
|
|
|
|
assert(stream[_inFlightCloseRequest] !== undefined);
|
|
|
|
stream[_inFlightCloseRequest].reject(error);
|
|
|
|
stream[_inFlightCloseRequest] = undefined;
|
|
|
|
assert(stream[_state] === "writable" || stream[_state] === "erroring");
|
|
|
|
if (stream[_pendingAbortRequest] !== undefined) {
|
|
|
|
stream[_pendingAbortRequest].deferred.reject(error);
|
|
|
|
stream[_pendingAbortRequest] = undefined;
|
|
|
|
}
|
|
|
|
writableStreamDealWithRejection(stream, error);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function writableStreamFinishInFlightWrite(stream) {
|
|
|
|
assert(stream[_inFlightWriteRequest] !== undefined);
|
|
|
|
stream[_inFlightWriteRequest].resolve(undefined);
|
|
|
|
stream[_inFlightWriteRequest] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {any=} error
|
|
|
|
*/
|
|
|
|
function writableStreamFinishInFlightWriteWithError(stream, error) {
|
|
|
|
assert(stream[_inFlightWriteRequest] !== undefined);
|
|
|
|
stream[_inFlightWriteRequest].reject(error);
|
|
|
|
stream[_inFlightWriteRequest] = undefined;
|
|
|
|
assert(stream[_state] === "writable" || stream[_state] === "erroring");
|
|
|
|
writableStreamDealWithRejection(stream, error);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function writableStreamHasOperationMarkedInFlight(stream) {
|
|
|
|
if (
|
|
|
|
stream[_inFlightWriteRequest] === undefined &&
|
|
|
|
stream[_controller][_inFlightCloseRequest] === undefined
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function writableStreamMarkCloseRequestInFlight(stream) {
|
|
|
|
assert(stream[_inFlightCloseRequest] === undefined);
|
|
|
|
assert(stream[_closeRequest] !== undefined);
|
|
|
|
stream[_inFlightCloseRequest] = stream[_closeRequest];
|
|
|
|
stream[_closeRequest] = undefined;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template W
|
|
|
|
* @param {WritableStream<W>} stream
|
|
|
|
* */
|
|
|
|
function writableStreamMarkFirstWriteRequestInFlight(stream) {
|
|
|
|
assert(stream[_inFlightWriteRequest] === undefined);
|
|
|
|
assert(stream[_writeRequests].length);
|
|
|
|
const writeRequest = stream[_writeRequests].shift();
|
|
|
|
stream[_inFlightWriteRequest] = writeRequest;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {WritableStream} stream */
|
|
|
|
function writableStreamRejectCloseAndClosedPromiseIfNeeded(stream) {
|
|
|
|
assert(stream[_state] === "errored");
|
|
|
|
if (stream[_closeRequest] !== undefined) {
|
|
|
|
assert(stream[_inFlightCloseRequest] === undefined);
|
|
|
|
stream[_closeRequest].reject(stream[_storedError]);
|
|
|
|
stream[_closeRequest] = undefined;
|
|
|
|
}
|
|
|
|
const writer = stream[_writer];
|
|
|
|
if (writer !== undefined) {
|
|
|
|
writer[_closedPromise].reject(stream[_storedError]);
|
|
|
|
setPromiseIsHandledToTrue(writer[_closedPromise].promise);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {any=} reason
|
|
|
|
*/
|
|
|
|
function writableStreamStartErroring(stream, reason) {
|
|
|
|
assert(stream[_storedError] === undefined);
|
|
|
|
assert(stream[_state] === "writable");
|
|
|
|
const controller = stream[_controller];
|
|
|
|
assert(controller);
|
|
|
|
stream[_state] = "erroring";
|
|
|
|
stream[_storedError] = reason;
|
|
|
|
const writer = stream[_writer];
|
|
|
|
if (writer) {
|
|
|
|
writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
writableStreamHasOperationMarkedInFlight(stream) === false &&
|
|
|
|
controller[_started] === true
|
|
|
|
) {
|
|
|
|
writableStreamFinishErroring(stream);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream} stream
|
|
|
|
* @param {boolean} backpressure
|
|
|
|
*/
|
|
|
|
function writableStreamUpdateBackpressure(stream, backpressure) {
|
|
|
|
assert(stream[_state] === "writable");
|
|
|
|
assert(writableStreamCloseQueuedOrInFlight(stream) === false);
|
|
|
|
const writer = stream[_writer];
|
|
|
|
if (writer !== undefined && backpressure !== stream[_backpressure]) {
|
|
|
|
if (backpressure === true) {
|
|
|
|
writer[_readyPromise] = new Deferred();
|
|
|
|
} else {
|
|
|
|
assert(backpressure === false);
|
|
|
|
writer[_readyPromise].resolve(undefined);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
stream[_backpressure] = backpressure;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {T} value
|
|
|
|
* @param {boolean} done
|
|
|
|
* @returns {IteratorResult<T>}
|
|
|
|
*/
|
|
|
|
function createIteratorResult(value, done) {
|
|
|
|
const result = Object.create(null);
|
|
|
|
Object.defineProperties(result, {
|
|
|
|
value: { value, writable: true, enumerable: true, configurable: true },
|
|
|
|
done: {
|
|
|
|
value: done,
|
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {AsyncIterator<unknown, unknown>} */
|
|
|
|
const asyncIteratorPrototype = Object.getPrototypeOf(
|
|
|
|
Object.getPrototypeOf(async function* () {}).prototype,
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @type {AsyncIterator<unknown>} */
|
|
|
|
const readableStreamAsyncIteratorPrototype = Object.setPrototypeOf({
|
|
|
|
/** @returns {Promise<IteratorResult<unknown>>} */
|
|
|
|
next() {
|
|
|
|
/** @type {ReadableStreamDefaultReader} */
|
|
|
|
const reader = this[_reader];
|
|
|
|
if (reader[_stream] === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError(
|
|
|
|
"Cannot get the next iteration result once the reader has been released.",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/** @type {Deferred<IteratorResult<any>>} */
|
|
|
|
const promise = new Deferred();
|
|
|
|
/** @type {ReadRequest} */
|
|
|
|
const readRequest = {
|
|
|
|
chunkSteps(chunk) {
|
|
|
|
promise.resolve(createIteratorResult(chunk, false));
|
|
|
|
},
|
|
|
|
closeSteps() {
|
|
|
|
readableStreamReaderGenericRelease(reader);
|
|
|
|
promise.resolve(createIteratorResult(undefined, true));
|
|
|
|
},
|
|
|
|
errorSteps(e) {
|
|
|
|
readableStreamReaderGenericRelease(reader);
|
|
|
|
promise.reject(e);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
readableStreamDefaultReaderRead(reader, readRequest);
|
|
|
|
return promise.promise;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {unknown} arg
|
|
|
|
* @returns {Promise<IteratorResult<unknown>>}
|
|
|
|
*/
|
|
|
|
async return(arg) {
|
|
|
|
/** @type {ReadableStreamDefaultReader} */
|
|
|
|
const reader = this[_reader];
|
|
|
|
if (reader[_stream] === undefined) {
|
|
|
|
return createIteratorResult(undefined, true);
|
|
|
|
}
|
|
|
|
assert(reader[_readRequests].length === 0);
|
|
|
|
if (this[_preventCancel] === false) {
|
|
|
|
const result = readableStreamReaderGenericCancel(reader, arg);
|
|
|
|
readableStreamReaderGenericRelease(reader);
|
|
|
|
await result;
|
|
|
|
return createIteratorResult(arg, true);
|
|
|
|
}
|
|
|
|
readableStreamReaderGenericRelease(reader);
|
|
|
|
return createIteratorResult(undefined, true);
|
|
|
|
},
|
|
|
|
}, asyncIteratorPrototype);
|
|
|
|
|
|
|
|
class ByteLengthQueuingStrategy {
|
|
|
|
/** @type {number} */
|
|
|
|
highWaterMark;
|
|
|
|
|
|
|
|
/** @param {{ highWaterMark: number }} init */
|
|
|
|
constructor(init) {
|
|
|
|
if (
|
|
|
|
typeof init !== "object" || init === null || !("highWaterMark" in init)
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
|
|
|
"init must be an object that contains a property named highWaterMark",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { highWaterMark } = init;
|
|
|
|
this[_globalObject] = window;
|
|
|
|
this.highWaterMark = Number(highWaterMark);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {(chunk: ArrayBufferView) => number} */
|
|
|
|
get size() {
|
|
|
|
initializeByteLengthSizeFunction(this[_globalObject]);
|
|
|
|
return byteSizeFunctionWeakMap.get(this[_globalObject]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {WeakMap<typeof globalThis, (chunk: ArrayBufferView) => number>} */
|
|
|
|
const byteSizeFunctionWeakMap = new WeakMap();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
function initializeByteLengthSizeFunction(globalObject) {
|
|
|
|
if (byteSizeFunctionWeakMap.has(globalObject)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
byteSizeFunctionWeakMap.set(globalObject, function size(chunk) {
|
|
|
|
return chunk.byteLength;
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2021-01-14 16:57:19 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
class CountQueuingStrategy {
|
|
|
|
/** @type {number} */
|
|
|
|
highWaterMark;
|
|
|
|
|
|
|
|
/** @param {{ highWaterMark: number }} init */
|
|
|
|
constructor(init) {
|
|
|
|
if (
|
|
|
|
typeof init !== "object" || init === null || !("highWaterMark" in init)
|
|
|
|
) {
|
|
|
|
throw new TypeError(
|
|
|
|
"init must be an object that contains a property named highWaterMark",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { highWaterMark } = init;
|
|
|
|
this[_globalObject] = window;
|
|
|
|
this.highWaterMark = Number(highWaterMark);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {(chunk: any) => 1} */
|
|
|
|
get size() {
|
|
|
|
initializeCountSizeFunction(this[_globalObject]);
|
|
|
|
return countSizeFunctionWeakMap.get(this[_globalObject]);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {WeakMap<typeof globalThis, () => 1>} */
|
|
|
|
const countSizeFunctionWeakMap = new WeakMap();
|
|
|
|
|
|
|
|
/** @param {typeof globalThis} globalObject */
|
|
|
|
function initializeCountSizeFunction(globalObject) {
|
|
|
|
if (countSizeFunctionWeakMap.has(globalObject)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
countSizeFunctionWeakMap.set(globalObject, function size() {
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @template R */
|
|
|
|
class ReadableStream {
|
|
|
|
/** @type {ReadableStreamDefaultController | ReadableByteStreamController} */
|
|
|
|
[_controller];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_detached];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_disturbed];
|
|
|
|
/** @type {ReadableStreamDefaultReader | undefined} */
|
|
|
|
[_reader];
|
|
|
|
/** @type {"readable" | "closed" | "errored"} */
|
|
|
|
[_state];
|
|
|
|
/** @type {any} */
|
|
|
|
[_storedError];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {UnderlyingSource<R>=} underlyingSource
|
|
|
|
* @param {QueuingStrategy<R>=} strategy
|
|
|
|
*/
|
|
|
|
constructor(underlyingSource, strategy = {}) {
|
|
|
|
const underlyingSourceDict = convertUnderlyingSource(underlyingSource);
|
|
|
|
initializeReadableStream(this);
|
|
|
|
if (underlyingSourceDict.type === "bytes") {
|
|
|
|
if (strategy.size !== undefined) {
|
|
|
|
throw new RangeError(
|
|
|
|
`When underlying source is "bytes", strategy.size must be undefined.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const highWaterMark = extractHighWaterMark(strategy, 0);
|
|
|
|
setUpReadableByteStreamControllerFromUnderlyingSource(
|
|
|
|
// @ts-ignore cannot easily assert this is ReadableStream<ArrayBuffer>
|
|
|
|
this,
|
|
|
|
underlyingSource,
|
|
|
|
underlyingSourceDict,
|
|
|
|
highWaterMark,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert(!("type" in underlyingSourceDict));
|
|
|
|
const sizeAlgorithm = extractSizeAlgorithm(strategy);
|
|
|
|
const highWaterMark = extractHighWaterMark(strategy, 1);
|
|
|
|
setUpReadableStreamDefaultControllerFromUnderlyingSource(
|
|
|
|
this,
|
|
|
|
underlyingSource,
|
|
|
|
underlyingSourceDict,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {boolean} */
|
|
|
|
get locked() {
|
|
|
|
return isReadableStreamLocked(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
cancel(reason) {
|
|
|
|
if (isReadableStreamLocked(this)) {
|
|
|
|
Promise.reject(new TypeError("Cannot cancel a locked ReadableStream."));
|
|
|
|
}
|
|
|
|
return readableStreamCancel(this, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadableStreamGetReaderOptions=} options
|
|
|
|
* @returns {ReadableStreamDefaultReader<R>}
|
|
|
|
*/
|
|
|
|
getReader(options = {}) {
|
|
|
|
if (typeof options !== "object") {
|
|
|
|
throw new TypeError("options must be an object");
|
|
|
|
}
|
|
|
|
if (options === null) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
/** @type {any} */
|
|
|
|
let { mode } = options;
|
|
|
|
if (mode === undefined) {
|
|
|
|
return acquireReadableStreamDefaultReader(this);
|
|
|
|
}
|
|
|
|
mode = String(mode);
|
|
|
|
if (mode !== "byob") {
|
|
|
|
throw new TypeError("Invalid mode.");
|
|
|
|
}
|
|
|
|
// 3. Return ? AcquireReadableStreamBYOBReader(this).
|
|
|
|
throw new RangeError(`Unsupported mode "${String(mode)}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {{ readable: ReadableStream<T>, writable: WritableStream<R> }} transform
|
|
|
|
* @param {PipeOptions=} options
|
|
|
|
* @returns {ReadableStream<T>}
|
|
|
|
*/
|
|
|
|
pipeThrough(
|
|
|
|
{ readable, writable },
|
|
|
|
{ preventClose, preventAbort, preventCancel, signal } = {},
|
|
|
|
) {
|
|
|
|
if (isReadableStreamLocked(this)) {
|
|
|
|
throw new TypeError("ReadableStream is already locked.");
|
|
|
|
}
|
|
|
|
if (isWritableStreamLocked(writable)) {
|
|
|
|
throw new TypeError("Target WritableStream is already locked.");
|
|
|
|
}
|
|
|
|
const promise = readableStreamPipeTo(
|
|
|
|
this,
|
|
|
|
writable,
|
|
|
|
preventClose,
|
|
|
|
preventAbort,
|
|
|
|
preventCancel,
|
|
|
|
signal,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2021-01-14 16:57:19 -05:00
|
|
|
setPromiseIsHandledToTrue(promise);
|
|
|
|
return readable;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {WritableStream<R>} destination
|
|
|
|
* @param {PipeOptions=} options
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
pipeTo(
|
|
|
|
destination,
|
|
|
|
{
|
|
|
|
preventClose = false,
|
|
|
|
preventAbort = false,
|
|
|
|
preventCancel = false,
|
|
|
|
signal,
|
|
|
|
} = {},
|
|
|
|
) {
|
|
|
|
if (isReadableStreamLocked(this)) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("ReadableStream is already locked."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isWritableStreamLocked(destination)) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("destination WritableStream is already locked."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return readableStreamPipeTo(
|
|
|
|
this,
|
|
|
|
destination,
|
|
|
|
preventClose,
|
|
|
|
preventAbort,
|
|
|
|
preventCancel,
|
|
|
|
signal,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {[ReadableStream<R>, ReadableStream<R>]} */
|
|
|
|
tee() {
|
|
|
|
return readableStreamTee(this, false);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ReadableStreamIteratorOptions=} options
|
|
|
|
* @returns {AsyncIterableIterator<R>}
|
|
|
|
*/
|
|
|
|
[Symbol.asyncIterator]({ preventCancel } = {}) {
|
|
|
|
/** @type {AsyncIterableIterator<R>} */
|
|
|
|
const iterator = Object.create(readableStreamAsyncIteratorPrototype);
|
|
|
|
const reader = acquireReadableStreamDefaultReader(this);
|
|
|
|
iterator[_reader] = reader;
|
|
|
|
iterator[_preventCancel] = preventCancel;
|
|
|
|
return iterator;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
[customInspect]() {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
Deno.inspect({ locked: this.locked })
|
|
|
|
}`;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template R */
|
|
|
|
class ReadableStreamGenericReader {
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
[_closedPromise];
|
|
|
|
/** @type {ReadableStream<R> | undefined} */
|
|
|
|
[_stream];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
get closed() {
|
|
|
|
return this[_closedPromise].promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
cancel(reason) {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("Reader has no associated stream."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return readableStreamReaderGenericCancel(this, reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template R */
|
|
|
|
class ReadableStreamDefaultReader extends ReadableStreamGenericReader {
|
|
|
|
/** @type {ReadRequest[]} */
|
|
|
|
[_readRequests];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @param {ReadableStream<R>} stream */
|
|
|
|
constructor(stream) {
|
|
|
|
if (!(stream instanceof ReadableStream)) {
|
|
|
|
throw new TypeError("stream is not a ReadableStream");
|
|
|
|
}
|
|
|
|
super();
|
|
|
|
setUpReadableStreamDefaultReader(this, stream);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {Promise<ReadableStreamReadResult<R>>} */
|
|
|
|
read() {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("Reader has no associated stream."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/** @type {Deferred<ReadableStreamReadResult<R>>} */
|
|
|
|
const promise = new Deferred();
|
|
|
|
/** @type {ReadRequest<R>} */
|
|
|
|
const readRequest = {
|
|
|
|
chunkSteps(chunk) {
|
|
|
|
promise.resolve({ value: chunk, done: false });
|
|
|
|
},
|
|
|
|
closeSteps() {
|
|
|
|
promise.resolve({ value: undefined, done: true });
|
|
|
|
},
|
|
|
|
errorSteps(e) {
|
|
|
|
promise.reject(e);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
readableStreamDefaultReaderRead(this, readRequest);
|
|
|
|
return promise.promise;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {void} */
|
|
|
|
releaseLock() {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this[_readRequests].length) {
|
|
|
|
throw new TypeError(
|
|
|
|
"There are pending read requests, so the reader cannot be release.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
readableStreamReaderGenericRelease(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
[customInspect]() {
|
|
|
|
return `${this.constructor.name} { closed: ${String(this.closed)} }`;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
class ReadableByteStreamController {
|
|
|
|
/** @type {number | undefined} */
|
|
|
|
[_autoAllocateChunkSize];
|
|
|
|
/** @type {null} */
|
|
|
|
[_byobRequest];
|
|
|
|
/** @type {(reason: any) => Promise<void>} */
|
|
|
|
[_cancelAlgorithm];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_closeRequested];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_pullAgain];
|
|
|
|
/** @type {(controller: this) => Promise<void>} */
|
|
|
|
[_pullAlgorithm];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_pulling];
|
|
|
|
/** @type {ReadableByteStreamQueueEntry[]} */
|
|
|
|
[_queue];
|
|
|
|
/** @type {number} */
|
|
|
|
[_queueTotalSize];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_started];
|
|
|
|
/** @type {number} */
|
|
|
|
[_strategyHWM];
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
[_stream];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
get byobRequest() {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {number | null} */
|
|
|
|
get desiredSize() {
|
|
|
|
return readableByteStreamControllerGetDesiredSize(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
|
|
|
if (this[_closeRequested] === true) {
|
|
|
|
throw new TypeError("Closed already requested.");
|
|
|
|
}
|
|
|
|
if (this[_stream][_state] !== "readable") {
|
|
|
|
throw new TypeError(
|
|
|
|
"ReadableByteStreamController's stream is not in a readable state.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
readableByteStreamControllerClose(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {ArrayBufferView} chunk
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
enqueue(chunk) {
|
|
|
|
if (chunk.byteLength === 0) {
|
|
|
|
throw new TypeError("chunk must have a non-zero byteLength.");
|
|
|
|
}
|
|
|
|
if (chunk.buffer.byteLength === 0) {
|
|
|
|
throw new TypeError("chunk's buffer must have a non-zero byteLength.");
|
|
|
|
}
|
|
|
|
if (this[_closeRequested] === true) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Cannot enqueue chunk after a close has been requested.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this[_stream][_state] !== "readable") {
|
|
|
|
throw new TypeError(
|
|
|
|
"Cannot enqueue chunk when underlying stream is not readable.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return readableByteStreamControllerEnqueue(this, chunk);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any=} e
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
error(e) {
|
|
|
|
readableByteStreamControllerError(this, e);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
[_cancelSteps](reason) {
|
|
|
|
// 4.7.4. CancelStep 1. If this.[[pendingPullIntos]] is not empty,
|
|
|
|
resetQueue(this);
|
|
|
|
const result = this[_cancelAlgorithm](reason);
|
|
|
|
readableByteStreamControllerClearAlgorithms(this);
|
|
|
|
return result;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadRequest<ArrayBuffer>} readRequest
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
[_pullSteps](readRequest) {
|
|
|
|
/** @type {ReadableStream<ArrayBuffer>} */
|
|
|
|
const stream = this[_stream];
|
|
|
|
assert(readableStreamHasDefaultReader(stream));
|
|
|
|
if (this[_queueTotalSize] > 0) {
|
|
|
|
assert(readableStreamGetNumReadRequests(stream) === 0);
|
|
|
|
const entry = this[_queue].shift();
|
|
|
|
this[_queueTotalSize] -= entry.byteLength;
|
|
|
|
readableByteStreamControllerHandleQueueDrain(this);
|
|
|
|
const view = new Uint8Array(
|
|
|
|
entry.buffer,
|
|
|
|
entry.byteOffset,
|
|
|
|
entry.byteLength,
|
|
|
|
);
|
|
|
|
readRequest.chunkSteps(view);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 4. Let autoAllocateChunkSize be this.[[autoAllocateChunkSize]].
|
|
|
|
// 5. If autoAllocateChunkSize is not undefined,
|
|
|
|
readableStreamAddReadRequest(stream, readRequest);
|
|
|
|
readableByteStreamControllerCallPullIfNeeded(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template R */
|
|
|
|
class ReadableStreamDefaultController {
|
|
|
|
/** @type {(reason: any) => Promise<void>} */
|
|
|
|
[_cancelAlgorithm];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_closeRequested];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_pullAgain];
|
|
|
|
/** @type {(controller: this) => Promise<void>} */
|
|
|
|
[_pullAlgorithm];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_pulling];
|
|
|
|
/** @type {Array<ValueWithSize<R>>} */
|
|
|
|
[_queue];
|
|
|
|
/** @type {number} */
|
|
|
|
[_queueTotalSize];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_started];
|
|
|
|
/** @type {number} */
|
|
|
|
[_strategyHWM];
|
|
|
|
/** @type {(chunk: R) => number} */
|
|
|
|
[_strategySizeAlgorithm];
|
|
|
|
/** @type {ReadableStream<R>} */
|
|
|
|
[_stream];
|
|
|
|
|
|
|
|
/** @returns {number | null} */
|
|
|
|
get desiredSize() {
|
|
|
|
return readableStreamDefaultControllerGetDesiredSize(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
|
|
|
if (readableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
|
|
|
|
throw new TypeError("The stream controller cannot close or enqueue.");
|
|
|
|
}
|
|
|
|
readableStreamDefaultControllerClose(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {R} chunk
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
enqueue(chunk) {
|
|
|
|
if (readableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
|
|
|
|
throw new TypeError("The stream controller cannot close or enqueue.");
|
|
|
|
}
|
|
|
|
readableStreamDefaultControllerEnqueue(this, chunk);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any=} e
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
error(e) {
|
|
|
|
readableStreamDefaultControllerError(this, e);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
[_cancelSteps](reason) {
|
|
|
|
resetQueue(this);
|
|
|
|
const result = this[_cancelAlgorithm](reason);
|
|
|
|
readableStreamDefaultControllerClearAlgorithms(this);
|
|
|
|
return result;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ReadRequest<R>} readRequest
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
[_pullSteps](readRequest) {
|
|
|
|
const stream = this[_stream];
|
|
|
|
if (this[_queue].length) {
|
|
|
|
const chunk = dequeueValue(this);
|
|
|
|
if (this[_closeRequested] && this[_queue].length === 0) {
|
|
|
|
readableStreamDefaultControllerClearAlgorithms(this);
|
|
|
|
readableStreamClose(stream);
|
|
|
|
} else {
|
|
|
|
readableStreamDefaultControllerCallPullIfNeeded(this);
|
|
|
|
}
|
|
|
|
readRequest.chunkSteps(chunk);
|
|
|
|
} else {
|
|
|
|
readableStreamAddReadRequest(stream, readRequest);
|
|
|
|
readableStreamDefaultControllerCallPullIfNeeded(this);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @template I
|
|
|
|
* @template O
|
|
|
|
*/
|
|
|
|
class TransformStream {
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_backpressure];
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
[_backpressureChangePromise];
|
|
|
|
/** @type {TransformStreamDefaultController<O>} */
|
|
|
|
[_controller];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_detached];
|
|
|
|
/** @type {ReadableStream<O>} */
|
|
|
|
[_readable];
|
|
|
|
/** @type {WritableStream<I>} */
|
|
|
|
[_writable];
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Transformer<I, O>} transformer
|
|
|
|
* @param {QueuingStrategy<I>} writableStrategy
|
|
|
|
* @param {QueuingStrategy<O>} readableStrategy
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
transformer = null,
|
|
|
|
writableStrategy = {},
|
|
|
|
readableStrategy = {},
|
|
|
|
) {
|
|
|
|
const transformerDict = convertTransformer(transformer);
|
|
|
|
if (transformerDict.readableType) {
|
|
|
|
throw new RangeError("readableType transformers not supported.");
|
|
|
|
}
|
|
|
|
if (transformerDict.writableType) {
|
|
|
|
throw new RangeError("writableType transformers not supported.");
|
|
|
|
}
|
|
|
|
const readableHighWaterMark = extractHighWaterMark(readableStrategy, 0);
|
|
|
|
const readableSizeAlgorithm = extractSizeAlgorithm(readableStrategy);
|
|
|
|
const writableHighWaterMark = extractHighWaterMark(writableStrategy, 1);
|
|
|
|
const writableSizeAlgorithm = extractSizeAlgorithm(writableStrategy);
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
const startPromise = new Deferred();
|
|
|
|
initializeTransformStream(
|
|
|
|
this,
|
|
|
|
startPromise,
|
|
|
|
writableHighWaterMark,
|
|
|
|
writableSizeAlgorithm,
|
|
|
|
readableHighWaterMark,
|
|
|
|
readableSizeAlgorithm,
|
|
|
|
);
|
|
|
|
setUpTransformStreamDefaultControllerFromTransformer(
|
|
|
|
this,
|
|
|
|
transformer,
|
|
|
|
transformerDict,
|
|
|
|
);
|
|
|
|
if ("start" in transformerDict) {
|
|
|
|
startPromise.resolve(
|
|
|
|
transformerDict.start.call(transformer, this[_controller]),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
startPromise.resolve(undefined);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @returns {ReadableStream<O>} */
|
|
|
|
get readable() {
|
|
|
|
return this[_readable];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {WritableStream<I>} */
|
|
|
|
get writable() {
|
|
|
|
return this[_writable];
|
|
|
|
}
|
|
|
|
|
|
|
|
[customInspect]() {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
Deno.inspect(
|
|
|
|
{ readable: this.readable, writable: this.writable },
|
|
|
|
{ depth: 1 },
|
|
|
|
)
|
|
|
|
}`;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template O */
|
|
|
|
class TransformStreamDefaultController {
|
|
|
|
/** @type {(controller: this) => Promise<void>} */
|
|
|
|
[_flushAlgorithm];
|
|
|
|
/** @type {TransformStream<O>} */
|
|
|
|
[_stream];
|
|
|
|
/** @type {(chunk: O, controller: this) => Promise<void>} */
|
|
|
|
[_transformAlgorithm];
|
|
|
|
|
|
|
|
/** @returns {number | null} */
|
|
|
|
get desiredSize() {
|
|
|
|
const readableController = this[_stream][_readable][_controller];
|
|
|
|
return readableStreamDefaultControllerGetDesiredSize(
|
|
|
|
/** @type {ReadableStreamDefaultController<O>} */ (readableController),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {O} chunk
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
enqueue(chunk) {
|
|
|
|
transformStreamDefaultControllerEnqueue(this, chunk);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
error(reason) {
|
|
|
|
transformStreamDefaultControllerError(this, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {void} */
|
|
|
|
terminate() {
|
|
|
|
transformStreamDefaultControllerTerminate(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template W */
|
|
|
|
class WritableStream {
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_backpressure];
|
|
|
|
/** @type {Deferred<void> | undefined} */
|
|
|
|
[_closeRequest];
|
|
|
|
/** @type {WritableStreamDefaultController<W>} */
|
|
|
|
[_controller];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_detached];
|
|
|
|
/** @type {Deferred<void> | undefined} */
|
|
|
|
[_inFlightWriteRequest];
|
|
|
|
/** @type {Deferred<void> | undefined} */
|
|
|
|
[_inFlightCloseRequest];
|
|
|
|
/** @type {PendingAbortRequest | undefined} */
|
|
|
|
[_pendingAbortRequest];
|
|
|
|
/** @type {"writable" | "closed" | "erroring" | "errored"} */
|
|
|
|
[_state];
|
|
|
|
/** @type {any} */
|
|
|
|
[_storedError];
|
|
|
|
/** @type {WritableStreamDefaultWriter<W>} */
|
|
|
|
[_writer];
|
|
|
|
/** @type {Deferred<void>[]} */
|
|
|
|
[_writeRequests];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {UnderlyingSink<W>=} underlyingSink
|
|
|
|
* @param {QueuingStrategy<W>=} strategy
|
|
|
|
*/
|
|
|
|
constructor(underlyingSink = null, strategy = {}) {
|
|
|
|
const underlyingSinkDict = convertUnderlyingSink(underlyingSink);
|
|
|
|
if (underlyingSinkDict.type != null) {
|
|
|
|
throw new RangeError(
|
|
|
|
'WritableStream does not support "type" in the underlying sink.',
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
initializeWritableStream(this);
|
|
|
|
const sizeAlgorithm = extractSizeAlgorithm(strategy);
|
|
|
|
const highWaterMark = extractHighWaterMark(strategy, 1);
|
|
|
|
setUpWritableStreamDefaultControllerFromUnderlyingSink(
|
|
|
|
this,
|
|
|
|
underlyingSink,
|
|
|
|
underlyingSinkDict,
|
|
|
|
highWaterMark,
|
|
|
|
sizeAlgorithm,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @returns {boolean} */
|
|
|
|
get locked() {
|
|
|
|
return isWritableStreamLocked(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
abort(reason) {
|
|
|
|
if (isWritableStreamLocked(this)) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError(
|
|
|
|
"The writable stream is locked, therefore cannot be aborted.",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return writableStreamAbort(this, reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {Promise<void>} */
|
|
|
|
close() {
|
|
|
|
if (isWritableStreamLocked(this)) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError(
|
|
|
|
"The writable stream is locked, therefore cannot be closed.",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (writableStreamCloseQueuedOrInFlight(this) === true) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("The writable stream is already closing."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return writableStreamClose(this);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {WritableStreamDefaultWriter<W>} */
|
|
|
|
getWriter() {
|
|
|
|
return acquireWritableStreamDefaultWriter(this);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
[customInspect]() {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
Deno.inspect({ locked: this.locked })
|
|
|
|
}`;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template W */
|
|
|
|
class WritableStreamDefaultWriter {
|
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
[_closedPromise];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {Deferred<void>} */
|
|
|
|
[_readyPromise];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @type {WritableStream<W>} */
|
|
|
|
[_stream];
|
|
|
|
|
|
|
|
constructor(stream) {
|
|
|
|
setUpWritableStreamDefaultWriter(this, stream);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @returns {Promise<void>} */
|
|
|
|
get closed() {
|
|
|
|
return this[_closedPromise].promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {number} */
|
|
|
|
get desiredSize() {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
throw new TypeError(
|
|
|
|
"A writable stream is not associated with the writer.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return writableStreamDefaultWriterGetDesiredSize(this);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
|
|
|
|
/** @returns {Promise<void>} */
|
|
|
|
get ready() {
|
|
|
|
return this[_readyPromise].promise;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
abort(reason) {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("A writable stream is not associated with the writer."),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-01-14 16:57:19 -05:00
|
|
|
return writableStreamDefaultWriterAbort(this, reason);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {Promise<void>} */
|
|
|
|
close() {
|
|
|
|
const stream = this[_stream];
|
|
|
|
if (stream === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("A writable stream is not associated with the writer."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (writableStreamCloseQueuedOrInFlight(stream) === true) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("The associated stream is already closing."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return writableStreamDefaultWriterClose(this);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @returns {void} */
|
|
|
|
releaseLock() {
|
|
|
|
const stream = this[_stream];
|
|
|
|
if (stream === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(stream[_writer] !== undefined);
|
|
|
|
writableStreamDefaultWriterRelease(this);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {W} chunk
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
write(chunk) {
|
|
|
|
if (this[_stream] === undefined) {
|
|
|
|
return Promise.reject(
|
|
|
|
new TypeError("A writable stream is not associate with the writer."),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return writableStreamDefaultWriterWrite(this, chunk);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/** @template W */
|
|
|
|
class WritableStreamDefaultController {
|
|
|
|
/** @type {(reason?: any) => Promise<void>} */
|
|
|
|
[_abortAlgorithm];
|
|
|
|
/** @type {() => Promise<void>} */
|
|
|
|
[_closeAlgorithm];
|
|
|
|
/** @type {ValueWithSize<W | _close>[]} */
|
|
|
|
[_queue];
|
|
|
|
/** @type {number} */
|
|
|
|
[_queueTotalSize];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_started];
|
|
|
|
/** @type {number} */
|
|
|
|
[_strategyHWM];
|
|
|
|
/** @type {(chunk: W) => number} */
|
|
|
|
[_strategySizeAlgorithm];
|
|
|
|
/** @type {WritableStream<W>} */
|
|
|
|
[_stream];
|
|
|
|
/** @type {(chunk: W, controller: this) => Promise<void>} */
|
|
|
|
[_writeAlgorithm];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any=} e
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
error(e) {
|
|
|
|
const state = this[_stream][_state];
|
|
|
|
if (state !== "writable") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
writableStreamDefaultControllerError(this, e);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
/**
|
|
|
|
* @param {any=} reason
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
[_abortSteps](reason) {
|
|
|
|
const result = this[_abortAlgorithm](reason);
|
|
|
|
writableStreamDefaultControllerClearAlgorithms(this);
|
|
|
|
return result;
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:57:19 -05:00
|
|
|
[_errorSteps]() {
|
|
|
|
resetQueue(this);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.streams = {
|
2021-01-14 16:57:19 -05:00
|
|
|
// Non-Public
|
|
|
|
isReadableStreamDisturbed,
|
|
|
|
// Exposed in global runtime scope
|
|
|
|
ByteLengthQueuingStrategy,
|
|
|
|
CountQueuingStrategy,
|
2020-07-19 13:49:44 -04:00
|
|
|
ReadableStream,
|
2021-01-14 16:57:19 -05:00
|
|
|
ReadableStreamDefaultReader,
|
2020-07-19 13:49:44 -04:00
|
|
|
TransformStream,
|
|
|
|
WritableStream,
|
2021-01-14 16:57:19 -05:00
|
|
|
WritableStreamDefaultWriter,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|