@nativestudio/local-runtime 0.0.0-stage → 0.3.0-rc.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ import { spawn } from "node:child_process";
2
+ import { watchOwnedParent } from "./parent-lifetime.mjs";
3
+ if (!process.send) throw Error("OWNED_AGENT_SUPERVISOR_REQUIRED");
4
+ const controller = new AbortController(),
5
+ watcher = watchOwnedParent(Number(process.argv[2]), process.argv[3], controller);
6
+ let child,
7
+ timer,
8
+ settled = false;
9
+ const stop = () => controller.abort();
10
+ controller.signal.addEventListener(
11
+ "abort",
12
+ () => {
13
+ // This supervisor is always a fresh detached group. Kill descendants too,
14
+ // including children that closed their inherited stdout/stderr descriptors.
15
+ try {
16
+ process.kill(-process.pid, "SIGKILL");
17
+ } catch {
18
+ process.exit(1);
19
+ }
20
+ },
21
+ { once: true },
22
+ );
23
+ const startup = setTimeout(stop, 5000);
24
+ function finish(ok) {
25
+ if (settled) return;
26
+ settled = true;
27
+ clearTimeout(startup);
28
+ clearTimeout(timer);
29
+ watcher.close();
30
+ process.exitCode = ok && !controller.signal.aborted ? 0 : 1;
31
+ }
32
+ process.stdout.on("error", stop);
33
+ process.once("message", async (r) => {
34
+ clearTimeout(startup);
35
+ try {
36
+ if (
37
+ !r ||
38
+ typeof r.binary !== "string" ||
39
+ !Array.isArray(r.args) ||
40
+ !r.args.every((v) => typeof v === "string") ||
41
+ typeof r.input !== "string" ||
42
+ Buffer.byteLength(r.input) > 65536 ||
43
+ typeof r.cwd !== "string" ||
44
+ !Number.isSafeInteger(r.expiresAt) ||
45
+ r.expiresAt <= Date.now() ||
46
+ !Number.isSafeInteger(r.maxOutputBytes) ||
47
+ r.maxOutputBytes < 1 ||
48
+ !(await watcher.alive()) ||
49
+ controller.signal.aborted
50
+ )
51
+ return finish(false);
52
+ const env = { PATH: process.env.PATH ?? "/usr/bin:/bin", LANG: "C", LC_ALL: "C" };
53
+ const supplied = Object.entries(r.environment ?? {});
54
+ if (
55
+ supplied.length > 16 ||
56
+ supplied.some(
57
+ ([k, v]) =>
58
+ !/^[A-Z][A-Z0-9_]{0,60}$/.test(k) ||
59
+ /^(?:NODE_OPTIONS|LD_|DYLD_)/.test(k) ||
60
+ typeof v !== "string" ||
61
+ v.length > 4096,
62
+ )
63
+ )
64
+ return finish(false);
65
+ Object.assign(env, r.environment);
66
+ timer = setTimeout(stop, Math.min(2147483647, r.expiresAt - Date.now()));
67
+ child = spawn(r.binary, r.args, {
68
+ shell: false,
69
+ cwd: r.cwd,
70
+ env,
71
+ stdio: ["pipe", "pipe", "pipe"],
72
+ });
73
+ let out = 0,
74
+ err = 0;
75
+ child.stdout.on("data", (b) => {
76
+ out += b.length;
77
+ if (out > r.maxOutputBytes) stop();
78
+ });
79
+ child.stderr.on("data", (b) => {
80
+ err += b.length;
81
+ if (err > 65536) stop();
82
+ });
83
+ child.stdout.pipe(process.stdout, { end: false });
84
+ // Agent diagnostics may contain source/credentials; count but never forward.
85
+ child.stdin.on("error", stop);
86
+ child.once("error", () => finish(false));
87
+ child.once("close", (code) => finish(code === 0));
88
+ child.stdin.end(r.input);
89
+ if (controller.signal.aborted) child.kill("SIGKILL");
90
+ } catch {
91
+ stop();
92
+ if (!child) finish(false);
93
+ }
94
+ });
95
+ await watcher.check();