import { type NatsClient, type NatsStatus, type NatsStatusEvent } from './nats'; export declare const NATS_DEFAULTS: { readonly SHARED_CLOSE_DELAY_MS: 3000; readonly CONNECT_TIMEOUT_MS: 10000; readonly PING_INTERVAL_MS: 30000; readonly MAX_PING_OUT: 3; readonly RETRY_INITIAL_DELAY_MS: 1000; readonly RETRY_MAX_DELAY_MS: 30000; readonly RETRY_MULTIPLIER: 2; }; export interface NatsReconnectionBackoff { /** Number of fast retries before exponential phase kicks in. Default: 0. */ fastRetries?: number; /** Delay used during the fast-retry phase. Default: RETRY_INITIAL_DELAY_MS. */ fastRetryDelayMs?: number; /** Base delay for the exponential phase. Default: RETRY_INITIAL_DELAY_MS. */ initialDelayMs?: number; /** Upper cap on any single retry delay. Default: RETRY_MAX_DELAY_MS. */ maxDelayMs?: number; /** Per-attempt multiplier during exponential phase. Default: RETRY_MULTIPLIER. */ multiplier?: number; } export interface SharedConnection { wsUrl: string; client: NatsClient; refCount: number; closeTimer: ReturnType | null; retryTimer: ReturnType | null; /** * The lifecycle driving reconnect, held as its own `scheduleRetry`. When set, * other consumers observe status only and skip their own scheduleRetry — * otherwise every disconnect starts one backoff schedule per attached * consumer, all dialling the same connection on their own clocks. */ retryOwner: (() => void) | null; /** * The `scheduleRetry` of every lifecycle currently observing this connection, * in attach order — the same value that goes into `retryOwner`, so a * lifecycle has one identity rather than two. Only the owner drives * reconnect, so when it gives the loop up the roster is what a successor is * found in; see {@link handOffRetry}. */ retrySchedulers: Set<() => void>; } export interface AcquireClientOptions { name?: string; user?: string; pass?: string; connectTimeoutMs?: number; pingIntervalMs?: number; maxPingOut?: number; } export interface ReleaseClientOptions { delayMs?: number; } /** Legacy accessor from the single-slot era: returns the first live shared * connection, or null. With MULTIPLE URLs connected (e.g. `/ws/nats` client * chat + `/ws/nats-api` dashboard mounted together) "first" is whichever * surface acquired first — an arbitrary, mount-order-dependent answer. * Prefer `getSharedConnectionFor(url)`; this stays only for external * registry-pinned consumers of the old single-connection API. */ export declare function getSharedConnection(): SharedConnection | null; export declare function acquireClient(url: string, opts?: AcquireClientOptions): SharedConnection; export declare function releaseClient(url: string, opts?: ReleaseClientOptions): void; export declare function getSharedConnectionFor(url: string | null | undefined): SharedConnection | null; export interface ConnectionLifecycleOptions { conn: SharedConnection; wsUrl: string; onBeforeReconnect?: () => Promise | void; backoff?: NatsReconnectionBackoff; getFreshUrl: () => string | null; /** Called on every status change (after closed-guard). */ onStatusChange?: (status: NatsStatus, evt: NatsStatusEvent) => void; /** * Decide which statuses should trigger a retry attempt. Defaults to closed + * disconnected. Override to skip 'error' (JetStream protocol errors that * don't close the WS) or include it. */ shouldRetryOn?: (status: NatsStatus) => boolean; } export interface ConnectionLifecycleHandle { /** Stop observing status, clear any pending retry, release ownership if held. */ stop(): void; } export declare function startConnectionLifecycle(options: ConnectionLifecycleOptions): ConnectionLifecycleHandle; //# sourceMappingURL=shared-connection.d.ts.map