( hostname: string, port: number, password: string, options: SpawnLocalServerOptions, )
| 53 | } |
| 54 | |
| 55 | export async function spawnLocalServer( |
| 56 | hostname: string, |
| 57 | port: number, |
| 58 | password: string, |
| 59 | options: SpawnLocalServerOptions, |
| 60 | ) { |
| 61 | const sidecar = join(dirname(fileURLToPath(import.meta.url)), "sidecar.js") |
| 62 | const child = utilityProcess.fork(sidecar, [], { |
| 63 | cwd: process.cwd(), |
| 64 | env: createSidecarEnv(), |
| 65 | serviceName: SIDECAR_SERVICE_NAME, |
| 66 | stdio: "pipe", |
| 67 | }) |
| 68 | let exited = false |
| 69 | const exit = defer<number>() |
| 70 | |
| 71 | const onProcessGone = (_event: unknown, details: Details) => { |
| 72 | if (details.type !== "Utility" || details.name !== SIDECAR_SERVICE_NAME) return |
| 73 | options.onStderr?.(`utility process gone reason=${details.reason} exitCode=${details.exitCode}`) |
| 74 | } |
| 75 | |
| 76 | app.on("child-process-gone", onProcessGone) |
| 77 | child.once("exit", (code) => { |
| 78 | exited = true |
| 79 | app.off("child-process-gone", onProcessGone) |
| 80 | options.onExit?.(code) |
| 81 | exit.resolve(code) |
| 82 | }) |
| 83 | child.on("error", (error) => options.onStderr?.(`utility process error: ${serializeError(error).message}`)) |
| 84 | |
| 85 | child.stdout?.on("data", (chunk: Buffer) => options.onStdout?.(chunk.toString("utf8").trimEnd())) |
| 86 | child.stderr?.on("data", (chunk: Buffer) => options.onStderr?.(chunk.toString("utf8").trimEnd())) |
| 87 | |
| 88 | await new Promise<void>((resolve, reject) => { |
| 89 | let done = false |
| 90 | let timeout: NodeJS.Timeout |
| 91 | |
| 92 | const fail = (error: Error) => { |
| 93 | if (done) return |
| 94 | done = true |
| 95 | cleanup() |
| 96 | reject(error) |
| 97 | } |
| 98 | |
| 99 | const refreshTimeout = () => { |
| 100 | clearTimeout(timeout) |
| 101 | timeout = setTimeout(() => { |
| 102 | fail(new Error(`Sidecar did not become ready within ${SIDECAR_START_STALL_TIMEOUT}ms: ${sidecar}`)) |
| 103 | }, SIDECAR_START_STALL_TIMEOUT) |
| 104 | } |
| 105 | |
| 106 | const onMessage = (message: SidecarMessage) => { |
| 107 | if (message.type === "ready") { |
| 108 | if (done) return |
| 109 | done = true |
| 110 | cleanup() |
| 111 | resolve() |
| 112 | return |
no test coverage detected