/** * HTTP Client with Connection Pooling (P-004) * * Provides a centralized HTTP client with: * - Connection pooling via Node.js Agent * - Keep-alive support for connection reuse * - Per-domain agent management * - Configurable socket limits * - Metrics tracking for pool usage */ import * as http from 'node:http'; import * as https from 'node:https'; /** * HTTP client configuration options */ export interface HttpClientConfig { /** Maximum sockets per host (default: 10) */ maxSockets?: number; /** Maximum total sockets across all hosts (default: 50) */ maxTotalSockets?: number; /** Keep-alive timeout in milliseconds (default: 30000) */ keepAliveTimeout?: number; /** Socket timeout in milliseconds (default: 60000) */ timeout?: number; /** Enable keep-alive (default: true) */ keepAlive?: boolean; /** Enable connection scheduling (default: true) */ scheduling?: 'fifo' | 'lifo'; } /** * Connection pool statistics */ export interface PoolStats { /** Number of active sockets per host */ activeSockets: Record; /** Number of pending requests per host */ pendingRequests: Record; /** Total active sockets */ totalActiveSockets: number; /** Total pending requests */ totalPendingRequests: number; /** Number of unique hosts with connections */ uniqueHosts: number; /** Total requests made */ totalRequests: number; /** Total connections reused */ connectionsReused: number; /** Total new connections created */ newConnections: number; } /** * Fetch options with agent support */ export interface PooledFetchOptions extends RequestInit { /** Custom timeout in milliseconds */ timeout?: number; /** Skip connection pooling for this request */ skipPooling?: boolean; } /** * HTTP client with connection pooling. * * Uses Node.js http.Agent and https.Agent for connection management. * Provides a drop-in replacement for fetch() with automatic pooling. * * @example * ```ts * const client = new HttpClient({ maxSockets: 20 }); * const response = await client.fetch('https://api.example.com/data'); * console.log(client.getStats()); * ``` */ export declare class HttpClient { private readonly config; private readonly httpAgent; private readonly httpsAgent; private totalRequests; private connectionsReused; private newConnections; constructor(config?: HttpClientConfig); /** * Fetch with connection pooling. * * Drop-in replacement for native fetch() with automatic connection reuse. */ fetch(url: string | URL, options?: PooledFetchOptions): Promise; /** * Get connection pool statistics. */ getStats(): PoolStats; /** * Get the HTTP agent for custom usage. */ getHttpAgent(): http.Agent; /** * Get the HTTPS agent for custom usage. */ getHttpsAgent(): https.Agent; /** * Destroy all pooled connections. */ destroy(): void; /** * Reset metrics counters. */ resetMetrics(): void; /** * Get connection pool utilization percentage. */ getUtilization(): { http: number; https: number; total: number; }; /** * Get stats from an agent. */ private getAgentStats; /** * Get active socket count for a specific host. */ private getActiveSocketCount; /** * Get total sockets across all hosts. */ private getTotalSockets; /** * Merge two abort signals into one. */ private mergeAbortSignals; } /** * Get the global HTTP client instance. * * Creates a singleton client with default configuration. * Use this for application-wide connection pooling. * * @example * ```ts * const response = await getGlobalHttpClient().fetch('https://example.com'); * ``` */ export declare function getGlobalHttpClient(): HttpClient; /** * Configure the global HTTP client. * * Must be called before any requests are made. * Throws if client is already initialized with requests made. */ export declare function configureGlobalHttpClient(config: HttpClientConfig): void; /** * Reset the global HTTP client. * Destroys existing connections and resets metrics. */ export declare function resetGlobalHttpClient(): void; /** * Fetch with connection pooling using the global client. * * Drop-in replacement for fetch() with automatic connection reuse. * * @example * ```ts * // Use like native fetch * const response = await pooledFetch('https://api.example.com/data'); * const json = await response.json(); * ``` */ export declare function pooledFetch(url: string | URL, options?: PooledFetchOptions): Promise; /** * Get connection pool statistics from the global client. */ export declare function getPoolStats(): PoolStats; /** * Get pool utilization percentage. */ export declare function getPoolUtilization(): { http: number; https: number; total: number; }; /** * Create a domain-specific fetch function with pooling. * * Useful for creating fetch functions for specific site handlers. * * @example * ```ts * const githubFetch = createDomainFetch('api.github.com'); * const response = await githubFetch('/repos/user/repo'); * ``` */ export declare function createDomainFetch(domain: string, defaultOptions?: PooledFetchOptions): (path: string, options?: PooledFetchOptions) => Promise; //# sourceMappingURL=http-client.d.ts.map