| 81 | }; |
| 82 | |
| 83 | export const waitForHttp = async ( |
| 84 | url: string, |
| 85 | options: { readonly timeoutMs?: number; readonly expectRedirect?: boolean } = {}, |
| 86 | ): Promise<void> => { |
| 87 | const deadline = Date.now() + (options.timeoutMs ?? 90_000); |
| 88 | let lastError: unknown; |
| 89 | while (Date.now() < deadline) { |
| 90 | try { |
| 91 | const response = await fetch(url, { redirect: "manual" }); |
| 92 | // During a cold vite compile /api/* falls back to the SPA's 200 HTML — |
| 93 | // expectRedirect waits for the real handler (302) instead. |
| 94 | if (options.expectRedirect ? response.status === 302 : response.status < 500) return; |
| 95 | lastError = new Error(`status ${response.status}`); |
| 96 | } catch (error) { |
| 97 | lastError = error; |
| 98 | } |
| 99 | await new Promise((resolve) => setTimeout(resolve, 400)); |
| 100 | } |
| 101 | throw new Error(`timed out waiting for ${url}: ${String(lastError)}`); |
| 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * Reboot DOWN-GATE: wait until `url` stops answering (fetch rejects — connection |