@ipab/cli 0.1.0-rc.6 → 0.1.0-rc.8
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 +96 -34
- package/dist/app-authoring.d.ts +5 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +6 -45
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-prebuild.d.ts +18 -0
- package/dist/app-prebuild.d.ts.map +1 -1
- package/dist/app-prebuild.js +46 -0
- package/dist/app-prebuild.js.map +1 -1
- package/dist/app-templates.d.ts +0 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +6 -45
- package/dist/app-templates.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.bundle-viewer.car +0 -0
- package/dist/builtins/template.check.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +1 -39
- package/dist/builtins.js.map +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +668 -221
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -4
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +20 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +31 -2
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +1 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -20
- package/dist/errors.js.map +1 -1
- package/dist/host-error.d.ts +9 -0
- package/dist/host-error.d.ts.map +1 -0
- package/dist/host-error.js +21 -0
- package/dist/host-error.js.map +1 -0
- package/dist/ipc.d.ts +2 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +61 -3
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.js +1 -1
- package/dist/kubo-install.js.map +1 -1
- package/dist/kubo-peers.d.ts.map +1 -1
- package/dist/kubo-peers.js +2 -1
- package/dist/kubo-peers.js.map +1 -1
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +105 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +10 -0
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +96 -15
- package/dist/kubo.js.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +6 -3
- package/dist/runtime.js.map +1 -1
- package/dist/skill-definition.d.ts +6 -0
- package/dist/skill-definition.d.ts.map +1 -0
- package/dist/skill-definition.js +38 -0
- package/dist/skill-definition.js.map +1 -0
- package/dist/skill-source.d.ts +5 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +12 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +84 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +736 -0
- package/dist/skills.js.map +1 -0
- package/dist/space-path.d.ts +3 -0
- package/dist/space-path.d.ts.map +1 -0
- package/dist/space-path.js +17 -0
- package/dist/space-path.js.map +1 -0
- package/dist/space.d.ts +16 -4
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +155 -18
- package/dist/space.js.map +1 -1
- package/dist/state-storage.js +1 -1
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +32 -13
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +2 -2
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +4 -4
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm-meta.d.ts +14 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +65 -0
- package/dist/swarm-meta.js.map +1 -0
- package/dist/swarm-observations.d.ts +40 -0
- package/dist/swarm-observations.d.ts.map +1 -0
- package/dist/swarm-observations.js +262 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +172 -6
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1106 -132
- package/dist/swarm.js.map +1 -1
- package/dist/template-paths.d.ts +7 -0
- package/dist/template-paths.d.ts.map +1 -0
- package/dist/template-paths.js +23 -0
- package/dist/template-paths.js.map +1 -0
- package/dist/template-presentation.d.ts +20 -0
- package/dist/template-presentation.d.ts.map +1 -0
- package/dist/template-presentation.js +133 -0
- package/dist/template-presentation.js.map +1 -0
- package/dist/template-source.d.ts +2 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +7 -1
- package/dist/template-source.js.map +1 -1
- package/dist/templates.d.ts +11 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +53 -0
- package/dist/templates.js.map +1 -0
- package/package.json +7 -6
- package/src/app-authoring.ts +30 -55
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +8 -72
- package/src/builtins.ts +2 -48
- package/src/cli.ts +830 -247
- package/src/config.ts +12 -4
- package/src/context.ts +60 -2
- package/src/errors.ts +1 -25
- package/src/host-error.ts +26 -0
- package/src/ipc.ts +82 -2
- package/src/kubo-install.ts +1 -1
- package/src/kubo-peers.ts +2 -3
- package/src/kubo-topology.ts +135 -0
- package/src/kubo.ts +134 -25
- package/src/runtime.ts +6 -2
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +15 -0
- package/src/skills.ts +1005 -0
- package/src/space-path.ts +30 -0
- package/src/space.ts +216 -33
- package/src/state-storage.ts +1 -1
- package/src/state.ts +38 -13
- package/src/swarm-files.ts +4 -2
- package/src/swarm-meta.ts +90 -0
- package/src/swarm-observations.ts +374 -0
- package/src/swarm.ts +1637 -193
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +9 -2
- package/src/templates.ts +77 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { HostError } from "./host-error.js";
|
|
2
|
+
|
|
3
|
+
export const CHUNK_BYTES = 64 * 1024;
|
|
4
|
+
|
|
5
|
+
export function relativePath(path: string): string {
|
|
6
|
+
if (
|
|
7
|
+
path.length > 1024 ||
|
|
8
|
+
!path ||
|
|
9
|
+
path.includes("\\") ||
|
|
10
|
+
path.includes("%") ||
|
|
11
|
+
[...path].some(
|
|
12
|
+
(char) =>
|
|
13
|
+
char.charCodeAt(0) < 32 ||
|
|
14
|
+
char.charCodeAt(0) === 127 ||
|
|
15
|
+
':*?"<>|'.includes(char),
|
|
16
|
+
) ||
|
|
17
|
+
path
|
|
18
|
+
.split("/")
|
|
19
|
+
.some(
|
|
20
|
+
(part) => !part || part === "." || part === ".." || /[. ]$/.test(part),
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
throw new HostError(
|
|
24
|
+
"Unsafe Space path; use an unambiguous directory-relative / path.",
|
|
25
|
+
1,
|
|
26
|
+
"PATH_INVALID",
|
|
27
|
+
"execution",
|
|
28
|
+
);
|
|
29
|
+
return path;
|
|
30
|
+
}
|
package/src/space.ts
CHANGED
|
@@ -5,44 +5,20 @@ import { CID } from "multiformats/cid";
|
|
|
5
5
|
import { sha256 } from "multiformats/hashes/sha2";
|
|
6
6
|
import type { IpfsStore } from "@ipab/ipfs-store";
|
|
7
7
|
import { openBundle, type ContentBundle } from "@ipab/bundle";
|
|
8
|
-
import { HostError, message } from "./errors.js";
|
|
9
|
-
import
|
|
8
|
+
import { HostError, message, object } from "./errors.js";
|
|
9
|
+
import { Ownership } from "./history.js";
|
|
10
|
+
import { randomUUID } from "node:crypto";
|
|
11
|
+
import { ReadableStream as NodeReadableStream } from "node:stream/web";
|
|
10
12
|
import { appDescription, appSchemaCid } from "./app-definition.js";
|
|
11
13
|
import { readSupportedSchemas } from "./supported-schemas.js";
|
|
12
14
|
export { appSchema, appSchemaCid } from "./app-definition.js";
|
|
13
|
-
|
|
15
|
+
import { CHUNK_BYTES, relativePath } from "./space-path.js";
|
|
16
|
+
export { CHUNK_BYTES, relativePath } from "./space-path.js";
|
|
14
17
|
export const OUTPUT_BYTES = 64 * 1024 * 1024;
|
|
15
18
|
export const MAX_PATHS = 4096;
|
|
16
19
|
|
|
17
|
-
export function relativePath(path: string): string {
|
|
18
|
-
if (
|
|
19
|
-
path.length > 1024 ||
|
|
20
|
-
!path ||
|
|
21
|
-
path.includes("\\") ||
|
|
22
|
-
path.includes("%") ||
|
|
23
|
-
[...path].some(
|
|
24
|
-
(char) =>
|
|
25
|
-
char.charCodeAt(0) < 32 ||
|
|
26
|
-
char.charCodeAt(0) === 127 ||
|
|
27
|
-
':*?"<>|'.includes(char),
|
|
28
|
-
) ||
|
|
29
|
-
path
|
|
30
|
-
.split("/")
|
|
31
|
-
.some(
|
|
32
|
-
(part) => !part || part === "." || part === ".." || /[. ]$/.test(part),
|
|
33
|
-
)
|
|
34
|
-
)
|
|
35
|
-
throw new HostError(
|
|
36
|
-
"Unsafe Space path; use an unambiguous directory-relative / path.",
|
|
37
|
-
1,
|
|
38
|
-
"PATH_INVALID",
|
|
39
|
-
"execution",
|
|
40
|
-
);
|
|
41
|
-
return path;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
20
|
export async function bytes(
|
|
45
|
-
stream: ReadableStream<Uint8Array>,
|
|
21
|
+
stream: ReadableStream<Uint8Array> | NodeReadableStream<Uint8Array>,
|
|
46
22
|
limit: number,
|
|
47
23
|
): Promise<Uint8Array> {
|
|
48
24
|
const reader = stream.getReader();
|
|
@@ -157,6 +133,75 @@ export async function loadApp(
|
|
|
157
133
|
};
|
|
158
134
|
}
|
|
159
135
|
|
|
136
|
+
/** Checkpoints are owned by the execution, not pins; only the final result is handed to history. */
|
|
137
|
+
export async function deriveBundle(
|
|
138
|
+
inputCid: string,
|
|
139
|
+
replacements: unknown,
|
|
140
|
+
store: IpfsStore,
|
|
141
|
+
owner: Ownership,
|
|
142
|
+
signal?: AbortSignal,
|
|
143
|
+
): Promise<string> {
|
|
144
|
+
signal?.throwIfAborted();
|
|
145
|
+
const changes = Object.entries(object(replacements, "Bundle replacements"));
|
|
146
|
+
if (changes.length > MAX_PATHS)
|
|
147
|
+
throw new HostError("Bundle replacement path limit exceeded.");
|
|
148
|
+
let total = 0;
|
|
149
|
+
for (const [path, value] of changes) {
|
|
150
|
+
relativePath(path);
|
|
151
|
+
if (!(value instanceof Uint8Array))
|
|
152
|
+
throw new HostError("Bundle replacements must contain byte arrays.");
|
|
153
|
+
total += value.byteLength;
|
|
154
|
+
if (total > OUTPUT_BYTES)
|
|
155
|
+
throw new HostError("Bundle replacement byte limit exceeded.");
|
|
156
|
+
}
|
|
157
|
+
const input = CID.parse(inputCid).toV1().toString();
|
|
158
|
+
const original = await validatedBundle(input, store, owner, signal);
|
|
159
|
+
const id = randomUUID();
|
|
160
|
+
const session = `ipab-derive:${owner.state.identity}:${id}`;
|
|
161
|
+
const temporary = await Ownership.create(
|
|
162
|
+
owner.state,
|
|
163
|
+
store,
|
|
164
|
+
`derive:${id}`,
|
|
165
|
+
session,
|
|
166
|
+
);
|
|
167
|
+
try {
|
|
168
|
+
const output = new OutputSpace(store, temporary, session, signal);
|
|
169
|
+
const replaced = new Set(changes.map(([path]) => path));
|
|
170
|
+
for (const path of original.files) {
|
|
171
|
+
signal?.throwIfAborted();
|
|
172
|
+
if (replaced.has(path)) continue;
|
|
173
|
+
const file = await store.read(
|
|
174
|
+
`${input}/${path}`,
|
|
175
|
+
signal ? { signal } : {},
|
|
176
|
+
);
|
|
177
|
+
await output.link(path, file.cid.toString(), signal);
|
|
178
|
+
}
|
|
179
|
+
for (const [path, value] of changes) {
|
|
180
|
+
const content = value as Uint8Array;
|
|
181
|
+
const chunks = async function* () {
|
|
182
|
+
for (let offset = 0; offset < content.length; offset += CHUNK_BYTES) {
|
|
183
|
+
signal?.throwIfAborted();
|
|
184
|
+
yield await Promise.resolve(
|
|
185
|
+
content.subarray(offset, offset + CHUNK_BYTES),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
await output.write(path, chunks(), signal);
|
|
190
|
+
}
|
|
191
|
+
const result = await output.finalize();
|
|
192
|
+
const derived = await validatedBundle(result, store, temporary, signal);
|
|
193
|
+
if (derived.schemaKey !== original.schemaKey)
|
|
194
|
+
throw new HostError(
|
|
195
|
+
"Derived Bundle must preserve the input's exact type CID.",
|
|
196
|
+
);
|
|
197
|
+
await owner.retain([result], signal);
|
|
198
|
+
signal?.throwIfAborted();
|
|
199
|
+
return result;
|
|
200
|
+
} finally {
|
|
201
|
+
await temporary.release();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
160
205
|
interface Entry {
|
|
161
206
|
cid: CID;
|
|
162
207
|
size: bigint;
|
|
@@ -175,6 +220,11 @@ export class OutputSpace {
|
|
|
175
220
|
private readonly pending = new Set<string>();
|
|
176
221
|
private totalBytes = 0;
|
|
177
222
|
private fault: Error | undefined;
|
|
223
|
+
private bundleMode = false;
|
|
224
|
+
private bundleSchema: string | undefined;
|
|
225
|
+
private submittedBundle: string | undefined;
|
|
226
|
+
private selectingBundle = false;
|
|
227
|
+
private sessionStarted = false;
|
|
178
228
|
constructor(
|
|
179
229
|
readonly store: IpfsStore,
|
|
180
230
|
readonly owner: Ownership,
|
|
@@ -184,6 +234,10 @@ export class OutputSpace {
|
|
|
184
234
|
this.session = session;
|
|
185
235
|
}
|
|
186
236
|
private claim(path: string): void {
|
|
237
|
+
if (this.bundleMode)
|
|
238
|
+
throw new HostError(
|
|
239
|
+
"Explicit Bundle results cannot be mixed with output Space writes.",
|
|
240
|
+
);
|
|
187
241
|
relativePath(path);
|
|
188
242
|
if (this.claimed.size >= MAX_PATHS)
|
|
189
243
|
throw new HostError("Output path limit exceeded.");
|
|
@@ -199,7 +253,125 @@ export class OutputSpace {
|
|
|
199
253
|
this.pending.add(path);
|
|
200
254
|
}
|
|
201
255
|
fail(error: unknown): void {
|
|
202
|
-
this.fault ??=
|
|
256
|
+
this.fault ??=
|
|
257
|
+
error instanceof HostError ? error : new HostError(message(error));
|
|
258
|
+
}
|
|
259
|
+
private selectBundleMode(): void {
|
|
260
|
+
if (this.fault) throw this.fault;
|
|
261
|
+
if (this.claimed.size)
|
|
262
|
+
throw new HostError(
|
|
263
|
+
"Explicit Bundle results cannot be mixed with output Space writes.",
|
|
264
|
+
);
|
|
265
|
+
if (this.selectingBundle)
|
|
266
|
+
throw new HostError(
|
|
267
|
+
"Concurrent Bundle result selection is not supported.",
|
|
268
|
+
);
|
|
269
|
+
this.bundleMode = true;
|
|
270
|
+
}
|
|
271
|
+
async beginOpenSession(
|
|
272
|
+
inputCid: string,
|
|
273
|
+
signal?: AbortSignal,
|
|
274
|
+
supportedTypes?: ReadonlySet<string>,
|
|
275
|
+
expectedTypes?: unknown,
|
|
276
|
+
): Promise<void> {
|
|
277
|
+
try {
|
|
278
|
+
this.selectBundleMode();
|
|
279
|
+
if (this.sessionStarted || this.submittedBundle)
|
|
280
|
+
throw new HostError(
|
|
281
|
+
"Open session has already started or submitted a result.",
|
|
282
|
+
);
|
|
283
|
+
this.sessionStarted = true;
|
|
284
|
+
this.selectingBundle = true;
|
|
285
|
+
if (expectedTypes !== undefined) {
|
|
286
|
+
if (
|
|
287
|
+
!Array.isArray(expectedTypes) ||
|
|
288
|
+
expectedTypes.length > MAX_PATHS ||
|
|
289
|
+
!expectedTypes.every(
|
|
290
|
+
(cid: unknown) =>
|
|
291
|
+
typeof cid === "string" &&
|
|
292
|
+
CID.parse(cid).toV1().toString() === cid,
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
throw new HostError(
|
|
296
|
+
"Open supportedTypes must be exact canonical CID strings.",
|
|
297
|
+
);
|
|
298
|
+
const expected = new Set<string>(expectedTypes as string[]);
|
|
299
|
+
if (expected.size !== expectedTypes.length)
|
|
300
|
+
throw new HostError(
|
|
301
|
+
"Open supportedTypes must not contain duplicate CIDs.",
|
|
302
|
+
);
|
|
303
|
+
if (
|
|
304
|
+
!supportedTypes ||
|
|
305
|
+
expected.size !== supportedTypes.size ||
|
|
306
|
+
[...expected].some((cid) => !supportedTypes.has(cid))
|
|
307
|
+
)
|
|
308
|
+
throw new HostError(
|
|
309
|
+
"Open supportedTypes must exactly match the App supportedSchemas declaration.",
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
const bundle = await validatedBundle(
|
|
313
|
+
inputCid,
|
|
314
|
+
this.store,
|
|
315
|
+
this.owner,
|
|
316
|
+
signal,
|
|
317
|
+
);
|
|
318
|
+
signal?.throwIfAborted();
|
|
319
|
+
if (supportedTypes && !supportedTypes.has(bundle.schemaKey))
|
|
320
|
+
throw new HostError(
|
|
321
|
+
"App does not declare support for the Open input's exact type CID.",
|
|
322
|
+
);
|
|
323
|
+
this.bundleSchema = bundle.schemaKey;
|
|
324
|
+
} catch (error) {
|
|
325
|
+
this.fail(error);
|
|
326
|
+
throw error;
|
|
327
|
+
} finally {
|
|
328
|
+
this.selectingBundle = false;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
cancelOpenSession(): void {
|
|
332
|
+
this.bundleMode = true;
|
|
333
|
+
this.fail(
|
|
334
|
+
new HostError(
|
|
335
|
+
"Open session cancelled without a committed Bundle result.",
|
|
336
|
+
130,
|
|
337
|
+
"CANCELLED",
|
|
338
|
+
"execution",
|
|
339
|
+
),
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
discardBundleResult(): void {
|
|
343
|
+
this.submittedBundle = undefined;
|
|
344
|
+
}
|
|
345
|
+
async submitBundleResult(root: string, signal?: AbortSignal): Promise<void> {
|
|
346
|
+
try {
|
|
347
|
+
this.selectBundleMode();
|
|
348
|
+
} catch (error) {
|
|
349
|
+
this.fail(error);
|
|
350
|
+
throw error;
|
|
351
|
+
}
|
|
352
|
+
this.selectingBundle = true;
|
|
353
|
+
try {
|
|
354
|
+
const cid = CID.parse(root).toV1().toString();
|
|
355
|
+
if (this.submittedBundle && this.submittedBundle !== cid) {
|
|
356
|
+
const error = new HostError(
|
|
357
|
+
"A different Bundle result has already been submitted.",
|
|
358
|
+
);
|
|
359
|
+
this.fail(error);
|
|
360
|
+
throw error;
|
|
361
|
+
}
|
|
362
|
+
signal?.throwIfAborted();
|
|
363
|
+
if (this.submittedBundle === cid) return;
|
|
364
|
+
const bundle = await validatedBundle(cid, this.store, this.owner, signal);
|
|
365
|
+
signal?.throwIfAborted();
|
|
366
|
+
if (this.bundleSchema && bundle.schemaKey !== this.bundleSchema)
|
|
367
|
+
throw new HostError(
|
|
368
|
+
"Bundle result must preserve the Open session's exact type CID.",
|
|
369
|
+
);
|
|
370
|
+
this.submittedBundle = cid;
|
|
371
|
+
} finally {
|
|
372
|
+
// Validation/storage failures leave the explicit session open for retry, never a Space result.
|
|
373
|
+
this.selectingBundle = false;
|
|
374
|
+
}
|
|
203
375
|
}
|
|
204
376
|
async write(
|
|
205
377
|
path: string,
|
|
@@ -235,7 +407,7 @@ export class OutputSpace {
|
|
|
235
407
|
block instanceof Uint8Array
|
|
236
408
|
? block
|
|
237
409
|
: await bytes(
|
|
238
|
-
|
|
410
|
+
NodeReadableStream.from(block),
|
|
239
411
|
this.store.limits.blockBytes,
|
|
240
412
|
);
|
|
241
413
|
await this.store.stageBlock(
|
|
@@ -312,7 +484,18 @@ export class OutputSpace {
|
|
|
312
484
|
}
|
|
313
485
|
}
|
|
314
486
|
async finalize(): Promise<string> {
|
|
487
|
+
this.signal?.throwIfAborted();
|
|
315
488
|
if (this.fault) throw this.fault;
|
|
489
|
+
if (this.bundleMode) {
|
|
490
|
+
if (this.selectingBundle || !this.submittedBundle)
|
|
491
|
+
throw new HostError(
|
|
492
|
+
"Open session ended without submitting a Bundle result.",
|
|
493
|
+
130,
|
|
494
|
+
"CANCELLED",
|
|
495
|
+
"commit",
|
|
496
|
+
);
|
|
497
|
+
return this.submittedBundle;
|
|
498
|
+
}
|
|
316
499
|
if (this.pending.size)
|
|
317
500
|
throw new HostError(
|
|
318
501
|
"Output has unfinished stream writes.",
|
package/src/state-storage.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from "node:fs";
|
|
14
14
|
import { homedir, tmpdir } from "node:os";
|
|
15
15
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
16
|
-
import { HostError } from "./
|
|
16
|
+
import { HostError } from "./host-error.js";
|
|
17
17
|
import { windowsStateAccess } from "./state-storage-windows.js";
|
|
18
18
|
|
|
19
19
|
// Written only after the existing layout/owner checks, before the first DB
|
package/src/state.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type ConfigKey,
|
|
11
11
|
type ConfigValue,
|
|
12
12
|
} from "./config.js";
|
|
13
|
-
import { HostError, object, syntax } from "./
|
|
13
|
+
import { HostError, object, syntax } from "./host-error.js";
|
|
14
14
|
import { openStateStorage } from "./state-storage.js";
|
|
15
15
|
|
|
16
16
|
export interface Association {
|
|
@@ -47,7 +47,7 @@ export interface SwarmProjection {
|
|
|
47
47
|
epoch: number;
|
|
48
48
|
membership: string;
|
|
49
49
|
}
|
|
50
|
-
const projectionVersion =
|
|
50
|
+
const projectionVersion = 2;
|
|
51
51
|
export function swarmAlias(value: string): void {
|
|
52
52
|
pinName(value);
|
|
53
53
|
}
|
|
@@ -375,7 +375,7 @@ export class State {
|
|
|
375
375
|
const entry = this.swarms().find((entry) => entry.cid === cid);
|
|
376
376
|
if (!entry)
|
|
377
377
|
throw new HostError(
|
|
378
|
-
`Swarm "${reference}" is not registered. Run ipab swarm
|
|
378
|
+
`Swarm "${reference}" is not registered. Run ipab swarm set ALIAS ${cid}.`,
|
|
379
379
|
);
|
|
380
380
|
return entry;
|
|
381
381
|
}
|
|
@@ -384,23 +384,46 @@ export class State {
|
|
|
384
384
|
const existing = this.swarms().find((entry) => entry.alias === alias);
|
|
385
385
|
if (existing && existing.cid !== cid)
|
|
386
386
|
throw new HostError(
|
|
387
|
-
`Swarm alias "${alias}" is already assigned to "${existing.cid}".
|
|
387
|
+
`Swarm alias "${alias}" is already assigned to "${existing.cid}". Choose a different alias, or explicitly retarget it with ipab swarm set ${alias} CONFIG_URL.`,
|
|
388
388
|
);
|
|
389
389
|
}
|
|
390
390
|
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
391
391
|
cid = swarmUrl(cid);
|
|
392
392
|
return this.transaction(() => {
|
|
393
393
|
if (alias !== undefined) this.checkSwarmAlias(alias, cid);
|
|
394
|
-
|
|
395
|
-
const entry = { cid, alias: alias ?? previous?.alias ?? null };
|
|
396
|
-
this.db
|
|
397
|
-
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
398
|
-
.run(cid, entry.alias);
|
|
399
|
-
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
400
|
-
this.setConfig("swarm.default", cid);
|
|
401
|
-
return entry;
|
|
394
|
+
return this.saveSwarmRegistration(cid, alias);
|
|
402
395
|
});
|
|
403
396
|
}
|
|
397
|
+
setSwarm(
|
|
398
|
+
alias: string,
|
|
399
|
+
url: string,
|
|
400
|
+
expectedTarget: string | null,
|
|
401
|
+
): RegisteredSwarm {
|
|
402
|
+
swarmAlias(alias);
|
|
403
|
+
url = swarmUrl(url);
|
|
404
|
+
return this.transaction(() => {
|
|
405
|
+
const current = this.swarms().find((entry) => entry.alias === alias);
|
|
406
|
+
if ((current?.cid ?? null) !== expectedTarget)
|
|
407
|
+
throw new HostError(
|
|
408
|
+
`Swarm alias "${alias}" changed while setting it. Run ipab swarm list and retry.`,
|
|
409
|
+
);
|
|
410
|
+
if (current && current.cid !== url)
|
|
411
|
+
this.db
|
|
412
|
+
.prepare("UPDATE swarms SET alias=NULL WHERE cid=?")
|
|
413
|
+
.run(current.cid);
|
|
414
|
+
return this.saveSwarmRegistration(url, alias);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
private saveSwarmRegistration(cid: string, alias?: string): RegisteredSwarm {
|
|
418
|
+
const previous = this.swarms().find((entry) => entry.cid === cid);
|
|
419
|
+
const entry = { cid, alias: alias ?? previous?.alias ?? null };
|
|
420
|
+
this.db
|
|
421
|
+
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
422
|
+
.run(cid, entry.alias);
|
|
423
|
+
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
424
|
+
this.setConfig("swarm.default", cid);
|
|
425
|
+
return entry;
|
|
426
|
+
}
|
|
404
427
|
useSwarm(reference: string): string {
|
|
405
428
|
return this.transaction(() => {
|
|
406
429
|
const entry = this.registeredSwarm(reference);
|
|
@@ -725,7 +748,9 @@ export class State {
|
|
|
725
748
|
(exactCid(pin.reference) !== pin.reference ||
|
|
726
749
|
(pin.cid !== null && pin.cid !== pin.reference))) ||
|
|
727
750
|
(pin.swarm !== null &&
|
|
728
|
-
(!pin.
|
|
751
|
+
(!pin.reference.startsWith("ipab://") ||
|
|
752
|
+
(pin.name === "" &&
|
|
753
|
+
!/^ipab:\/\/[^/]+\/$/u.test(pin.reference)))) ||
|
|
729
754
|
(pin.status === "ready" && (!pin.cid || !pin.targetCid))
|
|
730
755
|
)
|
|
731
756
|
throw new HostError(
|
package/src/swarm-files.ts
CHANGED
|
@@ -30,6 +30,7 @@ export async function readPrivate<T>(
|
|
|
30
30
|
directory: string,
|
|
31
31
|
name: string,
|
|
32
32
|
additionalPaths: readonly ProtectedPath[] = [],
|
|
33
|
+
maximumBytes = 2 * 1024 * 1024,
|
|
33
34
|
): Promise<T | undefined> {
|
|
34
35
|
const path = join(directory, name);
|
|
35
36
|
if (additionalPaths.length) {
|
|
@@ -48,7 +49,7 @@ export async function readPrivate<T>(
|
|
|
48
49
|
throw cause;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
if ((await lstat(path)).size >
|
|
52
|
+
if ((await lstat(path)).size > maximumBytes)
|
|
52
53
|
throw new HostError("Protected state is oversized.");
|
|
53
54
|
return JSON.parse(await readFile(path, "utf8")) as T;
|
|
54
55
|
}
|
|
@@ -77,6 +78,7 @@ export async function withPrivateLock<T>(
|
|
|
77
78
|
directory: string,
|
|
78
79
|
operation: () => Promise<T>,
|
|
79
80
|
signal?: AbortSignal,
|
|
81
|
+
attempts = 300,
|
|
80
82
|
): Promise<T> {
|
|
81
83
|
signal?.throwIfAborted();
|
|
82
84
|
const path = join(directory, "operation.sqlite");
|
|
@@ -92,7 +94,7 @@ export async function withPrivateLock<T>(
|
|
|
92
94
|
let locked = false;
|
|
93
95
|
try {
|
|
94
96
|
db.exec("PRAGMA busy_timeout=0");
|
|
95
|
-
for (let attempt = 0; attempt <
|
|
97
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
96
98
|
signal?.throwIfAborted();
|
|
97
99
|
try {
|
|
98
100
|
db.exec("BEGIN IMMEDIATE");
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { encodeSchema, type BundleType } from "@ipab/bundle";
|
|
2
|
+
import { HostError, object } from "./errors.js";
|
|
3
|
+
import { relativePath } from "./space.js";
|
|
4
|
+
import {
|
|
5
|
+
swarmStateSchema,
|
|
6
|
+
previousSwarmStateSchema,
|
|
7
|
+
legacySwarmStateSchema,
|
|
8
|
+
} from "@ipab/bundle/swarm-state";
|
|
9
|
+
export {
|
|
10
|
+
swarmStateType,
|
|
11
|
+
swarmStateSchema,
|
|
12
|
+
previousSwarmStateType,
|
|
13
|
+
previousSwarmStateSchema,
|
|
14
|
+
legacySwarmStateType,
|
|
15
|
+
legacySwarmStateSchema,
|
|
16
|
+
parseSwarmState,
|
|
17
|
+
} from "@ipab/bundle/swarm-state";
|
|
18
|
+
|
|
19
|
+
export const legacySwarmMetaType: BundleType = {
|
|
20
|
+
contentType: "application/json",
|
|
21
|
+
schema: {
|
|
22
|
+
type: "object",
|
|
23
|
+
required: ["templatePath", "registryPath"],
|
|
24
|
+
properties: {
|
|
25
|
+
templatePath: { type: "string", minLength: 1, maxLength: 196 },
|
|
26
|
+
registryPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
27
|
+
},
|
|
28
|
+
additionalProperties: false,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
export const swarmMetaType: BundleType = {
|
|
32
|
+
contentType: "application/json",
|
|
33
|
+
schema: {
|
|
34
|
+
type: "object",
|
|
35
|
+
additionalProperties: false,
|
|
36
|
+
required: ["templatePath", "registryPath", "skillPath"],
|
|
37
|
+
properties: {
|
|
38
|
+
templatePath: { type: "string", minLength: 1, maxLength: 196 },
|
|
39
|
+
registryPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
40
|
+
skillPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
export const swarmMetaSchema = await encodeSchema(swarmMetaType);
|
|
45
|
+
export const legacySwarmMetaSchema = await encodeSchema(legacySwarmMetaType);
|
|
46
|
+
export interface SwarmMeta {
|
|
47
|
+
templatePath: string;
|
|
48
|
+
registryPath: string;
|
|
49
|
+
skillPath?: string;
|
|
50
|
+
}
|
|
51
|
+
export function isSwarmStateSchema(cid: string): boolean {
|
|
52
|
+
return (
|
|
53
|
+
cid === swarmStateSchema.cid.toString() ||
|
|
54
|
+
cid === previousSwarmStateSchema.cid.toString() ||
|
|
55
|
+
cid === legacySwarmStateSchema.cid.toString()
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
export function parseSwarmMeta(value: unknown): SwarmMeta {
|
|
59
|
+
const data = object(value, "Swarm Meta");
|
|
60
|
+
if (
|
|
61
|
+
![
|
|
62
|
+
"registryPath,templatePath",
|
|
63
|
+
"registryPath,skillPath,templatePath",
|
|
64
|
+
].includes(Object.keys(data).sort().join()) ||
|
|
65
|
+
typeof data.templatePath !== "string" ||
|
|
66
|
+
typeof data.registryPath !== "string" ||
|
|
67
|
+
("skillPath" in data && typeof data.skillPath !== "string")
|
|
68
|
+
)
|
|
69
|
+
throw new HostError(
|
|
70
|
+
"Swarm Meta requires templatePath, registryPath and (for the new exact type) skillPath.",
|
|
71
|
+
);
|
|
72
|
+
for (const path of [
|
|
73
|
+
data.templatePath,
|
|
74
|
+
data.registryPath,
|
|
75
|
+
...(typeof data.skillPath === "string" ? [data.skillPath] : []),
|
|
76
|
+
]) {
|
|
77
|
+
relativePath(path);
|
|
78
|
+
if (path.length > 196 || path.includes(":"))
|
|
79
|
+
throw new HostError(
|
|
80
|
+
"Swarm Meta paths must be current-Swarm ordinary Name prefixes of at most 196 characters, not URIs.",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
templatePath: data.templatePath,
|
|
85
|
+
registryPath: data.registryPath,
|
|
86
|
+
...(typeof data.skillPath === "string"
|
|
87
|
+
? { skillPath: data.skillPath }
|
|
88
|
+
: {}),
|
|
89
|
+
};
|
|
90
|
+
}
|