| 8 | * @throws Error if stdout is not available, timeout occurs, or stdout closes before readiness |
| 9 | */ |
| 10 | export async function waitForWranglerReady(processPromise: ProcessPromise) { |
| 11 | const stdout = processPromise.stdout |
| 12 | |
| 13 | if (!stdout) { |
| 14 | throw new Error('Wrangler stdout is not available; cannot detect readiness') |
| 15 | } |
| 16 | |
| 17 | const controller = new AbortController() |
| 18 | const timeoutId = setTimeout(() => { |
| 19 | controller.abort(new Error('Timed out waiting for wrangler to report readiness')) |
| 20 | }, 30_000) |
| 21 | |
| 22 | const rl = createInterface({ input: stdout, crlfDelay: Infinity }) |
| 23 | |
| 24 | try { |
| 25 | controller.signal.throwIfAborted() |
| 26 | |
| 27 | for await (const line of rl) { |
| 28 | controller.signal.throwIfAborted() |
| 29 | |
| 30 | if (line.includes('Ready')) { |
| 31 | return |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | throw new Error('Wrangler stdout closed before reporting readiness') |
| 36 | } catch (error) { |
| 37 | if (controller.signal.aborted) { |
| 38 | throw controller.signal.reason as Error |
| 39 | } |
| 40 | |
| 41 | throw error |
| 42 | } finally { |
| 43 | clearTimeout(timeoutId) |
| 44 | rl.close() |
| 45 | stdout.resume() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Stops a Wrangler process gracefully by sending SIGINT and waiting for it to exit. |