(url: string, timeoutMs: number)
| 80 | } |
| 81 | |
| 82 | export function waitForHttpOk(url: string, timeoutMs: number): Promise<void> { |
| 83 | const deadline = Date.now() + timeoutMs; |
| 84 | return new Promise((resolve, reject) => { |
| 85 | const attempt = () => { |
| 86 | const req = http.get(url, (res) => { |
| 87 | res.resume(); |
| 88 | if ((res.statusCode ?? 500) < 500) { |
| 89 | resolve(); |
| 90 | return; |
| 91 | } |
| 92 | retry(); |
| 93 | }); |
| 94 | req.on('error', retry); |
| 95 | }; |
| 96 | const retry = () => { |
| 97 | if (Date.now() >= deadline) { |
| 98 | reject(new Error(`Timed out waiting for ${url}.`)); |
| 99 | return; |
| 100 | } |
| 101 | setTimeout(attempt, 25); |
| 102 | }; |
| 103 | attempt(); |
| 104 | }); |
| 105 | } |
no test coverage detected