import { type StandardSchemaV1 } from "./standard"; export declare class AckIdGenerator { #private; next(): string; } /** * A cache for ack response objects that keeps responses for proc timeout duration. */ export declare class AckResponseCache { #private; get(ackId: string): Uint8Array | undefined; set(proc: Proc, ackId: string, message: Uint8Array): void; entries(): MapIterator<[string, Uint8Array]>; } export interface AckListener { proc: Proc; timestamp: number; resolve(output: O): void; reject(error: Error): void; } export interface ProcSchema { /** * What the proc function should receive. */ takes?: StandardSchemaV1; /** * What the proc function should return. */ returns?: StandardSchemaV1; } /** * Configuration object passed to the Proc constructor. */ export interface ProcConfig extends ProcSchema { /** * A unique name for this proc. */ name: string; /** * Time to wait before considering this call a failure. * Value in milliseconds. Default 30000 (30 seconds). */ timeout?: number; } /** * Infers the input value type based on a Proc schema. */ export type ProcInput> = T["takes"] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput : null; /** * Infers the handler return value type based on a Proc schema. */ export type ProcOutput> = T["returns"] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput : void; export type MaybePromise = T | Promise; /** * Defines a proc with a name, schema and default options. * Can be implemented by the client or server. */ export declare function createProc>(config: Config): Proc, ProcOutput>; /** * An abstract definition of a procedure that can be implemented or called. */ export declare class Proc { readonly name: string; readonly schema: ProcSchema; readonly timeout: number; constructor(config: ProcConfig); /** * Parses potential input for this procedure. Rejects if input doesn't match the schema. */ parseInput(value: any): Promise; /** * Parses potential output from this procedure. Rejects if output doesn't match the schema. */ parseOutput(value: any): Promise; } export interface Impl { proc: Proc; handler: Handler; } /** * Represents a link from client->server or server->client. */ export interface Connection { call(proc: Proc, input: I): Promise; } export declare enum MessageType { /** * Server sends Ready to client when the connection is configured. */ Ready = 0, /** * Client sends pings periodically to keep the connection alive. */ Ping = 1, /** * Server responds to Ping with Pong. */ Pong = 2, /** * An RPC event, sent when either end of the connection wants to call a function on the other side. */ Proc = 3, /** * Procs always receive an acknowledgement. */ Ack = 4, /** * A broadcast is a proc call to many clients without acknowledgement. */ Broadcast = 5 } export declare enum MessageDataType { Empty = 0, CBOR = 1, Binary = 2 } export declare function getMessageType(message: Uint8Array): MessageType; /** * Broadcast is a proc call that expects no acknowledgement. */ export declare function encodeBroadcast(proc: Proc, input: I): Uint8Array; export declare function decodeBroadcast(message: Uint8Array): { name: string; input: unknown; }; export declare function encodeProc(proc: Proc, ackId: string, input: I): Uint8Array; export declare function decodeProc(message: Uint8Array): { name: string; ackId: string; input: unknown; }; export declare function encodeAck(ackId: string, success: boolean, output: any): Uint8Array; export declare function decodeAck(message: Uint8Array): { ackId: string; success: true; output: unknown; error?: undefined; } | { ackId: string; success: false; error: string; output?: undefined; }; export declare const PING_MESSAGE: Uint8Array; export declare const PONG_MESSAGE: Uint8Array; export declare const READY_MESSAGE: Uint8Array; /** * Prints a human readable representation of a message for debug purposes. * * @param message - The binary message. * @param callback - If passed, the decoded object will be passed to this function instead of console.log() */ export declare function printMessage(message: Uint8Array, callback?: (data: any) => void): void;