@ipab/cli 0.1.0-rc.12 → 0.1.0-rc.13
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/dist/cli-daemon.d.ts +14 -0
- package/dist/cli-daemon.d.ts.map +1 -0
- package/dist/cli-daemon.js +400 -0
- package/dist/cli-daemon.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1260 -1404
- package/dist/cli.js.map +1 -1
- package/dist/daemon-api.d.ts +80 -0
- package/dist/daemon-api.d.ts.map +1 -0
- package/dist/daemon-api.js +2411 -0
- package/dist/daemon-api.js.map +1 -0
- package/dist/daemon-client.d.ts +322 -0
- package/dist/daemon-client.d.ts.map +1 -0
- package/dist/daemon-client.js +400 -0
- package/dist/daemon-client.js.map +1 -0
- package/dist/daemon-protocol.d.ts +22 -0
- package/dist/daemon-protocol.d.ts.map +1 -0
- package/dist/daemon-protocol.js +39 -0
- package/dist/daemon-protocol.js.map +1 -0
- package/dist/daemon-telemetry.d.ts +86 -0
- package/dist/daemon-telemetry.d.ts.map +1 -0
- package/dist/daemon-telemetry.js +502 -0
- package/dist/daemon-telemetry.js.map +1 -0
- package/dist/daemon-ui.d.ts +68 -0
- package/dist/daemon-ui.d.ts.map +1 -1
- package/dist/daemon-ui.js +283 -4
- package/dist/daemon-ui.js.map +1 -1
- package/dist/daemon.d.ts +10 -0
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +289 -38
- package/dist/daemon.js.map +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +7 -2
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +19 -7
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +96 -70
- package/dist/ipc.js.map +1 -1
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +18 -3
- package/dist/pins.js.map +1 -1
- package/dist/state-storage.d.ts.map +1 -1
- package/dist/state-storage.js +2 -1
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +1 -1
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +3 -2
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +7 -2
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +10 -1
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +230 -56
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli-daemon.ts +482 -0
- package/src/cli.ts +1689 -1948
- package/src/daemon-api.ts +3148 -0
- package/src/daemon-client.ts +975 -0
- package/src/daemon-protocol.ts +61 -0
- package/src/daemon-telemetry.ts +656 -0
- package/src/daemon-ui.ts +361 -4
- package/src/daemon.ts +337 -43
- package/src/identity.ts +10 -2
- package/src/ipc.ts +113 -79
- package/src/pins.ts +23 -3
- package/src/state-storage.ts +5 -1
- package/src/state.ts +3 -1
- package/src/swarm-files.ts +10 -2
- package/src/swarm.ts +698 -464
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ipab/cli",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.13",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Node subprocess IPAB host.",
|
|
6
6
|
"type": "module",
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"liquidjs": "10.30.1",
|
|
45
45
|
"@ipab/web-app": "0.1.0-rc.7",
|
|
46
46
|
"esbuild": "0.28.2",
|
|
47
|
-
"@ipab/bundle-builder": "0.1.0-rc.
|
|
47
|
+
"@ipab/bundle-builder": "0.1.0-rc.11",
|
|
48
48
|
"@ipld/car": "5.4.6",
|
|
49
49
|
"@ipld/dag-pb": "4.2.0",
|
|
50
|
-
"@ipab/bundle": "0.1.0-rc.
|
|
51
|
-
"@ipab/ipfs-store": "0.1.0-rc.
|
|
50
|
+
"@ipab/bundle": "0.1.0-rc.11",
|
|
51
|
+
"@ipab/ipfs-store": "0.1.0-rc.7",
|
|
52
52
|
"ipfs-unixfs": "13.1.2",
|
|
53
53
|
"ipfs-unixfs-importer": "17.1.1",
|
|
54
54
|
"multiformats": "14.0.5",
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
2
|
+
import { lstat } from "node:fs/promises";
|
|
3
|
+
import { isAbsolute, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { CID } from "multiformats/cid";
|
|
6
|
+
import {
|
|
7
|
+
isLocalSwarm,
|
|
8
|
+
LOCAL_SWARM_URL,
|
|
9
|
+
swarmUrl,
|
|
10
|
+
type GlobalOptions,
|
|
11
|
+
} from "./config.js";
|
|
12
|
+
import { requestFrame, type Reply } from "./context.js";
|
|
13
|
+
import { daemonClientEndpoint, ensureDaemon, serveDaemon } from "./daemon.js";
|
|
14
|
+
import { DaemonClient, type PreparedRun } from "./daemon-client.js";
|
|
15
|
+
import { HostError } from "./host-error.js";
|
|
16
|
+
import { parseNameReference } from "./name-reference.js";
|
|
17
|
+
import type { ExecutionLogs as ExecutionLogsType } from "./logs.js";
|
|
18
|
+
import type { RegisteredSwarm } from "./state.js";
|
|
19
|
+
|
|
20
|
+
export interface CliClientHandle {
|
|
21
|
+
client: DaemonClient;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function openCliClient(
|
|
26
|
+
stateDir: string,
|
|
27
|
+
env: NodeJS.ProcessEnv,
|
|
28
|
+
flags: GlobalOptions,
|
|
29
|
+
onDiagnostic: (text: string, progress?: boolean) => void,
|
|
30
|
+
options: { lightweight?: boolean } = {},
|
|
31
|
+
): Promise<CliClientHandle> {
|
|
32
|
+
const existing = await daemonClientEndpoint(stateDir);
|
|
33
|
+
if (existing) {
|
|
34
|
+
return {
|
|
35
|
+
client: new DaemonClient({
|
|
36
|
+
stateDir,
|
|
37
|
+
env,
|
|
38
|
+
endpoint: existing,
|
|
39
|
+
autoStart: false,
|
|
40
|
+
onDiagnostic,
|
|
41
|
+
}),
|
|
42
|
+
close: async () => {},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const useEphemeral =
|
|
47
|
+
process.env.VITEST === "true" &&
|
|
48
|
+
env.IPAB_DAEMON_AUTOSTART !== "1" &&
|
|
49
|
+
process.env.IPAB_DAEMON_AUTOSTART !== "1";
|
|
50
|
+
|
|
51
|
+
if (useEphemeral) {
|
|
52
|
+
const effectiveEnv =
|
|
53
|
+
flags.storeGateway !== undefined
|
|
54
|
+
? { ...env, IPAB_STORE_GATEWAY: flags.storeGateway }
|
|
55
|
+
: env;
|
|
56
|
+
if (options.lightweight) {
|
|
57
|
+
const [{ State }, { effectiveConfig, visibleConfig, LOCAL_SWARM_ALIAS }] =
|
|
58
|
+
await Promise.all([import("./state.js"), import("./config.js")]);
|
|
59
|
+
const state = new State(stateDir, [], effectiveEnv);
|
|
60
|
+
let closeServer: (() => Promise<void>) | undefined;
|
|
61
|
+
try {
|
|
62
|
+
closeServer = await serveDaemon(stateDir, {
|
|
63
|
+
port: 0,
|
|
64
|
+
env: effectiveEnv,
|
|
65
|
+
apiHandler: (req, res) => {
|
|
66
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
67
|
+
if (req.method === "GET" && url.pathname === "/v1/swarms") {
|
|
68
|
+
res.writeHead(200, {
|
|
69
|
+
"content-type": "application/json; charset=utf-8",
|
|
70
|
+
"cache-control": "no-store",
|
|
71
|
+
});
|
|
72
|
+
res.end(
|
|
73
|
+
JSON.stringify({
|
|
74
|
+
swarms: [
|
|
75
|
+
{
|
|
76
|
+
cid: LOCAL_SWARM_URL,
|
|
77
|
+
alias: LOCAL_SWARM_ALIAS,
|
|
78
|
+
local: true,
|
|
79
|
+
},
|
|
80
|
+
...state.swarms(),
|
|
81
|
+
],
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (req.method === "GET" && url.pathname === "/v1/host/config") {
|
|
87
|
+
const saved = state.config({ includeSecrets: false });
|
|
88
|
+
const items = effectiveConfig(
|
|
89
|
+
saved,
|
|
90
|
+
{ json: false, stateDir },
|
|
91
|
+
effectiveEnv,
|
|
92
|
+
(ref) => state.resolveSwarm(ref),
|
|
93
|
+
).map(visibleConfig);
|
|
94
|
+
res.writeHead(200, {
|
|
95
|
+
"content-type": "application/json; charset=utf-8",
|
|
96
|
+
"cache-control": "no-store",
|
|
97
|
+
});
|
|
98
|
+
res.end(
|
|
99
|
+
JSON.stringify({
|
|
100
|
+
items,
|
|
101
|
+
saved,
|
|
102
|
+
legacyPinCount: state.legacyPinCount,
|
|
103
|
+
legacyRegistrationCount: state.legacyRegistrationCount,
|
|
104
|
+
stateDir: state.directory,
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
return false;
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const endpoint = await daemonClientEndpoint(stateDir);
|
|
113
|
+
if (!endpoint) {
|
|
114
|
+
throw new HostError(
|
|
115
|
+
"In-process daemon did not publish a reachable loopback endpoint.",
|
|
116
|
+
1,
|
|
117
|
+
"DAEMON_UNAVAILABLE",
|
|
118
|
+
"daemon",
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
client: new DaemonClient({
|
|
123
|
+
stateDir,
|
|
124
|
+
env: effectiveEnv,
|
|
125
|
+
endpoint,
|
|
126
|
+
autoStart: false,
|
|
127
|
+
onDiagnostic,
|
|
128
|
+
}),
|
|
129
|
+
close: async () => {
|
|
130
|
+
try {
|
|
131
|
+
await closeServer?.();
|
|
132
|
+
} finally {
|
|
133
|
+
state.close();
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
} catch (cause) {
|
|
138
|
+
await closeServer?.().catch(() => {});
|
|
139
|
+
state.close();
|
|
140
|
+
throw cause;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const { createDaemonApiHandler, DaemonService } =
|
|
144
|
+
await import("./daemon-api.js");
|
|
145
|
+
let promoteToPersistentDaemon = false;
|
|
146
|
+
const service = await DaemonService.create({
|
|
147
|
+
stateDir,
|
|
148
|
+
env: effectiveEnv,
|
|
149
|
+
ensureDaemon: () => {
|
|
150
|
+
promoteToPersistentDaemon = true;
|
|
151
|
+
return Promise.resolve();
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
let closeServer: (() => Promise<void>) | undefined;
|
|
155
|
+
try {
|
|
156
|
+
closeServer = await serveDaemon(stateDir, {
|
|
157
|
+
port: 0,
|
|
158
|
+
env: effectiveEnv,
|
|
159
|
+
apiHandler: createDaemonApiHandler(service),
|
|
160
|
+
hasActiveWork: () => service.hasActiveRuns(),
|
|
161
|
+
});
|
|
162
|
+
const endpoint = await daemonClientEndpoint(stateDir);
|
|
163
|
+
if (!endpoint) {
|
|
164
|
+
throw new HostError(
|
|
165
|
+
"In-process daemon did not publish a reachable loopback endpoint.",
|
|
166
|
+
1,
|
|
167
|
+
"DAEMON_UNAVAILABLE",
|
|
168
|
+
"daemon",
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
client: new DaemonClient({
|
|
173
|
+
stateDir,
|
|
174
|
+
env: effectiveEnv,
|
|
175
|
+
endpoint,
|
|
176
|
+
autoStart: false,
|
|
177
|
+
onDiagnostic,
|
|
178
|
+
onAbortRequest: (reason) => {
|
|
179
|
+
service.abortActiveRequests(reason);
|
|
180
|
+
},
|
|
181
|
+
}),
|
|
182
|
+
close: async () => {
|
|
183
|
+
try {
|
|
184
|
+
await closeServer?.();
|
|
185
|
+
} finally {
|
|
186
|
+
await service.close();
|
|
187
|
+
}
|
|
188
|
+
if (promoteToPersistentDaemon) {
|
|
189
|
+
await ensureDaemon(stateDir, effectiveEnv, (text) => {
|
|
190
|
+
onDiagnostic(text);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
} catch (cause) {
|
|
196
|
+
await closeServer?.().catch(() => {});
|
|
197
|
+
await service.close().catch(() => {});
|
|
198
|
+
throw cause;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
client: new DaemonClient({
|
|
204
|
+
stateDir,
|
|
205
|
+
env,
|
|
206
|
+
autoStart: true,
|
|
207
|
+
onDiagnostic,
|
|
208
|
+
}),
|
|
209
|
+
close: async () => {},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function resolveLocalPathIfPresent(
|
|
214
|
+
input: string,
|
|
215
|
+
): Promise<string> {
|
|
216
|
+
if (parseNameReference(input) || input.startsWith("builtin:")) return input;
|
|
217
|
+
try {
|
|
218
|
+
CID.parse(input);
|
|
219
|
+
return input;
|
|
220
|
+
} catch {
|
|
221
|
+
/* Not a CID. */
|
|
222
|
+
}
|
|
223
|
+
const candidate = resolve(input);
|
|
224
|
+
if (
|
|
225
|
+
input.toLowerCase().endsWith(".car") ||
|
|
226
|
+
input.startsWith("./") ||
|
|
227
|
+
input.startsWith("../") ||
|
|
228
|
+
input.startsWith(".\\") ||
|
|
229
|
+
input.startsWith("..\\") ||
|
|
230
|
+
input === "." ||
|
|
231
|
+
input === ".." ||
|
|
232
|
+
isAbsolute(input)
|
|
233
|
+
) {
|
|
234
|
+
return candidate;
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
await lstat(candidate);
|
|
238
|
+
return candidate;
|
|
239
|
+
} catch {
|
|
240
|
+
return input;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function resolveSwarmFromList(
|
|
245
|
+
swarms: readonly RegisteredSwarm[],
|
|
246
|
+
reference: string,
|
|
247
|
+
): string {
|
|
248
|
+
const input = reference.trim();
|
|
249
|
+
if (!input) throw new HostError("Swarm reference cannot be empty.", 2);
|
|
250
|
+
if (isLocalSwarm(input)) return LOCAL_SWARM_URL;
|
|
251
|
+
const byAlias = swarms.find((entry) => entry.alias === input);
|
|
252
|
+
if (byAlias) return byAlias.cid;
|
|
253
|
+
const byCid = swarms.find((entry) => entry.cid === input);
|
|
254
|
+
if (byCid) return byCid.cid;
|
|
255
|
+
try {
|
|
256
|
+
return swarmUrl(input);
|
|
257
|
+
} catch {
|
|
258
|
+
/* Fall through to unknown swarm error. */
|
|
259
|
+
}
|
|
260
|
+
throw new HostError(
|
|
261
|
+
`Unknown Swarm "${reference}". Run ipab swarm list or register it with ipab swarm set ALIAS CONFIG_URL.`,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export async function runPreparedApp(
|
|
266
|
+
client: DaemonClient,
|
|
267
|
+
prepared: PreparedRun,
|
|
268
|
+
diagnostic: (text: string, progress?: boolean) => void,
|
|
269
|
+
): Promise<number> {
|
|
270
|
+
const [{ message }, { ExecutionLogs }] = await Promise.all([
|
|
271
|
+
import("./errors.js"),
|
|
272
|
+
import("./logs.js"),
|
|
273
|
+
]);
|
|
274
|
+
let child: ChildProcess | undefined;
|
|
275
|
+
let timer: NodeJS.Timeout | undefined;
|
|
276
|
+
let logs: ExecutionLogsType | undefined;
|
|
277
|
+
let forwarding: Promise<void>[] = [];
|
|
278
|
+
let logFailure: unknown;
|
|
279
|
+
let fatalError: Error | undefined;
|
|
280
|
+
let cancellation = 0;
|
|
281
|
+
let ipcClosed = false;
|
|
282
|
+
const ipcRequests = new Map<number, AbortController>();
|
|
283
|
+
const ipcTasks = new Set<Promise<void>>();
|
|
284
|
+
|
|
285
|
+
const stop = (error: Error, code = 1) => {
|
|
286
|
+
if (code !== 1) cancellation = code;
|
|
287
|
+
else fatalError ??= error;
|
|
288
|
+
for (const req of ipcRequests.values()) req.abort(error);
|
|
289
|
+
if (child && child.exitCode === null && child.signalCode === null) {
|
|
290
|
+
child.kill(code === 130 ? "SIGINT" : "SIGTERM");
|
|
291
|
+
timer ??= setTimeout(() => {
|
|
292
|
+
child?.kill("SIGKILL");
|
|
293
|
+
}, 2000);
|
|
294
|
+
timer.unref();
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const interrupt = () => {
|
|
299
|
+
stop(
|
|
300
|
+
new HostError("Execution cancelled.", 130, "CANCELLED", "execution"),
|
|
301
|
+
130,
|
|
302
|
+
);
|
|
303
|
+
};
|
|
304
|
+
const terminate = () => {
|
|
305
|
+
stop(
|
|
306
|
+
new HostError("Execution cancelled.", 143, "CANCELLED", "execution"),
|
|
307
|
+
143,
|
|
308
|
+
);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
process.on("SIGINT", interrupt);
|
|
312
|
+
process.on("SIGTERM", terminate);
|
|
313
|
+
|
|
314
|
+
const reply = (frame: Reply) => {
|
|
315
|
+
if (child?.connected) {
|
|
316
|
+
child.send(frame, (error) => {
|
|
317
|
+
if (error && !ipcClosed) stop(error);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const onMessage = (frame: unknown) => {
|
|
323
|
+
if (!requestFrame(frame)) {
|
|
324
|
+
stop(new HostError("Invalid IPAB context frame."));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (ipcClosed) {
|
|
328
|
+
reply({
|
|
329
|
+
version: 1,
|
|
330
|
+
id: frame.id,
|
|
331
|
+
error: "Context server is closed.",
|
|
332
|
+
});
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (frame.operation === "cancel") {
|
|
336
|
+
ipcRequests
|
|
337
|
+
.get(Number(frame.args))
|
|
338
|
+
?.abort(new HostError("Context operation cancelled."));
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (ipcRequests.size >= 32 || ipcRequests.has(frame.id)) {
|
|
342
|
+
stop(new HostError("Context request capacity or identity violation."));
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const controller = new AbortController();
|
|
346
|
+
ipcRequests.set(frame.id, controller);
|
|
347
|
+
const task = client.runs
|
|
348
|
+
.ipc(prepared.runId, frame.operation, frame.args, {
|
|
349
|
+
signal: controller.signal,
|
|
350
|
+
})
|
|
351
|
+
.then(
|
|
352
|
+
(result) => {
|
|
353
|
+
reply({ version: 1, id: frame.id, result });
|
|
354
|
+
},
|
|
355
|
+
(error: unknown) => {
|
|
356
|
+
reply({ version: 1, id: frame.id, error: message(error) });
|
|
357
|
+
},
|
|
358
|
+
)
|
|
359
|
+
.finally(() => {
|
|
360
|
+
ipcRequests.delete(frame.id);
|
|
361
|
+
ipcTasks.delete(task);
|
|
362
|
+
});
|
|
363
|
+
ipcTasks.add(task);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
let spawned = false;
|
|
367
|
+
try {
|
|
368
|
+
if (prepared.origin === "open" || prepared.localInput) {
|
|
369
|
+
diagnostic("Starting App...");
|
|
370
|
+
}
|
|
371
|
+
if (prepared.logConfig.enabled) {
|
|
372
|
+
logs = await ExecutionLogs.create(
|
|
373
|
+
prepared.logConfig.stateDir,
|
|
374
|
+
prepared.logConfig.runId,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
child = spawn(
|
|
378
|
+
process.execPath,
|
|
379
|
+
[
|
|
380
|
+
fileURLToPath(new URL("../dist/child.js", import.meta.url)),
|
|
381
|
+
prepared.entryPath,
|
|
382
|
+
...prepared.argv,
|
|
383
|
+
],
|
|
384
|
+
{
|
|
385
|
+
stdio: [
|
|
386
|
+
"inherit",
|
|
387
|
+
logs ? "pipe" : "inherit",
|
|
388
|
+
logs ? "pipe" : "inherit",
|
|
389
|
+
"ipc",
|
|
390
|
+
],
|
|
391
|
+
serialization: "advanced",
|
|
392
|
+
shell: false,
|
|
393
|
+
},
|
|
394
|
+
);
|
|
395
|
+
spawned = true;
|
|
396
|
+
child.on("message", onMessage);
|
|
397
|
+
|
|
398
|
+
if (logs) {
|
|
399
|
+
forwarding = [
|
|
400
|
+
logs.forward(child.stdout!, process.stdout, "stdout"),
|
|
401
|
+
logs.forward(child.stderr!, process.stderr, "stderr"),
|
|
402
|
+
].map((task) =>
|
|
403
|
+
task.catch((error: unknown) => {
|
|
404
|
+
logFailure ??= error;
|
|
405
|
+
stop(new HostError(`Execution logging failed: ${message(error)}`));
|
|
406
|
+
}),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const result = await new Promise<{
|
|
411
|
+
code: number | null;
|
|
412
|
+
signal: NodeJS.Signals | null;
|
|
413
|
+
}>((accept, reject) => {
|
|
414
|
+
child!.once("error", reject);
|
|
415
|
+
child!.once("close", (code, signal) => {
|
|
416
|
+
accept({ code, signal });
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
await Promise.all(forwarding);
|
|
421
|
+
let logComplete = false;
|
|
422
|
+
if (logs) {
|
|
423
|
+
await logs.close();
|
|
424
|
+
logs = undefined;
|
|
425
|
+
if (!logFailure) logComplete = true;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
ipcClosed = true;
|
|
429
|
+
child.off("message", onMessage);
|
|
430
|
+
for (const req of ipcRequests.values()) {
|
|
431
|
+
req.abort(new HostError("App finished."));
|
|
432
|
+
}
|
|
433
|
+
await Promise.allSettled([...ipcTasks]);
|
|
434
|
+
|
|
435
|
+
const finished = await client.runs.finish(
|
|
436
|
+
prepared.runId,
|
|
437
|
+
{
|
|
438
|
+
code: result.code,
|
|
439
|
+
signal: result.signal,
|
|
440
|
+
...(prepared.logConfig.enabled ? { logComplete } : {}),
|
|
441
|
+
...(logFailure ? { logFailure: message(logFailure) } : {}),
|
|
442
|
+
...(fatalError ? { fatalError: message(fatalError) } : {}),
|
|
443
|
+
...(cancellation ? { cancellation } : {}),
|
|
444
|
+
},
|
|
445
|
+
{ ndjson: true, onDiagnostic: diagnostic },
|
|
446
|
+
);
|
|
447
|
+
return finished.exitCode;
|
|
448
|
+
} catch (cause) {
|
|
449
|
+
ipcClosed = true;
|
|
450
|
+
child?.off("message", onMessage);
|
|
451
|
+
for (const req of ipcRequests.values()) {
|
|
452
|
+
req.abort(new HostError("App failed."));
|
|
453
|
+
}
|
|
454
|
+
await Promise.allSettled([...ipcTasks]);
|
|
455
|
+
await Promise.all(forwarding).catch(() => {});
|
|
456
|
+
await logs?.close().catch(() => {});
|
|
457
|
+
if (spawned) {
|
|
458
|
+
const cancelCode = cancellation || 1;
|
|
459
|
+
const cancelled = await client.runs
|
|
460
|
+
.cancel(
|
|
461
|
+
prepared.runId,
|
|
462
|
+
{ exitCode: cancelCode, reason: message(cause) },
|
|
463
|
+
{ ndjson: true, onDiagnostic: diagnostic },
|
|
464
|
+
)
|
|
465
|
+
.catch(() => null);
|
|
466
|
+
if (cancelled) return cancelled.exitCode;
|
|
467
|
+
} else {
|
|
468
|
+
await client.runs
|
|
469
|
+
.cancel(
|
|
470
|
+
prepared.runId,
|
|
471
|
+
{ exitCode: cancellation || 1, reason: message(cause) },
|
|
472
|
+
{ ndjson: true, onDiagnostic: diagnostic },
|
|
473
|
+
)
|
|
474
|
+
.catch(() => {});
|
|
475
|
+
}
|
|
476
|
+
throw cause;
|
|
477
|
+
} finally {
|
|
478
|
+
if (timer) clearTimeout(timer);
|
|
479
|
+
process.off("SIGINT", interrupt);
|
|
480
|
+
process.off("SIGTERM", terminate);
|
|
481
|
+
}
|
|
482
|
+
}
|