export function withTimeout(promise: Promise, timeoutMs: number): Promise { if (!timeoutMs || timeoutMs <= 0) { return promise; } let timer: NodeJS.Timeout | null = null; const timeout = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("timeout")), timeoutMs); }); return Promise.race([promise, timeout]).finally(() => { if (timer) { clearTimeout(timer); } }); }