import { CommandType } from '../tcp/types/command.types'; import { AuthenticatedUser, ConnectionState } from './types/client.types'; /** Per-connection options a PooledConnection needs (pool-level options like maxPoolSize live one level up). */ export interface PooledConnectionOptions { timeout: number; reconnectAttempts: number; reconnectDelay: number; heartbeatInterval: number; /** Encrypt this connection with TLS instead of plaintext. */ tls?: boolean; /** Pre-read CA certificate content (see AxioDBCloud's tlsCAPath) - read once at the pool level, not per connection. */ tlsCA?: Buffer; tlsRejectUnauthorized?: boolean; } /** Callbacks a PooledConnection uses to surface lifecycle events to its owning AxioDBCloud pool. */ export interface PooledConnectionCallbacks { onError: (error: Error) => void; onDisconnected: (hadError: boolean) => void; onReconnecting: (attempt: number) => void; onReconnected: () => void; /** This member exhausted its reconnect attempts and is giving up permanently. */ onExhausted: () => void; } /** * One physical TCP connection within an AxioDBCloud connection pool. Owns its own socket, * message-framing buffer, in-flight request map, and reconnect/heartbeat lifecycle, so a * failure on one pooled connection never affects the others in the pool. */ export default class PooledConnection { private readonly host; private readonly port; private readonly options; private readonly getCredentials; private readonly callbacks; private socket; private messageBuffer; private pendingRequests; private reconnectAttempt; private heartbeatInterval; state: ConnectionState; authUser?: AuthenticatedUser; /** Number of requests sent on this connection awaiting a response - used by the owning pool to route new commands to the least-busy member instead of blind round-robin. */ get pendingCount(): number; constructor(host: string, port: number, options: PooledConnectionOptions, getCredentials: () => { username: string; password: string; } | undefined, callbacks: PooledConnectionCallbacks); /** * Connect (or reconnect) this pooled connection, authenticating automatically if * credentials are available. On authentication failure, marks this connection FAILED and * prevents its own background auto-reconnect from retrying the same bad credentials. */ connect(): Promise; /** Sends the AUTHENTICATE command over this connection and stores the resulting identity. */ authenticate(username: string, password: string): Promise; sendCommand(command: CommandType | string, params: any): Promise; /** Disconnect this connection permanently (no further auto-reconnect). */ disconnect(): Promise; private setupSocketHandlers; private handleResponse; private handleDisconnection; private attemptReconnect; private startHeartbeat; private stopHeartbeat; }