import { EventEmitter } from 'events'; import type { ClientOptions } from '../types'; import { Logger } from '../utils/logger'; import type { ConnectionState } from './reconnect'; export { MAX_JOB_DATA_SIZE, MAX_BATCH_SIZE, validateQueueName, validateJobDataSize, mapJobToPayload, } from './validation'; export type { JobPayload } from './validation'; /** Client options with required fields except hooks */ type ResolvedClientOptions = Required> & { hooks?: ClientOptions['hooks']; }; export declare class FlashQConnection extends EventEmitter { protected _options: ResolvedClientOptions; protected logger: Logger; private socket; private connectionState; private authenticated; private pendingRequests; private jsonBuffer; private binaryBuffer; private reconnectAttempts; private reconnectTimer; private manualClose; private retryConfig; private queueOnDisconnect; private maxQueuedRequests; private requestQueue; private trackRequestIds; private compression; private compressionThreshold; private requestIdGenerator; constructor(options?: ClientOptions); /** @deprecated Use logger.debug() instead */ protected debug(message: string, data?: unknown): void; /** * Invokes the connection hook if configured. * * Creates a ConnectionHookContext and calls the onConnection hook * with the appropriate event type. Hook errors are caught and logged * to prevent breaking the main flow. * * @param event - The connection event type * @param error - Error object for error events * @param attempt - Reconnection attempt number */ private callConnectionHook; get options(): ResolvedClientOptions; connect(): Promise; /** * Schedules a reconnection attempt with exponential backoff. * * Calculates delay using: min(initialDelay * 2^attempt, maxDelay) + jitter * Jitter is 0-30% of base delay to prevent thundering herd. * * Emits 'reconnecting' before attempt and 'reconnected' on success. * On failure, recursively schedules next attempt until max reached. */ private scheduleReconnect; /** * Sets up event handlers on the TCP socket. * * Handlers: * - 'data': Routes to JSON or binary buffer processor based on protocol * - 'close': Triggers disconnect event and schedules reconnection * - 'error': Emits error event and calls connection hook */ private setupSocketHandlers; /** * Processes the JSON text buffer to extract complete responses. * * Delegates to JsonBufferHandler for line extraction, then parses * each complete line as JSON and passes to handleResponse. */ private processBuffer; /** * Processes the binary buffer to extract MessagePack frames. * * Delegates to BinaryBufferHandler for frame extraction and decoding, * then passes each decoded object to handleResponse. */ private processBinaryBuffer; /** * Handles a parsed response from the server. * * Matches response.reqId to pending request, clears timeout, * and resolves/rejects the promise. Server errors are parsed * into typed FlashQError subclasses. * * @param response - Parsed response object with reqId */ private handleResponse; close(): Promise; /** Get number of queued requests */ getQueuedRequestCount(): number; isConnected(): boolean; getConnectionState(): ConnectionState; ping(): Promise; auth(token: string): Promise; send(command: Record, customTimeout?: number): Promise; /** * Internal send implementation without retry wrapper. * * Routes to queueRequest if disconnecting with queue enabled, * waits for reconnection if in progress, then delegates to * sendTcp or sendHttp based on protocol configuration. * * @param command - Command object to send * @param customTimeout - Optional timeout override * @returns Promise resolving to response */ private doSend; /** * Waits for an in-progress reconnection to complete. * * Listens for 'reconnected' or 'reconnect_failed' events. * Times out after options.timeout milliseconds. * * @returns Promise that resolves when reconnected * @throws ConnectionError on timeout or reconnection failure */ private waitForReconnection; /** * Queues a request for later execution after reconnection. * * Used when queueOnDisconnect is enabled and connection is lost. * Requests are stored and replayed in order when reconnected. * * @param command - Command to queue * @param customTimeout - Optional timeout for when request is eventually sent * @returns Promise that resolves when request completes after reconnection * @throws ConnectionError if queue is full */ private queueRequest; /** * Processes all queued requests after successful reconnection. * * Sends each queued request in order, resolving or rejecting * the original promises. Clears the queue before processing * to handle any new requests that may be queued during replay. */ private processRequestQueue; /** * Sends a command over the TCP socket. * * Handles request ID generation, optional compression, * timeout management, and protocol serialization (JSON or MessagePack). * * @param command - Command object to send * @param customTimeout - Optional timeout override in milliseconds * @returns Promise resolving to typed response * @throws ConnectionError if not connected * @throws TimeoutError if request times out */ private sendTcp; /** * Sends a command via HTTP REST API. * * Converts the command to appropriate HTTP method and URL, * handles authentication headers, and parses the JSON response. * * @param command - Command object to convert to HTTP request * @param customTimeout - Optional timeout override in milliseconds * @returns Promise resolving to typed response * @throws TimeoutError if request times out */ private sendHttp; } //# sourceMappingURL=connection.d.ts.map