@jini-ai/platform 0.1.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/LICENSE +201 -0
- package/dist/asset-cache.d.ts +80 -0
- package/dist/asset-cache.d.ts.map +1 -0
- package/dist/asset-cache.js +423 -0
- package/dist/asset-cache.js.map +1 -0
- package/dist/aws-sigv4.d.ts +51 -0
- package/dist/aws-sigv4.d.ts.map +1 -0
- package/dist/aws-sigv4.js +104 -0
- package/dist/aws-sigv4.js.map +1 -0
- package/dist/blob-storage.d.ts +89 -0
- package/dist/blob-storage.d.ts.map +1 -0
- package/dist/blob-storage.js +315 -0
- package/dist/blob-storage.js.map +1 -0
- package/dist/command.d.ts +32 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +101 -0
- package/dist/command.js.map +1 -0
- package/dist/download.d.ts +92 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +856 -0
- package/dist/download.js.map +1 -0
- package/dist/fs.d.ts +55 -0
- package/dist/fs.d.ts.map +1 -0
- package/dist/fs.js +124 -0
- package/dist/fs.js.map +1 -0
- package/dist/home-expansion.d.ts +18 -0
- package/dist/home-expansion.d.ts.map +1 -0
- package/dist/home-expansion.js +58 -0
- package/dist/home-expansion.js.map +1 -0
- package/dist/http.d.ts +14 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +39 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -0
- package/dist/process.d.ts +159 -0
- package/dist/process.d.ts.map +1 -0
- package/dist/process.js +362 -0
- package/dist/process.js.map +1 -0
- package/dist/proxy-env.d.ts +50 -0
- package/dist/proxy-env.d.ts.map +1 -0
- package/dist/proxy-env.js +373 -0
- package/dist/proxy-env.js.map +1 -0
- package/dist/resource-paths.d.ts +68 -0
- package/dist/resource-paths.d.ts.map +1 -0
- package/dist/resource-paths.js +137 -0
- package/dist/resource-paths.js.map +1 -0
- package/dist/sandbox-env.d.ts +86 -0
- package/dist/sandbox-env.d.ts.map +1 -0
- package/dist/sandbox-env.js +215 -0
- package/dist/sandbox-env.js.map +1 -0
- package/dist/shell.d.ts +46 -0
- package/dist/shell.d.ts.map +1 -0
- package/dist/shell.js +78 -0
- package/dist/shell.js.map +1 -0
- package/dist/terminal.d.ts +137 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +398 -0
- package/dist/terminal.js.map +1 -0
- package/dist/toolchain.d.ts +17 -0
- package/dist/toolchain.d.ts.map +1 -0
- package/dist/toolchain.js +251 -0
- package/dist/toolchain.js.map +1 -0
- package/package.json +47 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module process
|
|
3
|
+
*
|
|
4
|
+
* OS process lifecycle and stamp primitives: encode/decode `--flag=value`
|
|
5
|
+
* process stamps and match them against a contract, spawn background and logged
|
|
6
|
+
* child processes, probe liveness, enumerate process snapshots (POSIX `ps` /
|
|
7
|
+
* Windows `Get-CimInstance`), walk a process tree, and stop a set of PIDs with
|
|
8
|
+
* SIGTERM-then-SIGKILL escalation.
|
|
9
|
+
*
|
|
10
|
+
* Depends on the `command` module for invocation construction; keeps a private
|
|
11
|
+
* `errorCode` copy so it owns no cross-module runtime surface.
|
|
12
|
+
*/
|
|
13
|
+
import { type ChildProcess } from "node:child_process";
|
|
14
|
+
import { type CommandInvocationRequest } from "./command.js";
|
|
15
|
+
export type ProcessStampShape = object;
|
|
16
|
+
export type ProcessStampField<TStamp extends ProcessStampShape> = Extract<keyof TStamp, string>;
|
|
17
|
+
export type ProcessStampContract<TStamp extends ProcessStampShape, TCriteria extends Partial<TStamp> = Partial<TStamp>> = {
|
|
18
|
+
normalizeStamp(input: unknown): TStamp;
|
|
19
|
+
normalizeStampCriteria(input?: unknown): TCriteria;
|
|
20
|
+
stampFields: readonly ProcessStampField<TStamp>[];
|
|
21
|
+
stampFlags: {
|
|
22
|
+
readonly [K in ProcessStampField<TStamp>]: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export type SpawnProcessRequest = CommandInvocationRequest & {
|
|
26
|
+
cwd?: string;
|
|
27
|
+
detached?: boolean;
|
|
28
|
+
logFd?: number | null;
|
|
29
|
+
};
|
|
30
|
+
export type ProcessSnapshot = {
|
|
31
|
+
command: string;
|
|
32
|
+
pid: number;
|
|
33
|
+
ppid: number;
|
|
34
|
+
};
|
|
35
|
+
export type StampedProcessMatchCriteria<TStamp extends ProcessStampShape> = Partial<TStamp>;
|
|
36
|
+
export type StopProcessesResult = {
|
|
37
|
+
alreadyStopped: boolean;
|
|
38
|
+
forcedPids: number[];
|
|
39
|
+
matchedPids: number[];
|
|
40
|
+
remainingPids: number[];
|
|
41
|
+
stoppedPids: number[];
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Serialize a process stamp into `--flag=value` CLI arguments per the contract.
|
|
45
|
+
* Every stamp field must normalize to a string or an error is thrown.
|
|
46
|
+
*
|
|
47
|
+
* @param stamp - The stamp object to encode.
|
|
48
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
49
|
+
* @returns The `--flag=value` argument strings, one per stamp field.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createProcessStampArgs<TStamp extends ProcessStampShape>(stamp: TStamp, contract: ProcessStampContract<TStamp>): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Read the value of a CLI flag from an argument list, supporting both the
|
|
54
|
+
* `--flag value` and inline `--flag=value` forms.
|
|
55
|
+
*
|
|
56
|
+
* @param args - The argument list to search.
|
|
57
|
+
* @param flagName - The flag name (including any leading dashes).
|
|
58
|
+
* @returns The flag's value, or `null` when the flag is absent.
|
|
59
|
+
*/
|
|
60
|
+
export declare function readFlagValue(args: readonly string[], flagName: string): string | null;
|
|
61
|
+
/**
|
|
62
|
+
* Decode a process stamp from a raw argument list per the contract, returning
|
|
63
|
+
* `null` when normalization fails (e.g. a required field is missing).
|
|
64
|
+
*
|
|
65
|
+
* @param args - The process argument list to read stamp flags from.
|
|
66
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
67
|
+
* @returns The decoded stamp, or `null` when it cannot be normalized.
|
|
68
|
+
*/
|
|
69
|
+
export declare function readProcessStamp<TStamp extends ProcessStampShape>(args: readonly string[], contract: ProcessStampContract<TStamp>): TStamp | null;
|
|
70
|
+
/**
|
|
71
|
+
* Decode a process stamp from a full command-line string by tokenizing it first.
|
|
72
|
+
*
|
|
73
|
+
* @param command - The full command line to read stamp flags from.
|
|
74
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
75
|
+
* @returns The decoded stamp, or `null` when it cannot be normalized.
|
|
76
|
+
*/
|
|
77
|
+
export declare function readProcessStampFromCommand<TStamp extends ProcessStampShape>(command: string, contract: ProcessStampContract<TStamp>): TStamp | null;
|
|
78
|
+
/**
|
|
79
|
+
* Test whether a stamp matches criteria: every criterion field that is set must
|
|
80
|
+
* equal the corresponding normalized stamp field; unset criteria fields match
|
|
81
|
+
* anything.
|
|
82
|
+
*
|
|
83
|
+
* @param stamp - The stamp to test.
|
|
84
|
+
* @param criteria - The partial criteria to match against (undefined matches all).
|
|
85
|
+
* @param contract - The stamp contract providing field list and normalization.
|
|
86
|
+
* @returns `true` when every specified criterion matches the stamp.
|
|
87
|
+
*/
|
|
88
|
+
export declare function matchesProcessStamp<TStamp extends ProcessStampShape, TCriteria extends Partial<TStamp> = Partial<TStamp>>(stamp: TStamp, criteria: TCriteria | undefined, contract: ProcessStampContract<TStamp, TCriteria>): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Test whether a process snapshot's command line carries a stamp matching the
|
|
91
|
+
* criteria. Combines stamp decoding from the command with `matchesProcessStamp`.
|
|
92
|
+
*
|
|
93
|
+
* @param processInfo - A snapshot exposing at least the `command` string.
|
|
94
|
+
* @param criteria - The partial criteria to match against (undefined matches all).
|
|
95
|
+
* @param contract - The stamp contract providing field list and normalization.
|
|
96
|
+
* @returns `true` when the command carries a decodable stamp that matches.
|
|
97
|
+
*/
|
|
98
|
+
export declare function matchesStampedProcess<TStamp extends ProcessStampShape, TCriteria extends Partial<TStamp> = Partial<TStamp>>(processInfo: Pick<ProcessSnapshot, "command">, criteria: TCriteria | undefined, contract: ProcessStampContract<TStamp, TCriteria>): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Spawn a detached background process, wait for it to actually start, then
|
|
101
|
+
* `unref` it so the parent can exit independently.
|
|
102
|
+
*
|
|
103
|
+
* @param request - The command/args/env plus cwd, detached, and log-fd options.
|
|
104
|
+
* @returns The spawned child's `{ pid }`.
|
|
105
|
+
* @throws If the child fails to spawn or reports no pid.
|
|
106
|
+
*/
|
|
107
|
+
export declare function spawnBackgroundProcess(request: SpawnProcessRequest): Promise<{
|
|
108
|
+
pid: number;
|
|
109
|
+
}>;
|
|
110
|
+
/**
|
|
111
|
+
* Spawn a (by default non-detached) child process with stdout/stderr routed to
|
|
112
|
+
* an optional log fd, waiting for it to start before returning the handle.
|
|
113
|
+
*
|
|
114
|
+
* @param request - The command/args/env plus cwd, detached, and log-fd options.
|
|
115
|
+
* @returns The live `ChildProcess` handle.
|
|
116
|
+
* @throws If the child fails to spawn or reports no pid.
|
|
117
|
+
*/
|
|
118
|
+
export declare function spawnLoggedProcess(request: SpawnProcessRequest): Promise<ChildProcess>;
|
|
119
|
+
/**
|
|
120
|
+
* Probe whether a process is alive via a signal-0 `process.kill`. Treats
|
|
121
|
+
* `ESRCH` as dead and any other error (e.g. `EPERM`) as alive.
|
|
122
|
+
*
|
|
123
|
+
* @param pid - The PID to probe (non-number values are treated as dead).
|
|
124
|
+
* @returns `true` when the process appears to exist.
|
|
125
|
+
*/
|
|
126
|
+
export declare function isProcessAlive(pid: number | null | undefined): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Poll until a process exits or the timeout elapses.
|
|
129
|
+
*
|
|
130
|
+
* @param pid - The PID to wait on.
|
|
131
|
+
* @param timeoutMs - Maximum time to wait, in milliseconds (default 5000).
|
|
132
|
+
* @returns `true` if the process is gone by the deadline, otherwise `false`.
|
|
133
|
+
*/
|
|
134
|
+
export declare function waitForProcessExit(pid: number | null | undefined, timeoutMs?: number): Promise<boolean>;
|
|
135
|
+
/**
|
|
136
|
+
* Enumerate all running processes as `{ pid, ppid, command }` snapshots, using
|
|
137
|
+
* the platform-appropriate backend. Returns an empty list on any failure.
|
|
138
|
+
*
|
|
139
|
+
* @returns The current process snapshots (empty on error).
|
|
140
|
+
*/
|
|
141
|
+
export declare function listProcessSnapshots(): Promise<ProcessSnapshot[]>;
|
|
142
|
+
/**
|
|
143
|
+
* Collect the transitive set of descendant PIDs (including the roots) from a
|
|
144
|
+
* process snapshot list, returned sorted descending so children precede parents.
|
|
145
|
+
*
|
|
146
|
+
* @param processes - The full process snapshot list to walk.
|
|
147
|
+
* @param rootPids - The root PIDs whose subtrees to collect (non-numbers ignored).
|
|
148
|
+
* @returns The unique PIDs of the roots and all their descendants, descending.
|
|
149
|
+
*/
|
|
150
|
+
export declare function collectProcessTreePids(processes: ProcessSnapshot[], rootPids: Array<number | null | undefined>): number[];
|
|
151
|
+
/**
|
|
152
|
+
* Stop a set of PIDs with escalation: SIGTERM, wait, then SIGKILL any
|
|
153
|
+
* survivors. Excludes the current process and de-duplicates the input.
|
|
154
|
+
*
|
|
155
|
+
* @param pids - The PIDs to stop (non-numbers and the current PID are ignored).
|
|
156
|
+
* @returns A result describing matched, stopped, force-killed, and remaining PIDs.
|
|
157
|
+
*/
|
|
158
|
+
export declare function stopProcesses(pids: Array<number | null | undefined>): Promise<StopProcessesResult>;
|
|
159
|
+
//# sourceMappingURL=process.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAmB,KAAK,YAAY,EAAqB,MAAM,oBAAoB,CAAC;AAG3F,OAAO,EAA2B,K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;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAcnG;AAED;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC,CAa5F;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAStE;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,SAAS,SAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAO3G;AAmDD;;;;;GAKG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC,CAQvE;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,eAAe,EAAE,EAC5B,QAAQ,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GACzC,MAAM,EAAE,CAkBV;AAwBD;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAgBxG"}
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module process
|
|
3
|
+
*
|
|
4
|
+
* OS process lifecycle and stamp primitives: encode/decode `--flag=value`
|
|
5
|
+
* process stamps and match them against a contract, spawn background and logged
|
|
6
|
+
* child processes, probe liveness, enumerate process snapshots (POSIX `ps` /
|
|
7
|
+
* Windows `Get-CimInstance`), walk a process tree, and stop a set of PIDs with
|
|
8
|
+
* SIGTERM-then-SIGKILL escalation.
|
|
9
|
+
*
|
|
10
|
+
* Depends on the `command` module for invocation construction; keeps a private
|
|
11
|
+
* `errorCode` copy so it owns no cross-module runtime surface.
|
|
12
|
+
*/
|
|
13
|
+
import { execFile, spawn } from "node:child_process";
|
|
14
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
15
|
+
import { createCommandInvocation } from "./command.js";
|
|
16
|
+
/** @internal Extract a Node `error.code` as a string, or `null` when the value carries no code. */
|
|
17
|
+
function errorCode(error) {
|
|
18
|
+
if (typeof error !== "object" || error == null || !("code" in error))
|
|
19
|
+
return null;
|
|
20
|
+
const code = error.code;
|
|
21
|
+
return code == null ? null : String(code);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Serialize a process stamp into `--flag=value` CLI arguments per the contract.
|
|
25
|
+
* Every stamp field must normalize to a string or an error is thrown.
|
|
26
|
+
*
|
|
27
|
+
* @param stamp - The stamp object to encode.
|
|
28
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
29
|
+
* @returns The `--flag=value` argument strings, one per stamp field.
|
|
30
|
+
*/
|
|
31
|
+
export function createProcessStampArgs(stamp, contract) {
|
|
32
|
+
const normalized = contract.normalizeStamp(stamp);
|
|
33
|
+
return contract.stampFields.map((field) => {
|
|
34
|
+
const value = normalized[field];
|
|
35
|
+
if (typeof value !== "string") {
|
|
36
|
+
throw new Error(`process stamp field ${field} must normalize to a string`);
|
|
37
|
+
}
|
|
38
|
+
return `${contract.stampFlags[field]}=${value}`;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/** @internal Split a command line string into whitespace-separated argument tokens. */
|
|
42
|
+
function commandArgs(command) {
|
|
43
|
+
return command.trim().split(/\s+/).filter((part) => part.length > 0);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Read the value of a CLI flag from an argument list, supporting both the
|
|
47
|
+
* `--flag value` and inline `--flag=value` forms.
|
|
48
|
+
*
|
|
49
|
+
* @param args - The argument list to search.
|
|
50
|
+
* @param flagName - The flag name (including any leading dashes).
|
|
51
|
+
* @returns The flag's value, or `null` when the flag is absent.
|
|
52
|
+
*/
|
|
53
|
+
export function readFlagValue(args, flagName) {
|
|
54
|
+
const inlinePrefix = `${flagName}=`;
|
|
55
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
56
|
+
const argument = args[index];
|
|
57
|
+
if (argument === flagName)
|
|
58
|
+
return args[index + 1] ?? null;
|
|
59
|
+
if (typeof argument === "string" && argument.startsWith(inlinePrefix)) {
|
|
60
|
+
return argument.slice(inlinePrefix.length);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Decode a process stamp from a raw argument list per the contract, returning
|
|
67
|
+
* `null` when normalization fails (e.g. a required field is missing).
|
|
68
|
+
*
|
|
69
|
+
* @param args - The process argument list to read stamp flags from.
|
|
70
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
71
|
+
* @returns The decoded stamp, or `null` when it cannot be normalized.
|
|
72
|
+
*/
|
|
73
|
+
export function readProcessStamp(args, contract) {
|
|
74
|
+
try {
|
|
75
|
+
const input = Object.fromEntries(contract.stampFields.map((field) => [field, readFlagValue(args, contract.stampFlags[field])]));
|
|
76
|
+
return contract.normalizeStamp(input);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Decode a process stamp from a full command-line string by tokenizing it first.
|
|
84
|
+
*
|
|
85
|
+
* @param command - The full command line to read stamp flags from.
|
|
86
|
+
* @param contract - The stamp contract providing field list, flags, and normalization.
|
|
87
|
+
* @returns The decoded stamp, or `null` when it cannot be normalized.
|
|
88
|
+
*/
|
|
89
|
+
export function readProcessStampFromCommand(command, contract) {
|
|
90
|
+
return readProcessStamp(commandArgs(command), contract);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Test whether a stamp matches criteria: every criterion field that is set must
|
|
94
|
+
* equal the corresponding normalized stamp field; unset criteria fields match
|
|
95
|
+
* anything.
|
|
96
|
+
*
|
|
97
|
+
* @param stamp - The stamp to test.
|
|
98
|
+
* @param criteria - The partial criteria to match against (undefined matches all).
|
|
99
|
+
* @param contract - The stamp contract providing field list and normalization.
|
|
100
|
+
* @returns `true` when every specified criterion matches the stamp.
|
|
101
|
+
*/
|
|
102
|
+
export function matchesProcessStamp(stamp, criteria, contract) {
|
|
103
|
+
const normalizedStamp = contract.normalizeStamp(stamp);
|
|
104
|
+
const normalizedCriteria = contract.normalizeStampCriteria(criteria ?? {});
|
|
105
|
+
return contract.stampFields.every((field) => {
|
|
106
|
+
const expected = normalizedCriteria[field];
|
|
107
|
+
return expected == null || normalizedStamp[field] === expected;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Test whether a process snapshot's command line carries a stamp matching the
|
|
112
|
+
* criteria. Combines stamp decoding from the command with `matchesProcessStamp`.
|
|
113
|
+
*
|
|
114
|
+
* @param processInfo - A snapshot exposing at least the `command` string.
|
|
115
|
+
* @param criteria - The partial criteria to match against (undefined matches all).
|
|
116
|
+
* @param contract - The stamp contract providing field list and normalization.
|
|
117
|
+
* @returns `true` when the command carries a decodable stamp that matches.
|
|
118
|
+
*/
|
|
119
|
+
export function matchesStampedProcess(processInfo, criteria, contract) {
|
|
120
|
+
const stamp = readProcessStampFromCommand(processInfo.command, contract);
|
|
121
|
+
return stamp != null && matchesProcessStamp(stamp, criteria, contract);
|
|
122
|
+
}
|
|
123
|
+
/** @internal Build the stdio triple for a spawned process, routing stdout/stderr to a log fd when provided. */
|
|
124
|
+
function createLoggedStdio(logFd) {
|
|
125
|
+
return logFd == null ? ["ignore", "ignore", "ignore"] : ["ignore", logFd, logFd];
|
|
126
|
+
}
|
|
127
|
+
/** @internal Resolve once the child emits `spawn`, or reject on the child's `error` event. */
|
|
128
|
+
async function waitForChildSpawn(child) {
|
|
129
|
+
await new Promise((resolveSpawn, rejectSpawn) => {
|
|
130
|
+
child.once("error", rejectSpawn);
|
|
131
|
+
child.once("spawn", resolveSpawn);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Spawn a detached background process, wait for it to actually start, then
|
|
136
|
+
* `unref` it so the parent can exit independently.
|
|
137
|
+
*
|
|
138
|
+
* @param request - The command/args/env plus cwd, detached, and log-fd options.
|
|
139
|
+
* @returns The spawned child's `{ pid }`.
|
|
140
|
+
* @throws If the child fails to spawn or reports no pid.
|
|
141
|
+
*/
|
|
142
|
+
export async function spawnBackgroundProcess(request) {
|
|
143
|
+
const invocation = createCommandInvocation(request);
|
|
144
|
+
const child = spawn(invocation.command, invocation.args, {
|
|
145
|
+
cwd: request.cwd,
|
|
146
|
+
detached: request.detached ?? true,
|
|
147
|
+
env: request.env,
|
|
148
|
+
stdio: createLoggedStdio(request.logFd),
|
|
149
|
+
windowsHide: process.platform === "win32",
|
|
150
|
+
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
|
|
151
|
+
});
|
|
152
|
+
await waitForChildSpawn(child);
|
|
153
|
+
if (child.pid == null)
|
|
154
|
+
throw new Error(`failed to spawn background process: ${invocation.command}`);
|
|
155
|
+
child.unref();
|
|
156
|
+
return { pid: child.pid };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Spawn a (by default non-detached) child process with stdout/stderr routed to
|
|
160
|
+
* an optional log fd, waiting for it to start before returning the handle.
|
|
161
|
+
*
|
|
162
|
+
* @param request - The command/args/env plus cwd, detached, and log-fd options.
|
|
163
|
+
* @returns The live `ChildProcess` handle.
|
|
164
|
+
* @throws If the child fails to spawn or reports no pid.
|
|
165
|
+
*/
|
|
166
|
+
export async function spawnLoggedProcess(request) {
|
|
167
|
+
const invocation = createCommandInvocation(request);
|
|
168
|
+
const child = spawn(invocation.command, invocation.args, {
|
|
169
|
+
cwd: request.cwd,
|
|
170
|
+
detached: request.detached ?? false,
|
|
171
|
+
env: request.env,
|
|
172
|
+
stdio: createLoggedStdio(request.logFd),
|
|
173
|
+
windowsHide: process.platform === "win32",
|
|
174
|
+
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
|
|
175
|
+
});
|
|
176
|
+
await waitForChildSpawn(child);
|
|
177
|
+
if (child.pid == null)
|
|
178
|
+
throw new Error(`failed to spawn process: ${invocation.command}`);
|
|
179
|
+
return child;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Probe whether a process is alive via a signal-0 `process.kill`. Treats
|
|
183
|
+
* `ESRCH` as dead and any other error (e.g. `EPERM`) as alive.
|
|
184
|
+
*
|
|
185
|
+
* @param pid - The PID to probe (non-number values are treated as dead).
|
|
186
|
+
* @returns `true` when the process appears to exist.
|
|
187
|
+
*/
|
|
188
|
+
export function isProcessAlive(pid) {
|
|
189
|
+
if (typeof pid !== "number")
|
|
190
|
+
return false;
|
|
191
|
+
try {
|
|
192
|
+
process.kill(pid, 0);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (errorCode(error) === "ESRCH")
|
|
197
|
+
return false;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Poll until a process exits or the timeout elapses.
|
|
203
|
+
*
|
|
204
|
+
* @param pid - The PID to wait on.
|
|
205
|
+
* @param timeoutMs - Maximum time to wait, in milliseconds (default 5000).
|
|
206
|
+
* @returns `true` if the process is gone by the deadline, otherwise `false`.
|
|
207
|
+
*/
|
|
208
|
+
export async function waitForProcessExit(pid, timeoutMs = 5000) {
|
|
209
|
+
const startedAt = Date.now();
|
|
210
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
211
|
+
if (!isProcessAlive(pid))
|
|
212
|
+
return true;
|
|
213
|
+
await sleep(100);
|
|
214
|
+
}
|
|
215
|
+
return !isProcessAlive(pid);
|
|
216
|
+
}
|
|
217
|
+
/** @internal Parse `ps -axo pid=,ppid=,command=` output into process snapshots. */
|
|
218
|
+
function parsePsOutput(stdout) {
|
|
219
|
+
return stdout
|
|
220
|
+
.split(/\r?\n/)
|
|
221
|
+
.map((line) => {
|
|
222
|
+
const match = line.match(/^\s*(\d+)\s+(\d+)\s+(.+)$/);
|
|
223
|
+
if (!match)
|
|
224
|
+
return null;
|
|
225
|
+
return { pid: Number(match[1]), ppid: Number(match[2]), command: match[3] };
|
|
226
|
+
})
|
|
227
|
+
.filter((snapshot) => snapshot != null);
|
|
228
|
+
}
|
|
229
|
+
/** @internal Enumerate process snapshots on POSIX via `ps`. */
|
|
230
|
+
async function listPosixProcessSnapshots() {
|
|
231
|
+
const stdout = await new Promise((resolveList, rejectList) => {
|
|
232
|
+
execFile("ps", ["-axo", "pid=,ppid=,command="], { encoding: "utf8", maxBuffer: 8 * 1024 * 1024 }, (error, out) => {
|
|
233
|
+
if (error)
|
|
234
|
+
rejectList(error);
|
|
235
|
+
else
|
|
236
|
+
resolveList(out);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
return parsePsOutput(stdout);
|
|
240
|
+
}
|
|
241
|
+
/** @internal Enumerate process snapshots on Windows via `Get-CimInstance Win32_Process` JSON. */
|
|
242
|
+
async function listWindowsProcessSnapshots() {
|
|
243
|
+
const command = [
|
|
244
|
+
"$ErrorActionPreference = 'Stop'",
|
|
245
|
+
"Get-CimInstance Win32_Process | Select-Object ProcessId, ParentProcessId, CommandLine | ConvertTo-Json -Compress",
|
|
246
|
+
].join("; ");
|
|
247
|
+
const stdout = await new Promise((resolveList, rejectList) => {
|
|
248
|
+
execFile("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", command], { encoding: "utf8", maxBuffer: 8 * 1024 * 1024 }, (error, out) => {
|
|
249
|
+
if (error)
|
|
250
|
+
rejectList(error);
|
|
251
|
+
else
|
|
252
|
+
resolveList(out);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
const payload = stdout.trim();
|
|
256
|
+
if (!payload)
|
|
257
|
+
return [];
|
|
258
|
+
const records = JSON.parse(payload);
|
|
259
|
+
return (Array.isArray(records) ? records : [records])
|
|
260
|
+
.map((record) => {
|
|
261
|
+
const pid = Number(record.ProcessId);
|
|
262
|
+
const ppid = Number(record.ParentProcessId);
|
|
263
|
+
const commandLine = record.CommandLine?.trim();
|
|
264
|
+
if (!commandLine || Number.isNaN(pid) || Number.isNaN(ppid))
|
|
265
|
+
return null;
|
|
266
|
+
return { command: commandLine, pid, ppid };
|
|
267
|
+
})
|
|
268
|
+
.filter((snapshot) => snapshot != null);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Enumerate all running processes as `{ pid, ppid, command }` snapshots, using
|
|
272
|
+
* the platform-appropriate backend. Returns an empty list on any failure.
|
|
273
|
+
*
|
|
274
|
+
* @returns The current process snapshots (empty on error).
|
|
275
|
+
*/
|
|
276
|
+
export async function listProcessSnapshots() {
|
|
277
|
+
try {
|
|
278
|
+
return process.platform === "win32"
|
|
279
|
+
? await listWindowsProcessSnapshots()
|
|
280
|
+
: await listPosixProcessSnapshots();
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Collect the transitive set of descendant PIDs (including the roots) from a
|
|
288
|
+
* process snapshot list, returned sorted descending so children precede parents.
|
|
289
|
+
*
|
|
290
|
+
* @param processes - The full process snapshot list to walk.
|
|
291
|
+
* @param rootPids - The root PIDs whose subtrees to collect (non-numbers ignored).
|
|
292
|
+
* @returns The unique PIDs of the roots and all their descendants, descending.
|
|
293
|
+
*/
|
|
294
|
+
export function collectProcessTreePids(processes, rootPids) {
|
|
295
|
+
const queue = [...new Set(rootPids.filter((pid) => typeof pid === "number"))];
|
|
296
|
+
const visited = new Set();
|
|
297
|
+
const childrenByParent = new Map();
|
|
298
|
+
for (const processInfo of processes) {
|
|
299
|
+
const children = childrenByParent.get(processInfo.ppid) ?? [];
|
|
300
|
+
children.push(processInfo.pid);
|
|
301
|
+
childrenByParent.set(processInfo.ppid, children);
|
|
302
|
+
}
|
|
303
|
+
while (queue.length > 0) {
|
|
304
|
+
const pid = queue.shift();
|
|
305
|
+
if (pid == null || visited.has(pid))
|
|
306
|
+
continue;
|
|
307
|
+
visited.add(pid);
|
|
308
|
+
for (const childPid of childrenByParent.get(pid) ?? []) {
|
|
309
|
+
if (!visited.has(childPid))
|
|
310
|
+
queue.push(childPid);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return [...visited].sort((left, right) => right - left);
|
|
314
|
+
}
|
|
315
|
+
/** @internal Send a signal to each PID, ignoring `ESRCH` (already-dead) but rethrowing other errors. */
|
|
316
|
+
function signalProcesses(pids, signal) {
|
|
317
|
+
for (const pid of pids) {
|
|
318
|
+
try {
|
|
319
|
+
process.kill(pid, signal);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
if (errorCode(error) !== "ESRCH")
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** @internal Poll until all PIDs exit or the timeout elapses; returns the PIDs still alive. */
|
|
328
|
+
async function waitForProcessesToExit(pids, timeoutMs = 5000) {
|
|
329
|
+
const startedAt = Date.now();
|
|
330
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
331
|
+
const remaining = pids.filter(isProcessAlive);
|
|
332
|
+
if (remaining.length === 0)
|
|
333
|
+
return [];
|
|
334
|
+
await sleep(100);
|
|
335
|
+
}
|
|
336
|
+
return pids.filter(isProcessAlive);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Stop a set of PIDs with escalation: SIGTERM, wait, then SIGKILL any
|
|
340
|
+
* survivors. Excludes the current process and de-duplicates the input.
|
|
341
|
+
*
|
|
342
|
+
* @param pids - The PIDs to stop (non-numbers and the current PID are ignored).
|
|
343
|
+
* @returns A result describing matched, stopped, force-killed, and remaining PIDs.
|
|
344
|
+
*/
|
|
345
|
+
export async function stopProcesses(pids) {
|
|
346
|
+
const uniquePids = [...new Set(pids)]
|
|
347
|
+
.filter((pid) => typeof pid === "number" && pid !== process.pid)
|
|
348
|
+
.sort((left, right) => right - left);
|
|
349
|
+
if (uniquePids.length === 0) {
|
|
350
|
+
return { alreadyStopped: true, forcedPids: [], matchedPids: [], remainingPids: [], stoppedPids: [] };
|
|
351
|
+
}
|
|
352
|
+
signalProcesses(uniquePids, "SIGTERM");
|
|
353
|
+
const remainingAfterTerm = await waitForProcessesToExit(uniquePids);
|
|
354
|
+
if (remainingAfterTerm.length === 0) {
|
|
355
|
+
return { alreadyStopped: false, forcedPids: [], matchedPids: uniquePids, remainingPids: [], stoppedPids: uniquePids };
|
|
356
|
+
}
|
|
357
|
+
signalProcesses(remainingAfterTerm, "SIGKILL");
|
|
358
|
+
const remainingAfterKill = await waitForProcessesToExit(remainingAfterTerm);
|
|
359
|
+
const stoppedPids = uniquePids.filter((pid) => !remainingAfterKill.includes(pid));
|
|
360
|
+
return { alreadyStopped: false, forcedPids: remainingAfterTerm, matchedPids: uniquePids, remainingPids: remainingAfterKill, stoppedPids };
|
|
361
|
+
}
|
|
362
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAwC,MAAM,oBAAoB,CAAC;AAC3F,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,uBAAuB,EAAiC,MAAM,cAAc,CAAC;AA4CtF,mGAAmG;AACnG,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAClF,MAAM,IAAI,GAAI,KAA4B,CAAC,IAAI,CAAC;IAChD,OAAO,IAAI,I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W,EAAE,UAAU,EAAE,aAAa,EAAE,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC;IACxH,CAAC;IACD,eAAe,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAAC;IAC/C,MAAM,kBAAkB,GAAG,MAAM,sBAAsB,CAAC,kBAAkB,CAAC,CAAC;IAC5E,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAClF,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,WAAW,EAAE,UAAU,EAAE,aAAa,EAAE,kBAAkB,EAAE,WAAW,EAAE,CAAC;AAC5I,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type SystemProxyCommandRunner = (command: string, args: string[]) => string;
|
|
2
|
+
export type ResolveSystemProxyEnvOptions = {
|
|
3
|
+
platform?: NodeJS.Platform;
|
|
4
|
+
runCommand?: SystemProxyCommandRunner;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Merge multiple env sources into one proxy-consistent environment. Proxy
|
|
8
|
+
* variables are collapsed to their canonical spelling (preferring lowercase
|
|
9
|
+
* inputs), non-proxy keys pass through, and `NODE_USE_ENV_PROXY=1` is set when
|
|
10
|
+
* a proxy endpoint is present but the flag is not.
|
|
11
|
+
*
|
|
12
|
+
* @param platform - Target platform, controlling lowercase-alias emission.
|
|
13
|
+
* @param sources - Env sources merged left-to-right (later sources win).
|
|
14
|
+
* @returns A new merged environment with normalized proxy variables.
|
|
15
|
+
*/
|
|
16
|
+
export declare function mergeProxyAwareEnv(platform: NodeJS.Platform, ...sources: Array<NodeJS.ProcessEnv | Record<string, string | undefined>>): NodeJS.ProcessEnv;
|
|
17
|
+
/**
|
|
18
|
+
* Parse macOS `scutil --proxy` output into a normalized proxy environment.
|
|
19
|
+
* Honors the HTTP/HTTPS/SOCKS enable flags, exception list, and the
|
|
20
|
+
* "exclude simple hostnames" toggle.
|
|
21
|
+
*
|
|
22
|
+
* @param stdout - Raw `scutil --proxy` output.
|
|
23
|
+
* @param platform - Target platform for env normalization (defaults to `"darwin"`).
|
|
24
|
+
* @returns A normalized proxy environment (empty when no proxy is enabled).
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseMacosScutilProxyOutput(stdout: string, platform?: NodeJS.Platform): NodeJS.ProcessEnv;
|
|
27
|
+
/**
|
|
28
|
+
* Parse Windows Internet Settings proxy registry output into a normalized proxy
|
|
29
|
+
* environment. Handles both the per-protocol (`http=…;https=…`) and shared
|
|
30
|
+
* single-endpoint `ProxyServer` forms, and the `ProxyOverride` bypass list.
|
|
31
|
+
*
|
|
32
|
+
* @param input - Raw `reg query` output for `ProxyEnable`, and optionally `ProxyServer` / `ProxyOverride`.
|
|
33
|
+
* @param platform - Target platform for env normalization (defaults to `"win32"`).
|
|
34
|
+
* @returns A normalized proxy environment (empty when proxying is disabled or unset).
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseWindowsInternetSettingsProxyOutput(input: {
|
|
37
|
+
proxyEnable: string;
|
|
38
|
+
proxyOverride?: string;
|
|
39
|
+
proxyServer?: string;
|
|
40
|
+
}, platform?: NodeJS.Platform): NodeJS.ProcessEnv;
|
|
41
|
+
/**
|
|
42
|
+
* Resolve the OS-level proxy configuration into a normalized proxy environment.
|
|
43
|
+
* Shells out to `scutil` on macOS and `reg query` on Windows (swallowing per-
|
|
44
|
+
* command failures), and returns an empty env on other platforms or on error.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Optional platform override and command runner (for testing).
|
|
47
|
+
* @returns A normalized proxy environment, or `{}` when no proxy is configured.
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveSystemProxyEnv(options?: ResolveSystemProxyEnvOptions): NodeJS.ProcessEnv;
|
|
50
|
+
//# sourceMappingURL=proxy-env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-env.d.ts","sourceRoot":"","sources":["../src/proxy-env.ts"],"names":[],"mappings":"AAcA,MAAM,MAAM,wBAAwB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,MAAM,CAAC;AAEnF,MAAM,MAAM,4BAA4B,GAAG;IACzC,QAAQ,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC;IAC3B,UAAU,CAAC,EAAE,wBAAwB,CAAC;CACvC,CAAC;AAiDF;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EACzB,GAAG,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GACxE,MAAM,CAAC,UAAU,CA4BnB;AAyID;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,MAAM,CAAC,QAAmB,GACnC,MAAM,CAAC,UAAU,CAgDnB;AAQD;;;;;;;;GAQG;AACH,wBAAgB,uCAAuC,CACrD,KAAK,EAAE;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,EAC5E,QAAQ,GAAE,MAAM,CAAC,QAAkB,GAClC,MAAM,CAAC,UAAU,CAkCnB;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,GAAE,4BAAiC,GAAG,MAAM,CAAC,UAAU,CAoDnG"}
|