@botiverse/oar 0.99.0 → 0.101.0
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.
- package/README.md +17 -17
- package/dist/contracts/login.d.ts +7 -1
- package/dist/contracts/login.d.ts.map +1 -1
- package/dist/contracts/provider-auth.d.ts +8 -4
- package/dist/contracts/provider-auth.d.ts.map +1 -1
- package/dist/contracts/session-options.d.ts +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/runtimes/antigravity/logout.d.ts.map +1 -1
- package/dist/runtimes/antigravity/logout.js +3 -2
- package/dist/runtimes/antigravity/logout.js.map +1 -1
- package/dist/runtimes/claude/logout.d.ts.map +1 -1
- package/dist/runtimes/claude/logout.js +3 -2
- package/dist/runtimes/claude/logout.js.map +1 -1
- package/dist/runtimes/codebuddy/index.d.ts +1 -0
- package/dist/runtimes/codebuddy/index.d.ts.map +1 -1
- package/dist/runtimes/codebuddy/index.js +1 -0
- package/dist/runtimes/codebuddy/index.js.map +1 -1
- package/dist/runtimes/codebuddy/logout.d.ts +5 -0
- package/dist/runtimes/codebuddy/logout.d.ts.map +1 -0
- package/dist/runtimes/codebuddy/logout.js +57 -0
- package/dist/runtimes/codebuddy/logout.js.map +1 -0
- package/dist/runtimes/codex/installation-candidates.d.ts +31 -0
- package/dist/runtimes/codex/installation-candidates.d.ts.map +1 -0
- package/dist/runtimes/codex/installation-candidates.js +92 -0
- package/dist/runtimes/codex/installation-candidates.js.map +1 -0
- package/dist/runtimes/codex/installation.d.ts +1 -19
- package/dist/runtimes/codex/installation.d.ts.map +1 -1
- package/dist/runtimes/codex/installation.js +2 -49
- package/dist/runtimes/codex/installation.js.map +1 -1
- package/dist/runtimes/codex/logout.d.ts.map +1 -1
- package/dist/runtimes/codex/logout.js +3 -2
- package/dist/runtimes/codex/logout.js.map +1 -1
- package/dist/runtimes/cursor/access.d.ts +68 -0
- package/dist/runtimes/cursor/access.d.ts.map +1 -0
- package/dist/runtimes/cursor/access.js +77 -0
- package/dist/runtimes/cursor/access.js.map +1 -0
- package/dist/runtimes/cursor/auth-status.d.ts +4 -3
- package/dist/runtimes/cursor/auth-status.d.ts.map +1 -1
- package/dist/runtimes/cursor/auth-status.js +20 -18
- package/dist/runtimes/cursor/auth-status.js.map +1 -1
- package/dist/runtimes/cursor/index.d.ts +50 -10
- package/dist/runtimes/cursor/index.d.ts.map +1 -1
- package/dist/runtimes/cursor/index.js +40 -21
- package/dist/runtimes/cursor/index.js.map +1 -1
- package/dist/runtimes/cursor/installation.d.ts +1 -1
- package/dist/runtimes/cursor/installation.d.ts.map +1 -1
- package/dist/runtimes/cursor/installation.js +5 -2
- package/dist/runtimes/cursor/installation.js.map +1 -1
- package/dist/runtimes/cursor/list-models.d.ts +4 -2
- package/dist/runtimes/cursor/list-models.d.ts.map +1 -1
- package/dist/runtimes/cursor/list-models.js +31 -14
- package/dist/runtimes/cursor/list-models.js.map +1 -1
- package/dist/runtimes/cursor/login.d.ts +8 -3
- package/dist/runtimes/cursor/login.d.ts.map +1 -1
- package/dist/runtimes/cursor/login.js +29 -22
- package/dist/runtimes/cursor/login.js.map +1 -1
- package/dist/runtimes/cursor/logout.d.ts +3 -3
- package/dist/runtimes/cursor/logout.d.ts.map +1 -1
- package/dist/runtimes/cursor/logout.js +19 -18
- package/dist/runtimes/cursor/logout.js.map +1 -1
- package/dist/runtimes/cursor/model.d.ts +5 -0
- package/dist/runtimes/cursor/model.d.ts.map +1 -1
- package/dist/runtimes/cursor/model.js +10 -1
- package/dist/runtimes/cursor/model.js.map +1 -1
- package/dist/runtimes/cursor/sdk.d.ts +0 -4
- package/dist/runtimes/cursor/sdk.d.ts.map +1 -1
- package/dist/runtimes/cursor/sdk.js +5 -11
- package/dist/runtimes/cursor/sdk.js.map +1 -1
- package/dist/runtimes/cursor/session-process.d.ts +31 -0
- package/dist/runtimes/cursor/session-process.d.ts.map +1 -0
- package/dist/runtimes/cursor/session-process.js +80 -0
- package/dist/runtimes/cursor/session-process.js.map +1 -0
- package/dist/runtimes/cursor/session.d.ts +2 -2
- package/dist/runtimes/cursor/session.d.ts.map +1 -1
- package/dist/runtimes/cursor/session.js +10 -10
- package/dist/runtimes/cursor/session.js.map +1 -1
- package/dist/runtimes/cursor/subprocess/channel.d.ts +37 -0
- package/dist/runtimes/cursor/subprocess/channel.d.ts.map +1 -0
- package/dist/runtimes/cursor/subprocess/channel.js +170 -0
- package/dist/runtimes/cursor/subprocess/channel.js.map +1 -0
- package/dist/runtimes/cursor/subprocess/child-script.d.ts +12 -0
- package/dist/runtimes/cursor/subprocess/child-script.d.ts.map +1 -0
- package/dist/runtimes/cursor/subprocess/child-script.js +257 -0
- package/dist/runtimes/cursor/subprocess/child-script.js.map +1 -0
- package/dist/runtimes/cursor/subprocess/proxy.d.ts +11 -0
- package/dist/runtimes/cursor/subprocess/proxy.d.ts.map +1 -0
- package/dist/runtimes/cursor/subprocess/proxy.js +242 -0
- package/dist/runtimes/cursor/subprocess/proxy.js.map +1 -0
- package/dist/runtimes/grok/auth.d.ts.map +1 -1
- package/dist/runtimes/grok/auth.js +3 -2
- package/dist/runtimes/grok/auth.js.map +1 -1
- package/dist/runtimes/kimi/logout.d.ts.map +1 -1
- package/dist/runtimes/kimi/logout.js +3 -2
- package/dist/runtimes/kimi/logout.js.map +1 -1
- package/dist/runtimes/opencode/index.d.ts +2 -0
- package/dist/runtimes/opencode/index.d.ts.map +1 -1
- package/dist/runtimes/opencode/index.js +1 -0
- package/dist/runtimes/opencode/index.js.map +1 -1
- package/dist/runtimes/opencode/provider-auth-error.d.ts +25 -0
- package/dist/runtimes/opencode/provider-auth-error.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-error.js +13 -0
- package/dist/runtimes/opencode/provider-auth-error.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth-facade.d.ts +28 -0
- package/dist/runtimes/opencode/provider-auth-facade.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-facade.js +178 -0
- package/dist/runtimes/opencode/provider-auth-facade.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth-oauth.d.ts +24 -0
- package/dist/runtimes/opencode/provider-auth-oauth.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-oauth.js +189 -0
- package/dist/runtimes/opencode/provider-auth-oauth.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth-prompts.d.ts +16 -0
- package/dist/runtimes/opencode/provider-auth-prompts.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-prompts.js +91 -0
- package/dist/runtimes/opencode/provider-auth-prompts.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth-registry.d.ts +79 -0
- package/dist/runtimes/opencode/provider-auth-registry.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-registry.js +146 -0
- package/dist/runtimes/opencode/provider-auth-registry.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth-server.d.ts +38 -0
- package/dist/runtimes/opencode/provider-auth-server.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth-server.js +221 -0
- package/dist/runtimes/opencode/provider-auth-server.js.map +1 -0
- package/dist/runtimes/opencode/provider-auth.d.ts +49 -0
- package/dist/runtimes/opencode/provider-auth.d.ts.map +1 -0
- package/dist/runtimes/opencode/provider-auth.js +39 -0
- package/dist/runtimes/opencode/provider-auth.js.map +1 -0
- package/dist/shared/acp/session.js +4 -1
- package/dist/shared/acp/session.js.map +1 -1
- package/dist/shared/acp/turn-terminals.d.ts +2 -1
- package/dist/shared/acp/turn-terminals.d.ts.map +1 -1
- package/dist/shared/acp/turn-terminals.js +47 -23
- package/dist/shared/acp/turn-terminals.js.map +1 -1
- package/dist/shared/executable/environment-marker.d.ts +21 -0
- package/dist/shared/executable/environment-marker.d.ts.map +1 -0
- package/dist/shared/executable/environment-marker.js +137 -0
- package/dist/shared/executable/environment-marker.js.map +1 -0
- package/dist/shared/executable/host-watchdog.d.ts +22 -0
- package/dist/shared/executable/host-watchdog.d.ts.map +1 -0
- package/dist/shared/executable/host-watchdog.js +79 -0
- package/dist/shared/executable/host-watchdog.js.map +1 -0
- package/dist/shared/executable/node-eval.d.ts +10 -0
- package/dist/shared/executable/node-eval.d.ts.map +1 -0
- package/dist/shared/executable/node-eval.js +17 -0
- package/dist/shared/executable/node-eval.js.map +1 -0
- package/dist/shared/executable/node-script.d.ts +53 -0
- package/dist/shared/executable/node-script.d.ts.map +1 -0
- package/dist/shared/executable/node-script.js +131 -0
- package/dist/shared/executable/node-script.js.map +1 -0
- package/dist/shared/login.d.ts +9 -4
- package/dist/shared/login.d.ts.map +1 -1
- package/dist/shared/login.js +15 -7
- package/dist/shared/login.js.map +1 -1
- package/dist/shared/logout.d.ts +8 -0
- package/dist/shared/logout.d.ts.map +1 -1
- package/dist/shared/logout.js +13 -0
- package/dist/shared/logout.js.map +1 -1
- package/dist/shared/shell-environment.d.ts.map +1 -1
- package/dist/shared/shell-environment.js +6 -13
- package/dist/shared/shell-environment.js.map +1 -1
- package/package.json +5 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { asRecord } from "../json.js";
|
|
2
|
-
|
|
2
|
+
/** The abort requests the runtime accepted. */
|
|
3
|
+
const accepted = (abort) => [...abort.requests].flatMap(([id, answer]) => answer === "accepted" ? [id] : []);
|
|
4
|
+
const passive = { aborting: () => ({ accepted() { }, refused() { } }) };
|
|
3
5
|
export function acpTurnTerminals(enabled, kernel, host, active) {
|
|
4
6
|
if (!enabled) {
|
|
5
7
|
return passive;
|
|
@@ -8,24 +10,33 @@ export function acpTurnTerminals(enabled, kernel, host, active) {
|
|
|
8
10
|
const creating = new Map();
|
|
9
11
|
const created = [];
|
|
10
12
|
const pending = new Map();
|
|
11
|
-
const record = (
|
|
12
|
-
for (const requestId of
|
|
13
|
+
const record = (requestIds, result) => {
|
|
14
|
+
for (const requestId of requestIds) {
|
|
13
15
|
kernel.respond(requestId, { kind: "cleanup", result });
|
|
14
16
|
}
|
|
15
17
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
/** The seq of the last frame that closed a turn: an abort sent after its turn closed is bounded by it. */
|
|
19
|
+
let lastClose = 0;
|
|
20
|
+
const end = async (requestIds, since, closedAt) => {
|
|
21
|
+
const ids = created.filter((entry) => entry.seq > since && entry.seq < closedAt).map((entry) => entry.terminalId);
|
|
18
22
|
try {
|
|
19
23
|
const { signalled } = await host.end(ids);
|
|
20
|
-
record(
|
|
24
|
+
record(requestIds, { kind: "completed", signalled });
|
|
21
25
|
}
|
|
22
26
|
catch (error) {
|
|
23
|
-
record(
|
|
27
|
+
record(requestIds, { kind: "failed", reason: `stopping the aborted turn's terminals failed: ${error instanceof Error ? error.message : String(error)}` });
|
|
24
28
|
}
|
|
25
29
|
};
|
|
26
|
-
|
|
30
|
+
/** Settle once the turn has closed and every abort sent to it has its answer: only accepted aborts end anything. */
|
|
31
|
+
const settle = (abort) => {
|
|
32
|
+
if (abort.closedAt === undefined || [...abort.requests.values()].includes("sent")) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
27
35
|
pending.delete(abort.state);
|
|
28
|
-
|
|
36
|
+
const requestIds = accepted(abort);
|
|
37
|
+
if (requestIds.length > 0) {
|
|
38
|
+
void end(requestIds, abort.state.since, abort.closedAt);
|
|
39
|
+
}
|
|
29
40
|
};
|
|
30
41
|
kernel.rawEvents((event) => {
|
|
31
42
|
if (event.kind === "request") {
|
|
@@ -46,32 +57,45 @@ export function acpTurnTerminals(enabled, kernel, host, active) {
|
|
|
46
57
|
if (event.body.kind === "exited" || event.body.kind === "released") {
|
|
47
58
|
const reason = event.body.kind === "exited" ? "the runtime exited before the aborted turn ended; its exit cleanup applies instead" : "OAR released the session before the aborted turn ended; dispose ends its terminals";
|
|
48
59
|
for (const abort of pending.values()) {
|
|
49
|
-
record(abort, { kind: "failed", reason });
|
|
60
|
+
record(accepted(abort), { kind: "failed", reason });
|
|
50
61
|
}
|
|
51
62
|
pending.clear();
|
|
52
63
|
}
|
|
53
64
|
return;
|
|
54
65
|
}
|
|
55
|
-
if (event.body.events.some((body) => body.kind === "turn_ended")) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
if (!event.body.events.some((body) => body.kind === "turn_ended")) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
lastClose = event.seq;
|
|
70
|
+
for (const abort of pending.values()) {
|
|
71
|
+
if (abort.closedAt === undefined && active() !== abort.state) {
|
|
72
|
+
abort.closedAt = event.seq;
|
|
73
|
+
settle(abort);
|
|
60
74
|
}
|
|
61
75
|
}
|
|
76
|
+
// With no abort waiting, nothing needs terminals from turns that have ended:
|
|
77
|
+
// a later abort only looks at terminals its own turn requested.
|
|
78
|
+
if (pending.size === 0) {
|
|
79
|
+
created.length = 0;
|
|
80
|
+
creating.clear();
|
|
81
|
+
}
|
|
62
82
|
});
|
|
63
83
|
return {
|
|
64
84
|
aborting(state, request) {
|
|
65
|
-
const abort = pending.get(state) ?? { state, requests: new
|
|
66
|
-
abort.requests.
|
|
85
|
+
const abort = pending.get(state) ?? { state, requests: new Map(), closedAt: active() === state ? undefined : lastClose };
|
|
86
|
+
abort.requests.set(request.id, "sent");
|
|
67
87
|
pending.set(state, abort);
|
|
68
88
|
return {
|
|
69
|
-
|
|
70
|
-
abort.requests.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
89
|
+
accepted() { if (abort.requests.has(request.id)) {
|
|
90
|
+
abort.requests.set(request.id, "accepted");
|
|
91
|
+
settle(abort);
|
|
92
|
+
} },
|
|
93
|
+
refused() { abort.requests.delete(request.id); if (abort.requests.size === 0) {
|
|
94
|
+
pending.delete(state);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
settle(abort);
|
|
98
|
+
} },
|
|
75
99
|
};
|
|
76
100
|
},
|
|
77
101
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turn-terminals.js","sources":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ACtJ,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YACvC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YAC1B,OAAO;gBACL,QAAQ,KAAK,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;oBAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;oBAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAAC,CAAC,CAAC,CAAC;gBACjH,OAAO,KAAK,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;oBAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAAC,CAAC;qBAAM,CAAC;oBAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAAC,CAAC,CAAC,CAAC;aACnI,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A fresh marker: `NAME=1`, the name unique to one child. Not a secret. */
|
|
2
|
+
export declare function newEnvironmentMarker(): {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly entry: string;
|
|
5
|
+
};
|
|
6
|
+
/** The pids of this user's processes, other than this one, started with `entry` (`NAME=value`) in their environment. Linux only; empty elsewhere. */
|
|
7
|
+
export declare function markedProcesses(entry: string): number[];
|
|
8
|
+
/** SIGKILL every process `markedProcesses(entry)` finds, pass after pass until one finds none (at most five). Synchronous, so an `exit` hook can call it. */
|
|
9
|
+
export declare function killMarked(entry: string): void;
|
|
10
|
+
/** The same without blocking the event loop: `/proc` is read asynchronously. */
|
|
11
|
+
export declare function markedProcessesAsync(entry: string): Promise<number[]>;
|
|
12
|
+
/** `killMarked` without blocking the event loop. */
|
|
13
|
+
export declare function killMarkedAsync(entry: string): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Keep `entry` scanned for while its child may run: a host that calls
|
|
16
|
+
* `process.exit` ends the marked processes from its `exit` hook too.
|
|
17
|
+
* Returns the release, for once the child exited and its marked processes
|
|
18
|
+
* were ended.
|
|
19
|
+
*/
|
|
20
|
+
export declare function trackEnvironmentMarker(entry: string): () => void;
|
|
21
|
+
//# sourceMappingURL=environment-marker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment-marker.d.ts","sources":["../../../src/shared/executable/environment-marker.ts"],"names":[],"mappings":"AAiBA,4EAA4E;AAC5E,wBAAgB,oBAAoB,IAAI;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAGxF;AAED,qJAAqJ;AACrJ,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAiBvD;AAcD,6JAA6J;AAC7J,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAM9C;AAED,gFAAgF;AAChF,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAkB3E;AAED,oDAAoD;AACpD,wBAAsB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAOlE;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,IAAI,CAShE"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
4
|
+
/*
|
|
5
|
+
* An environment marker: a variable a child is started with, which every
|
|
6
|
+
* process it starts inherits unless that process clears its environment.
|
|
7
|
+
* After the child is gone, the processes still carrying it are the ones it
|
|
8
|
+
* started, even those reparented away from it (a shell's `cmd &` or `nohup`
|
|
9
|
+
* job, once the shell exited) and outside its process group, which a
|
|
10
|
+
* parent-pid walk or a group kill cannot find. This is the approach of
|
|
11
|
+
* Jenkins' ProcessTreeKiller. Linux only: it reads `/proc/<pid>/environ`,
|
|
12
|
+
* which holds a process's environment as it was started; elsewhere there is
|
|
13
|
+
* no portable way to read another process's environment, and nothing is
|
|
14
|
+
* scanned.
|
|
15
|
+
*/
|
|
16
|
+
/** A fresh marker: `NAME=1`, the name unique to one child. Not a secret. */
|
|
17
|
+
export function newEnvironmentMarker() {
|
|
18
|
+
const name = `OAR_CURSOR_CHILD_${randomUUID().replaceAll("-", "")}`;
|
|
19
|
+
return { name, entry: `${name}=1` };
|
|
20
|
+
}
|
|
21
|
+
/** The pids of this user's processes, other than this one, started with `entry` (`NAME=value`) in their environment. Linux only; empty elsewhere. */
|
|
22
|
+
export function markedProcesses(entry) {
|
|
23
|
+
if (process.platform !== "linux") {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const uid = process.getuid?.();
|
|
27
|
+
const found = [];
|
|
28
|
+
let names = [];
|
|
29
|
+
try {
|
|
30
|
+
names = readdirSync("/proc");
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return found;
|
|
34
|
+
}
|
|
35
|
+
for (const name of names) {
|
|
36
|
+
const pid = Number(name);
|
|
37
|
+
if (!/^\d+$/u.test(name) || pid === process.pid) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
if (statSync(`/proc/${name}`).uid !== uid) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (readFileSync(`/proc/${name}/environ`, "latin1").split("\0").includes(entry)) {
|
|
45
|
+
found.push(pid);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Gone meanwhile, or not readable: not ours to end.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
const live = new Set();
|
|
55
|
+
let exitHookInstalled = false;
|
|
56
|
+
/** Scan passes at most: a marked process may fork between a pass's read and its kill, so a pass that found some is followed by another. */
|
|
57
|
+
const PASSES = 5;
|
|
58
|
+
function kill(pids) {
|
|
59
|
+
for (const pid of pids) {
|
|
60
|
+
try {
|
|
61
|
+
process.kill(pid, "SIGKILL");
|
|
62
|
+
}
|
|
63
|
+
catch { /* Gone meanwhile. */ }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** SIGKILL every process `markedProcesses(entry)` finds, pass after pass until one finds none (at most five). Synchronous, so an `exit` hook can call it. */
|
|
67
|
+
export function killMarked(entry) {
|
|
68
|
+
for (let pass = 0; pass < PASSES; pass += 1) {
|
|
69
|
+
const found = markedProcesses(entry);
|
|
70
|
+
if (found.length === 0) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
kill(found);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** The same without blocking the event loop: `/proc` is read asynchronously. */
|
|
77
|
+
export async function markedProcessesAsync(entry) {
|
|
78
|
+
if (process.platform !== "linux") {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
const uid = process.getuid?.();
|
|
82
|
+
let names = [];
|
|
83
|
+
try {
|
|
84
|
+
names = await readdir("/proc");
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const checked = await Promise.all(names.map(async (name) => {
|
|
90
|
+
const pid = Number(name);
|
|
91
|
+
if (!/^\d+$/u.test(name) || pid === process.pid) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const owner = await stat(`/proc/${name}`);
|
|
96
|
+
if (owner.uid !== uid) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const environment = await readFile(`/proc/${name}/environ`, "latin1");
|
|
100
|
+
return environment.split("\0").includes(entry) ? pid : null;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
return checked.filter((pid) => pid !== null);
|
|
107
|
+
}
|
|
108
|
+
/** `killMarked` without blocking the event loop. */
|
|
109
|
+
export async function killMarkedAsync(entry) {
|
|
110
|
+
for (let pass = 0; pass < PASSES; pass += 1) {
|
|
111
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- each pass reads the table after the previous pass's kills.
|
|
112
|
+
const found = await markedProcessesAsync(entry);
|
|
113
|
+
if (found.length === 0) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
kill(found);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Keep `entry` scanned for while its child may run: a host that calls
|
|
121
|
+
* `process.exit` ends the marked processes from its `exit` hook too.
|
|
122
|
+
* Returns the release, for once the child exited and its marked processes
|
|
123
|
+
* were ended.
|
|
124
|
+
*/
|
|
125
|
+
export function trackEnvironmentMarker(entry) {
|
|
126
|
+
live.add(entry);
|
|
127
|
+
if (!exitHookInstalled) {
|
|
128
|
+
exitHookInstalled = true;
|
|
129
|
+
process.once("exit", () => {
|
|
130
|
+
for (const marker of live) {
|
|
131
|
+
killMarked(marker);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return () => { live.delete(entry); };
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=environment-marker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"environment-marker.js","sources":["../../../src/shared/executable/environment-marker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AA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AC;YAAC,OAAO;QAAC,CAAC;QACnC,IAAI,CAAC,KAAK,CAAC,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAa;IAClD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAChB,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,iBAAiB,GAAG,IAAI,CAAC;QACzB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE;YACxB,KAAK,MAAM,MAAM,IAAI,IAAI,EAAE,CAAC;gBAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAAC,CAAC;QACpD,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface WatchdogTarget {
|
|
2
|
+
readonly command: string;
|
|
3
|
+
readonly args: readonly string[];
|
|
4
|
+
/** Node is started directly, never through cmd.exe. */
|
|
5
|
+
readonly windowsVerbatimArguments?: true;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The launch of `target` under the watchdog. The host must keep the child's
|
|
9
|
+
* stdin open as a pipe for as long as the command should run: ending it
|
|
10
|
+
* stops the command. The watchdog's own grace is half the host's
|
|
11
|
+
* (`killGraceMs()`), so it escalates before the host's own SIGKILL would end
|
|
12
|
+
* the watchdog first. Null in a compiled single executable host, which
|
|
13
|
+
* cannot run it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function hostWatchdogTarget(target: {
|
|
16
|
+
readonly command: string;
|
|
17
|
+
readonly args: readonly string[];
|
|
18
|
+
readonly windowsVerbatimArguments?: true;
|
|
19
|
+
}, env: Readonly<NodeJS.ProcessEnv>): (WatchdogTarget & {
|
|
20
|
+
readonly env: NodeJS.ProcessEnv;
|
|
21
|
+
}) | null;
|
|
22
|
+
//# sourceMappingURL=host-watchdog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-watchdog.d.ts","sources":["../../../src/shared/executable/host-watchdog.ts"],"names":[],"mappings":"AAiEA,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,uDAAuD;IACvD,QAAQ,CAAC,wBAAwB,CAAC,EAAE,IAAI,CAAC;CAC1C;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IAAC,QAAQ,CAAC,wBAAwB,CAAC,EAAE,IAAI,CAAA;CAAE,EAAE,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,cAAc,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,CAAA;CAAE,CAAC,GAAG,IAAI,CAKpP"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { nodeEvalExecutable } from "./node-eval.js";
|
|
2
|
+
import { killGraceMs } from "./process-signals.js";
|
|
3
|
+
/*
|
|
4
|
+
* A long-lived child that must not outlive the host even when the host is
|
|
5
|
+
* SIGKILLed: Node runs no `exit` hook then (ownership.ts), so nothing in the
|
|
6
|
+
* host can stop it. The command runs under a tiny Node watchdog (the host's
|
|
7
|
+
* own `process.execPath`) whose stdin is a pipe from the host. Every way the
|
|
8
|
+
* host can end closes that pipe, so the watchdog's stdin reaching EOF means
|
|
9
|
+
* "the host is gone or wants the command stopped". The watchdog then stops
|
|
10
|
+
* the command and everything it started: SIGTERM to the command's own
|
|
11
|
+
* process group, SIGKILL to it after the grace period or as soon as the
|
|
12
|
+
* command exits (POSIX); `taskkill /T /F` on its tree (Windows, which has no
|
|
13
|
+
* groups). On Windows the host starts the watchdog detached: Node puts a
|
|
14
|
+
* child it does not detach in a job object that ends it together with a
|
|
15
|
+
* dying host, and a grandchild the command starts (a `.cmd` shim's node) is
|
|
16
|
+
* outside that job, so it would outlive both. A watchdog that is itself
|
|
17
|
+
* killed outright leaves the same gap. A SIGTERM, SIGINT or SIGHUP to the
|
|
18
|
+
* watchdog does the same as EOF. The
|
|
19
|
+
* command's stdout and stderr are the watchdog's, so the host reads them
|
|
20
|
+
* unchanged; its stdin is not connected. The watchdog exits with the
|
|
21
|
+
* command's exit code (1 when a signal ended it). In an Electron host,
|
|
22
|
+
* `process.execPath` is Electron, which runs `-e` only as Node with
|
|
23
|
+
* `ELECTRON_RUN_AS_NODE=1`: the watchdog gets it, and the command gets the
|
|
24
|
+
* environment's own value back. A compiled single executable host (Node
|
|
25
|
+
* SEA, Bun standalone) cannot run the watchdog: its `process.execPath`
|
|
26
|
+
* would start the application again. There the command runs without one,
|
|
27
|
+
* so it stops with the host's own exit cleanup but outlives a SIGKILLed
|
|
28
|
+
* host.
|
|
29
|
+
*/
|
|
30
|
+
const SCRIPT = String.raw `
|
|
31
|
+
const { spawn, execFileSync } = require("node:child_process");
|
|
32
|
+
const [command, args, verbatim, graceMs, electronRunAsNode] = JSON.parse(process.argv[1]);
|
|
33
|
+
const windows = process.platform === "win32";
|
|
34
|
+
// The command gets the host's own ELECTRON_RUN_AS_NODE, not the one that made this watchdog run as Node.
|
|
35
|
+
const env = { ...process.env };
|
|
36
|
+
if (electronRunAsNode === null) { delete env.ELECTRON_RUN_AS_NODE; } else { env.ELECTRON_RUN_AS_NODE = electronRunAsNode; }
|
|
37
|
+
const child = spawn(command, args, { env, stdio: ["ignore", "inherit", "inherit"], detached: !windows, windowsHide: true, windowsVerbatimArguments: verbatim });
|
|
38
|
+
let stopping = false;
|
|
39
|
+
const group = (signal) => { try { process.kill(-child.pid, signal); } catch { try { child.kill(signal); } catch {} } };
|
|
40
|
+
const stop = () => {
|
|
41
|
+
if (stopping) { return; }
|
|
42
|
+
stopping = true;
|
|
43
|
+
if (child.pid === undefined || child.exitCode !== null || child.signalCode !== null) { return; }
|
|
44
|
+
if (windows) {
|
|
45
|
+
try { execFileSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore", windowsHide: true, timeout: 10000 }); } catch { child.kill(); }
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
group("SIGTERM");
|
|
49
|
+
setTimeout(() => { group("SIGKILL"); }, graceMs).unref();
|
|
50
|
+
};
|
|
51
|
+
child.once("error", () => { process.exit(1); });
|
|
52
|
+
child.once("exit", (code) => {
|
|
53
|
+
// What it started may still be in its group.
|
|
54
|
+
if (!windows && child.pid !== undefined) { group("SIGKILL"); }
|
|
55
|
+
process.exit(code ?? 1);
|
|
56
|
+
});
|
|
57
|
+
for (const signal of ["SIGTERM", "SIGINT", "SIGHUP"]) { process.on(signal, stop); }
|
|
58
|
+
process.stdin.on("end", stop);
|
|
59
|
+
process.stdin.on("close", stop);
|
|
60
|
+
process.stdin.on("error", stop);
|
|
61
|
+
process.stdin.resume();
|
|
62
|
+
`;
|
|
63
|
+
/**
|
|
64
|
+
* The launch of `target` under the watchdog. The host must keep the child's
|
|
65
|
+
* stdin open as a pipe for as long as the command should run: ending it
|
|
66
|
+
* stops the command. The watchdog's own grace is half the host's
|
|
67
|
+
* (`killGraceMs()`), so it escalates before the host's own SIGKILL would end
|
|
68
|
+
* the watchdog first. Null in a compiled single executable host, which
|
|
69
|
+
* cannot run it.
|
|
70
|
+
*/
|
|
71
|
+
export function hostWatchdogTarget(target, env) {
|
|
72
|
+
const node = nodeEvalExecutable();
|
|
73
|
+
if (node === null) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const spec = [target.command, target.args, target.windowsVerbatimArguments === true, Math.floor(killGraceMs() / 2), env.ELECTRON_RUN_AS_NODE ?? null];
|
|
77
|
+
return { command: node, args: ["-e", SCRIPT, JSON.stringify(spec)], env: { ...env, ELECTRON_RUN_AS_NODE: "1" } };
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=host-watchdog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-watchdog.js","sources":["../../../src/shared/executable/host-watchdog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCxB,CAAC;AASF;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAgH,EAAE,GAAgC;IACnL,MAAM,IAAI,GAAG,kBAAkB,EAAE,CAAC;IAClC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAAC,OAAO,IAAI,CAAC;IAAC,CAAC;IACnC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,wBAAwB,KAAK,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,oBAAoB,IAAI,IAAI,CAAC,CAAC;IACtJ,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,EAAE,oBAAoB,EAAE,GAAG,EAAE,EAAE,CAAC;AACnH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The executable that evaluates `-e SCRIPT` as Node: this host's own
|
|
3
|
+
* `process.execPath`, or null when the host is a compiled single executable
|
|
4
|
+
* (a Node single executable application, or a Bun standalone executable),
|
|
5
|
+
* whose `process.execPath` runs the application's own entry instead, so
|
|
6
|
+
* starting it would reenter the application. A Bun build that does not say
|
|
7
|
+
* whether it is standalone counts as one.
|
|
8
|
+
*/
|
|
9
|
+
export declare function nodeEvalExecutable(): string | null;
|
|
10
|
+
//# sourceMappingURL=node-eval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-eval.d.ts","sources":["../../../src/shared/executable/node-eval.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,GAAG,IAAI,CAIlD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { asRecord } from "../json.js";
|
|
2
|
+
/**
|
|
3
|
+
* The executable that evaluates `-e SCRIPT` as Node: this host's own
|
|
4
|
+
* `process.execPath`, or null when the host is a compiled single executable
|
|
5
|
+
* (a Node single executable application, or a Bun standalone executable),
|
|
6
|
+
* whose `process.execPath` runs the application's own entry instead, so
|
|
7
|
+
* starting it would reenter the application. A Bun build that does not say
|
|
8
|
+
* whether it is standalone counts as one.
|
|
9
|
+
*/
|
|
10
|
+
export function nodeEvalExecutable() {
|
|
11
|
+
const bun = asRecord(Reflect.get(globalThis, "Bun"));
|
|
12
|
+
if (bun !== null) {
|
|
13
|
+
return bun.isStandaloneExecutable === false ? process.execPath : null;
|
|
14
|
+
}
|
|
15
|
+
return process.getBuiltinModule("node:sea").isSea() ? null : process.execPath;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=node-eval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-eval.js","sources":["../../../src/shared/executable/node-eval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;IACrD,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAAC,OAAO,GAAG,CAAC,sBAAsB,KAAK,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC;IAAC,CAAC;IAC5F,OAAO,OAAO,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC;AAChF,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** How a Node script child ended, with the last 8 KiB of its stderr. */
|
|
2
|
+
export interface NodeScriptExit {
|
|
3
|
+
readonly code: number | null;
|
|
4
|
+
readonly signal: NodeJS.Signals | null;
|
|
5
|
+
readonly stderr: string;
|
|
6
|
+
}
|
|
7
|
+
/** A Node child running a script given on its command line, talking over Node IPC. */
|
|
8
|
+
export interface NodeScriptChild {
|
|
9
|
+
readonly pid: number | undefined;
|
|
10
|
+
/** False once the channel is gone; a message sent then is dropped, and the exit is what gets recorded. */
|
|
11
|
+
send(message: object): boolean;
|
|
12
|
+
onMessage(listener: (message: unknown) => void): void;
|
|
13
|
+
/** Settles once the child exited (or could not be spawned) and the descendants seen before a stop were ended. */
|
|
14
|
+
readonly exited: Promise<NodeScriptExit>;
|
|
15
|
+
/**
|
|
16
|
+
* Stop the child and the descendants found below it. First the process table is
|
|
17
|
+
* read and the child's descendants remembered (by parent pid, so also
|
|
18
|
+
* tools that lead process groups of their own). Then `ask` lets the child
|
|
19
|
+
* stop itself, if given; after the grace period (`killGraceMs()`), or at
|
|
20
|
+
* once without `ask`, SIGTERM to its process group, SIGKILL to the group
|
|
21
|
+
* and the remembered descendants after another grace period (POSIX);
|
|
22
|
+
* `taskkill /T /F` on its tree (Windows). Once the child exited, the
|
|
23
|
+
* remembered descendants still running are sent SIGKILL
|
|
24
|
+
* (process-shutdown.ts, `killTree`). `captureTree: false` skips the read,
|
|
25
|
+
* for a child that starts nothing. Idempotent; settles with the exit.
|
|
26
|
+
*/
|
|
27
|
+
stop(ask?: () => void, captureTree?: boolean): Promise<NodeScriptExit>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Run `script` with this host's own Node (`process.execPath -e script
|
|
31
|
+
* argument`), stdin ignored, stdout and stderr piped (stdout is drained
|
|
32
|
+
* unread, stderr kept as a tail), and a Node IPC channel with `advanced`
|
|
33
|
+
* serialization. The child is spawned detached on every platform. On POSIX
|
|
34
|
+
* it leads its own process group. On Windows Node otherwise puts it in a job
|
|
35
|
+
* object that ends it together with a dying host, before it can end its own
|
|
36
|
+
* tree; the cost is that a detached Windows child has no console, so a
|
|
37
|
+
* console program it starts without `windowsHide` may open a visible window
|
|
38
|
+
* (process-signals.ts). The script itself must end its tree when its IPC
|
|
39
|
+
* channel disconnects (the host died, SIGKILL included): nothing in the host
|
|
40
|
+
* can then. It is tracked with `trackOwnedProcess`, so a host that calls
|
|
41
|
+
* `process.exit` still ends it and its descendants. On any exit the child's
|
|
42
|
+
* process group is sent SIGKILL (POSIX), which reaches what is left of it.
|
|
43
|
+
* With `marker` (an `environment-marker.ts` entry the caller put in `env`),
|
|
44
|
+
* every process of this user still carrying it is sent SIGKILL once the
|
|
45
|
+
* child exited, for whatever reason (an asynchronous `/proc` scan, repeated
|
|
46
|
+
* until a pass finds none, at most five), and from the host's own `exit`
|
|
47
|
+
* hook (Linux only). The
|
|
48
|
+
* caller sets the environment, including `ELECTRON_RUN_AS_NODE=1` when
|
|
49
|
+
* `process.execPath` may be Electron. Throws in a compiled single executable
|
|
50
|
+
* host (node-eval.ts), which cannot run one.
|
|
51
|
+
*/
|
|
52
|
+
export declare function spawnNodeScript(script: string, argument: string, env: NodeJS.ProcessEnv, marker?: string): NodeScriptChild;
|
|
53
|
+
//# sourceMappingURL=node-script.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-script.d.ts","sources":["../../../src/shared/executable/node-script.ts"],"names":[],"mappings":"AAQA,wEAAwE;AACxE,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,sFAAsF;AACtF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,0GAA0G;IAC1G,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IAC/B,SAAS,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IACtD,iHAAiH;IACjH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;IACzC;;;;;;;;;;;OAWG;IACH,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,IAAI,EAAE,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CACxE;AAKD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,eAAe,CAoF1H"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { nodeEvalExecutable } from "./node-eval.js";
|
|
3
|
+
import { StderrTail } from "./diagnostics.js";
|
|
4
|
+
import { killMarkedAsync, trackEnvironmentMarker } from "./environment-marker.js";
|
|
5
|
+
import { trackOwnedProcess } from "./ownership.js";
|
|
6
|
+
import { processShutdown } from "./process-shutdown.js";
|
|
7
|
+
import { killGraceMs, OWN_PROCESS_GROUP, signalProcessGroup } from "./process-signals.js";
|
|
8
|
+
/** How long the exit waits for stderr to drain after the process itself ended. */
|
|
9
|
+
const STDERR_DRAIN_MS = 250;
|
|
10
|
+
/**
|
|
11
|
+
* Run `script` with this host's own Node (`process.execPath -e script
|
|
12
|
+
* argument`), stdin ignored, stdout and stderr piped (stdout is drained
|
|
13
|
+
* unread, stderr kept as a tail), and a Node IPC channel with `advanced`
|
|
14
|
+
* serialization. The child is spawned detached on every platform. On POSIX
|
|
15
|
+
* it leads its own process group. On Windows Node otherwise puts it in a job
|
|
16
|
+
* object that ends it together with a dying host, before it can end its own
|
|
17
|
+
* tree; the cost is that a detached Windows child has no console, so a
|
|
18
|
+
* console program it starts without `windowsHide` may open a visible window
|
|
19
|
+
* (process-signals.ts). The script itself must end its tree when its IPC
|
|
20
|
+
* channel disconnects (the host died, SIGKILL included): nothing in the host
|
|
21
|
+
* can then. It is tracked with `trackOwnedProcess`, so a host that calls
|
|
22
|
+
* `process.exit` still ends it and its descendants. On any exit the child's
|
|
23
|
+
* process group is sent SIGKILL (POSIX), which reaches what is left of it.
|
|
24
|
+
* With `marker` (an `environment-marker.ts` entry the caller put in `env`),
|
|
25
|
+
* every process of this user still carrying it is sent SIGKILL once the
|
|
26
|
+
* child exited, for whatever reason (an asynchronous `/proc` scan, repeated
|
|
27
|
+
* until a pass finds none, at most five), and from the host's own `exit`
|
|
28
|
+
* hook (Linux only). The
|
|
29
|
+
* caller sets the environment, including `ELECTRON_RUN_AS_NODE=1` when
|
|
30
|
+
* `process.execPath` may be Electron. Throws in a compiled single executable
|
|
31
|
+
* host (node-eval.ts), which cannot run one.
|
|
32
|
+
*/
|
|
33
|
+
export function spawnNodeScript(script, argument, env, marker) {
|
|
34
|
+
const node = nodeEvalExecutable();
|
|
35
|
+
if (node === null) {
|
|
36
|
+
throw new Error("this host is a compiled single executable (Node SEA or Bun standalone): its executable would start the application again instead of a Node script, so it cannot run a Node script child");
|
|
37
|
+
}
|
|
38
|
+
const child = spawn(node, ["-e", script, argument], {
|
|
39
|
+
env,
|
|
40
|
+
stdio: ["ignore", "pipe", "pipe", "ipc"],
|
|
41
|
+
serialization: "advanced",
|
|
42
|
+
detached: true,
|
|
43
|
+
windowsHide: true,
|
|
44
|
+
});
|
|
45
|
+
trackOwnedProcess(child);
|
|
46
|
+
const releaseMarker = marker === undefined ? null : trackEnvironmentMarker(marker);
|
|
47
|
+
const shutdown = processShutdown(child, { killTree: true });
|
|
48
|
+
const stderr = new StderrTail();
|
|
49
|
+
const stderrClosed = Promise.withResolvers();
|
|
50
|
+
child.stdout?.resume();
|
|
51
|
+
// As for line processes, `OAR_CHILD_STDERR=inherit` also forwards it to the host's stderr.
|
|
52
|
+
const inherit = process.env.OAR_CHILD_STDERR === "inherit";
|
|
53
|
+
child.stderr?.on("data", (chunk) => {
|
|
54
|
+
stderr.append(chunk);
|
|
55
|
+
if (inherit) {
|
|
56
|
+
process.stderr.write(chunk);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
child.stderr?.once("close", () => { stderrClosed.resolve(); });
|
|
60
|
+
if (child.stderr === null) {
|
|
61
|
+
stderrClosed.resolve();
|
|
62
|
+
}
|
|
63
|
+
const ended = Promise.withResolvers();
|
|
64
|
+
let backstop = undefined;
|
|
65
|
+
child.once("exit", (code, signal) => {
|
|
66
|
+
clearTimeout(backstop);
|
|
67
|
+
// What it started may still be in its group (POSIX); the group is ours to end.
|
|
68
|
+
if (OWN_PROCESS_GROUP && child.pid !== undefined) {
|
|
69
|
+
signalProcessGroup(child, "SIGKILL");
|
|
70
|
+
}
|
|
71
|
+
ended.resolve({ code, signal });
|
|
72
|
+
shutdown.nativeExited();
|
|
73
|
+
});
|
|
74
|
+
child.on("error", (error) => {
|
|
75
|
+
if (child.pid === undefined) {
|
|
76
|
+
stderr.append(error.message);
|
|
77
|
+
stderrClosed.resolve();
|
|
78
|
+
ended.resolve({ code: null, signal: null });
|
|
79
|
+
shutdown.nativeExited();
|
|
80
|
+
releaseMarker?.();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const exited = (async () => {
|
|
84
|
+
const { code, signal } = await ended.promise;
|
|
85
|
+
// And what it started that kept the marker, wherever it was reparented (Linux; read without blocking the loop).
|
|
86
|
+
if (marker !== undefined && child.pid !== undefined) {
|
|
87
|
+
await killMarkedAsync(marker);
|
|
88
|
+
}
|
|
89
|
+
releaseMarker?.();
|
|
90
|
+
await shutdown.finished;
|
|
91
|
+
const drained = Promise.withResolvers();
|
|
92
|
+
const timer = setTimeout(drained.resolve, STDERR_DRAIN_MS);
|
|
93
|
+
await Promise.race([stderrClosed.promise, drained.promise]);
|
|
94
|
+
clearTimeout(timer);
|
|
95
|
+
return { code, signal, stderr: stderr.text() };
|
|
96
|
+
})();
|
|
97
|
+
const running = () => child.pid !== undefined && child.exitCode === null && child.signalCode === null;
|
|
98
|
+
let stopping = false;
|
|
99
|
+
return {
|
|
100
|
+
pid: child.pid,
|
|
101
|
+
send(message) {
|
|
102
|
+
if (!child.connected) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return child.send(message, () => { });
|
|
106
|
+
},
|
|
107
|
+
onMessage(listener) { child.on("message", listener); },
|
|
108
|
+
exited,
|
|
109
|
+
async stop(ask, captureTree = true) {
|
|
110
|
+
if (!stopping && running()) {
|
|
111
|
+
stopping = true;
|
|
112
|
+
// Remember its descendants while it still runs: once it is gone they are no longer below it.
|
|
113
|
+
// A child known to start nothing skips the read (on Windows a process-table query of its own).
|
|
114
|
+
if (captureTree) {
|
|
115
|
+
await shutdown.prepare();
|
|
116
|
+
}
|
|
117
|
+
if (ask !== undefined && running()) {
|
|
118
|
+
shutdown.markNativeStop();
|
|
119
|
+
ask();
|
|
120
|
+
backstop = setTimeout(() => { shutdown.stop(); }, killGraceMs());
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
shutdown.stop();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const exit = await exited;
|
|
127
|
+
return exit;
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=node-script.js.map
|