/** * SinkAdapter — unified writable-sink view. * * Producers (zip writers, docx streamers, etc.) generate `Uint8Array` * chunks and want to push them through a user-supplied destination * without caring whether that destination is a Web `WritableStream`, * a Node `Writable`, or some duck-typed object. * * `SinkAdapter` collapses all three into one promise-based API: * - `write(chunk)` resolves once the chunk has been accepted by the * sink (after awaiting `drain` for Node-style sinks under * backpressure, or `writer.ready` for Web streams). * - `end()` finalises the sink and resolves on close. * - The first sink error is captured and re-thrown from the next * `write` / `end` so callers can't lose it. * * Producers are expected to either: * - drive `await sink.write(chunk)` directly (preferred — gives * true end-to-end backpressure), or * - chain writes onto a single shared promise so a synchronous * emit callback can still feed the adapter without blocking. */ import type { EventEmitterLike } from "../types.js"; /** * Loose Node `Writable`-like contract. Matches `stream.Writable` but only * the subset SinkAdapter touches; intentionally avoids depending on * `node:stream` so the adapter compiles in browsers. */ export interface NodeWritableLike extends EventEmitterLike { write(chunk: Uint8Array, cb?: (err?: Error | null) => void): boolean; end(cb?: () => void): unknown; destroyed?: boolean; } /** * Object that implements just enough of a writable to be useful: a * `write` returning sync bool / Promise, plus `end`. Used as a fallback * for legacy producers that don't fit either Node or Web streams. */ export interface DuckSinkLike extends EventEmitterLike { write(chunk: Uint8Array): boolean | Promise; end(): unknown; } /** Anything SinkAdapter knows how to drive. */ export type AnySink = WritableStream | NodeWritableLike | DuckSinkLike; /** Unified sink view with awaited write + error capture. */ export declare class SinkAdapter { private readonly _kind; private readonly _webWriter?; private readonly _nodeSink?; private readonly _duckSink?; private _error; private _ended; constructor(sink: AnySink); /** * The first error reported by the sink (or `null`). Producers that * cannot await `write()` (synchronous emit callbacks) should poll * this between operations. */ get error(): Error | null; write(chunk: Uint8Array): Promise; end(): Promise; private _captureError; private _throwIfErrored; private _attachErrorListener; }