File size: 2,069 Bytes
ec17af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import path from "node:path";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const apiScript = path.join(__dirname, "desktop_api.py");
const webScript = path.join(__dirname, "desktop_web.mjs");
const apiPort = process.env.DESKTOP_API_PORT || "18000";
const webPort = process.env.DESKTOP_WEB_PORT || "13000";

const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const waitForHttp = async (url, timeoutMs) => {
  const start = Date.now();
  while (Date.now() - start < timeoutMs) {
    try {
      const response = await fetch(url);
      if (response.ok) {
        return;
      }
    } catch {
      // Keep polling until the timeout.
    }
    await wait(500);
  }
  throw new Error(`Timed out waiting for ${url}`);
};

const forwardSignal = (child, signal) => {
  if (child.exitCode === null) {
    child.kill(signal);
  }
};

const main = async () => {
  const apiProcess = spawn(process.env.DESKTOP_PYTHON || "python3", [apiScript], {
    stdio: "inherit",
    env: process.env,
  });

  process.on("SIGINT", () => forwardSignal(apiProcess, "SIGINT"));
  process.on("SIGTERM", () => forwardSignal(apiProcess, "SIGTERM"));

  await waitForHttp(`http://127.0.0.1:${apiPort}/docs`, 60000);

  const webProcess = spawn(process.execPath, [webScript], {
    stdio: "inherit",
    env: process.env,
  });

  process.on("SIGINT", () => forwardSignal(webProcess, "SIGINT"));
  process.on("SIGTERM", () => forwardSignal(webProcess, "SIGTERM"));

  console.log(`Desktop runtime ready: web=http://127.0.0.1:${webPort} api=http://127.0.0.1:${apiPort}`);

  apiProcess.on("exit", (code) => {
    forwardSignal(webProcess, "SIGTERM");
    process.exitCode = code ?? 0;
  });
  webProcess.on("exit", (code) => {
    forwardSignal(apiProcess, "SIGTERM");
    process.exitCode = code ?? 0;
  });
};

main().catch((error) => {
  console.error(error instanceof Error ? error.message : String(error));
  process.exitCode = 1;
});