| 122 | * als Zeitueberschreitung ohne Begruendung endet. |
| 123 | */ |
| 124 | export async function startServer(binary, { home, port, log = [] }) { |
| 125 | const started = Date.now(); |
| 126 | const child = spawn(binary, ['--ui=true', `--port=${port}`], { |
| 127 | env: { ...process.env, HOME: home }, |
| 128 | stdio: ['pipe', 'pipe', 'pipe'], |
| 129 | }); |
| 130 | child.stdout.on('data', (d) => { log.push(`[server stdout] ${d.toString().trim()}`); }); |
| 131 | child.stderr.on('data', (d) => { log.push(`[server stderr] ${d.toString().trim()}`); }); |
| 132 | |
| 133 | let exited = null; |
| 134 | child.on('exit', (code, signal) => { exited = { code, signal }; }); |
| 135 | child.stdin.write(INITIALIZE + '\n'); |
| 136 | |
| 137 | const deadline = Date.now() + SERVER_READY_TIMEOUT_MS; |
| 138 | while (Date.now() < deadline) { |
| 139 | if (exited) { |
| 140 | throw new Error( |
| 141 | `C-Server beendete sich vorzeitig (code=${exited.code}, signal=${exited.signal})\n` + |
| 142 | log.slice(-20).join('\n'), |
| 143 | ); |
| 144 | } |
| 145 | try { |
| 146 | const res = await fetch(`http://127.0.0.1:${port}/`, { redirect: 'manual' }); |
| 147 | await res.arrayBuffer(); |
| 148 | if (res.status === 200) { |
| 149 | return { child, durationMs: Date.now() - started }; |
| 150 | } |
| 151 | } catch { |
| 152 | // noch nicht da |
| 153 | } |
| 154 | await sleep(200); |
| 155 | } |
| 156 | throw new Error( |
| 157 | `C-Server wurde binnen ${SERVER_READY_TIMEOUT_MS} ms nicht auf 127.0.0.1:${port} erreichbar\n` + |
| 158 | log.slice(-20).join('\n'), |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** Den Prozess beenden, erst hoeflich und dann nicht mehr. */ |
| 163 | export async function stopServer(child, label = 'C-Server') { |