* Start the server in the current process and block until shutdown. Shared by * the detached daemon (`daemon: true`, with idle-exit) and the foreground * runner (`daemon: false`). `onReady` fires once the server is listening.
(
options: ParsedServerOptions,
mode: { daemon: boolean },
onReady?: (origin: string) => void,
)
| 286 | * runner (`daemon: false`). `onReady` fires once the server is listening. |
| 287 | */ |
| 288 | async function runServerInProcess( |
| 289 | options: ParsedServerOptions, |
| 290 | mode: { daemon: boolean }, |
| 291 | onReady?: (origin: string) => void, |
| 292 | ): Promise<never> { |
| 293 | const version = getVersion(); |
| 294 | const telemetry = initializeServerTelemetry({ version }); |
| 295 | |
| 296 | let running: RunningServer | undefined; |
| 297 | let stopping = false; |
| 298 | |
| 299 | const idle = mode.daemon |
| 300 | ? createIdleShutdownHandler({ |
| 301 | graceMs: options.idleGraceMs, |
| 302 | onIdle: () => { |
| 303 | void shutdown('idle'); |
| 304 | }, |
| 305 | }) |
| 306 | : undefined; |
| 307 | |
| 308 | async function shutdown(reason: string): Promise<void> { |
| 309 | if (stopping) return; |
| 310 | stopping = true; |
| 311 | idle?.cancel(); |
| 312 | running?.logger.info({ reason }, 'server shutting down'); |
| 313 | try { |
| 314 | await running?.close(); |
| 315 | await shutdownTelemetry({ timeoutMs: CLI_SHUTDOWN_TIMEOUT_MS }); |
| 316 | } catch (error) { |
| 317 | running?.logger.error( |
| 318 | { err: error instanceof Error ? error : new Error(String(error)) }, |
| 319 | 'server shutdown error', |
| 320 | ); |
| 321 | } |
| 322 | process.exit(0); |
| 323 | } |
| 324 | |
| 325 | running = await startServer({ |
| 326 | host: options.host, |
| 327 | port: options.port, |
| 328 | logLevel: options.logLevel, |
| 329 | debugEndpoints: options.debugEndpoints, |
| 330 | insecureNoTls: options.insecureNoTls, |
| 331 | allowRemoteShutdown: options.allowRemoteShutdown, |
| 332 | allowRemoteTerminals: options.allowRemoteTerminals, |
| 333 | allowedHosts: options.allowedHosts, |
| 334 | webAssetsDir: serverWebAssetsDir(), |
| 335 | coreProcessOptions: { |
| 336 | identity: createKimiCodeHostIdentity(version), |
| 337 | telemetry, |
| 338 | }, |
| 339 | wsGatewayOptions: { |
| 340 | telemetry, |
| 341 | onConnectionCountChange: idle |
| 342 | ? (size) => { |
| 343 | idle.onConnectionCountChange(size); |
| 344 | } |
| 345 | : undefined, |
no test coverage detected