/** * `ChunkEnvelope` wire shape (snake_case field names per spec). * * `data` for the `"message"` variant is the complete inner application payload. * `bytes` for the `"chunk"` variant is a base64-encoded byte slice (RFC 4648 * standard, with padding) of the serialised inner payload. */ export type ChunkEnvelope = { readonly kind: "message"; readonly data: unknown; readonly generation?: number; } | { readonly kind: "chunk"; readonly group_id: string; readonly seq: number; readonly total: number; readonly bytes: string; readonly generation?: number; }; /** Default per-segment ceiling (serialised `ChunkEnvelope` bytes). */ export declare const DEFAULT_MAX_CHUNK_BYTES: number; /** Default reassembly timeout in milliseconds. */ export declare const DEFAULT_REASSEMBLY_TIMEOUT_MS = 30000; /** Recommended per-buffer ceiling (decoded payload bytes). */ export declare const DEFAULT_MAX_REASSEMBLY_BYTES: number; /** Recommended aggregate ceiling across all incomplete reassembly groups. */ export declare const DEFAULT_MAX_REASSEMBLY_TOTAL_BYTES: number; /** Recommended cap on the number of concurrent in-flight reassembly groups. */ export declare const DEFAULT_MAX_CONCURRENT_GROUPS = 64; /** Defensive cap on the `total` field of an individual chunked group. */ export declare const DEFAULT_MAX_SEGMENTS_PER_GROUP = 4096; /** * Error thrown by {@link Reassembler} on a contract violation and by * {@link chunk} on caller-side failures. */ export declare class ChunkingError extends Error { constructor(message: string); } /** Options accepted by {@link chunk}. */ export interface ChunkOptions { /** Per-segment ceiling in serialised-bytes. Defaults to {@link DEFAULT_MAX_CHUNK_BYTES}. */ readonly maxChunkBytes?: number; /** UUID factory invoked once per chunked publish to mint a `group_id`. */ readonly newGroupId?: () => string; } /** * Split an application payload into one or more {@link ChunkEnvelope}s. * * If the serialised single-frame envelope fits inside the ceiling, returns a * one-element array containing the `kind: "message"` envelope. Otherwise mints * a `group_id` and returns `total = ceil(len(B) / R)` `kind: "chunk"` * envelopes. The raw-byte budget is derived from the actual group id and the * widest permitted sequence fields so every serialized envelope fits. */ export declare function chunk(payload: unknown, options?: ChunkOptions): ChunkEnvelope[]; /** Options accepted by {@link Reassembler}. */ export interface ReassemblerOptions { /** Drop partial buffers older than this many milliseconds. */ readonly timeoutMs?: number; /** Maximum total decoded bytes a single in-flight group may accumulate. */ readonly maxBufferBytes?: number; /** Maximum decoded bytes retained across every in-flight group. */ readonly maxTotalBufferBytes?: number; /** Maximum number of concurrent in-flight groups. */ readonly maxConcurrentGroups?: number; /** Maximum `total` value the reassembler will accept on any chunked envelope. */ readonly maxSegmentsPerGroup?: number; /** Clock source. Defaults to {@link Date.now}. */ readonly now?: () => number; } /** * Stateful reassembler. One instance per Web PubSub subscriber connection. * * On `kind: "message"` {@link Reassembler.ingest} returns the inner payload * immediately. On `kind: "chunk"` it returns `null` while waiting for sibling * segments and the reassembled inner payload once the final segment arrives. * All contract violations throw {@link ChunkingError}. */ export declare class Reassembler { private readonly buffers; private readonly timeoutMs; private readonly maxBufferBytes; private readonly maxTotalBufferBytes; private readonly maxConcurrentGroups; private readonly maxSegmentsPerGroup; private readonly now; private accumulatedBytes; constructor(options?: ReassemblerOptions); /** * Process one inbound envelope. Returns the inner payload when reassembly * completes (or immediately, for a `kind: "message"` envelope), `null` while * waiting for sibling chunks. */ ingest(envelope: ChunkEnvelope): unknown; /** Drop any buffer older than the configured timeout, returning dropped `group_id`s. */ sweepExpired(): string[]; /** Number of in-flight reassembly buffers. Exposed for diagnostics + tests. */ get inFlightGroupCount(): number; /** Decoded bytes currently retained across all incomplete groups. */ get inFlightBytes(): number; private dropBuffer; }