@ipab/cli 0.1.0-rc.1 → 0.1.0-rc.11
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 +234 -9
- package/dist/app-authoring.d.ts +15 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +144 -58
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.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 +8 -46
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts +7 -0
- package/dist/assembly.d.ts.map +1 -0
- package/dist/assembly.js +130 -0
- package/dist/assembly.js.map +1 -0
- package/dist/authoring.d.ts +7 -8
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +59 -16
- package/dist/authoring.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 -23
- package/dist/builtins.js.map +1 -1
- package/dist/bundled-skills.d.ts +14 -0
- package/dist/bundled-skills.d.ts.map +1 -0
- package/dist/bundled-skills.js +34 -0
- package/dist/bundled-skills.js.map +1 -0
- package/dist/cli.d.ts +8 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1967 -258
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +3 -0
- package/dist/config-secret.d.ts.map +1 -0
- package/dist/config-secret.js +12 -0
- package/dist/config-secret.js.map +1 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +166 -8
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +32 -0
- package/dist/content-provider.d.ts.map +1 -0
- package/dist/content-provider.js +222 -0
- package/dist/content-provider.js.map +1 -0
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +26 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +54 -10
- package/dist/context.js.map +1 -1
- package/dist/daemon-ui.d.ts +93 -0
- package/dist/daemon-ui.d.ts.map +1 -0
- package/dist/daemon-ui.js +713 -0
- package/dist/daemon-ui.js.map +1 -0
- package/dist/daemon.d.ts +21 -0
- package/dist/daemon.d.ts.map +1 -0
- package/dist/daemon.js +317 -0
- package/dist/daemon.js.map +1 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +688 -0
- package/dist/doctor.js.map +1 -0
- 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/get.d.ts +3 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +80 -0
- package/dist/get.js.map +1 -0
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +6 -14
- package/dist/history.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/identity.d.ts +16 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +119 -0
- package/dist/identity.js.map +1 -0
- package/dist/ipc.d.ts +5 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +124 -12
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +119 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +113 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +85 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +755 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +16 -18
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/pivot-rpc.d.ts +62 -0
- package/dist/pivot-rpc.d.ts.map +1 -0
- package/dist/pivot-rpc.js +407 -0
- package/dist/pivot-rpc.js.map +1 -0
- package/dist/pivot-service.d.ts +16 -0
- package/dist/pivot-service.d.ts.map +1 -0
- package/dist/pivot-service.js +50 -0
- package/dist/pivot-service.js.map +1 -0
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +9 -4
- package/dist/runtime.js.map +1 -1
- package/dist/search-index.d.ts +64 -0
- package/dist/search-index.d.ts.map +1 -0
- package/dist/search-index.js +409 -0
- package/dist/search-index.js.map +1 -0
- package/dist/search-service.d.ts +23 -0
- package/dist/search-service.d.ts.map +1 -0
- package/dist/search-service.js +116 -0
- package/dist/search-service.js.map +1 -0
- 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 +9 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +24 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +104 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +894 -0
- package/dist/skills.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +17 -12
- package/dist/source-bundle.js.map +1 -1
- 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 +170 -24
- package/dist/space.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +333 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +135 -13
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +577 -92
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +57 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +299 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +6 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +116 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm-meta.d.ts +15 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +68 -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 +271 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +537 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +4991 -0
- package/dist/swarm.js.map +1 -0
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.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 +4 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +11 -4
- 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/dist/unixfs-tree.d.ts +67 -0
- package/dist/unixfs-tree.d.ts.map +1 -0
- package/dist/unixfs-tree.js +329 -0
- package/dist/unixfs-tree.js.map +1 -0
- package/package.json +14 -7
- package/src/app-authoring.ts +240 -76
- package/src/app-definition.ts +33 -39
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +17 -73
- package/src/assembly.ts +185 -0
- package/src/authoring.ts +99 -18
- package/src/builtins.ts +2 -27
- package/src/bundled-skills.ts +60 -0
- package/src/cli.ts +2461 -323
- package/src/config-secret.ts +14 -0
- package/src/config.ts +214 -8
- package/src/content-provider.ts +299 -0
- package/src/context-types.ts +11 -0
- package/src/context.ts +98 -10
- package/src/daemon-ui.ts +812 -0
- package/src/daemon.ts +386 -0
- package/src/doctor.ts +1092 -0
- package/src/errors.ts +1 -25
- package/src/get.ts +91 -0
- package/src/history.ts +5 -15
- package/src/host-error.ts +26 -0
- package/src/identity.ts +175 -0
- package/src/ipc.ts +160 -11
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +144 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo-topology.ts +148 -0
- package/src/kubo.ts +1088 -0
- package/src/pins.ts +188 -126
- package/src/pivot-rpc.ts +601 -0
- package/src/pivot-service.ts +87 -0
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +16 -4
- package/src/search-index.ts +560 -0
- package/src/search-service.ts +152 -0
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +29 -0
- package/src/skills.ts +1236 -0
- package/src/source-bundle.ts +26 -13
- package/src/space-path.ts +30 -0
- package/src/space.ts +230 -42
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +378 -0
- package/src/state.ts +875 -108
- package/src/swarm-envelope.ts +461 -0
- package/src/swarm-files.ts +126 -0
- package/src/swarm-meta.ts +98 -0
- package/src/swarm-observations.ts +393 -0
- package/src/swarm.ts +7392 -0
- package/src/template-definition.ts +36 -41
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +19 -5
- package/src/templates.ts +77 -0
- package/src/unixfs-tree.ts +401 -0
package/src/errors.ts
CHANGED
|
@@ -1,23 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
constructor(
|
|
3
|
-
message: string,
|
|
4
|
-
readonly exitCode = 1,
|
|
5
|
-
readonly code = "OPERATION_FAILED",
|
|
6
|
-
readonly phase = "state",
|
|
7
|
-
) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "HostError";
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function syntax(message: string): never {
|
|
14
|
-
throw new HostError(
|
|
15
|
-
`${message} Run ipab --help.`,
|
|
16
|
-
2,
|
|
17
|
-
"INVALID_ARGUMENT",
|
|
18
|
-
"arguments",
|
|
19
|
-
);
|
|
20
|
-
}
|
|
1
|
+
export { HostError, object, syntax } from "./host-error.js";
|
|
21
2
|
|
|
22
3
|
export function message(error: unknown): string {
|
|
23
4
|
let text =
|
|
@@ -38,9 +19,4 @@ export function message(error: unknown): string {
|
|
|
38
19
|
});
|
|
39
20
|
}
|
|
40
21
|
|
|
41
|
-
export function object(value: unknown, label: string): Record<string, unknown> {
|
|
42
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
43
|
-
throw new HostError(`Invalid ${label}.`);
|
|
44
|
-
return value as Record<string, unknown>;
|
|
45
|
-
}
|
|
46
22
|
import { ContentBundleError } from "@ipab/bundle";
|
package/src/get.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import { lstat, mkdtemp, realpath, rmdir } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { openBundle, bundleLimits } from "@ipab/bundle";
|
|
5
|
+
import { CID } from "multiformats/cid";
|
|
6
|
+
import type { IpfsStore } from "@ipab/ipfs-store";
|
|
7
|
+
import { scaffoldFiles, nodeError } from "./authoring.js";
|
|
8
|
+
import { relativePath } from "./space.js";
|
|
9
|
+
import { HostError } from "./errors.js";
|
|
10
|
+
|
|
11
|
+
export async function getBundle(
|
|
12
|
+
store: IpfsStore,
|
|
13
|
+
cid: string,
|
|
14
|
+
output?: string,
|
|
15
|
+
): Promise<string> {
|
|
16
|
+
await openBundle(CID.parse(cid), {
|
|
17
|
+
resolveCid: (cid) => store.block.get(cid.toString()),
|
|
18
|
+
});
|
|
19
|
+
const files = new Map<string, Uint8Array>();
|
|
20
|
+
let total = 0;
|
|
21
|
+
const visit = async (
|
|
22
|
+
root: CID,
|
|
23
|
+
path: string,
|
|
24
|
+
depth: number,
|
|
25
|
+
): Promise<void> => {
|
|
26
|
+
if (depth > bundleLimits.depth || files.size > bundleLimits.files)
|
|
27
|
+
throw new HostError("Download tree exceeds Bundle limits.");
|
|
28
|
+
const node = await store.read(root);
|
|
29
|
+
if (node.type === "directory") {
|
|
30
|
+
for await (const item of node.entries()) {
|
|
31
|
+
const target = path ? `${path}/${item.name}` : item.name;
|
|
32
|
+
relativePath(target);
|
|
33
|
+
await visit(item.cid, target, depth + 1);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
const chunks: Uint8Array[] = [];
|
|
37
|
+
let size = 0;
|
|
38
|
+
for await (const chunk of node.content()) {
|
|
39
|
+
total += chunk.length;
|
|
40
|
+
size += chunk.length;
|
|
41
|
+
if (total > bundleLimits.bytes)
|
|
42
|
+
throw new HostError("Download exceeds byte budget.");
|
|
43
|
+
chunks.push(chunk);
|
|
44
|
+
}
|
|
45
|
+
const value = new Uint8Array(size);
|
|
46
|
+
let offset = 0;
|
|
47
|
+
for (const chunk of chunks) {
|
|
48
|
+
value.set(chunk, offset);
|
|
49
|
+
offset += chunk.length;
|
|
50
|
+
}
|
|
51
|
+
files.set(path, value);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
await visit(CID.parse(cid), "", 0);
|
|
55
|
+
const directory =
|
|
56
|
+
output === undefined
|
|
57
|
+
? await mkdtemp(join(await realpath(tmpdir()), "ipab-get-"))
|
|
58
|
+
: resolve(output);
|
|
59
|
+
try {
|
|
60
|
+
for (let current = directory; ; current = dirname(current)) {
|
|
61
|
+
try {
|
|
62
|
+
const stat = await lstat(current);
|
|
63
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
64
|
+
throw new HostError(
|
|
65
|
+
"Download destination must not cross a symlink or file.",
|
|
66
|
+
);
|
|
67
|
+
} catch (cause) {
|
|
68
|
+
if (!nodeError(cause, "ENOENT") && !nodeError(cause, "ENOTDIR"))
|
|
69
|
+
throw cause;
|
|
70
|
+
}
|
|
71
|
+
if (dirname(current) === current) break;
|
|
72
|
+
}
|
|
73
|
+
return await scaffoldFiles(directory, files);
|
|
74
|
+
} catch (cause) {
|
|
75
|
+
const failures: unknown[] = [cause];
|
|
76
|
+
if (output === undefined) {
|
|
77
|
+
try {
|
|
78
|
+
await rmdir(directory);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
failures.push(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (failures.length > 1)
|
|
84
|
+
throw new AggregateError(
|
|
85
|
+
failures,
|
|
86
|
+
"Download failed and temporary directory cleanup also failed.",
|
|
87
|
+
{ cause },
|
|
88
|
+
);
|
|
89
|
+
throw cause;
|
|
90
|
+
}
|
|
91
|
+
}
|
package/src/history.ts
CHANGED
|
@@ -183,21 +183,11 @@ export class Ownership {
|
|
|
183
183
|
recordId: null,
|
|
184
184
|
};
|
|
185
185
|
state.saveHold(hold);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
} catch (error) {
|
|
192
|
-
if (
|
|
193
|
-
(error as { code?: unknown }).code !== "retention_unfinished" ||
|
|
194
|
-
Date.now() >= deadline
|
|
195
|
-
) {
|
|
196
|
-
state.saveHold({ ...hold, pid: null });
|
|
197
|
-
throw error;
|
|
198
|
-
}
|
|
199
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
200
|
-
}
|
|
186
|
+
try {
|
|
187
|
+
await store.registerOwnerAsync(owner, []);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
state.saveHold({ ...hold, pid: null });
|
|
190
|
+
throw error;
|
|
201
191
|
}
|
|
202
192
|
return new Ownership(state, store, owner);
|
|
203
193
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class HostError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
message: string,
|
|
4
|
+
readonly exitCode = 1,
|
|
5
|
+
readonly code = "OPERATION_FAILED",
|
|
6
|
+
readonly phase = "state",
|
|
7
|
+
) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "HostError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function syntax(message: string): never {
|
|
14
|
+
throw new HostError(
|
|
15
|
+
`${message} Run ipab --help.`,
|
|
16
|
+
2,
|
|
17
|
+
"INVALID_ARGUMENT",
|
|
18
|
+
"arguments",
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function object(value: unknown, label: string): Record<string, unknown> {
|
|
23
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
24
|
+
throw new HostError(`Invalid ${label}.`);
|
|
25
|
+
return value as Record<string, unknown>;
|
|
26
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { lstat, mkdir, open, readFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
protectAccountOnly,
|
|
6
|
+
requireAccountOnlyMany,
|
|
7
|
+
requireAccountOnlyDirectory,
|
|
8
|
+
} from "@ipab/bundle/pointer/protected-files";
|
|
9
|
+
import {
|
|
10
|
+
generateKeyPair,
|
|
11
|
+
importPrivateKeyPkcs8,
|
|
12
|
+
importPublicKeyRaw,
|
|
13
|
+
signEd25519,
|
|
14
|
+
verifyEd25519,
|
|
15
|
+
} from "@ipab/bundle/pointer/crypto";
|
|
16
|
+
import { HostError, object } from "./errors.js";
|
|
17
|
+
import { nodeError } from "./authoring.js";
|
|
18
|
+
|
|
19
|
+
export interface Identity {
|
|
20
|
+
publicKey: string;
|
|
21
|
+
privateKey: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function protectedPath(
|
|
25
|
+
path: string,
|
|
26
|
+
directory = false,
|
|
27
|
+
): Promise<void> {
|
|
28
|
+
await regularPath(path, directory);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ProtectedPath {
|
|
32
|
+
path: string;
|
|
33
|
+
directory?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function protectedPaths(
|
|
37
|
+
paths: readonly ProtectedPath[],
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
for (const entry of paths)
|
|
40
|
+
await regularPath(entry.path, entry.directory ?? false);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function regularPath(path: string, directory: boolean): Promise<void> {
|
|
44
|
+
const stat = await lstat(path);
|
|
45
|
+
if (
|
|
46
|
+
stat.isSymbolicLink() ||
|
|
47
|
+
(directory ? !stat.isDirectory() : !stat.isFile())
|
|
48
|
+
)
|
|
49
|
+
throw new HostError(
|
|
50
|
+
"Credential path is not a regular account-owned file/directory.",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function secureNew(
|
|
55
|
+
path: string,
|
|
56
|
+
directory = false,
|
|
57
|
+
options: { inherit?: boolean } = {},
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
await regularPath(path, directory);
|
|
60
|
+
if (options.inherit && !directory && process.platform === "win32") {
|
|
61
|
+
await inheritingParent(dirname(resolve(path)));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await protectAccountOnly(path);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const privateParents = new Map<
|
|
68
|
+
string,
|
|
69
|
+
{ identity: string; checked: Promise<void> }
|
|
70
|
+
>();
|
|
71
|
+
async function inheritingParent(path: string): Promise<void> {
|
|
72
|
+
const stat = await lstat(path);
|
|
73
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
74
|
+
throw new HostError("Secret parent must be a regular directory.");
|
|
75
|
+
const identity = `${stat.dev}:${stat.ino}:${stat.birthtimeMs}`;
|
|
76
|
+
let saved = privateParents.get(path);
|
|
77
|
+
if (!saved || saved.identity !== identity) {
|
|
78
|
+
saved = { identity, checked: requireAccountOnlyDirectory(path) };
|
|
79
|
+
privateParents.set(path, saved);
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
// Trust a verified directory for this process, not a path replaced later.
|
|
83
|
+
await saved.checked;
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
if (privateParents.get(path) === saved) privateParents.delete(path);
|
|
86
|
+
throw cause;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function readIdentityFile(path: string): Promise<Identity> {
|
|
91
|
+
await protectedPath(path);
|
|
92
|
+
return decodeIdentityFile(path);
|
|
93
|
+
}
|
|
94
|
+
async function decodeIdentityFile(path: string): Promise<Identity> {
|
|
95
|
+
const stat = await lstat(path);
|
|
96
|
+
if (stat.size > 16 * 1024)
|
|
97
|
+
throw new HostError(
|
|
98
|
+
"Invalid credential encoding. Restore the existing protected key file.",
|
|
99
|
+
);
|
|
100
|
+
const text = await readFile(path, "utf8");
|
|
101
|
+
let value: Record<string, unknown>;
|
|
102
|
+
try {
|
|
103
|
+
value = object(JSON.parse(text) as unknown, "identity");
|
|
104
|
+
} catch {
|
|
105
|
+
throw new HostError(
|
|
106
|
+
"Invalid credential encoding. Restore the existing protected key file.",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
typeof value.publicKey !== "string" ||
|
|
111
|
+
typeof value.privateKey !== "string"
|
|
112
|
+
)
|
|
113
|
+
throw new HostError(
|
|
114
|
+
"Invalid identity. Restore the existing key; do not initialize a replacement.",
|
|
115
|
+
);
|
|
116
|
+
const key = await importPrivateKeyPkcs8(value.privateKey);
|
|
117
|
+
const proof = await signEd25519(key, "IPAB identity consistency");
|
|
118
|
+
if (
|
|
119
|
+
!(await verifyEd25519(
|
|
120
|
+
await importPublicKeyRaw(value.publicKey),
|
|
121
|
+
proof,
|
|
122
|
+
"IPAB identity consistency",
|
|
123
|
+
))
|
|
124
|
+
)
|
|
125
|
+
throw new HostError("Identity public and private keys do not match.");
|
|
126
|
+
return { publicKey: value.publicKey, privateKey: value.privateKey };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function accountIdentity(
|
|
130
|
+
env: NodeJS.ProcessEnv,
|
|
131
|
+
initialize = false,
|
|
132
|
+
verifyPermissions = false,
|
|
133
|
+
): Promise<Identity> {
|
|
134
|
+
const home = resolve(env.HOME ?? env.USERPROFILE ?? homedir());
|
|
135
|
+
const parent = join(home, ".ipab");
|
|
136
|
+
const directory = join(parent, "identity");
|
|
137
|
+
try {
|
|
138
|
+
await lstat(directory);
|
|
139
|
+
} catch (cause) {
|
|
140
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
141
|
+
if (!initialize)
|
|
142
|
+
throw new HostError(
|
|
143
|
+
"No account identity. Start with swarm create or swarm request.",
|
|
144
|
+
);
|
|
145
|
+
const created = await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
146
|
+
if (created) await secureNew(parent, true);
|
|
147
|
+
else await protectedPath(parent, true);
|
|
148
|
+
if ((await lstat(parent)).isSymbolicLink())
|
|
149
|
+
throw new HostError("Identity parent must not be a symbolic link.");
|
|
150
|
+
await mkdir(directory, { mode: 0o700 });
|
|
151
|
+
await secureNew(directory, true);
|
|
152
|
+
const pair = await generateKeyPair();
|
|
153
|
+
const file = join(directory, "key.json");
|
|
154
|
+
const handle = await open(file, "wx", 0o600);
|
|
155
|
+
try {
|
|
156
|
+
await handle.writeFile(
|
|
157
|
+
JSON.stringify({
|
|
158
|
+
publicKey: pair.publicRaw,
|
|
159
|
+
privateKey: pair.privatePkcs8,
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
await handle.sync();
|
|
163
|
+
} finally {
|
|
164
|
+
await handle.close();
|
|
165
|
+
}
|
|
166
|
+
await secureNew(file);
|
|
167
|
+
}
|
|
168
|
+
const file = join(directory, "key.json");
|
|
169
|
+
await regularPath(parent, true);
|
|
170
|
+
await regularPath(directory, true);
|
|
171
|
+
await regularPath(file, false);
|
|
172
|
+
if (verifyPermissions)
|
|
173
|
+
await requireAccountOnlyMany([parent, directory, file]);
|
|
174
|
+
return decodeIdentityFile(file);
|
|
175
|
+
}
|
package/src/ipc.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { ChildProcess } from "node:child_process";
|
|
2
2
|
import type { IpfsStore } from "@ipab/ipfs-store";
|
|
3
|
+
import { openBundle } from "@ipab/bundle";
|
|
4
|
+
import { isSwarmRootSchema } from "./swarm-meta.js";
|
|
3
5
|
import { CID } from "multiformats/cid";
|
|
4
6
|
import { HostError, message, object } from "./errors.js";
|
|
5
|
-
import { requestFrame, type Reply } from "./context.js";
|
|
6
|
-
import {
|
|
7
|
+
import { requestFrame, type BundleInspection, type Reply } from "./context.js";
|
|
8
|
+
import {
|
|
9
|
+
CHUNK_BYTES,
|
|
10
|
+
deriveBundle,
|
|
11
|
+
type OutputSpace,
|
|
12
|
+
relativePath,
|
|
13
|
+
} from "./space.js";
|
|
7
14
|
import type { Ownership } from "./history.js";
|
|
8
15
|
|
|
9
16
|
class ByteQueue implements AsyncIterable<Uint8Array> {
|
|
@@ -65,6 +72,7 @@ export class ContextServer {
|
|
|
65
72
|
private readonly reads = new Map<number, ReadStream>();
|
|
66
73
|
private readonly writes = new Map<number, WriteStream>();
|
|
67
74
|
private readonly tasks = new Set<Promise<void>>();
|
|
75
|
+
private readonly shallowRoots = new Set<string>();
|
|
68
76
|
private openingReads = 0;
|
|
69
77
|
private sequence = 0;
|
|
70
78
|
private closed = false;
|
|
@@ -75,9 +83,12 @@ export class ContextServer {
|
|
|
75
83
|
readonly output: OutputSpace,
|
|
76
84
|
readonly fatal: (error: Error) => void,
|
|
77
85
|
readonly appCid: string,
|
|
78
|
-
readonly
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
readonly resolveBundleReference?: (
|
|
87
|
+
reference: string,
|
|
88
|
+
signal: AbortSignal,
|
|
89
|
+
swarm?: string,
|
|
90
|
+
) => Promise<Pick<BundleInspection, "cid" | "targetCid" | "swarm">>,
|
|
91
|
+
readonly supportedTypes?: ReadonlySet<string>,
|
|
81
92
|
) {
|
|
82
93
|
child.on("message", this.receive);
|
|
83
94
|
}
|
|
@@ -119,8 +130,14 @@ export class ContextServer {
|
|
|
119
130
|
if (controller.signal.aborted) {
|
|
120
131
|
if (frame.operation === "read")
|
|
121
132
|
await this.cancelRead(Number(result), controller.signal.reason);
|
|
122
|
-
if (
|
|
133
|
+
if (
|
|
134
|
+
["link", "beginOpenSession", "cancelOpenSession"].includes(
|
|
135
|
+
frame.operation,
|
|
136
|
+
)
|
|
137
|
+
)
|
|
123
138
|
this.output.fail(controller.signal.reason);
|
|
139
|
+
if (frame.operation === "submitBundleResult")
|
|
140
|
+
this.output.discardBundleResult();
|
|
124
141
|
controller.signal.throwIfAborted();
|
|
125
142
|
}
|
|
126
143
|
return result;
|
|
@@ -156,10 +173,127 @@ export class ContextServer {
|
|
|
156
173
|
): Promise<unknown> {
|
|
157
174
|
signal.throwIfAborted();
|
|
158
175
|
if (operation === "identity") return this.appCid;
|
|
159
|
-
if (operation === "
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
176
|
+
if (operation === "readBlock") {
|
|
177
|
+
const data = object(args, "block read request");
|
|
178
|
+
if (
|
|
179
|
+
typeof data.cid !== "string" ||
|
|
180
|
+
typeof data.maxBytes !== "number" ||
|
|
181
|
+
!Number.isSafeInteger(data.maxBytes) ||
|
|
182
|
+
data.maxBytes < 1 ||
|
|
183
|
+
data.maxBytes > 65536
|
|
184
|
+
)
|
|
185
|
+
throw new HostError("Block read limit must be 1..65536 bytes.");
|
|
186
|
+
const cid = CID.parse(data.cid);
|
|
187
|
+
if (cid.toV1().toString() !== data.cid)
|
|
188
|
+
throw new HostError("Block reads require an exact canonical CID.");
|
|
189
|
+
const bytes = await this.store.block.get(data.cid, { signal });
|
|
190
|
+
signal.throwIfAborted();
|
|
191
|
+
if (bytes.byteLength > data.maxBytes)
|
|
192
|
+
throw new HostError("Block exceeds the requested read limit.");
|
|
193
|
+
return bytes;
|
|
194
|
+
}
|
|
195
|
+
if (operation === "deriveBundle") {
|
|
196
|
+
const data = object(args, "Bundle derivation request");
|
|
197
|
+
if (typeof data.inputCid !== "string")
|
|
198
|
+
throw new HostError("Bundle derivation requires an exact input CID.");
|
|
199
|
+
return deriveBundle(
|
|
200
|
+
data.inputCid,
|
|
201
|
+
data.replacements,
|
|
202
|
+
this.store,
|
|
203
|
+
this.owner,
|
|
204
|
+
signal,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (
|
|
208
|
+
["beginOpenSession", "submitBundleResult", "cancelOpenSession"].includes(
|
|
209
|
+
operation,
|
|
210
|
+
)
|
|
211
|
+
) {
|
|
212
|
+
let validSubmission = false;
|
|
213
|
+
try {
|
|
214
|
+
if (operation === "cancelOpenSession") this.output.cancelOpenSession();
|
|
215
|
+
else {
|
|
216
|
+
const data = object(args, "Bundle result request");
|
|
217
|
+
const cid =
|
|
218
|
+
operation === "beginOpenSession" ? data.inputCid : data.cid;
|
|
219
|
+
if (typeof cid !== "string") {
|
|
220
|
+
const error = new HostError("Bundle result requires an exact CID.");
|
|
221
|
+
this.output.fail(error);
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
if (operation === "beginOpenSession")
|
|
225
|
+
await this.output.beginOpenSession(
|
|
226
|
+
cid,
|
|
227
|
+
signal,
|
|
228
|
+
this.supportedTypes,
|
|
229
|
+
data.supportedTypes,
|
|
230
|
+
);
|
|
231
|
+
else {
|
|
232
|
+
validSubmission = true;
|
|
233
|
+
await this.output.submitBundleResult(cid, signal);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
signal.throwIfAborted();
|
|
237
|
+
return null;
|
|
238
|
+
} catch (error) {
|
|
239
|
+
if (!validSubmission) this.output.fail(error);
|
|
240
|
+
else if (signal.aborted) this.output.discardBundleResult();
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (operation === "inspectBundle") {
|
|
245
|
+
const request = object(args, "Bundle inspection request");
|
|
246
|
+
if (
|
|
247
|
+
typeof request.reference !== "string" ||
|
|
248
|
+
!request.reference ||
|
|
249
|
+
request.reference.length > 2048
|
|
250
|
+
)
|
|
251
|
+
throw new HostError(
|
|
252
|
+
"Bundle reference must contain 1..2048 characters.",
|
|
253
|
+
);
|
|
254
|
+
if (
|
|
255
|
+
request.swarm !== undefined &&
|
|
256
|
+
(typeof request.swarm !== "string" || request.swarm.length > 2048)
|
|
257
|
+
)
|
|
258
|
+
throw new HostError("Invalid Bundle inspection Swarm.");
|
|
259
|
+
if (!this.resolveBundleReference)
|
|
260
|
+
throw new HostError(
|
|
261
|
+
"Bundle inspection is unavailable in this context.",
|
|
262
|
+
);
|
|
263
|
+
const resolved = await this.resolveBundleReference(
|
|
264
|
+
request.reference,
|
|
265
|
+
signal,
|
|
266
|
+
request.swarm,
|
|
267
|
+
);
|
|
268
|
+
const shallow = await openBundle(CID.parse(resolved.cid), {
|
|
269
|
+
signal,
|
|
270
|
+
shallow: true,
|
|
271
|
+
resolveCid: (cid, options) =>
|
|
272
|
+
this.store.block.get(cid.toString(), options),
|
|
273
|
+
});
|
|
274
|
+
let bundle = shallow;
|
|
275
|
+
if (isSwarmRootSchema(shallow.schemaKey)) {
|
|
276
|
+
this.shallowRoots.add(resolved.cid);
|
|
277
|
+
} else {
|
|
278
|
+
await this.owner.retain([resolved.targetCid, resolved.cid], signal);
|
|
279
|
+
bundle = await openBundle(CID.parse(resolved.cid), {
|
|
280
|
+
signal,
|
|
281
|
+
resolveCid: (cid, options) =>
|
|
282
|
+
this.store.block.get(cid.toString(), options),
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
const result: BundleInspection = {
|
|
286
|
+
...resolved,
|
|
287
|
+
title: bundle.manifest.title,
|
|
288
|
+
description: bundle.manifest.description,
|
|
289
|
+
entry: bundle.manifest.entry,
|
|
290
|
+
contentType: bundle.schema.type.contentType,
|
|
291
|
+
schemaCid: bundle.schemaKey,
|
|
292
|
+
files: [...bundle.files],
|
|
293
|
+
};
|
|
294
|
+
if (Buffer.byteLength(JSON.stringify(result)) > 1024 * 1024)
|
|
295
|
+
throw new HostError("Bundle inspection exceeds 1 MiB.");
|
|
296
|
+
return result;
|
|
163
297
|
}
|
|
164
298
|
if (operation === "read") {
|
|
165
299
|
if (this.reads.size + this.writes.size + this.openingReads >= 32)
|
|
@@ -171,7 +305,22 @@ export class ContextServer {
|
|
|
171
305
|
relativePath(data.path);
|
|
172
306
|
this.openingReads++;
|
|
173
307
|
try {
|
|
174
|
-
|
|
308
|
+
if (!this.shallowRoots.has(root)) {
|
|
309
|
+
let isV7Root = false;
|
|
310
|
+
try {
|
|
311
|
+
const probe = await openBundle(CID.parse(root), {
|
|
312
|
+
signal,
|
|
313
|
+
shallow: true,
|
|
314
|
+
resolveCid: (cid, options) =>
|
|
315
|
+
this.store.block.get(cid.toString(), options),
|
|
316
|
+
});
|
|
317
|
+
isV7Root = isSwarmRootSchema(probe.schemaKey);
|
|
318
|
+
} catch {
|
|
319
|
+
isV7Root = false;
|
|
320
|
+
}
|
|
321
|
+
if (isV7Root) this.shallowRoots.add(root);
|
|
322
|
+
else await this.owner.retain([root], signal);
|
|
323
|
+
}
|
|
175
324
|
signal.throwIfAborted();
|
|
176
325
|
const directory = await this.store.read(root, { signal });
|
|
177
326
|
signal.throwIfAborted();
|