import type { AnySchema, ExtractParams, ExtractRequestMethod, ExtractReturn, ExtractSubscriptionMethod, Schema } from './schema.js'; export type UnsubscribeCleanup = (context: { request: (method: string, params?: unknown[] | Record) => Promise; method: string; params: unknown[] | Record; initialResult: unknown; }) => Promise; export type Unsubscribe = (cleanup?: UnsubscribeCleanup) => Promise; export interface Transport { url: string; connect(): Promise; request>(method: M, params?: ExtractParams): Promise>; subscribe>(method: M, params: ExtractParams, callback: (data: ExtractReturn) => void): Promise; close(): Promise; } export interface BatchConfig { wait?: number; batchSize?: number; /** Cooldown in ms before re-enabling batching after server rejection (default: 5_000) */ disabledCooldown?: number; } export interface RetryConfig { /** Maximum number of retry attempts (default: 3) */ retryCount?: number; /** Base delay between retries in ms (default: 150). Exponential backoff is applied. */ retryDelay?: number; } export interface PingConfig { interval: number; method?: string; params?: unknown[]; } export interface HandshakeConfig { method: string; params?: unknown[]; } export interface WebSocketTransportConfig extends RetryConfig { url: string; /** Request timeout in milliseconds */ timeout?: number; /** Connection timeout in milliseconds */ connectTimeout?: number; handshake?: HandshakeConfig; keepAlive?: number | PingConfig; reconnect?: { delay: number; attempts: number; }; headers?: Record; batch?: boolean | BatchConfig; onUnsubscribe?: UnsubscribeCleanup; /** * Transform the initial subscription result before delivering to the callback. * Useful for normalizing initial results to match the notification format. * `result` is the raw value wrapped in a single-element array. * For example, Electrum Cash notifications arrive as [...params, ...result] * while the initial result is just the raw value `[value]`. * * Return `undefined` to suppress delivery of the initial result to the callback. * This is useful for protocols like Ethereum where the initial result is a * subscription ID (metadata) rather than actual data. */ transformInitialResult?: (method: string, params: unknown[] | Record, result: unknown[]) => unknown; /** * Filter notifications before delivery to subscribers. Return true to deliver. * Used by protocols like Electrum Cash where multiple subscriptions share the * same method name but use different params for routing. */ notificationFilter?: (subscriptionParams: unknown[], notificationParams: unknown[]) => boolean; } /** Extended WebSocket transport with socket access methods */ export interface WebSocketTransport extends Transport { /** Get the underlying WebSocket instance (null if not connected) */ getSocket(): WebSocket | null; /** Get the underlying WebSocket instance, connecting first if needed */ getSocketAsync(): Promise; } /** Extended TCP transport with socket access methods */ export interface TcpTransport extends Transport { /** Get the underlying TCP socket (null if not connected) */ getSocket(): import('node:net').Socket | null; /** Get the underlying TCP socket, connecting first if needed */ getSocketAsync(): Promise; } export type TcpTransportConfig = ({ url: string; tls?: import('tls').ConnectionOptions; } & TcpTransportOptions) | ({ host: string; port: number; tls?: boolean | import('tls').ConnectionOptions; } & TcpTransportOptions); export interface TcpTransportOptions extends RetryConfig { /** Request timeout in milliseconds */ timeout?: number; /** Connection timeout in milliseconds */ connectTimeout?: number; handshake?: HandshakeConfig; keepAlive?: number | PingConfig; reconnect?: { delay: number; attempts: number; }; batch?: boolean | BatchConfig; onUnsubscribe?: UnsubscribeCleanup; /** * Transform the initial subscription result before delivering to the callback. * `result` is the raw value wrapped in a single-element array. * * Return `undefined` to suppress delivery of the initial result to the callback. */ transformInitialResult?: (method: string, params: unknown[] | Record, result: unknown[]) => unknown; /** * Filter notifications before delivery to subscribers. Return true to deliver. * Used by protocols like Electrum Cash where multiple subscriptions share the * same method name but use different params for routing. */ notificationFilter?: (subscriptionParams: unknown[], notificationParams: unknown[]) => boolean; } export interface HttpTransportConfig extends RetryConfig { url: string; /** Request timeout in milliseconds */ timeout?: number; headers?: Record; batch?: boolean | BatchConfig; /** Custom fetch implementation (e.g., node-fetch, undici) */ fetchFn?: typeof fetch; /** Extra options passed to fetch (excluding method, headers, body, signal) */ fetchOptions?: Omit; /** Callback before each fetch request */ onRequest?: (info: { url: string; body: unknown; }) => void; /** Callback after each fetch response */ onResponse?: (info: { status: number; body: unknown; }) => void; /** Return RPC errors as results instead of throwing (default: false) */ raw?: boolean; } export interface RankConfig { interval?: number; ping?: (transport: Transport) => Promise; sampleCount?: number; timeout?: number; weights?: { latency?: number; stability?: number; }; } export interface TransportScore { transport: Transport; score: number; latency: number; stability: number; } export interface TransportResponse { method: string; params: unknown[] | Record; transport: Transport; response?: unknown; error?: unknown; status: 'success' | 'error'; } export interface FallbackTransport extends Transport { transports: Transport[]; scores: TransportScore[]; onScores(listener: (scores: TransportScore[]) => void): () => void; onResponse(listener: (info: TransportResponse) => void): () => void; } export interface ClusterTransport extends Transport { transports: Transport[]; onResponse(listener: (info: TransportResponse) => void): () => void; } export interface FallbackTransportOptions { shouldThrow?: (error: Error) => boolean | undefined; rank?: boolean | RankConfig; eagerConnect?: boolean; } export interface ClusterTransportOptions { quorum: number; timeout?: number; } //# sourceMappingURL=types.d.ts.map