@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/daemon.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { randomBytes } from "node:crypto";
|
|
3
|
-
import { open, unlink } from "node:fs/promises";
|
|
4
|
-
import {
|
|
2
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
3
|
+
import { lstat, mkdir, open, unlink } from "node:fs/promises";
|
|
4
|
+
import {
|
|
5
|
+
createServer,
|
|
6
|
+
request as httpRequest,
|
|
7
|
+
type IncomingMessage,
|
|
8
|
+
type ServerResponse,
|
|
9
|
+
} from "node:http";
|
|
5
10
|
import { join, resolve } from "node:path";
|
|
6
11
|
import { setTimeout as delay } from "node:timers/promises";
|
|
7
12
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import {
|
|
9
|
-
import { HostError, object } from "./errors.js";
|
|
13
|
+
import { HostError, object } from "./host-error.js";
|
|
10
14
|
import { protectedPath, secureNew } from "./identity.js";
|
|
11
15
|
import { renderDaemonUiHtml, type DaemonStatusSnapshot } from "./daemon-ui.js";
|
|
12
16
|
import {
|
|
@@ -16,6 +20,27 @@ import {
|
|
|
16
20
|
writePrivate,
|
|
17
21
|
} from "./swarm-files.js";
|
|
18
22
|
|
|
23
|
+
function nodeError(cause: unknown, code: string): boolean {
|
|
24
|
+
return (
|
|
25
|
+
typeof cause === "object" &&
|
|
26
|
+
cause !== null &&
|
|
27
|
+
"code" in cause &&
|
|
28
|
+
(cause as { code?: unknown }).code === code
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function ensureStateDirectory(stateDir: string): Promise<string> {
|
|
33
|
+
const resolvedDir = resolve(stateDir);
|
|
34
|
+
try {
|
|
35
|
+
await lstat(resolvedDir);
|
|
36
|
+
} catch (cause) {
|
|
37
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
38
|
+
await mkdir(resolvedDir, { recursive: true, mode: 0o700 });
|
|
39
|
+
await secureNew(resolvedDir, true);
|
|
40
|
+
}
|
|
41
|
+
return resolvedDir;
|
|
42
|
+
}
|
|
43
|
+
|
|
19
44
|
interface Endpoint {
|
|
20
45
|
pid: number;
|
|
21
46
|
port: number;
|
|
@@ -29,10 +54,43 @@ export interface DaemonEndpointInfo {
|
|
|
29
54
|
stateDir: string;
|
|
30
55
|
}
|
|
31
56
|
|
|
57
|
+
export interface DaemonClientEndpointInfo extends DaemonEndpointInfo {
|
|
58
|
+
token: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type DaemonApiHandler = (
|
|
62
|
+
request: IncomingMessage,
|
|
63
|
+
response: ServerResponse,
|
|
64
|
+
) => Promise<boolean> | boolean;
|
|
65
|
+
|
|
32
66
|
export interface ServeDaemonOptions {
|
|
33
67
|
port?: number;
|
|
34
68
|
status?: () => DaemonStatusSnapshot;
|
|
35
69
|
onListen?: (info: { port: number; url: string }) => void;
|
|
70
|
+
apiHandler?: DaemonApiHandler;
|
|
71
|
+
onShutdown?: () => void;
|
|
72
|
+
hasActiveWork?: () => boolean;
|
|
73
|
+
env?: NodeJS.ProcessEnv;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function bearerMatches(
|
|
77
|
+
header: string | undefined,
|
|
78
|
+
expectedToken: string,
|
|
79
|
+
): boolean {
|
|
80
|
+
if (!header) return false;
|
|
81
|
+
const expected = `Bearer ${expectedToken}`;
|
|
82
|
+
const a = Buffer.from(header, "utf8");
|
|
83
|
+
const b = Buffer.from(expected, "utf8");
|
|
84
|
+
if (a.length !== b.length) return false;
|
|
85
|
+
return timingSafeEqual(a, b);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseIdleTimeoutMs(env: NodeJS.ProcessEnv): number | null {
|
|
89
|
+
const raw = env.IPAB_DAEMON_IDLE_TIMEOUT_MS?.trim();
|
|
90
|
+
if (!raw) return null;
|
|
91
|
+
const parsed = Number(raw);
|
|
92
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) return null;
|
|
93
|
+
return parsed;
|
|
36
94
|
}
|
|
37
95
|
|
|
38
96
|
function allowedLoopbackHost(host: string | undefined, port: number): boolean {
|
|
@@ -55,9 +113,9 @@ function allowedLoopbackOrigin(
|
|
|
55
113
|
);
|
|
56
114
|
}
|
|
57
115
|
|
|
58
|
-
export async function
|
|
116
|
+
export async function daemonClientEndpoint(
|
|
59
117
|
stateDir: string,
|
|
60
|
-
): Promise<
|
|
118
|
+
): Promise<DaemonClientEndpointInfo | null> {
|
|
61
119
|
let raw: unknown;
|
|
62
120
|
try {
|
|
63
121
|
raw = await readPrivate<unknown>(
|
|
@@ -85,36 +143,83 @@ export async function daemonEndpoint(
|
|
|
85
143
|
const resolvedDir = resolve(stateDir);
|
|
86
144
|
const pid = Number(endpoint.pid);
|
|
87
145
|
const port = Number(endpoint.port);
|
|
146
|
+
const token = endpoint.token;
|
|
147
|
+
let response: { status: number; body: string };
|
|
88
148
|
try {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
149
|
+
response = await new Promise<{ status: number; body: string }>(
|
|
150
|
+
(accept, reject) => {
|
|
151
|
+
const req = httpRequest(
|
|
152
|
+
{
|
|
153
|
+
host: "127.0.0.1",
|
|
154
|
+
port,
|
|
155
|
+
path: "/health",
|
|
156
|
+
method: "GET",
|
|
157
|
+
headers: {
|
|
158
|
+
host: `127.0.0.1:${port}`,
|
|
159
|
+
authorization: `Bearer ${token}`,
|
|
160
|
+
},
|
|
161
|
+
timeout: 750,
|
|
162
|
+
},
|
|
163
|
+
(res) => {
|
|
164
|
+
const chunks: Buffer[] = [];
|
|
165
|
+
res.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
166
|
+
res.on("end", () => {
|
|
167
|
+
accept({
|
|
168
|
+
status: res.statusCode ?? 0,
|
|
169
|
+
body: Buffer.concat(chunks).toString("utf8"),
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
res.on("error", reject);
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
req.on("timeout", () => {
|
|
176
|
+
req.destroy(new Error("Health check timed out."));
|
|
177
|
+
});
|
|
178
|
+
req.on("error", reject);
|
|
179
|
+
req.end();
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
} catch {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
if (response.status < 200 || response.status >= 300) return null;
|
|
186
|
+
let health: unknown;
|
|
187
|
+
try {
|
|
188
|
+
health = JSON.parse(response.body);
|
|
189
|
+
} catch {
|
|
190
|
+
return null;
|
|
117
191
|
}
|
|
192
|
+
if (typeof health !== "object" || health === null || Array.isArray(health))
|
|
193
|
+
return null;
|
|
194
|
+
const record = health as Record<string, unknown>;
|
|
195
|
+
if (record.pid !== pid || record.stateDir !== resolvedDir) return null;
|
|
196
|
+
if (record.api !== "v1")
|
|
197
|
+
throw new HostError(
|
|
198
|
+
`An older IPAB daemon (pid ${pid}) is running for this state directory without the v1 loopback API. Stop or restart the daemon and retry.`,
|
|
199
|
+
1,
|
|
200
|
+
"DAEMON_VERSION_MISMATCH",
|
|
201
|
+
"daemon",
|
|
202
|
+
);
|
|
203
|
+
return {
|
|
204
|
+
pid,
|
|
205
|
+
port,
|
|
206
|
+
url: `http://127.0.0.1:${port}/`,
|
|
207
|
+
stateDir: resolvedDir,
|
|
208
|
+
token,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export async function daemonEndpoint(
|
|
213
|
+
stateDir: string,
|
|
214
|
+
): Promise<DaemonEndpointInfo | null> {
|
|
215
|
+
const client = await daemonClientEndpoint(stateDir);
|
|
216
|
+
if (!client) return null;
|
|
217
|
+
return {
|
|
218
|
+
pid: client.pid,
|
|
219
|
+
port: client.port,
|
|
220
|
+
url: client.url,
|
|
221
|
+
stateDir: client.stateDir,
|
|
222
|
+
};
|
|
118
223
|
}
|
|
119
224
|
|
|
120
225
|
export async function daemonResponsive(stateDir: string): Promise<boolean> {
|
|
@@ -151,9 +256,9 @@ export async function serveDaemon(
|
|
|
151
256
|
requestedPort > 65535
|
|
152
257
|
)
|
|
153
258
|
throw new HostError("Daemon UI port must be an integer from 0 to 65535.");
|
|
154
|
-
const
|
|
259
|
+
const resolvedDir = await ensureStateDirectory(stateDir);
|
|
260
|
+
const directory = await privateDirectory(resolvedDir, "swarm-daemon");
|
|
155
261
|
const token = randomBytes(32).toString("hex");
|
|
156
|
-
const resolvedDir = resolve(stateDir);
|
|
157
262
|
const startedAt = new Date().toISOString();
|
|
158
263
|
let boundPort = 0;
|
|
159
264
|
|
|
@@ -170,13 +275,74 @@ export async function serveDaemon(
|
|
|
170
275
|
logs: [],
|
|
171
276
|
});
|
|
172
277
|
|
|
278
|
+
const idleTimeoutMs = parseIdleTimeoutMs(options.env ?? process.env);
|
|
279
|
+
let activeRequests = 0;
|
|
280
|
+
let idleTimer: NodeJS.Timeout | undefined;
|
|
281
|
+
let shuttingDown = false;
|
|
282
|
+
|
|
283
|
+
const clearIdleTimer = () => {
|
|
284
|
+
if (idleTimer) {
|
|
285
|
+
clearTimeout(idleTimer);
|
|
286
|
+
idleTimer = undefined;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const scheduleIdleShutdown = () => {
|
|
290
|
+
clearIdleTimer();
|
|
291
|
+
if (idleTimeoutMs === null || shuttingDown) return;
|
|
292
|
+
if (activeRequests > 0) return;
|
|
293
|
+
idleTimer = setTimeout(() => {
|
|
294
|
+
idleTimer = undefined;
|
|
295
|
+
if (shuttingDown) return;
|
|
296
|
+
if (activeRequests > 0 || options.hasActiveWork?.()) {
|
|
297
|
+
scheduleIdleShutdown();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
shuttingDown = true;
|
|
301
|
+
options.onShutdown?.();
|
|
302
|
+
}, idleTimeoutMs);
|
|
303
|
+
idleTimer.unref();
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const applyShortTimeout = (
|
|
307
|
+
request: IncomingMessage,
|
|
308
|
+
response: ServerResponse,
|
|
309
|
+
) => {
|
|
310
|
+
const onTimeout = () => {
|
|
311
|
+
if (!response.headersSent) {
|
|
312
|
+
response
|
|
313
|
+
.writeHead(408, {
|
|
314
|
+
"content-type": "text/plain; charset=utf-8",
|
|
315
|
+
"cache-control": "no-store",
|
|
316
|
+
})
|
|
317
|
+
.end("Request Timeout");
|
|
318
|
+
} else if (!response.writableEnded) {
|
|
319
|
+
response.destroy();
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
request.setTimeout(5000, onTimeout);
|
|
323
|
+
response.setTimeout(5000, onTimeout);
|
|
324
|
+
};
|
|
325
|
+
|
|
173
326
|
const server = createServer((request, response) => {
|
|
174
|
-
|
|
327
|
+
activeRequests++;
|
|
328
|
+
clearIdleTimer();
|
|
329
|
+
let requestSettled = false;
|
|
330
|
+
const onRequestDone = () => {
|
|
331
|
+
if (requestSettled) return;
|
|
332
|
+
requestSettled = true;
|
|
333
|
+
activeRequests = Math.max(0, activeRequests - 1);
|
|
334
|
+
scheduleIdleShutdown();
|
|
335
|
+
};
|
|
336
|
+
response.once("finish", onRequestDone);
|
|
337
|
+
response.once("close", onRequestDone);
|
|
338
|
+
|
|
339
|
+
const pathname = (request.url ?? "").split("?", 1)[0] ?? "";
|
|
175
340
|
if (pathname === "/health") {
|
|
341
|
+
applyShortTimeout(request, response);
|
|
176
342
|
if (
|
|
177
343
|
request.method !== "GET" ||
|
|
178
344
|
request.url !== "/health" ||
|
|
179
|
-
request.headers.authorization
|
|
345
|
+
!bearerMatches(request.headers.authorization, token)
|
|
180
346
|
) {
|
|
181
347
|
response.writeHead(403).end();
|
|
182
348
|
return;
|
|
@@ -187,12 +353,14 @@ export async function serveDaemon(
|
|
|
187
353
|
JSON.stringify({
|
|
188
354
|
pid: process.pid,
|
|
189
355
|
stateDir: resolvedDir,
|
|
356
|
+
api: "v1",
|
|
190
357
|
}),
|
|
191
358
|
);
|
|
192
359
|
return;
|
|
193
360
|
}
|
|
194
361
|
|
|
195
362
|
if (pathname === "/" || pathname === "/api/status") {
|
|
363
|
+
applyShortTimeout(request, response);
|
|
196
364
|
if (
|
|
197
365
|
!allowedLoopbackHost(request.headers.host, boundPort) ||
|
|
198
366
|
!allowedLoopbackOrigin(request.headers.origin, boundPort)
|
|
@@ -240,14 +408,134 @@ export async function serveDaemon(
|
|
|
240
408
|
return;
|
|
241
409
|
}
|
|
242
410
|
|
|
411
|
+
if (pathname.startsWith("/v1/")) {
|
|
412
|
+
if (
|
|
413
|
+
!allowedLoopbackHost(request.headers.host, boundPort) ||
|
|
414
|
+
!allowedLoopbackOrigin(request.headers.origin, boundPort) ||
|
|
415
|
+
!bearerMatches(request.headers.authorization, token)
|
|
416
|
+
) {
|
|
417
|
+
response
|
|
418
|
+
.writeHead(403, {
|
|
419
|
+
"content-type": "application/json; charset=utf-8",
|
|
420
|
+
"cache-control": "no-store",
|
|
421
|
+
"x-content-type-options": "nosniff",
|
|
422
|
+
})
|
|
423
|
+
.end(
|
|
424
|
+
JSON.stringify({
|
|
425
|
+
code: "FORBIDDEN",
|
|
426
|
+
message: "Forbidden: invalid authorization, Host, or Origin.",
|
|
427
|
+
exitCode: 1,
|
|
428
|
+
phase: "daemon",
|
|
429
|
+
}),
|
|
430
|
+
);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (pathname === "/v1/daemon/shutdown") {
|
|
435
|
+
if (request.method !== "POST") {
|
|
436
|
+
response
|
|
437
|
+
.writeHead(405, {
|
|
438
|
+
allow: "POST",
|
|
439
|
+
"content-type": "application/json; charset=utf-8",
|
|
440
|
+
"cache-control": "no-store",
|
|
441
|
+
"x-content-type-options": "nosniff",
|
|
442
|
+
})
|
|
443
|
+
.end(
|
|
444
|
+
JSON.stringify({
|
|
445
|
+
code: "METHOD_NOT_ALLOWED",
|
|
446
|
+
message: "Method Not Allowed: expected POST.",
|
|
447
|
+
exitCode: 1,
|
|
448
|
+
phase: "daemon",
|
|
449
|
+
}),
|
|
450
|
+
);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
shuttingDown = true;
|
|
454
|
+
clearIdleTimer();
|
|
455
|
+
response.writeHead(200, {
|
|
456
|
+
"content-type": "application/json; charset=utf-8",
|
|
457
|
+
"cache-control": "no-store",
|
|
458
|
+
"x-content-type-options": "nosniff",
|
|
459
|
+
});
|
|
460
|
+
response.end(JSON.stringify({ ok: true }), () => {
|
|
461
|
+
options.onShutdown?.();
|
|
462
|
+
});
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (!options.apiHandler) {
|
|
467
|
+
response
|
|
468
|
+
.writeHead(404, {
|
|
469
|
+
"content-type": "application/json; charset=utf-8",
|
|
470
|
+
"cache-control": "no-store",
|
|
471
|
+
"x-content-type-options": "nosniff",
|
|
472
|
+
})
|
|
473
|
+
.end(
|
|
474
|
+
JSON.stringify({
|
|
475
|
+
code: "NOT_FOUND",
|
|
476
|
+
message: "Daemon API handler is not mounted.",
|
|
477
|
+
exitCode: 1,
|
|
478
|
+
phase: "daemon",
|
|
479
|
+
}),
|
|
480
|
+
);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
void Promise.resolve()
|
|
485
|
+
.then(() => options.apiHandler!(request, response))
|
|
486
|
+
.then((handled) => {
|
|
487
|
+
if (!handled && !response.headersSent) {
|
|
488
|
+
response
|
|
489
|
+
.writeHead(404, {
|
|
490
|
+
"content-type": "application/json; charset=utf-8",
|
|
491
|
+
"cache-control": "no-store",
|
|
492
|
+
"x-content-type-options": "nosniff",
|
|
493
|
+
})
|
|
494
|
+
.end(
|
|
495
|
+
JSON.stringify({
|
|
496
|
+
code: "NOT_FOUND",
|
|
497
|
+
message: `Unknown daemon API endpoint: ${request.method ?? "GET"} ${pathname}`,
|
|
498
|
+
exitCode: 1,
|
|
499
|
+
phase: "daemon",
|
|
500
|
+
}),
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
})
|
|
504
|
+
.catch((cause: unknown) => {
|
|
505
|
+
const text = (cause instanceof Error ? cause.message : String(cause))
|
|
506
|
+
.split(token)
|
|
507
|
+
.join("[redacted]");
|
|
508
|
+
if (!response.headersSent) {
|
|
509
|
+
response
|
|
510
|
+
.writeHead(500, {
|
|
511
|
+
"content-type": "application/json; charset=utf-8",
|
|
512
|
+
"cache-control": "no-store",
|
|
513
|
+
"x-content-type-options": "nosniff",
|
|
514
|
+
})
|
|
515
|
+
.end(
|
|
516
|
+
JSON.stringify({
|
|
517
|
+
code: "DAEMON_ERROR",
|
|
518
|
+
message: text,
|
|
519
|
+
exitCode: 1,
|
|
520
|
+
phase: "daemon",
|
|
521
|
+
}),
|
|
522
|
+
);
|
|
523
|
+
} else if (!response.writableEnded) {
|
|
524
|
+
response.end();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
|
|
243
530
|
response.writeHead(403).end();
|
|
244
531
|
});
|
|
245
|
-
server.requestTimeout =
|
|
532
|
+
server.requestTimeout = 0;
|
|
246
533
|
server.headersTimeout = 5000;
|
|
247
534
|
server.keepAliveTimeout = 5000;
|
|
248
|
-
server.maxConnections =
|
|
535
|
+
server.maxConnections = 128;
|
|
249
536
|
const close = () =>
|
|
250
537
|
new Promise<void>((accept, reject) => {
|
|
538
|
+
clearIdleTimer();
|
|
251
539
|
server.close((error) => {
|
|
252
540
|
if (error) reject(error);
|
|
253
541
|
else accept();
|
|
@@ -284,6 +572,7 @@ export async function serveDaemon(
|
|
|
284
572
|
token,
|
|
285
573
|
};
|
|
286
574
|
await writePrivate(directory, "endpoint.json", endpoint);
|
|
575
|
+
scheduleIdleShutdown();
|
|
287
576
|
options.onListen?.({
|
|
288
577
|
port: boundPort,
|
|
289
578
|
url: `http://127.0.0.1:${boundPort}/`,
|
|
@@ -293,8 +582,12 @@ export async function serveDaemon(
|
|
|
293
582
|
throw cause;
|
|
294
583
|
}
|
|
295
584
|
return async () => {
|
|
585
|
+
shuttingDown = true;
|
|
586
|
+
clearIdleTimer();
|
|
296
587
|
try {
|
|
297
|
-
await unlink(join(directory, "endpoint.json"))
|
|
588
|
+
await unlink(join(directory, "endpoint.json")).catch((cause: unknown) => {
|
|
589
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
590
|
+
});
|
|
298
591
|
} finally {
|
|
299
592
|
await close();
|
|
300
593
|
}
|
|
@@ -307,7 +600,8 @@ export async function ensureDaemon(
|
|
|
307
600
|
diagnostic?: (text: string) => void,
|
|
308
601
|
): Promise<void> {
|
|
309
602
|
if (await daemonResponsive(stateDir)) return;
|
|
310
|
-
const
|
|
603
|
+
const resolvedStateDir = await ensureStateDirectory(stateDir);
|
|
604
|
+
const directory = await privateDirectory(resolvedStateDir, "swarm-daemon");
|
|
311
605
|
const launch = await privateDirectory(directory, "launch");
|
|
312
606
|
await withPrivateLock(launch, async () => {
|
|
313
607
|
if (await daemonResponsive(stateDir)) return;
|
package/src/history.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
IpfsStore,
|
|
3
|
+
type IpfsReadOptions,
|
|
4
|
+
type StagedBlock,
|
|
5
|
+
type StoreConfiguration,
|
|
6
|
+
} from "@ipab/ipfs-store";
|
|
7
|
+
import { CID } from "multiformats/cid";
|
|
2
8
|
import type { SQLInputValue } from "node:sqlite";
|
|
3
9
|
import { HostError, message, syntax } from "./errors.js";
|
|
4
10
|
import type { Hold, RunRecord, State } from "./state.js";
|
|
@@ -208,6 +214,25 @@ export class Ownership {
|
|
|
208
214
|
signal ? { signal } : {},
|
|
209
215
|
);
|
|
210
216
|
}
|
|
217
|
+
async importBlocks(
|
|
218
|
+
session: string,
|
|
219
|
+
blocks: readonly StagedBlock[],
|
|
220
|
+
root: CID | string,
|
|
221
|
+
options: IpfsReadOptions = {},
|
|
222
|
+
): Promise<string> {
|
|
223
|
+
const cid = CID.parse(root.toString()).toV1().toString();
|
|
224
|
+
await this.store.importBlocks(session, blocks, cid, this.owner, {
|
|
225
|
+
...options,
|
|
226
|
+
onRetained: () => {
|
|
227
|
+
const latest = this.state.hold(this.owner)!;
|
|
228
|
+
this.state.saveHold({
|
|
229
|
+
...latest,
|
|
230
|
+
roots: [...new Set([...latest.roots, cid])],
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
return cid;
|
|
235
|
+
}
|
|
211
236
|
async release(): Promise<void> {
|
|
212
237
|
const hold = this.state.hold(this.owner);
|
|
213
238
|
if (!hold) return;
|
package/src/identity.ts
CHANGED
|
@@ -13,8 +13,16 @@ import {
|
|
|
13
13
|
signEd25519,
|
|
14
14
|
verifyEd25519,
|
|
15
15
|
} from "@ipab/bundle/pointer/crypto";
|
|
16
|
-
import { HostError, object } from "./
|
|
17
|
-
|
|
16
|
+
import { HostError, object } from "./host-error.js";
|
|
17
|
+
|
|
18
|
+
function nodeError(cause: unknown, code: string): boolean {
|
|
19
|
+
return (
|
|
20
|
+
typeof cause === "object" &&
|
|
21
|
+
cause !== null &&
|
|
22
|
+
"code" in cause &&
|
|
23
|
+
(cause as { code?: unknown }).code === code
|
|
24
|
+
);
|
|
25
|
+
}
|
|
18
26
|
|
|
19
27
|
export interface Identity {
|
|
20
28
|
publicKey: string;
|