/** * TCP protocol handler for FlashQ client. * * Handles buffer management, message framing, and protocol serialization * for both JSON (newline-delimited) and binary (MessagePack) protocols. */ /** * Pending request tracking for async responses. */ export interface PendingRequest { /** Resolves with the response data */ resolve: (value: unknown) => void; /** Rejects with an error */ reject: (error: Error) => void; /** Timeout timer handle */ timer: ReturnType; } /** * Handles JSON protocol buffer parsing. * * Accumulates incoming data chunks and extracts complete * newline-delimited JSON messages. */ export declare class JsonBufferHandler { /** Accumulated string chunks waiting to be joined */ private chunks; /** Partial line from previous parse */ private remainder; /** * Appends new data to the buffer. * * @param data - Raw data from socket */ append(data: Buffer | string): void; /** * Extracts all complete lines from the buffer. * * Complete lines are separated by newlines. Any partial line * at the end is kept for the next call. * * @returns Array of complete JSON strings */ extractLines(): string[]; /** * Resets the buffer state. */ reset(): void; } /** * Handles binary (MessagePack) protocol buffer parsing. * * Each message is framed with a 4-byte big-endian length prefix * followed by the MessagePack-encoded payload. * * Uses chunked accumulation for O(n) performance instead of * O(n²) from Buffer.concat on every append. */ export declare class BinaryBufferHandler { /** Accumulated binary chunks waiting to be joined */ private chunks; /** Total length of all chunks */ private totalLength; /** Consolidated buffer for frame extraction */ private buffer; /** * Appends new data to the buffer. * * Chunks are accumulated and only consolidated when extracting frames, * avoiding O(n²) performance from repeated Buffer.concat calls. * * @param data - Raw data from socket */ append(data: Buffer): void; /** * Consolidates accumulated chunks into a single buffer. * Only called when extracting frames. */ private consolidate; /** * Extracts all complete frames from the buffer. * * Frame format: [4-byte length BE][payload] * * @returns Array of decoded message objects */ extractFrames(): Record[]; /** * Resets the buffer state. */ reset(): void; } /** * Encodes a command for TCP transmission. * * @param command - Command object to encode * @param reqId - Request ID to include * @param useBinary - Whether to use MessagePack encoding * @returns Buffer ready for socket.write() */ export declare function encodeCommand(command: Record, reqId: string, useBinary: boolean): Buffer; /** * Parses a JSON response from a string. * * @param line - JSON string to parse * @returns Parsed response object or null if invalid */ export declare function parseJsonResponse(line: string): Record | null; /** * Generates unique request IDs for a client instance. * * Each instance has its own counter to avoid conflicts in multi-client scenarios. * Includes a unique prefix based on timestamp and random value for global uniqueness. */ export declare class RequestIdGenerator { private counter; private readonly prefix; constructor(); /** * Generates a unique request ID. * * Format: "{prefix}-{incrementing number}" * * @returns Unique request ID string */ generate(): string; /** * Resets the counter. * Primarily used for testing. */ reset(): void; } /** * Generates a unique request ID using the default generator. * * @deprecated Use RequestIdGenerator instance for multi-client scenarios * @returns Unique request ID string */ export declare function generateRequestId(): string; /** * Resets the default request ID counter. * Primarily used for testing. * * @deprecated Use RequestIdGenerator instance for multi-client scenarios */ export declare function resetRequestIdCounter(): void; //# sourceMappingURL=handler.d.ts.map