(opts: WaitForDaemonReadyOptions)
| 160 | * Throws if the daemon doesn't respond within the timeout. |
| 161 | */ |
| 162 | export async function waitForDaemonReady(opts: WaitForDaemonReadyOptions): Promise<void> { |
| 163 | const client = new DaemonClient({ |
| 164 | socketPath: opts.socketPath, |
| 165 | timeout: Math.min(opts.timeoutMs, 2000), // Short timeout for each status check |
| 166 | }); |
| 167 | |
| 168 | const pollInterval = opts.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS; |
| 169 | const startTime = Date.now(); |
| 170 | |
| 171 | while (Date.now() - startTime < opts.timeoutMs) { |
| 172 | try { |
| 173 | // Use status() to confirm protocol handler is ready (not just connect) |
| 174 | await client.status(); |
| 175 | return; // Success |
| 176 | } catch { |
| 177 | // Not ready yet, wait and retry |
| 178 | await new Promise((resolve) => setTimeout(resolve, pollInterval)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | throw new Error( |
| 183 | `Daemon failed to start within ${opts.timeoutMs}ms. ` + |
| 184 | `Check if another daemon is running or if there are permission issues.`, |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | export interface EnsureDaemonRunningOptions { |
| 189 | socketPath: string; |
no test coverage detected