export type { TypeContract, ReplacerType, StreamingReplacerType, ReviverType, StreamingProducer, ClientReviverContext, ServerReviverContext, ClientReplacerContext, ServerReplacerContext, StreamSource, StreamReadOptions, StreamingMetadata, StreamingValueServer, AsyncGeneratorContract, ReadableStreamContract, ReadableStreamRequestContract, PromiseContract, FileRequestContract, BlobRequestContract, FileResponseContract, BlobResponseContract, FileDownloadResponseContract, BlobDownloadResponseContract, FileMetadata, BlobMetadata, FileResponseMetadata, BlobResponseMetadata, FileDownloadMetadata, BlobDownloadMetadata, ChannelContract, BroadcastContract, FunctionContract, FileDownload, BlobDownload, DownloadProgress, }; import type { ServerChannel } from './server/channel.js'; import type { ServerBroadcast } from './server/server-broadcast.js'; import type { ClientChannel, ClientBroadcast } from './client/channel.js'; import type { AbortError } from '../shared/Abort.js'; import type { ShieldValidators } from '../node/server/shield.js'; import type { FileDownload, BlobDownload } from './client/response/DownloadClasses.js'; import type { PendingValue } from './client/response/PendingValue.js'; /** Contract tying replacer and reviver for one serializable type. */ type TypeContract = Record> = { value: V; result: R; metadata: M; }; /** Replacer: detect a value during serialization and replace it with prefix+metadata on the wire. * `replace` is the primary verb of the Replacer API — it runs once per serialized value and may * perform side effects (register channels, install listeners, wire lifecycle). The returned * `metadata` is what crosses the wire; `close` / `abort` are lifecycle hooks tracked by the * registry and invoked when the carrier message completes or is aborted. * * Duplicated references run `replace` once and revive as one client object (see * createStreamingReplacer), so metadata must identify the value uniquely within the * payload: mint a fresh id per `replace` call, the way every built-in does. */ type ReplacerType = { prefix: string; detect(value: unknown): value is C['value']; replace(value: C['value'], context: Context): { metadata: C['metadata']; close: () => Promise | void; abort: (abortError: AbortError) => void; }; }; /** Streaming replacer: replacer + producer factory for chunk-based streaming. */ type StreamingReplacerType = ReplacerType & { createProducer(value: C['value']): StreamingProducer; }; /** Reviver: reconstruct a live value from prefix+metadata during deserialization. * `revive` is the primary verb of the Reviver API — it runs once per deserialized value and may * perform side effects (create channels, start readers, attach validators). Mirrors `replace` * on the Replacer side. * * Duplicated references (equal wire strings) run `revive` once and resolve to the object the * first occurrence revived — server-side `===` holds on the client (see createStreamingReviver). */ type ReviverType = { prefix: string; revive(metadata: C['metadata'], context: Context): { value: C['result']; close: () => Promise | void; abort: (abortError: AbortError) => void; }; }; type StreamingProducer = { chunks: AsyncIterator>; cancel: (reason?: unknown) => void; }; type StreamingValueServer = { createProducer: () => StreamingProducer; index: number; }; type StreamCancelBehavior = 'error' | 'close'; type StreamReadOptions = { expectedSize?: number; onChunk?: (chunkSize: number) => void; /** How source-side `cancel()` should surface to the current consumer. * - `'error'` (default): interrupted delivery rejects / errors instead of looking like EOF. * - `'close'`: treat source cancellation as graceful end-of-stream / partial-buffer completion. */ cancelBehavior?: StreamCancelBehavior; }; type StreamSource = { readNextChunk: () => Promise | null>; /** Buffers all chunks into a single Uint8Array. By default throws on cancel * mid-stream or on `expectedSize` mismatch. */ bytes: (opts?: StreamReadOptions) => Promise>; /** WHATWG `ReadableStream` view over `readNextChunk`. `cancel()` on the stream * propagates upstream. With `opts.onChunk`, fires per chunk; with `opts.expectedSize`, * errors the stream on truncation. Source-side cancellation defaults to an error, * but callers can opt into graceful close with `cancelBehavior: 'close'`. */ stream: (opts?: StreamReadOptions) => ReadableStream>; cancel: () => void; abort: (abortError: AbortError) => void; }; /** Context for all client-side response revivers (streaming + placeholder). */ type ClientReviverContext = { createChannel(opts: { channelId: string; ack?: boolean; }): ClientChannel; createBroadcast(opts: { channelId: string; key: string; }): ClientBroadcast; receiveStream(metadata: StreamingMetadata): StreamSource; }; /** Context for all server-side request revivers (File/Blob + Function + ReadableStream). */ type ServerReviverContext = { registerFile(index: number, size: number): void; consumeFile(index: number, size: number): ReadableStream>; createChannel(opts: { id: string; ack?: boolean; }): ServerChannel; receiveStream(metadata: { channelId: string; }): StreamSource; /** Shield validators for the value being revived, keyed by the name declared in `[TELEFUNC_SHIELDS]`. * Populated per-value based on the telefunction's argument shield metadata. Revivers pick the names * relevant to their data flow and call them inline at the point where client data enters. */ validators: ShieldValidators; }; /** Context for all server-side response replacers (streaming + placeholder). */ type ServerReplacerContext = { createChannel(opts?: { ack?: boolean; }): ServerChannel; /** Registers a channel with the response lifecycle. Also installs shield validators if the channel has shields. */ registerChannel(channel: ServerChannel): void; sendStream(createProducer: () => StreamingProducer): { metadata: StreamingMetadata; close: () => Promise | void; abort: (abortError: AbortError) => void; }; /** Shield validators for the value being serialized, keyed by the name declared in `[TELEFUNC_SHIELDS]`. * Replacers pick the names relevant to their data flow. Each returns `true` on success or an error * string — call sites decide the action (throw, drop, ...). */ validators: ShieldValidators; }; /** Context for all client-side request replacers (File/Blob + Function + ReadableStream). */ type ClientReplacerContext = { registerFile(body: Blob): number; createChannel(opts?: { ack?: boolean; }): ClientChannel; sendStream(createProducer: () => StreamingProducer): { metadata: { channelId: string; }; close: () => Promise | void; abort: (abortError: AbortError) => void; }; }; type StreamingMetadata = { channelId: string; } | { __index: number; }; /** Request-direction (client→server): bytes are framed back-to-back in the request body, * so the reader needs an explicit `index` and `size` to know where this file starts and ends. */ type FileMetadata = { index: number; name: string; size: number; type: string; lastModified: number; }; type BlobMetadata = { index: number; size: number; type: string; }; type FileResponseMetadata = StreamingMetadata & { name: string; type: string; lastModified: number; size?: number; }; type BlobResponseMetadata = StreamingMetadata & { type: string; size?: number; }; type FileDownloadMetadata = FileResponseMetadata; type BlobDownloadMetadata = BlobResponseMetadata; type AsyncGeneratorContract = TypeContract, AsyncGenerator, StreamingMetadata>; type ReadableStreamContract = TypeContract>, ReadableStream>, StreamingMetadata>; type ReadableStreamRequestContract = TypeContract>, ReadableStream>, { channelId: string; }>; type PromiseContract = TypeContract, Promise, StreamingMetadata>; type FileRequestContract = TypeContract; type BlobRequestContract = TypeContract; type FileResponseContract = TypeContract, FileResponseMetadata>; type BlobResponseContract = TypeContract, BlobResponseMetadata>; type FileDownloadResponseContract = TypeContract; type BlobDownloadResponseContract = TypeContract; type DownloadProgress = (loaded: number, total: number | undefined) => void; type ChannelContract = TypeContract; type BroadcastContract = TypeContract; type FunctionContract = TypeContract<(...args: readonly unknown[]) => unknown, (...args: readonly unknown[]) => Promise, { channelId: string; }>;