@yaks/process 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +249 -2
- package/_dist/clock.d.ts +5 -0
- package/_dist/clock.d.ts.map +1 -0
- package/_dist/command.d.ts +43 -0
- package/_dist/command.d.ts.map +1 -0
- package/_dist/comp.d.ts +49 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/env.d.ts +8 -0
- package/_dist/env.d.ts.map +1 -0
- package/_dist/graph.d.ts +3 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/http.d.ts +11 -0
- package/_dist/http.d.ts.map +1 -0
- package/_dist/isolate.d.ts +11 -0
- package/_dist/isolate.d.ts.map +1 -0
- package/_dist/mod.d.ts +49 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/modules.d.ts +31 -0
- package/_dist/modules.d.ts.map +1 -0
- package/_dist/platform.d.ts +7 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/plugin.d.ts +15 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/run.d.ts +219 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/self.d.ts +32 -0
- package/_dist/self.d.ts.map +1 -0
- package/_dist/socket.d.ts +11 -0
- package/_dist/socket.d.ts.map +1 -0
- package/_dist/store.d.ts +22 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/system.d.ts +64 -0
- package/_dist/system.d.ts.map +1 -0
- package/_dist/trash.d.ts +6 -0
- package/_dist/trash.d.ts.map +1 -0
- package/_dist/views.d.ts +4 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +8 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/wind.d.ts +35 -0
- package/_dist/wind.d.ts.map +1 -0
- package/clock.js +11 -0
- package/clock.js.map +1 -0
- package/clock.ts +11 -0
- package/command.js +172 -0
- package/command.js.map +1 -0
- package/command.ts +215 -0
- package/comp.js +54 -0
- package/comp.js.map +1 -0
- package/comp.ts +123 -0
- package/deno.json +1 -0
- package/detached.js +60 -0
- package/detached.js.map +1 -0
- package/detached.ts +92 -0
- package/detached_worker.ts +22 -0
- package/env.js +13 -0
- package/env.js.map +1 -0
- package/env.ts +24 -0
- package/graph.js +7 -0
- package/graph.js.map +1 -0
- package/graph.ts +8 -0
- package/http.js +158 -0
- package/http.js.map +1 -0
- package/http.ts +184 -0
- package/isolate.js +41 -0
- package/isolate.js.map +1 -0
- package/isolate.ts +52 -0
- package/mod.js +49 -0
- package/mod.js.map +1 -0
- package/mod.ts +50 -0
- package/modules.js +41 -0
- package/modules.js.map +1 -0
- package/modules.ts +69 -0
- package/package.json +81 -4
- package/platform.js +9 -0
- package/platform.js.map +1 -0
- package/platform.ts +9 -0
- package/plugin.js +27 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +28 -0
- package/run.js +899 -0
- package/run.js.map +1 -0
- package/run.ts +1139 -0
- package/self.js +93 -0
- package/self.js.map +1 -0
- package/self.ts +144 -0
- package/socket.js +38 -0
- package/socket.js.map +1 -0
- package/socket.ts +49 -0
- package/store.js +30 -0
- package/store.js.map +1 -0
- package/store.ts +50 -0
- package/system.js +161 -0
- package/system.js.map +1 -0
- package/system.ts +216 -0
- package/testing.ts +39 -0
- package/tidy.ts +28 -0
- package/trash.js +117 -0
- package/trash.js.map +1 -0
- package/trash.ts +117 -0
- package/views.js +28 -0
- package/views.js.map +1 -0
- package/views.ts +30 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +154 -0
- package/vocab.ts +18 -0
- package/wind.js +88 -0
- package/wind.js.map +1 -0
- package/wind.ts +114 -0
package/command.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Commands and their byte streams, implemented at the process boundary.
|
|
2
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
3
|
+
import { statSync } from 'node:fs';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { delimiter, join } from 'node:path';
|
|
6
|
+
import { Readable, Writable } from 'node:stream';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { signalCode } from "./system.js";
|
|
9
|
+
let path = (value)=>value instanceof URL ? fileURLToPath(value) : value;
|
|
10
|
+
let stdio = (value)=>value == 'piped' ? 'pipe' : value == 'null' ? 'ignore' : value;
|
|
11
|
+
let options = (o, collect = false)=>({
|
|
12
|
+
cwd: o.cwd == null ? undefined : path(o.cwd),
|
|
13
|
+
env: o.clearEnv ? {
|
|
14
|
+
...o.env
|
|
15
|
+
} : o.env ? {
|
|
16
|
+
...process.env,
|
|
17
|
+
...o.env
|
|
18
|
+
} : undefined,
|
|
19
|
+
stdio: [
|
|
20
|
+
stdio(o.stdin ?? 'null'),
|
|
21
|
+
stdio(o.stdout ?? (collect ? 'piped' : 'inherit')),
|
|
22
|
+
stdio(o.stderr ?? (collect ? 'piped' : 'inherit')),
|
|
23
|
+
...o.ipc ? [
|
|
24
|
+
'ipc'
|
|
25
|
+
] : []
|
|
26
|
+
],
|
|
27
|
+
signal: o.signal,
|
|
28
|
+
detached: o.detached,
|
|
29
|
+
uid: o.uid,
|
|
30
|
+
gid: o.gid,
|
|
31
|
+
windowsVerbatimArguments: o.windowsRawArguments
|
|
32
|
+
});
|
|
33
|
+
let status = (code, signal)=>({
|
|
34
|
+
success: code == 0 && signal == null,
|
|
35
|
+
code: code ?? signalCode(signal),
|
|
36
|
+
signal
|
|
37
|
+
});
|
|
38
|
+
let bytes = async (stream)=>stream ? new Uint8Array(await new Response(stream).arrayBuffer()) : new Uint8Array();
|
|
39
|
+
let held = (p)=>p.pid ? [
|
|
40
|
+
p,
|
|
41
|
+
p.stdin,
|
|
42
|
+
p.stdout,
|
|
43
|
+
p.stderr
|
|
44
|
+
].filter((s)=>s) : [];
|
|
45
|
+
let child = (p)=>{
|
|
46
|
+
let started = new Promise((resolve, reject)=>{
|
|
47
|
+
p.once('spawn', resolve);
|
|
48
|
+
p.once('error', reject);
|
|
49
|
+
});
|
|
50
|
+
started.catch(()=>{});
|
|
51
|
+
let done = new Promise((resolve, reject)=>{
|
|
52
|
+
p.once('error', reject);
|
|
53
|
+
p.once('exit', (code, signal)=>resolve(status(code, signal)));
|
|
54
|
+
});
|
|
55
|
+
let out = p.stdout ? Readable.toWeb(p.stdout) : undefined;
|
|
56
|
+
let err = p.stderr ? Readable.toWeb(p.stderr) : undefined;
|
|
57
|
+
let input = p.stdin ? Writable.toWeb(p.stdin) : undefined;
|
|
58
|
+
let missing = ()=>{
|
|
59
|
+
throw new TypeError('command stream was not piped');
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
pid: p.pid ?? 0,
|
|
63
|
+
started,
|
|
64
|
+
status: done,
|
|
65
|
+
get stdin () {
|
|
66
|
+
return input ?? missing();
|
|
67
|
+
},
|
|
68
|
+
get stdout () {
|
|
69
|
+
return out ?? missing();
|
|
70
|
+
},
|
|
71
|
+
get stderr () {
|
|
72
|
+
return err ?? missing();
|
|
73
|
+
},
|
|
74
|
+
output: async ()=>{
|
|
75
|
+
let [result, stdout, stderr] = await Promise.all([
|
|
76
|
+
done,
|
|
77
|
+
bytes(out),
|
|
78
|
+
bytes(err)
|
|
79
|
+
]);
|
|
80
|
+
return {
|
|
81
|
+
...result,
|
|
82
|
+
stdout,
|
|
83
|
+
stderr
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
// By pid, not `p.kill`: Deno's `p.kill` also closes an IPC channel, and a
|
|
87
|
+
// child tied to its parent by one (@yaks/cli runtime.ts) reads that as the
|
|
88
|
+
// parent gone and ends at once, instead of draining on the signal.
|
|
89
|
+
kill: (signal = 'SIGTERM')=>{
|
|
90
|
+
if (p.exitCode != null || p.signalCode != null || !p.pid) return;
|
|
91
|
+
try {
|
|
92
|
+
process.kill(p.pid, signal);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
// Exited before its exit was heard: as `p.kill`, nothing to signal.
|
|
95
|
+
if (error.code != 'ESRCH') throw error;
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
ref: ()=>{
|
|
99
|
+
for (let h of held(p))h.ref();
|
|
100
|
+
},
|
|
101
|
+
unref: ()=>{
|
|
102
|
+
for (let h of held(p))h.unref();
|
|
103
|
+
},
|
|
104
|
+
[Symbol.asyncDispose]: async ()=>{
|
|
105
|
+
if (p.exitCode == null && p.signalCode == null) p.kill('SIGTERM');
|
|
106
|
+
await done;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
/// found('sh', '/nowhere:/bin') -> '/bin/sh'
|
|
111
|
+
/// found('no-such-program', '/bin') -> undefined
|
|
112
|
+
/** The executable a bare program name runs from `PATH`, as `execvp` finds it. */ export let found = (name, dirs = process.env.PATH ?? '')=>dirs.split(delimiter).filter(Boolean).map((dir)=>join(dir, name)).find((file)=>{
|
|
113
|
+
let stat = statSync(file, {
|
|
114
|
+
throwIfNoEntry: false
|
|
115
|
+
});
|
|
116
|
+
return stat?.isFile() && (stat.mode & 0o111) != 0;
|
|
117
|
+
});
|
|
118
|
+
// Deno starts a child in its own session by forking, which copies the
|
|
119
|
+
// parent's page tables on the calling thread: about 50 ms for each GB the
|
|
120
|
+
// parent holds, so a long-lived server spent seconds a minute starting git.
|
|
121
|
+
// A child started through util-linux's setsid(1) takes the session itself,
|
|
122
|
+
// after Deno started it with posix_spawn, and keeps its pid, so its group is
|
|
123
|
+
// still `-pid`. A program setsid can't find starts the forking way, which
|
|
124
|
+
// reports it missing.
|
|
125
|
+
let launch = (program, o, collect = false)=>{
|
|
126
|
+
let given = options(o, collect);
|
|
127
|
+
let file = path(program), args = o.args ?? [];
|
|
128
|
+
if (!o.detached || o.uid != null || o.gid != null || process.platform == 'win32') return [
|
|
129
|
+
file,
|
|
130
|
+
args,
|
|
131
|
+
given
|
|
132
|
+
];
|
|
133
|
+
let dirs = (given.env ?? process.env).PATH;
|
|
134
|
+
let setsid = found('setsid', dirs);
|
|
135
|
+
let runs = file.includes('/') || found(file, dirs) != null;
|
|
136
|
+
if (!setsid || !runs) return [
|
|
137
|
+
file,
|
|
138
|
+
args,
|
|
139
|
+
given
|
|
140
|
+
];
|
|
141
|
+
return [
|
|
142
|
+
setsid,
|
|
143
|
+
[
|
|
144
|
+
'-w',
|
|
145
|
+
'--',
|
|
146
|
+
file,
|
|
147
|
+
...args
|
|
148
|
+
],
|
|
149
|
+
{
|
|
150
|
+
...given,
|
|
151
|
+
detached: false
|
|
152
|
+
}
|
|
153
|
+
];
|
|
154
|
+
};
|
|
155
|
+
export let command = (program, o = {})=>({
|
|
156
|
+
spawn: ()=>child(spawn(...launch(program, o))),
|
|
157
|
+
output: ()=>child(spawn(...launch(program, o, true))).output(),
|
|
158
|
+
outputSync: ()=>{
|
|
159
|
+
let [file, args, given] = launch(program, o, true);
|
|
160
|
+
let result = spawnSync(file, args, {
|
|
161
|
+
...given,
|
|
162
|
+
maxBuffer: Infinity
|
|
163
|
+
});
|
|
164
|
+
if (result.error) throw result.error;
|
|
165
|
+
return {
|
|
166
|
+
...status(result.status, result.signal),
|
|
167
|
+
stdout: new Uint8Array(result.stdout ?? []),
|
|
168
|
+
stderr: new Uint8Array(result.stderr ?? [])
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
//# sourceMappingURL=command.js.map
|
package/command.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.js","sources":["./command.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,SAEE,KAAK,EACL,SAAS,QACJ,qBAAoB;AAC3B,SAAS,QAAQ,QAAQ,UAAS;AAClC,OAAO,aAAa,eAAc;AAClC,SAAS,SAAS,EAAE,IAAI,QAAQ,YAAW;AAC3C,SAAS,QAAQ,EAAE,QAAQ,QAAQ,cAAa;AAChD,SAAS,aAAa,QAAQ,WAAU;AACxC,SAAsB,UAAU,sBAAqB;AAyCrD,IAAI,OAAO,CAAC,QACV,iBAAiB,MAAM,cA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AC;IACvE,OAAO,IAAoB,MAAM,SAAS,OAAO,SAAS;IAC1D,QAAQ,IACN,MAAM,SAAS,OAAO,SAAS,GAAG,QAAQ,MAAM;IAClD,YAAY;MACV,IAAI,CAAC,MAAM,MAAM,MAAM,GAAG,OAAO,SAAS,GAAG;MAC7C,IAAI,SAAS,UAAU,MAAM,MAAM;QAAE,GAAG,KAAK;QAAE,WAAW;MAAS;MACnE,IAAI,OAAO,KAAK,EAAE,MAAM,OAAO,KAAK;MACpC,OAAO;QACL,GAAG,OAAO,OAAO,MAAM,EAAE,OAAO,MAAM,CAAC;QACvC,QAAQ,IAAI,WAAW,OAAO,MAAM,IAAI,EAAE;QAC1C,QAAQ,IAAI,WAAW,OAAO,MAAM,IAAI,EAAE;MAC5C;IACF;EACF,CAAC,EAAC"}
|
package/command.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Commands and their byte streams, implemented at the process boundary.
|
|
2
|
+
import {
|
|
3
|
+
type ChildProcess as NativeChild,
|
|
4
|
+
spawn,
|
|
5
|
+
spawnSync,
|
|
6
|
+
} from 'node:child_process'
|
|
7
|
+
import { statSync } from 'node:fs'
|
|
8
|
+
import process from 'node:process'
|
|
9
|
+
import { delimiter, join } from 'node:path'
|
|
10
|
+
import { Readable, Writable } from 'node:stream'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
import { type Signal, signalCode } from './system.js'
|
|
13
|
+
|
|
14
|
+
export type CommandOptions = {
|
|
15
|
+
args?: string[]
|
|
16
|
+
cwd?: string | URL
|
|
17
|
+
env?: Record<string, string>
|
|
18
|
+
clearEnv?: boolean
|
|
19
|
+
stdin?: 'piped' | 'inherit' | 'null'
|
|
20
|
+
stdout?: 'piped' | 'inherit' | 'null'
|
|
21
|
+
stderr?: 'piped' | 'inherit' | 'null'
|
|
22
|
+
signal?: AbortSignal
|
|
23
|
+
detached?: boolean
|
|
24
|
+
ipc?: boolean
|
|
25
|
+
uid?: number
|
|
26
|
+
gid?: number
|
|
27
|
+
windowsRawArguments?: boolean
|
|
28
|
+
}
|
|
29
|
+
export type CommandStatus = {
|
|
30
|
+
success: boolean
|
|
31
|
+
code: number
|
|
32
|
+
signal: Signal | null
|
|
33
|
+
}
|
|
34
|
+
export type CommandOutput = CommandStatus & {
|
|
35
|
+
stdout: Uint8Array<ArrayBuffer>
|
|
36
|
+
stderr: Uint8Array<ArrayBuffer>
|
|
37
|
+
}
|
|
38
|
+
export type ChildProcess = {
|
|
39
|
+
pid: number
|
|
40
|
+
/** Settles once the child is running, or rejects when it could not start
|
|
41
|
+
* (a missing program), before any of its pipes are used. */
|
|
42
|
+
started: Promise<void>
|
|
43
|
+
status: Promise<CommandStatus>
|
|
44
|
+
stdin: WritableStream<Uint8Array>
|
|
45
|
+
stdout: ReadableStream<Uint8Array<ArrayBuffer>>
|
|
46
|
+
stderr: ReadableStream<Uint8Array<ArrayBuffer>>
|
|
47
|
+
output: () => Promise<CommandOutput>
|
|
48
|
+
kill: (signal?: Signal) => void
|
|
49
|
+
ref: () => void
|
|
50
|
+
unref: () => void
|
|
51
|
+
[Symbol.asyncDispose]: () => Promise<void>
|
|
52
|
+
}
|
|
53
|
+
let path = (value: string | URL) =>
|
|
54
|
+
value instanceof URL ? fileURLToPath(value) : value
|
|
55
|
+
let stdio = (value: 'piped' | 'inherit' | 'null') =>
|
|
56
|
+
value == 'piped' ? 'pipe' : value == 'null' ? 'ignore' : value
|
|
57
|
+
let options = (o: CommandOptions, collect = false) => ({
|
|
58
|
+
cwd: o.cwd == null ? undefined : path(o.cwd),
|
|
59
|
+
env: o.clearEnv
|
|
60
|
+
? { ...o.env }
|
|
61
|
+
: o.env
|
|
62
|
+
? { ...process.env, ...o.env }
|
|
63
|
+
: undefined,
|
|
64
|
+
stdio: [
|
|
65
|
+
stdio(o.stdin ?? 'null'),
|
|
66
|
+
stdio(o.stdout ?? (collect ? 'piped' : 'inherit')),
|
|
67
|
+
stdio(o.stderr ?? (collect ? 'piped' : 'inherit')),
|
|
68
|
+
...o.ipc ? ['ipc' as const] : [],
|
|
69
|
+
] as [
|
|
70
|
+
'pipe' | 'inherit' | 'ignore',
|
|
71
|
+
'pipe' | 'inherit' | 'ignore',
|
|
72
|
+
'pipe' | 'inherit' | 'ignore',
|
|
73
|
+
'ipc'?,
|
|
74
|
+
],
|
|
75
|
+
signal: o.signal,
|
|
76
|
+
detached: o.detached,
|
|
77
|
+
uid: o.uid,
|
|
78
|
+
gid: o.gid,
|
|
79
|
+
windowsVerbatimArguments: o.windowsRawArguments,
|
|
80
|
+
})
|
|
81
|
+
let status = (code: number | null, signal: Signal | null): CommandStatus => ({
|
|
82
|
+
success: code == 0 && signal == null,
|
|
83
|
+
code: code ?? signalCode(signal),
|
|
84
|
+
signal,
|
|
85
|
+
})
|
|
86
|
+
let bytes = async (
|
|
87
|
+
stream: ReadableStream<Uint8Array<ArrayBuffer>> | undefined,
|
|
88
|
+
) =>
|
|
89
|
+
stream
|
|
90
|
+
? new Uint8Array(await new Response(stream).arrayBuffer())
|
|
91
|
+
: new Uint8Array()
|
|
92
|
+
// A child and its pipes hold the process open, and can be ref'd and unref'd
|
|
93
|
+
// together. A child that never started (no pid) holds nothing.
|
|
94
|
+
type Held = { ref: () => void; unref: () => void }
|
|
95
|
+
let held = (p: NativeChild) =>
|
|
96
|
+
(p.pid ? [p, p.stdin, p.stdout, p.stderr].filter((s) => s) : []) as Held[]
|
|
97
|
+
let child = (p: NativeChild): ChildProcess => {
|
|
98
|
+
let started = new Promise<void>((resolve, reject) => {
|
|
99
|
+
p.once('spawn', resolve)
|
|
100
|
+
p.once('error', reject)
|
|
101
|
+
})
|
|
102
|
+
started.catch(() => {})
|
|
103
|
+
let done = new Promise<CommandStatus>((resolve, reject) => {
|
|
104
|
+
p.once('error', reject)
|
|
105
|
+
p.once('exit', (code, signal) => resolve(status(code, signal)))
|
|
106
|
+
})
|
|
107
|
+
let out = p.stdout
|
|
108
|
+
? Readable.toWeb(p.stdout) as ReadableStream<Uint8Array<ArrayBuffer>>
|
|
109
|
+
: undefined
|
|
110
|
+
let err = p.stderr
|
|
111
|
+
? Readable.toWeb(p.stderr) as ReadableStream<Uint8Array<ArrayBuffer>>
|
|
112
|
+
: undefined
|
|
113
|
+
let input = p.stdin ? Writable.toWeb(p.stdin) : undefined
|
|
114
|
+
let missing = (): never => {
|
|
115
|
+
throw new TypeError('command stream was not piped')
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
pid: p.pid ?? 0,
|
|
119
|
+
started,
|
|
120
|
+
status: done,
|
|
121
|
+
get stdin() {
|
|
122
|
+
return input ?? missing()
|
|
123
|
+
},
|
|
124
|
+
get stdout() {
|
|
125
|
+
return out ?? missing()
|
|
126
|
+
},
|
|
127
|
+
get stderr() {
|
|
128
|
+
return err ?? missing()
|
|
129
|
+
},
|
|
130
|
+
output: async () => {
|
|
131
|
+
let [result, stdout, stderr] = await Promise.all([
|
|
132
|
+
done,
|
|
133
|
+
bytes(out),
|
|
134
|
+
bytes(err),
|
|
135
|
+
])
|
|
136
|
+
return { ...result, stdout, stderr }
|
|
137
|
+
},
|
|
138
|
+
// By pid, not `p.kill`: Deno's `p.kill` also closes an IPC channel, and a
|
|
139
|
+
// child tied to its parent by one (@yaks/cli runtime.ts) reads that as the
|
|
140
|
+
// parent gone and ends at once, instead of draining on the signal.
|
|
141
|
+
kill: (signal = 'SIGTERM') => {
|
|
142
|
+
if (p.exitCode != null || p.signalCode != null || !p.pid) return
|
|
143
|
+
try {
|
|
144
|
+
process.kill(p.pid, signal)
|
|
145
|
+
} catch (error) {
|
|
146
|
+
// Exited before its exit was heard: as `p.kill`, nothing to signal.
|
|
147
|
+
if ((error as { code?: string }).code != 'ESRCH') throw error
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
ref: () => {
|
|
151
|
+
for (let h of held(p)) h.ref()
|
|
152
|
+
},
|
|
153
|
+
unref: () => {
|
|
154
|
+
for (let h of held(p)) h.unref()
|
|
155
|
+
},
|
|
156
|
+
[Symbol.asyncDispose]: async () => {
|
|
157
|
+
if (p.exitCode == null && p.signalCode == null) p.kill('SIGTERM')
|
|
158
|
+
await done
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/// found('sh', '/nowhere:/bin') -> '/bin/sh'
|
|
163
|
+
/// found('no-such-program', '/bin') -> undefined
|
|
164
|
+
/** The executable a bare program name runs from `PATH`, as `execvp` finds it. */
|
|
165
|
+
export let found = (
|
|
166
|
+
name: string,
|
|
167
|
+
dirs = process.env.PATH ?? '',
|
|
168
|
+
): string | undefined =>
|
|
169
|
+
dirs.split(delimiter).filter(Boolean).map((dir) => join(dir, name)).find(
|
|
170
|
+
(file) => {
|
|
171
|
+
let stat = statSync(file, { throwIfNoEntry: false })
|
|
172
|
+
return stat?.isFile() && (stat.mode & 0o111) != 0
|
|
173
|
+
},
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
// Deno starts a child in its own session by forking, which copies the
|
|
177
|
+
// parent's page tables on the calling thread: about 50 ms for each GB the
|
|
178
|
+
// parent holds, so a long-lived server spent seconds a minute starting git.
|
|
179
|
+
// A child started through util-linux's setsid(1) takes the session itself,
|
|
180
|
+
// after Deno started it with posix_spawn, and keeps its pid, so its group is
|
|
181
|
+
// still `-pid`. A program setsid can't find starts the forking way, which
|
|
182
|
+
// reports it missing.
|
|
183
|
+
let launch = (
|
|
184
|
+
program: string | URL,
|
|
185
|
+
o: CommandOptions,
|
|
186
|
+
collect = false,
|
|
187
|
+
): [string, string[], ReturnType<typeof options>] => {
|
|
188
|
+
let given = options(o, collect)
|
|
189
|
+
let file = path(program), args = o.args ?? []
|
|
190
|
+
if (
|
|
191
|
+
!o.detached || o.uid != null || o.gid != null ||
|
|
192
|
+
process.platform == 'win32'
|
|
193
|
+
) return [file, args, given]
|
|
194
|
+
let dirs = (given.env ?? process.env).PATH
|
|
195
|
+
let setsid = found('setsid', dirs)
|
|
196
|
+
let runs = file.includes('/') || found(file, dirs) != null
|
|
197
|
+
if (!setsid || !runs) return [file, args, given]
|
|
198
|
+
return [setsid, ['-w', '--', file, ...args], { ...given, detached: false }]
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export let command = (program: string | URL, o: CommandOptions = {}) => ({
|
|
202
|
+
spawn: (): ChildProcess => child(spawn(...launch(program, o))),
|
|
203
|
+
output: (): Promise<CommandOutput> =>
|
|
204
|
+
child(spawn(...launch(program, o, true))).output(),
|
|
205
|
+
outputSync: (): CommandOutput => {
|
|
206
|
+
let [file, args, given] = launch(program, o, true)
|
|
207
|
+
let result = spawnSync(file, args, { ...given, maxBuffer: Infinity })
|
|
208
|
+
if (result.error) throw result.error
|
|
209
|
+
return {
|
|
210
|
+
...status(result.status, result.signal),
|
|
211
|
+
stdout: new Uint8Array(result.stdout ?? []),
|
|
212
|
+
stderr: new Uint8Array(result.stderr ?? []),
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
})
|
package/comp.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The four components this package ships, as one vocabulary document to load
|
|
2
|
+
// beside your own.
|
|
3
|
+
//
|
|
4
|
+
// service{command, cwd, restart, attempts} a program that should run
|
|
5
|
+
// process{pid, command, cwd} a program that is running
|
|
6
|
+
// runtime{name, version, os} what one of ours runs on
|
|
7
|
+
// exit{code} it is over, and how
|
|
8
|
+
//
|
|
9
|
+
// A process is an entity, not a field on whatever asked for it. That is the
|
|
10
|
+
// whole idea: the same components describe a provider CLI a session is a
|
|
11
|
+
// transcript of, a web server a supervisor keeps running, and a long-running
|
|
12
|
+
// tool call — so one piece of code records every exit code, and one component
|
|
13
|
+
// name covers every pid.
|
|
14
|
+
//
|
|
15
|
+
// `pid` is the only property an adopted process has, because a pid is the only
|
|
16
|
+
// handle you get on a process nobody here launched. `command` and `cwd` are
|
|
17
|
+
// present exactly when we started it: they hold the command we asked for, not
|
|
18
|
+
// what the kernel reports, and a supervisor that has to start the program again
|
|
19
|
+
// keeps its own copy rather than reading it back.
|
|
20
|
+
//
|
|
21
|
+
// `service` is the wanted half of the same entity: the supervisor acts on a
|
|
22
|
+
// component recording that a program should be running, never on a function
|
|
23
|
+
// call. It is stored on the same entity the process lands on — one entity is
|
|
24
|
+
// one supervised program, and reading it tells the whole story (what we want,
|
|
25
|
+
// what is running, how the last attempt ended, how many times it has crashed
|
|
26
|
+
// and been restarted). To stop a service, write a `stop` component, the marker
|
|
27
|
+
// @yaks/session already defines for "nothing happens after this", on the
|
|
28
|
+
// service entity itself: no second entity to clean up and no reference to fill
|
|
29
|
+
// in.
|
|
30
|
+
//
|
|
31
|
+
// `exit` is a separate component rather than a nullable property on `process`,
|
|
32
|
+
// because their absence means different things: a process with no `exit` is
|
|
33
|
+
// running (or was, when we last looked), and that is the query the server makes
|
|
34
|
+
// when it starts up. `exit` is never rewritten; on a supervised entity the
|
|
35
|
+
// transaction that records the next attempt deletes it, so nothing ever reads a
|
|
36
|
+
// fresh pid beside a stale exit code. An unsupervised process keeps its `exit`
|
|
37
|
+
// forever, and running the program again creates a new entity.
|
|
38
|
+
//
|
|
39
|
+
// Output is not a component: what a process prints stays in its files, and
|
|
40
|
+
// `tail` (./run.ts) reads it.
|
|
41
|
+
import doc from './vocab.json' with {
|
|
42
|
+
type: 'json'
|
|
43
|
+
};
|
|
44
|
+
/** The component recording that a program should be running. */ export let SERVICE = 'service';
|
|
45
|
+
/** The component naming a running program. */ export let PROCESS = 'process';
|
|
46
|
+
/** The component recording what a process of ours runs on. */ export let RUNTIME = 'runtime';
|
|
47
|
+
/** The component recording that it is over. */ export let EXIT = 'exit';
|
|
48
|
+
/**
|
|
49
|
+
* The process vocabulary, to load beside your own:
|
|
50
|
+
* `loadVocab([processDoc, ...mine])`. It does not describe why a process
|
|
51
|
+
* runs — that is an ordinary entity in your own vocabulary pointing at this
|
|
52
|
+
* one.
|
|
53
|
+
*/ export let processDoc = doc;
|
|
54
|
+
//# sourceMappingURL=comp.js.map
|
package/comp.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comp.js","sources":["./comp.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,mBAAmB;AACnB,EAAE;AACF,yEAAyE;AACzE,yEAAyE;AACzE,wEAAwE;AACxE,mEAAmE;AACnE,EAAE;AACF,4EAA4E;AAC5E,yEAAyE;AACzE,6EAA6E;AAC7E,8EAA8E;AAC9E,yBAAyB;AACzB,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,8EAA8E;AAC9E,gFAAgF;AAChF,kDAAkD;AAClD,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,6EAA6E;AAC7E,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,yEAAyE;AACzE,+EAA+E;AAC/E,MAAM;AACN,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,gFAAgF;AAChF,2EAA2E;AAC3E,gFAAgF;AAChF,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,2EAA2E;AAC3E,8BAA8B;AAI9B,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AAEpD,8DAA8D,GAC9D,OAAO,IAAI,UAAU,UAAS;AAE9B,4CAA4C,GAC5C,OAAO,IAAI,UAAU,UAAS;AAE9B,4DAA4D,GAC5D,OAAO,IAAI,UAAU,UAAS;AAE9B,6CAA6C,GAC7C,OAAO,IAAI,OAAO,OAAM;AA6DxB;;;;;CAKC,GACD,OAAO,IAAI,aAAuB,IAAG"}
|
package/comp.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// The four components this package ships, as one vocabulary document to load
|
|
2
|
+
// beside your own.
|
|
3
|
+
//
|
|
4
|
+
// service{command, cwd, restart, attempts} a program that should run
|
|
5
|
+
// process{pid, command, cwd} a program that is running
|
|
6
|
+
// runtime{name, version, os} what one of ours runs on
|
|
7
|
+
// exit{code} it is over, and how
|
|
8
|
+
//
|
|
9
|
+
// A process is an entity, not a field on whatever asked for it. That is the
|
|
10
|
+
// whole idea: the same components describe a provider CLI a session is a
|
|
11
|
+
// transcript of, a web server a supervisor keeps running, and a long-running
|
|
12
|
+
// tool call — so one piece of code records every exit code, and one component
|
|
13
|
+
// name covers every pid.
|
|
14
|
+
//
|
|
15
|
+
// `pid` is the only property an adopted process has, because a pid is the only
|
|
16
|
+
// handle you get on a process nobody here launched. `command` and `cwd` are
|
|
17
|
+
// present exactly when we started it: they hold the command we asked for, not
|
|
18
|
+
// what the kernel reports, and a supervisor that has to start the program again
|
|
19
|
+
// keeps its own copy rather than reading it back.
|
|
20
|
+
//
|
|
21
|
+
// `service` is the wanted half of the same entity: the supervisor acts on a
|
|
22
|
+
// component recording that a program should be running, never on a function
|
|
23
|
+
// call. It is stored on the same entity the process lands on — one entity is
|
|
24
|
+
// one supervised program, and reading it tells the whole story (what we want,
|
|
25
|
+
// what is running, how the last attempt ended, how many times it has crashed
|
|
26
|
+
// and been restarted). To stop a service, write a `stop` component, the marker
|
|
27
|
+
// @yaks/session already defines for "nothing happens after this", on the
|
|
28
|
+
// service entity itself: no second entity to clean up and no reference to fill
|
|
29
|
+
// in.
|
|
30
|
+
//
|
|
31
|
+
// `exit` is a separate component rather than a nullable property on `process`,
|
|
32
|
+
// because their absence means different things: a process with no `exit` is
|
|
33
|
+
// running (or was, when we last looked), and that is the query the server makes
|
|
34
|
+
// when it starts up. `exit` is never rewritten; on a supervised entity the
|
|
35
|
+
// transaction that records the next attempt deletes it, so nothing ever reads a
|
|
36
|
+
// fresh pid beside a stale exit code. An unsupervised process keeps its `exit`
|
|
37
|
+
// forever, and running the program again creates a new entity.
|
|
38
|
+
//
|
|
39
|
+
// Output is not a component: what a process prints stays in its files, and
|
|
40
|
+
// `tail` (./run.ts) reads it.
|
|
41
|
+
|
|
42
|
+
import type { Entity } from 'jsr:@yaks/graph@0.2.36'
|
|
43
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
44
|
+
import doc from './vocab.json' with { type: 'json' }
|
|
45
|
+
|
|
46
|
+
/** The component recording that a program should be running. */
|
|
47
|
+
export let SERVICE = 'service'
|
|
48
|
+
|
|
49
|
+
/** The component naming a running program. */
|
|
50
|
+
export let PROCESS = 'process'
|
|
51
|
+
|
|
52
|
+
/** The component recording what a process of ours runs on. */
|
|
53
|
+
export let RUNTIME = 'runtime'
|
|
54
|
+
|
|
55
|
+
/** The component recording that it is over. */
|
|
56
|
+
export let EXIT = 'exit'
|
|
57
|
+
|
|
58
|
+
/** What to do when a service's process exits — systemd's three values. */
|
|
59
|
+
export type Restart = 'never' | 'on-failure' | 'always'
|
|
60
|
+
|
|
61
|
+
/** A program that should be running. */
|
|
62
|
+
export type Service = {
|
|
63
|
+
/** the command line to run, argv joined by spaces */
|
|
64
|
+
command?: string | null
|
|
65
|
+
/** where to run it; absent means the supervisor's own working directory */
|
|
66
|
+
cwd?: string | null
|
|
67
|
+
/** what to do when it exits; absent means never */
|
|
68
|
+
restart?: Restart | null
|
|
69
|
+
/** how many times it has been started again after an exit */
|
|
70
|
+
attempts?: number | null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A tracked process. */
|
|
74
|
+
export type Process = {
|
|
75
|
+
/** original launch workspace; a local observer must not inspect its pid */
|
|
76
|
+
workspace?: string | null
|
|
77
|
+
/** the process id on the machine tracking it */
|
|
78
|
+
pid?: number | null
|
|
79
|
+
/** the command line it was launched with — absent on an adopted process */
|
|
80
|
+
command?: string | null
|
|
81
|
+
/** where it was launched — absent on an adopted process */
|
|
82
|
+
cwd?: string | null
|
|
83
|
+
/** the roles it serves, where it opened a graph */
|
|
84
|
+
roles?: string[] | null
|
|
85
|
+
/** the process launching it and reading how it goes, or the one that took
|
|
86
|
+
* it up after: an entity, not a pid */
|
|
87
|
+
watcher?: string | null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** What a process of ours runs on, as it says about itself. */
|
|
91
|
+
export type Runtime = {
|
|
92
|
+
/** the runtime, lowercase: `deno` */
|
|
93
|
+
name?: string | null
|
|
94
|
+
/** the runtime's version */
|
|
95
|
+
version?: string | null
|
|
96
|
+
/** the operating system and its release: `linux 6.8.0-45-generic` */
|
|
97
|
+
os?: string | null
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What is recorded about a process that has ended. */
|
|
101
|
+
export type Exit = {
|
|
102
|
+
/** the exit code it reported; absent when the end was observed but the code
|
|
103
|
+
* was not */
|
|
104
|
+
code?: number | null
|
|
105
|
+
/** how many of the run's processes the kernel's OOM killer ended at its
|
|
106
|
+
* memory limit while it ran (Linux, its scope's memory.events); absent when
|
|
107
|
+
* none were or nothing counts them */
|
|
108
|
+
oom?: number
|
|
109
|
+
/** a storage failure observed while the process was ending, or the machine
|
|
110
|
+
* restarting under it (`reboot`), which ends a run with no code */
|
|
111
|
+
reason?: 'disk_full' | 'exit_write_failed' | 'reboot'
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A bundle carrying a process, as {@link Store.running} returns. */
|
|
115
|
+
export type Tracked = { entity: Entity } & { process: Process; exit?: Exit }
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The process vocabulary, to load beside your own:
|
|
119
|
+
* `loadVocab([processDoc, ...mine])`. It does not describe why a process
|
|
120
|
+
* runs — that is an ordinary entity in your own vocabulary pointing at this
|
|
121
|
+
* one.
|
|
122
|
+
*/
|
|
123
|
+
export let processDoc: VocabDoc = doc
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/process","version":"0.2.36","license":"Apache-2.0","description":"Starts, adopts and supervises operating-system processes, recording each one as a graph entity with its pid and exit code.","exports":{".":"./mod.ts","./vocab":"./vocab.ts","./graph":"./graph.ts","./self":"./self.ts","./wind":"./wind.ts","./isolate":"./isolate.ts","./views":"./views.ts","./http":"./http.ts","./system":"./system.ts","./env":"./env.ts","./platform":"./platform.ts","./command":"./command.ts","./modules":"./modules.ts","./socket":"./socket.ts","./clock":"./clock.ts","./trash":"./trash.ts"},"imports":{"@yaks/process":"./mod.ts","@yaks/process/vocab":"./vocab.ts","@yaks/process/graph":"./graph.ts","@yaks/process/self":"./self.ts","@yaks/process/wind":"./wind.ts","@yaks/process/isolate":"./isolate.ts","@yaks/process/views":"./views.ts","@yaks/process/http":"./http.ts","@yaks/process/system":"./system.ts","@yaks/process/env":"./env.ts","@yaks/process/platform":"./platform.ts","@yaks/process/command":"./command.ts","@yaks/process/modules":"./modules.ts","@yaks/process/socket":"./socket.ts","@yaks/process/clock":"./clock.ts","@yaks/process/trash":"./trash.ts"}}
|
package/detached.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Spawning can sleep in kernel_clone even with asynchronous child streams.
|
|
2
|
+
// One shared thread owns detached launches; callers only send command data.
|
|
3
|
+
import { Worker } from 'node:worker_threads';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
/** A reusable command door. Its thread holds the caller open only while a
|
|
6
|
+
* launch is outstanding; a failed thread rejects every waiter and the next
|
|
7
|
+
* request gets a fresh one. */ export let detaching = (start = ()=>new Worker(new URL('./detached_worker.ts', import.meta.url)))=>{
|
|
8
|
+
let worker;
|
|
9
|
+
let serial = 0;
|
|
10
|
+
let pending = new Map();
|
|
11
|
+
let open = ()=>{
|
|
12
|
+
if (worker) return worker;
|
|
13
|
+
let w = start();
|
|
14
|
+
worker = w;
|
|
15
|
+
let fail = (error)=>{
|
|
16
|
+
if (worker !== w) return;
|
|
17
|
+
worker = undefined;
|
|
18
|
+
if (!pending.size) reportError(error);
|
|
19
|
+
for (let p of pending.values())p.reject(error);
|
|
20
|
+
pending.clear();
|
|
21
|
+
void w.terminate();
|
|
22
|
+
};
|
|
23
|
+
w.on('error', fail);
|
|
24
|
+
w.on('exit', (code)=>fail(new Error(`command thread exited (${code})`)));
|
|
25
|
+
w.on('message', ({ id, error })=>{
|
|
26
|
+
let p = pending.get(id);
|
|
27
|
+
if (!p) return;
|
|
28
|
+
pending.delete(id);
|
|
29
|
+
if (!pending.size) w.unref();
|
|
30
|
+
if (error) p.reject(error);
|
|
31
|
+
else p.resolve();
|
|
32
|
+
});
|
|
33
|
+
return w;
|
|
34
|
+
};
|
|
35
|
+
return (program, options = {})=>new Promise((resolve, reject)=>{
|
|
36
|
+
let id = ++serial;
|
|
37
|
+
try {
|
|
38
|
+
let w = open();
|
|
39
|
+
pending.set(id, {
|
|
40
|
+
resolve,
|
|
41
|
+
reject
|
|
42
|
+
});
|
|
43
|
+
w.ref();
|
|
44
|
+
w.postMessage({
|
|
45
|
+
id,
|
|
46
|
+
program,
|
|
47
|
+
options: options.cwd instanceof URL ? {
|
|
48
|
+
...options,
|
|
49
|
+
cwd: fileURLToPath(options.cwd)
|
|
50
|
+
} : options
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
pending.delete(id);
|
|
54
|
+
if (!pending.size) worker?.unref();
|
|
55
|
+
reject(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
export let detached = detaching();
|
|
60
|
+
//# sourceMappingURL=detached.js.map
|
package/detached.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detached.js","sources":["./detached.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,SAAS,MAAM,QAAQ,sBAAqB;AAE5C,SAAS,aAAa,QAAQ,WAAU;AAuBxC;;6BAE6B,GAC7B,OAAO,IAAI,YAAY,CACrB,QAAsB,IACpB,IAAI,OAAO,IAAI,IAAI,wBAAwB,YAAY,GAAG,EAAE;EAE9D,IAAI;EACJ,IAAI,SAAS;EACb,IAAI,UAAU,IAAI;EAIlB,IAAI,OAAO;IACT,IAAI,QAAQ,OAAO;IACnB,IAAI,IAAI;IACR,SAAS;IACT,IAAI,OAAO,CAAC;MACV,IAAI,WAAW,GAAG;MAClB,SAAS;MACT,IAAI,CAAC,QAAQ,IAAI,EAAE,YAAY;MAC/B,KA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