export { StreamReader, OversizeFrameError, StreamTruncatedError }; import type { Readable } from 'node:stream'; declare class OversizeFrameError extends Error { } /** The request body ended mid-frame — the client disconnected or truncated the upload. Typed so error * sinks discriminate an expected client truncation from a real server bug by class, not message text. */ declare class StreamTruncatedError extends Error { } /** * Pull-based byte-counting stream reader for the binary frame protocol. * * Wire format: [u32 metadata length][metadata bytes][file0 bytes][file1 bytes]... * File sizes are known from the metadata — no boundary scanning needed, * just read exact byte counts sequentially. * * Accepts either a Web `ReadableStream` or a Node `Readable`. The caller picks — * the Node `serve()` adapter passes `IncomingMessage` directly to skip the * `Readable.toWeb` round-trip (whose pure-JS `dequeueValue` dominates CPU on hot * SSE / binary-RPC paths); other adapters pass `request.body`. Both shapes * collapse to one `AsyncIterator` so the rest of the class is * source-agnostic. * * Fields use TS `private` rather than JS `#private`: the inner read loop touches * `buffer`/`source` per chunk, and `#fields` compile to per-access `WeakMap.get/set` * which dominates CPU on hot streaming paths. Plain property access is cheap. */ declare class StreamReader { private source; private buffer; private fileSizes; private nextFileIndex; private queue; private disconnected; constructor(source: ReadableStream | Readable); /** Read the metadata: [u32 big-endian length][UTF-8 bytes]. `maxBytes` bounds what a peer buffers. */ readMetadata(maxBytes: number): Promise; /** Read one length-prefixed chunk, or null if the stream is cleanly exhausted. */ readLengthPrefixedBytesOrNull(maxBytes: number): Promise | null>; /** Register a file's size (called during deserialization). */ registerFile(index: number, size: number): void; /** * Consume file at given index — returns a `ReadableStream` of exactly `size` bytes. * * Queued: concurrent calls are serialized via `start`/`pull` waiting on `queue`. * Out-of-order access skips earlier files (their bytes are read and discarded). */ consumeFile(index: number, size: number): ReadableStream>; /** Pull one chunk from the underlying source, or null if disconnected. */ private pullChunk; /** Take up to `max` bytes from the internal buffer, or null if empty. */ private takeBuffered; /** Read a big-endian u32. Throws on disconnect. */ private readU32; /** Read exactly `n` bytes. Throws on disconnect. */ private readExact; /** Read exactly `n` bytes, or null on clean EOF before any bytes are read. */ private readExactOrNull; /** Skip exactly `n` bytes. Throws on disconnect. */ private skipBytes; }