@ipab/cli 0.1.0-rc.11 → 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/README.md +15 -10
- package/dist/authoring.d.ts +6 -2
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +65 -18
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- 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 +2 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1631 -1100
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.js +1 -1
- package/dist/config-secret.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 +69 -0
- package/dist/daemon-ui.d.ts.map +1 -1
- package/dist/daemon-ui.js +285 -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/history.d.ts +3 -1
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +16 -1
- package/dist/history.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/kubo-topology.d.ts +2 -0
- package/dist/kubo-topology.d.ts.map +1 -1
- package/dist/kubo-topology.js +17 -20
- package/dist/kubo-topology.js.map +1 -1
- package/dist/kubo.d.ts +5 -3
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +83 -13
- package/dist/kubo.js.map +1 -1
- package/dist/name-reference.d.ts +8 -0
- package/dist/name-reference.d.ts.map +1 -0
- package/dist/name-reference.js +63 -0
- package/dist/name-reference.js.map +1 -0
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +18 -3
- package/dist/pins.js.map +1 -1
- package/dist/pivot-rpc.d.ts +37 -2
- package/dist/pivot-rpc.d.ts.map +1 -1
- package/dist/pivot-rpc.js +188 -8
- package/dist/pivot-rpc.js.map +1 -1
- package/dist/search-index.d.ts.map +1 -1
- package/dist/search-index.js +1 -0
- package/dist/search-index.js.map +1 -1
- package/dist/skill-definition.d.ts +9 -0
- package/dist/skill-definition.d.ts.map +1 -1
- package/dist/skill-definition.js +26 -2
- package/dist/skill-definition.js.map +1 -1
- package/dist/skill-source.d.ts +1 -0
- package/dist/skill-source.d.ts.map +1 -1
- package/dist/skill-source.js +10 -4
- package/dist/skill-source.js.map +1 -1
- package/dist/skills.d.ts +8 -2
- package/dist/skills.d.ts.map +1 -1
- package/dist/skills.js +31 -2
- package/dist/skills.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-fs-core.d.ts +217 -0
- package/dist/swarm-fs-core.d.ts.map +1 -0
- package/dist/swarm-fs-core.js +885 -0
- package/dist/swarm-fs-core.js.map +1 -0
- package/dist/swarm-fs-path.d.ts +17 -0
- package/dist/swarm-fs-path.d.ts.map +1 -0
- package/dist/swarm-fs-path.js +46 -0
- package/dist/swarm-fs-path.js.map +1 -0
- package/dist/swarm-fs.d.ts +9 -0
- package/dist/swarm-fs.d.ts.map +1 -0
- package/dist/swarm-fs.js +21 -0
- package/dist/swarm-fs.js.map +1 -0
- package/dist/swarm-observations.d.ts +6 -0
- package/dist/swarm-observations.d.ts.map +1 -1
- package/dist/swarm-observations.js +28 -8
- package/dist/swarm-observations.js.map +1 -1
- package/dist/swarm.d.ts +134 -18
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1293 -484
- package/dist/swarm.js.map +1 -1
- package/dist/unixfs-tree.d.ts +55 -0
- package/dist/unixfs-tree.d.ts.map +1 -1
- package/dist/unixfs-tree.js +361 -2
- package/dist/unixfs-tree.js.map +1 -1
- package/package.json +5 -5
- package/src/authoring.ts +88 -28
- package/src/cli-daemon.ts +482 -0
- package/src/cli.ts +2080 -1464
- package/src/config-secret.ts +1 -1
- 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 +364 -4
- package/src/daemon.ts +337 -43
- package/src/history.ts +26 -1
- package/src/identity.ts +10 -2
- package/src/ipc.ts +113 -79
- package/src/kubo-topology.ts +25 -22
- package/src/kubo.ts +127 -9
- package/src/name-reference.ts +91 -0
- package/src/pins.ts +23 -3
- package/src/pivot-rpc.ts +272 -14
- package/src/search-index.ts +1 -0
- package/src/skill-definition.ts +45 -3
- package/src/skill-source.ts +10 -4
- package/src/skills.ts +43 -2
- package/src/state-storage.ts +5 -1
- package/src/state.ts +3 -1
- package/src/swarm-files.ts +10 -2
- package/src/swarm-fs-core.ts +1461 -0
- package/src/swarm-fs-path.ts +68 -0
- package/src/swarm-fs.ts +104 -0
- package/src/swarm-observations.ts +40 -14
- package/src/swarm.ts +2149 -1033
- package/src/unixfs-tree.ts +447 -2
package/src/authoring.ts
CHANGED
|
@@ -45,7 +45,7 @@ import {
|
|
|
45
45
|
} from "@ipab/bundle-builder";
|
|
46
46
|
import type { EncodedBundleCar } from "@ipab/bundle-builder";
|
|
47
47
|
import { filesystemTree } from "@ipab/bundle-builder/node";
|
|
48
|
-
import type { IpfsStore } from "@ipab/ipfs-store";
|
|
48
|
+
import type { IpfsStore, StagedBlock } from "@ipab/ipfs-store";
|
|
49
49
|
import { HostError, message } from "./errors.js";
|
|
50
50
|
import type { Ownership } from "./history.js";
|
|
51
51
|
import { builtinAppSchema, schemaAlias } from "./app-definition.js";
|
|
@@ -250,11 +250,15 @@ function cidInput(source: string): CID | undefined {
|
|
|
250
250
|
return undefined;
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
|
-
export
|
|
253
|
+
export interface LocalBundleInput {
|
|
254
|
+
kind: "directory" | "car";
|
|
255
|
+
path: string;
|
|
256
|
+
}
|
|
257
|
+
export async function resolveLocalBundleInput(
|
|
254
258
|
source: string,
|
|
255
|
-
): Promise<
|
|
259
|
+
): Promise<LocalBundleInput | undefined> {
|
|
256
260
|
if (
|
|
257
|
-
|
|
261
|
+
/^(ipab|name):/i.test(source) ||
|
|
258
262
|
source.startsWith("builtin:") ||
|
|
259
263
|
cidInput(source)
|
|
260
264
|
)
|
|
@@ -263,46 +267,67 @@ export async function localBundleDirectory(
|
|
|
263
267
|
const stat = await existing(path);
|
|
264
268
|
if (!stat)
|
|
265
269
|
throw new HostError(
|
|
266
|
-
`Local
|
|
270
|
+
`Local bundle input does not exist: ${path}. Pass a Bundle directory containing .bundle.yaml, a .car file, a Bundle CID or an IPAB URI.`,
|
|
267
271
|
);
|
|
268
|
-
if (
|
|
272
|
+
if (stat.isSymbolicLink())
|
|
269
273
|
throw new HostError(
|
|
270
|
-
`Local
|
|
274
|
+
`Local bundle input must be a regular Bundle directory or .car file, not a symbolic link: ${path}.`,
|
|
271
275
|
);
|
|
272
|
-
return path;
|
|
276
|
+
if (stat.isDirectory()) return { kind: "directory", path };
|
|
277
|
+
if (stat.isFile() && path.toLowerCase().endsWith(".car"))
|
|
278
|
+
return { kind: "car", path };
|
|
279
|
+
throw new HostError(
|
|
280
|
+
`Local bundle input must be a regular Bundle directory or .car file, not a file: ${path}.`,
|
|
281
|
+
);
|
|
273
282
|
}
|
|
274
|
-
export async function
|
|
275
|
-
|
|
283
|
+
export async function snapshotLocalBundle(
|
|
284
|
+
input: LocalBundleInput,
|
|
276
285
|
owner: Ownership,
|
|
277
286
|
session: string,
|
|
278
287
|
options: AuthoringOptions = {},
|
|
279
288
|
): Promise<string> {
|
|
280
289
|
try {
|
|
290
|
+
if (input.kind === "car") {
|
|
291
|
+
options.diagnostic?.("Validating local Bundle CAR...");
|
|
292
|
+
const car = await readCar(input.path, options);
|
|
293
|
+
const loaded = await fromRoot(car.root, car.resolveCid, "car", options);
|
|
294
|
+
if (loaded.kind !== "content-bundle")
|
|
295
|
+
throw new HostError(
|
|
296
|
+
"CAR contains a Bundle type schema, not a Content or App Bundle.",
|
|
297
|
+
);
|
|
298
|
+
const blocks: StagedBlock[] = [];
|
|
299
|
+
for (const [key, blockBytes] of car.blocks)
|
|
300
|
+
blocks.push({ cid: CID.parse(key), bytes: blockBytes });
|
|
301
|
+
options.diagnostic?.("Importing local Bundle snapshot...");
|
|
302
|
+
return await owner.importBlocks(
|
|
303
|
+
session,
|
|
304
|
+
blocks,
|
|
305
|
+
car.root.toV1(),
|
|
306
|
+
options,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
options.diagnostic?.("Validating and packing local Bundle directory...");
|
|
281
310
|
const encoded = await encodeBundleCar(
|
|
282
|
-
await selectedTree(
|
|
311
|
+
await selectedTree(input.path, undefined, undefined, options),
|
|
283
312
|
options,
|
|
284
313
|
);
|
|
285
314
|
try {
|
|
286
315
|
const iterator = await CarBlockIterator.fromIterable(
|
|
287
316
|
streamChunks(encoded.stream, options),
|
|
288
317
|
);
|
|
318
|
+
const blocks: StagedBlock[] = [];
|
|
289
319
|
for await (const block of iterator)
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
encoded.cid,
|
|
294
|
-
owner.owner,
|
|
295
|
-
options,
|
|
296
|
-
);
|
|
297
|
-
const cid = encoded.cid.toString();
|
|
298
|
-
await owner.retain([cid], options.signal);
|
|
299
|
-
return cid;
|
|
320
|
+
blocks.push({ cid: block.cid, bytes: block.bytes });
|
|
321
|
+
options.diagnostic?.("Importing local Bundle snapshot...");
|
|
322
|
+
return await owner.importBlocks(session, blocks, encoded.cid, options);
|
|
300
323
|
} finally {
|
|
301
324
|
await encoded.close();
|
|
302
325
|
}
|
|
303
326
|
} catch (cause) {
|
|
304
327
|
throw new HostError(
|
|
305
|
-
|
|
328
|
+
input.kind === "car"
|
|
329
|
+
? `Cannot load local Bundle CAR ${input.path}: ${message(cause)} Ensure the CAR contains a valid single-root Content or App Bundle.`
|
|
330
|
+
: `Cannot load local Bundle directory ${input.path}: ${message(cause)} Ensure .bundle.yaml, its type definition and entry form a valid Content or App Bundle.`,
|
|
306
331
|
);
|
|
307
332
|
}
|
|
308
333
|
}
|
|
@@ -433,12 +458,47 @@ export async function importBundle(
|
|
|
433
458
|
): Promise<ImportResult> {
|
|
434
459
|
const path = resolve(source);
|
|
435
460
|
const stat = await lstat(path);
|
|
436
|
-
if (
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
)
|
|
441
|
-
|
|
461
|
+
if (stat.isSymbolicLink())
|
|
462
|
+
throw new HostError(
|
|
463
|
+
"Import requires a regular .car file or Bundle directory, not a symbolic link.",
|
|
464
|
+
);
|
|
465
|
+
if (stat.isDirectory()) {
|
|
466
|
+
const encoded = await encodeBundleCar(
|
|
467
|
+
await selectedTree(path, undefined, undefined, options),
|
|
468
|
+
options,
|
|
469
|
+
);
|
|
470
|
+
try {
|
|
471
|
+
options.signal?.throwIfAborted();
|
|
472
|
+
const store = await getStore();
|
|
473
|
+
let blocks = 0;
|
|
474
|
+
let bytes = 0;
|
|
475
|
+
const iterator = await CarBlockIterator.fromIterable(
|
|
476
|
+
streamChunks(encoded.stream, options),
|
|
477
|
+
);
|
|
478
|
+
for await (const block of iterator) {
|
|
479
|
+
options.signal?.throwIfAborted();
|
|
480
|
+
await store.put(block.cid.toV1(), block.bytes, options);
|
|
481
|
+
blocks++;
|
|
482
|
+
bytes += block.bytes.length;
|
|
483
|
+
}
|
|
484
|
+
options.signal?.throwIfAborted();
|
|
485
|
+
return {
|
|
486
|
+
kind: "bundle-import",
|
|
487
|
+
cid: encoded.cid.toV1().toString(),
|
|
488
|
+
schemaCid: encoded.schema.cid.toString(),
|
|
489
|
+
source: path,
|
|
490
|
+
validation: encoded.validation,
|
|
491
|
+
blocks,
|
|
492
|
+
bytes,
|
|
493
|
+
};
|
|
494
|
+
} finally {
|
|
495
|
+
await encoded.close();
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (!stat.isFile() || !path.toLowerCase().endsWith(".car"))
|
|
499
|
+
throw new HostError(
|
|
500
|
+
"Import requires a regular .car file or Bundle directory.",
|
|
501
|
+
);
|
|
442
502
|
const car = await readCar(path, options);
|
|
443
503
|
const loaded = await fromRoot(car.root, car.resolveCid, "car", options);
|
|
444
504
|
options.signal?.throwIfAborted();
|
|
@@ -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
|
+
}
|