export type ReadinessStatus = "ready" | "not-ready"; export interface ReadinessResult { status: ReadinessStatus; checks: Record; } async function withinTimeout( check: () => Promise, timeoutMs: number, ): Promise { let timeout: ReturnType | undefined; try { await Promise.race([ check().then(() => undefined), new Promise((_, reject) => { timeout = setTimeout( () => reject(new Error("Dependency check timed out.")), timeoutMs, ); }), ]); } finally { if (timeout) clearTimeout(timeout); } } export async function checkReadiness( checks: Record Promise>, timeoutMs = 2_500, ): Promise { const boundedTimeout = Math.max(100, Math.min(10_000, timeoutMs)); const entries = await Promise.all( Object.entries(checks).map(async ([name, check]) => { try { await withinTimeout(check, boundedTimeout); return [name, "ok"] as const; } catch { return [name, "failed"] as const; } }), ); const result = Object.fromEntries(entries); return { status: Object.values(result).every((value) => value === "ok") ? "ready" : "not-ready", checks: result, }; }