export { buildInlineResponseBody, encodeErrorPayload }; import type { Readable } from 'node:stream'; import type { StreamingValueServer } from '../../types.js'; import type { ResponseAbortSource } from '../../../node/server/context/requestContext.js'; import type { TelefuncId } from '../../../node/server/runTelefunc/serializeTelefunctionResult.js'; import type { Context } from '../../../node/server/context/context.js'; /** * Stream the inline response body: framed metadata + multiplexed streaming values. * * Wire shape: * `[u32 metadata_len][metadata_bytes] (([u32 frame_len][u8 index][payload])* | error_frame) [u32 terminator]` * - terminator: `0x00000000` on success. * - error frame: `0xFFFFFFFF [u32 payload_len][payload]` instead of terminator. * * For `'binary-inline'`: each frame is the raw bytes. * For `'sse-inline'`: `encodeFrame` wraps each frame as `data: \n\n`. * * Push-based, not generator-based: the merge loop is a plain `async` function * that calls `sink.push(frame)` per emit, into a backpressured sink — `PushReadable` * on Node, `PushReadableStream` on Web. Generators paid V8's async-iterator protocol * cost on every `yield`; push avoids it. Backpressure: `sink.push` returns `false` * when the consumer's buffer is full; the loop awaits `wakeDrain` (resolved by the * sink's `read` / `pull` callback) before the next push. * * Cleanup paths — one `cancelled` flag means "consumer gone, stop emitting": * - `abortSignal` fires → onConsumerGone: cancelled=true + cancelProducers; emits no-op. * - consumer cancels sink → onConsumerGone (same). * - producer throws (caught) → cancelProducers only; emits still flow if wire alive. * - normal completion → emit terminator. * `finally` always closes the sink and calls `onComplete()`. */ declare function buildInlineResponseBody(runContext: { metadataSerialized: string; streamingValues: StreamingValueServer[]; telefuncId: TelefuncId; context: Context; abortSignal: AbortSignal; responseAbort: Pick; onComplete: () => void; encodeFrame: (frame: Uint8Array) => Uint8Array; useNodeStream: boolean; }): Readable | ReadableStream>; /** Serialize a streaming error as its on-wire payload string. Used by both the * inline path (with a u32 marker prefix) and the channel pump path (with a * tag prefix) — only the framing differs. */ declare function encodeErrorPayload(err: unknown, telefuncId: TelefuncId): Uint8Array;