// TODO reenable this code when we enable writableStreams and transport types // // Forked from https://github.com/stardazed/sd-streams/tree/8928cf04b035fd02fb1340b7eb541c76be37e546 // // Copyright (c) 2018-Present by Arthur Langereis - @zenmumbler MIT // /** // * streams/transform-internals - internal types and functions for transform streams // * Part of Stardazed // * (c) 2018-Present by Arthur Langereis - @zenmumbler // * https://github.com/stardazed/sd-streams // */ // /* eslint-disable @typescript-eslint/no-explicit-any */ // // TODO reenable this lint here // import * as rs from "./readable-internals.ts"; // import * as ws from "./writable-internals.ts"; // import * as shared from "./shared-internals.ts"; // import { createReadableStream } from "./readable-stream.ts"; // import { createWritableStream } from "./writable-stream.ts"; // import { QueuingStrategy, QueuingStrategySizeCallback } from "../dom_types.ts"; // export const state_ = Symbol("transformState_"); // export const backpressure_ = Symbol("backpressure_"); // export const backpressureChangePromise_ = Symbol("backpressureChangePromise_"); // export const readable_ = Symbol("readable_"); // export const transformStreamController_ = Symbol("transformStreamController_"); // export const writable_ = Symbol("writable_"); // export const controlledTransformStream_ = Symbol("controlledTransformStream_"); // export const flushAlgorithm_ = Symbol("flushAlgorithm_"); // export const transformAlgorithm_ = Symbol("transformAlgorithm_"); // // ---- // export type TransformFunction = ( // chunk: InputType, // controller: TransformStreamDefaultController // ) => void | PromiseLike; // export type TransformAlgorithm = (chunk: InputType) => Promise; // export type FlushFunction = ( // controller: TransformStreamDefaultController // ) => void | PromiseLike; // export type FlushAlgorithm = () => Promise; // // ---- // export interface TransformStreamDefaultController { // readonly desiredSize: number | null; // enqueue(chunk: OutputType): void; // error(reason: shared.ErrorResult): void; // terminate(): void; // [controlledTransformStream_]: TransformStream; // The TransformStream instance controlled; also used for the IsTransformStreamDefaultController brand check // [flushAlgorithm_]: FlushAlgorithm; // A promise - returning algorithm which communicates a requested close to the transformer // [transformAlgorithm_]: TransformAlgorithm; // A promise - returning algorithm, taking one argument(the chunk to transform), which requests the transformer perform its transformation // } // export interface Transformer { // start?( // controller: TransformStreamDefaultController // ): void | PromiseLike; // transform?: TransformFunction; // flush?: FlushFunction; // readableType?: undefined; // for future spec changes // writableType?: undefined; // for future spec changes // } // export declare class TransformStream { // constructor( // transformer: Transformer, // writableStrategy: QueuingStrategy, // readableStrategy: QueuingStrategy // ); // readonly readable: rs.SDReadableStream; // readonly writable: ws.WritableStream; // [backpressure_]: boolean | undefined; // Whether there was backpressure on [[readable]] the last time it was observed // [backpressureChangePromise_]: shared.ControlledPromise | undefined; // A promise which is fulfilled and replaced every time the value of[[backpressure]] changes // [readable_]: rs.SDReadableStream; // The ReadableStream instance controlled by this object // [transformStreamController_]: TransformStreamDefaultController< // InputType, // OutputType // >; // A TransformStreamDefaultController created with the ability to control[[readable]] and[[writable]]; also used for the IsTransformStream brand check // [writable_]: ws.WritableStream; // The WritableStream instance controlled by this object // } // // ---- TransformStream // export function isTransformStream( // value: unknown // ): value is TransformStream { // if (typeof value !== "object" || value === null) { // return false; // } // return transformStreamController_ in value; // } // export function initializeTransformStream( // stream: TransformStream, // startPromise: Promise, // writableHighWaterMark: number, // writableSizeAlgorithm: QueuingStrategySizeCallback, // readableHighWaterMark: number, // readableSizeAlgorithm: QueuingStrategySizeCallback // ): void { // const startAlgorithm = function(): Promise { // return startPromise; // }; // const writeAlgorithm = function(chunk: InputType): Promise { // return transformStreamDefaultSinkWriteAlgorithm(stream, chunk); // }; // const abortAlgorithm = function(reason: shared.ErrorResult): Promise { // return transformStreamDefaultSinkAbortAlgorithm(stream, reason); // }; // const closeAlgorithm = function(): Promise { // return transformStreamDefaultSinkCloseAlgorithm(stream); // }; // stream[writable_] = createWritableStream( // startAlgorithm, // writeAlgorithm, // closeAlgorithm, // abortAlgorithm, // writableHighWaterMark, // writableSizeAlgorithm // ); // const pullAlgorithm = function(): Promise { // return transformStreamDefaultSourcePullAlgorithm(stream); // }; // const cancelAlgorithm = function( // reason: shared.ErrorResult // ): Promise { // transformStreamErrorWritableAndUnblockWrite(stream, reason); // return Promise.resolve(undefined); // }; // stream[readable_] = createReadableStream( // startAlgorithm, // pullAlgorithm, // cancelAlgorithm, // readableHighWaterMark, // readableSizeAlgorithm // ); // stream[backpressure_] = undefined; // stream[backpressureChangePromise_] = undefined; // transformStreamSetBackpressure(stream, true); // stream[transformStreamController_] = undefined!; // initialize slot for brand-check // } // export function transformStreamError( // stream: TransformStream, // error: shared.ErrorResult // ): void { // rs.readableStreamDefaultControllerError( // stream[readable_][ // rs.readableStreamController_ // ] as rs.SDReadableStreamDefaultController, // error // ); // transformStreamErrorWritableAndUnblockWrite(stream, error); // } // export function transformStreamErrorWritableAndUnblockWrite< // InputType, // OutputType // >( // stream: TransformStream, // error: shared.ErrorResult // ): void { // transformStreamDefaultControllerClearAlgorithms( // stream[transformStreamController_] // ); // ws.writableStreamDefaultControllerErrorIfNeeded( // stream[writable_][ws.writableStreamController_]!, // error // ); // if (stream[backpressure_]) { // transformStreamSetBackpressure(stream, false); // } // } // export function transformStreamSetBackpressure( // stream: TransformStream, // backpressure: boolean // ): void { // // Assert: stream.[[backpressure]] is not backpressure. // if (stream[backpressure_] !== undefined) { // stream[backpressureChangePromise_]!.resolve(undefined); // } // stream[backpressureChangePromise_] = shared.createControlledPromise(); // stream[backpressure_] = backpressure; // } // // ---- TransformStreamDefaultController // export function isTransformStreamDefaultController( // value: unknown // ): value is TransformStreamDefaultController { // if (typeof value !== "object" || value === null) { // return false; // } // return controlledTransformStream_ in value; // } // export function setUpTransformStreamDefaultController( // stream: TransformStream, // controller: TransformStreamDefaultController, // transformAlgorithm: TransformAlgorithm, // flushAlgorithm: FlushAlgorithm // ): void { // // Assert: ! IsTransformStream(stream) is true. // // Assert: stream.[[transformStreamController]] is undefined. // controller[controlledTransformStream_] = stream; // stream[transformStreamController_] = controller; // controller[transformAlgorithm_] = transformAlgorithm; // controller[flushAlgorithm_] = flushAlgorithm; // } // export function transformStreamDefaultControllerClearAlgorithms< // InputType, // OutputType // >(controller: TransformStreamDefaultController): void { // // Use ! assertions to override type check here, this way we don't // // have to perform type checks/assertions everywhere else. // controller[transformAlgorithm_] = undefined!; // controller[flushAlgorithm_] = undefined!; // } // export function transformStreamDefaultControllerEnqueue( // controller: TransformStreamDefaultController, // chunk: OutputType // ): void { // const stream = controller[controlledTransformStream_]; // const readableController = stream[readable_][ // rs.readableStreamController_ // ] as rs.SDReadableStreamDefaultController; // if ( // !rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController) // ) { // throw new TypeError(); // } // try { // rs.readableStreamDefaultControllerEnqueue(readableController, chunk); // } catch (error) { // transformStreamErrorWritableAndUnblockWrite(stream, error); // throw stream[readable_][shared.storedError_]; // } // const backpressure = rs.readableStreamDefaultControllerHasBackpressure( // readableController // ); // if (backpressure !== stream[backpressure_]) { // // Assert: backpressure is true. // transformStreamSetBackpressure(stream, true); // } // } // export function transformStreamDefaultControllerError( // controller: TransformStreamDefaultController, // error: shared.ErrorResult // ): void { // transformStreamError(controller[controlledTransformStream_], error); // } // export function transformStreamDefaultControllerPerformTransform< // InputType, // OutputType // >( // controller: TransformStreamDefaultController, // chunk: InputType // ): Promise { // const transformPromise = controller[transformAlgorithm_](chunk); // return transformPromise.catch(error => { // transformStreamError(controller[controlledTransformStream_], error); // throw error; // }); // } // export function transformStreamDefaultControllerTerminate< // InputType, // OutputType // >(controller: TransformStreamDefaultController): void { // const stream = controller[controlledTransformStream_]; // const readableController = stream[readable_][ // rs.readableStreamController_ // ] as rs.SDReadableStreamDefaultController; // if (rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController)) { // rs.readableStreamDefaultControllerClose(readableController); // } // const error = new TypeError("The transform stream has been terminated"); // transformStreamErrorWritableAndUnblockWrite(stream, error); // } // // ---- Transform Sinks // export function transformStreamDefaultSinkWriteAlgorithm( // stream: TransformStream, // chunk: InputType // ): Promise { // // Assert: stream.[[writable]].[[state]] is "writable". // const controller = stream[transformStreamController_]; // if (stream[backpressure_]) { // const backpressureChangePromise = stream[backpressureChangePromise_]!; // // Assert: backpressureChangePromise is not undefined. // return backpressureChangePromise.promise.then(_ => { // const writable = stream[writable_]; // const state = writable[shared.state_]; // if (state === "erroring") { // throw writable[shared.storedError_]; // } // // Assert: state is "writable". // return transformStreamDefaultControllerPerformTransform( // controller, // chunk // ); // }); // } // return transformStreamDefaultControllerPerformTransform(controller, chunk); // } // export function transformStreamDefaultSinkAbortAlgorithm( // stream: TransformStream, // reason: shared.ErrorResult // ): Promise { // transformStreamError(stream, reason); // return Promise.resolve(undefined); // } // export function transformStreamDefaultSinkCloseAlgorithm( // stream: TransformStream // ): Promise { // const readable = stream[readable_]; // const controller = stream[transformStreamController_]; // const flushPromise = controller[flushAlgorithm_](); // transformStreamDefaultControllerClearAlgorithms(controller); // return flushPromise.then( // _ => { // if (readable[shared.state_] === "errored") { // throw readable[shared.storedError_]; // } // const readableController = readable[ // rs.readableStreamController_ // ] as rs.SDReadableStreamDefaultController; // if ( // rs.readableStreamDefaultControllerCanCloseOrEnqueue(readableController) // ) { // rs.readableStreamDefaultControllerClose(readableController); // } // }, // error => { // transformStreamError(stream, error); // throw readable[shared.storedError_]; // } // ); // } // // ---- Transform Sources // export function transformStreamDefaultSourcePullAlgorithm< // InputType, // OutputType // >(stream: TransformStream): Promise { // // Assert: stream.[[backpressure]] is true. // // Assert: stream.[[backpressureChangePromise]] is not undefined. // transformStreamSetBackpressure(stream, false); // return stream[backpressureChangePromise_]!.promise; // }