UNPKG

@johngw/stream

Version:

Reactive programming tools using the WHATWG Streams API.

63 lines 2.77 kB
import { overrideObject } from '@johngw/stream-common/Object'; /** * A much less abstract version of the {@see TransformStream} that lets you interact * with both the `Readable` and `Writable` side of the stream. * * @remarks * We're implementing the {@see ReadableWritablePair} interface here, but the name * `WritableReadablePair` is closer to the truth. Chunks are first given to the `WritableStream` * which is then read from the `ReadableStream`. */ export class WritableReadablePair { writable; readable; #writableController; #proxiedWritableController; #readableController; #proxiedReadableController; constructor(sink = {}, source = {}, { readableStrategy = new CountQueuingStrategy({ highWaterMark: 0 }), writableStrategy, } = {}) { this.writable = new WritableStream({ start: async (controller) => { this.#writableController = controller; this.#proxiedWritableController = overrideObject(controller, { error: (controller, reason) => { controller.error(reason); this.#readableController.error(reason); }, }); await sink.start?.(this.#proxiedWritableController); }, abort: async (reason) => { await sink.abort?.(reason); this.#readableController.error(reason); }, close: async () => { await sink.close?.(); this.#readableController.close(); }, write: (chunk) => sink.write?.(chunk, this.#proxiedReadableController, this.#proxiedWritableController), }, writableStrategy); this.readable = new ReadableStream({ start: async (controller) => { this.#readableController = controller; this.#proxiedReadableController = overrideObject(controller, { close: (controller) => { controller.close(); this.#writableController.error(new Error('Readable side was closed')); }, error: (controller, reason) => { controller.error(reason); this.#writableController.error(reason); }, }); await source.start?.(this.#proxiedReadableController); }, cancel: async (reason) => { await source.cancel?.(reason); this.#writableController.error(reason); }, pull: () => source.pull?.(this.#proxiedReadableController), }, readableStrategy); } } //# sourceMappingURL=WritableReadablePair.js.map