import type { Server } from 'node:http'; const DEFAULT_KEEP_ALIVE_TIMEOUT_MS = 120_000; const DEFAULT_HEADERS_TIMEOUT_MS = 125_000; export function configureApiHttpServerTimeouts( server: Pick, env: NodeJS.ProcessEnv, ) { const keepAliveTimeoutMs = positiveInteger(env.API_HTTP_KEEP_ALIVE_TIMEOUT_MS, DEFAULT_KEEP_ALIVE_TIMEOUT_MS); const configuredHeadersTimeoutMs = positiveInteger(env.API_HTTP_HEADERS_TIMEOUT_MS, DEFAULT_HEADERS_TIMEOUT_MS); const headersTimeoutMs = Math.max(configuredHeadersTimeoutMs, keepAliveTimeoutMs + 1_000); server.keepAliveTimeout = keepAliveTimeoutMs; server.headersTimeout = headersTimeoutMs; return { keepAliveTimeoutMs, headersTimeoutMs }; } function positiveInteger(value: string | undefined, fallback: number) { const parsed = Number(value); return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback; }