@ipab/cli 0.1.0-rc.0 → 0.1.0-rc.10

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.
Files changed (285) hide show
  1. package/README.md +230 -7
  2. package/dist/app-authoring.d.ts +16 -2
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +151 -66
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/app-definition.d.ts +52 -63
  7. package/dist/app-definition.d.ts.map +1 -1
  8. package/dist/app-definition.js +33 -44
  9. package/dist/app-definition.js.map +1 -1
  10. package/dist/app-prebuild.d.ts +18 -0
  11. package/dist/app-prebuild.d.ts.map +1 -1
  12. package/dist/app-prebuild.js +46 -0
  13. package/dist/app-prebuild.js.map +1 -1
  14. package/dist/app-templates.d.ts +0 -1
  15. package/dist/app-templates.d.ts.map +1 -1
  16. package/dist/app-templates.js +15 -45
  17. package/dist/app-templates.js.map +1 -1
  18. package/dist/assembly.d.ts +7 -0
  19. package/dist/assembly.d.ts.map +1 -0
  20. package/dist/assembly.js +130 -0
  21. package/dist/assembly.js.map +1 -0
  22. package/dist/authoring.d.ts +8 -21
  23. package/dist/authoring.d.ts.map +1 -1
  24. package/dist/authoring.js +35 -54
  25. package/dist/authoring.js.map +1 -1
  26. package/dist/builtins/skill.ipab.car +0 -0
  27. package/dist/builtins/template.bundle-viewer.car +0 -0
  28. package/dist/builtins/template.check.car +0 -0
  29. package/dist/builtins/template.node.car +0 -0
  30. package/dist/builtins/template.web-app.car +0 -0
  31. package/dist/builtins.d.ts.map +1 -1
  32. package/dist/builtins.js +5 -32
  33. package/dist/builtins.js.map +1 -1
  34. package/dist/bundled-skills.d.ts +14 -0
  35. package/dist/bundled-skills.d.ts.map +1 -0
  36. package/dist/bundled-skills.js +34 -0
  37. package/dist/bundled-skills.js.map +1 -0
  38. package/dist/cli.d.ts +8 -2
  39. package/dist/cli.d.ts.map +1 -1
  40. package/dist/cli.js +2009 -308
  41. package/dist/cli.js.map +1 -1
  42. package/dist/config-secret.d.ts +3 -0
  43. package/dist/config-secret.d.ts.map +1 -0
  44. package/dist/config-secret.js +12 -0
  45. package/dist/config-secret.js.map +1 -0
  46. package/dist/config.d.ts +9 -4
  47. package/dist/config.d.ts.map +1 -1
  48. package/dist/config.js +164 -23
  49. package/dist/config.js.map +1 -1
  50. package/dist/content-provider.d.ts +32 -0
  51. package/dist/content-provider.d.ts.map +1 -0
  52. package/dist/content-provider.js +222 -0
  53. package/dist/content-provider.js.map +1 -0
  54. package/dist/context-types.d.ts +12 -0
  55. package/dist/context-types.d.ts.map +1 -0
  56. package/dist/context-types.js +2 -0
  57. package/dist/context-types.js.map +1 -0
  58. package/dist/context.d.ts +26 -0
  59. package/dist/context.d.ts.map +1 -1
  60. package/dist/context.js +56 -2
  61. package/dist/context.js.map +1 -1
  62. package/dist/daemon-ui.d.ts +93 -0
  63. package/dist/daemon-ui.d.ts.map +1 -0
  64. package/dist/daemon-ui.js +713 -0
  65. package/dist/daemon-ui.js.map +1 -0
  66. package/dist/daemon.d.ts +21 -0
  67. package/dist/daemon.d.ts.map +1 -0
  68. package/dist/daemon.js +317 -0
  69. package/dist/daemon.js.map +1 -0
  70. package/dist/doctor.d.ts +32 -0
  71. package/dist/doctor.d.ts.map +1 -0
  72. package/dist/doctor.js +684 -0
  73. package/dist/doctor.js.map +1 -0
  74. package/dist/errors.d.ts +1 -8
  75. package/dist/errors.d.ts.map +1 -1
  76. package/dist/errors.js +1 -20
  77. package/dist/errors.js.map +1 -1
  78. package/dist/get.d.ts +3 -0
  79. package/dist/get.d.ts.map +1 -0
  80. package/dist/get.js +80 -0
  81. package/dist/get.js.map +1 -0
  82. package/dist/history.d.ts.map +1 -1
  83. package/dist/history.js +6 -14
  84. package/dist/history.js.map +1 -1
  85. package/dist/host-error.d.ts +9 -0
  86. package/dist/host-error.d.ts.map +1 -0
  87. package/dist/host-error.js +21 -0
  88. package/dist/host-error.js.map +1 -0
  89. package/dist/identity.d.ts +16 -0
  90. package/dist/identity.d.ts.map +1 -0
  91. package/dist/identity.js +119 -0
  92. package/dist/identity.js.map +1 -0
  93. package/dist/ipc.d.ts +5 -1
  94. package/dist/ipc.d.ts.map +1 -1
  95. package/dist/ipc.js +125 -4
  96. package/dist/ipc.js.map +1 -1
  97. package/dist/kubo-install.d.ts +26 -0
  98. package/dist/kubo-install.d.ts.map +1 -0
  99. package/dist/kubo-install.js +724 -0
  100. package/dist/kubo-install.js.map +1 -0
  101. package/dist/kubo-peering.d.ts +11 -0
  102. package/dist/kubo-peering.d.ts.map +1 -0
  103. package/dist/kubo-peering.js +93 -0
  104. package/dist/kubo-peering.js.map +1 -0
  105. package/dist/kubo-peers.d.ts +12 -0
  106. package/dist/kubo-peers.d.ts.map +1 -0
  107. package/dist/kubo-peers.js +119 -0
  108. package/dist/kubo-peers.js.map +1 -0
  109. package/dist/kubo-release.d.ts +22 -0
  110. package/dist/kubo-release.d.ts.map +1 -0
  111. package/dist/kubo-release.js +176 -0
  112. package/dist/kubo-release.js.map +1 -0
  113. package/dist/kubo-topology.d.ts +19 -0
  114. package/dist/kubo-topology.d.ts.map +1 -0
  115. package/dist/kubo-topology.js +113 -0
  116. package/dist/kubo-topology.js.map +1 -0
  117. package/dist/kubo.d.ts +85 -0
  118. package/dist/kubo.d.ts.map +1 -0
  119. package/dist/kubo.js +755 -0
  120. package/dist/kubo.js.map +1 -0
  121. package/dist/pins.d.ts +18 -20
  122. package/dist/pins.d.ts.map +1 -1
  123. package/dist/pins.js +146 -126
  124. package/dist/pins.js.map +1 -1
  125. package/dist/pivot-rpc.d.ts +62 -0
  126. package/dist/pivot-rpc.d.ts.map +1 -0
  127. package/dist/pivot-rpc.js +407 -0
  128. package/dist/pivot-rpc.js.map +1 -0
  129. package/dist/pivot-service.d.ts +16 -0
  130. package/dist/pivot-service.d.ts.map +1 -0
  131. package/dist/pivot-service.js +50 -0
  132. package/dist/pivot-service.js.map +1 -0
  133. package/dist/publish-source.d.ts +3 -0
  134. package/dist/publish-source.d.ts.map +1 -0
  135. package/dist/publish-source.js +85 -0
  136. package/dist/publish-source.js.map +1 -0
  137. package/dist/runtime.d.ts +2 -0
  138. package/dist/runtime.d.ts.map +1 -1
  139. package/dist/runtime.js +9 -4
  140. package/dist/runtime.js.map +1 -1
  141. package/dist/search-index.d.ts +64 -0
  142. package/dist/search-index.d.ts.map +1 -0
  143. package/dist/search-index.js +409 -0
  144. package/dist/search-index.js.map +1 -0
  145. package/dist/search-service.d.ts +23 -0
  146. package/dist/search-service.d.ts.map +1 -0
  147. package/dist/search-service.js +116 -0
  148. package/dist/search-service.js.map +1 -0
  149. package/dist/skill-definition.d.ts +6 -0
  150. package/dist/skill-definition.d.ts.map +1 -0
  151. package/dist/skill-definition.js +38 -0
  152. package/dist/skill-definition.js.map +1 -0
  153. package/dist/skill-source.d.ts +9 -0
  154. package/dist/skill-source.d.ts.map +1 -0
  155. package/dist/skill-source.js +24 -0
  156. package/dist/skill-source.js.map +1 -0
  157. package/dist/skills.d.ts +104 -0
  158. package/dist/skills.d.ts.map +1 -0
  159. package/dist/skills.js +894 -0
  160. package/dist/skills.js.map +1 -0
  161. package/dist/source-bundle.d.ts +3 -9
  162. package/dist/source-bundle.d.ts.map +1 -1
  163. package/dist/source-bundle.js +17 -13
  164. package/dist/source-bundle.js.map +1 -1
  165. package/dist/space-path.d.ts +3 -0
  166. package/dist/space-path.d.ts.map +1 -0
  167. package/dist/space-path.js +17 -0
  168. package/dist/space-path.js.map +1 -0
  169. package/dist/space.d.ts +16 -4
  170. package/dist/space.d.ts.map +1 -1
  171. package/dist/space.js +170 -24
  172. package/dist/space.js.map +1 -1
  173. package/dist/state-storage-windows.d.ts +2 -0
  174. package/dist/state-storage-windows.d.ts.map +1 -0
  175. package/dist/state-storage-windows.js +114 -0
  176. package/dist/state-storage-windows.js.map +1 -0
  177. package/dist/state-storage.d.ts +5 -0
  178. package/dist/state-storage.d.ts.map +1 -0
  179. package/dist/state-storage.js +333 -0
  180. package/dist/state-storage.js.map +1 -0
  181. package/dist/state.d.ts +128 -16
  182. package/dist/state.d.ts.map +1 -1
  183. package/dist/state.js +506 -94
  184. package/dist/state.js.map +1 -1
  185. package/dist/swarm-envelope.d.ts +57 -0
  186. package/dist/swarm-envelope.d.ts.map +1 -0
  187. package/dist/swarm-envelope.js +299 -0
  188. package/dist/swarm-envelope.js.map +1 -0
  189. package/dist/swarm-files.d.ts +6 -0
  190. package/dist/swarm-files.d.ts.map +1 -0
  191. package/dist/swarm-files.js +116 -0
  192. package/dist/swarm-files.js.map +1 -0
  193. package/dist/swarm-meta.d.ts +15 -0
  194. package/dist/swarm-meta.d.ts.map +1 -0
  195. package/dist/swarm-meta.js +68 -0
  196. package/dist/swarm-meta.js.map +1 -0
  197. package/dist/swarm-observations.d.ts +40 -0
  198. package/dist/swarm-observations.d.ts.map +1 -0
  199. package/dist/swarm-observations.js +271 -0
  200. package/dist/swarm-observations.js.map +1 -0
  201. package/dist/swarm.d.ts +469 -0
  202. package/dist/swarm.d.ts.map +1 -0
  203. package/dist/swarm.js +4708 -0
  204. package/dist/swarm.js.map +1 -0
  205. package/dist/template-definition.d.ts +45 -67
  206. package/dist/template-definition.d.ts.map +1 -1
  207. package/dist/template-definition.js +35 -49
  208. package/dist/template-definition.js.map +1 -1
  209. package/dist/template-paths.d.ts +7 -0
  210. package/dist/template-paths.d.ts.map +1 -0
  211. package/dist/template-paths.js +23 -0
  212. package/dist/template-paths.js.map +1 -0
  213. package/dist/template-presentation.d.ts +20 -0
  214. package/dist/template-presentation.d.ts.map +1 -0
  215. package/dist/template-presentation.js +133 -0
  216. package/dist/template-presentation.js.map +1 -0
  217. package/dist/template-source.d.ts +4 -1
  218. package/dist/template-source.d.ts.map +1 -1
  219. package/dist/template-source.js +11 -4
  220. package/dist/template-source.js.map +1 -1
  221. package/dist/templates.d.ts +11 -0
  222. package/dist/templates.d.ts.map +1 -0
  223. package/dist/templates.js +53 -0
  224. package/dist/templates.js.map +1 -0
  225. package/dist/unixfs-tree.d.ts +67 -0
  226. package/dist/unixfs-tree.d.ts.map +1 -0
  227. package/dist/unixfs-tree.js +329 -0
  228. package/dist/unixfs-tree.js.map +1 -0
  229. package/package.json +18 -11
  230. package/src/app-authoring.ts +254 -88
  231. package/src/app-definition.ts +33 -45
  232. package/src/app-prebuild.ts +61 -0
  233. package/src/app-templates.ts +28 -71
  234. package/src/assembly.ts +185 -0
  235. package/src/authoring.ts +63 -72
  236. package/src/builtins.ts +7 -38
  237. package/src/bundled-skills.ts +60 -0
  238. package/src/cli.ts +2500 -376
  239. package/src/config-secret.ts +14 -0
  240. package/src/config.ts +215 -26
  241. package/src/content-provider.ts +299 -0
  242. package/src/context-types.ts +11 -0
  243. package/src/context.ts +100 -2
  244. package/src/daemon-ui.ts +812 -0
  245. package/src/daemon.ts +386 -0
  246. package/src/doctor.ts +1082 -0
  247. package/src/errors.ts +1 -25
  248. package/src/get.ts +90 -0
  249. package/src/history.ts +5 -15
  250. package/src/host-error.ts +26 -0
  251. package/src/identity.ts +175 -0
  252. package/src/ipc.ts +161 -4
  253. package/src/kubo-install.ts +936 -0
  254. package/src/kubo-peering.ts +126 -0
  255. package/src/kubo-peers.ts +144 -0
  256. package/src/kubo-release.ts +230 -0
  257. package/src/kubo-topology.ts +148 -0
  258. package/src/kubo.ts +1088 -0
  259. package/src/pins.ts +190 -142
  260. package/src/pivot-rpc.ts +601 -0
  261. package/src/pivot-service.ts +87 -0
  262. package/src/publish-source.ts +103 -0
  263. package/src/runtime.ts +16 -3
  264. package/src/search-index.ts +560 -0
  265. package/src/search-service.ts +152 -0
  266. package/src/skill-definition.ts +53 -0
  267. package/src/skill-source.ts +29 -0
  268. package/src/skills.ts +1236 -0
  269. package/src/source-bundle.ts +26 -14
  270. package/src/space-path.ts +30 -0
  271. package/src/space.ts +230 -42
  272. package/src/state-storage-windows.ts +134 -0
  273. package/src/state-storage.ts +378 -0
  274. package/src/state.ts +782 -116
  275. package/src/swarm-envelope.ts +461 -0
  276. package/src/swarm-files.ts +126 -0
  277. package/src/swarm-meta.ts +98 -0
  278. package/src/swarm-observations.ts +393 -0
  279. package/src/swarm.ts +7051 -0
  280. package/src/template-definition.ts +35 -49
  281. package/src/template-paths.ts +43 -0
  282. package/src/template-presentation.ts +151 -0
  283. package/src/template-source.ts +19 -5
  284. package/src/templates.ts +77 -0
  285. package/src/unixfs-tree.ts +401 -0
package/src/errors.ts CHANGED
@@ -1,23 +1,4 @@
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
- }
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,90 @@
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")) throw cause;
69
+ }
70
+ if (dirname(current) === current) break;
71
+ }
72
+ return await scaffoldFiles(directory, files);
73
+ } catch (cause) {
74
+ const failures: unknown[] = [cause];
75
+ if (output === undefined) {
76
+ try {
77
+ await rmdir(directory);
78
+ } catch (error) {
79
+ failures.push(error);
80
+ }
81
+ }
82
+ if (failures.length > 1)
83
+ throw new AggregateError(
84
+ failures,
85
+ "Download failed and temporary directory cleanup also failed.",
86
+ { cause },
87
+ );
88
+ throw cause;
89
+ }
90
+ }
package/src/history.ts CHANGED
@@ -183,21 +183,11 @@ export class Ownership {
183
183
  recordId: null,
184
184
  };
185
185
  state.saveHold(hold);
186
- const deadline = Date.now() + 30_000;
187
- for (;;) {
188
- try {
189
- store.registerOwner(owner, []);
190
- break;
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
+ }
@@ -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 { CHUNK_BYTES, type OutputSpace, relativePath } from "./space.js";
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,6 +83,12 @@ export class ContextServer {
75
83
  readonly output: OutputSpace,
76
84
  readonly fatal: (error: Error) => void,
77
85
  readonly appCid: string,
86
+ readonly resolveBundleReference?: (
87
+ reference: string,
88
+ signal: AbortSignal,
89
+ swarm?: string,
90
+ ) => Promise<Pick<BundleInspection, "cid" | "targetCid" | "swarm">>,
91
+ readonly supportedTypes?: ReadonlySet<string>,
78
92
  ) {
79
93
  child.on("message", this.receive);
80
94
  }
@@ -116,8 +130,14 @@ export class ContextServer {
116
130
  if (controller.signal.aborted) {
117
131
  if (frame.operation === "read")
118
132
  await this.cancelRead(Number(result), controller.signal.reason);
119
- if (frame.operation === "link")
133
+ if (
134
+ ["link", "beginOpenSession", "cancelOpenSession"].includes(
135
+ frame.operation,
136
+ )
137
+ )
120
138
  this.output.fail(controller.signal.reason);
139
+ if (frame.operation === "submitBundleResult")
140
+ this.output.discardBundleResult();
121
141
  controller.signal.throwIfAborted();
122
142
  }
123
143
  return result;
@@ -153,6 +173,128 @@ export class ContextServer {
153
173
  ): Promise<unknown> {
154
174
  signal.throwIfAborted();
155
175
  if (operation === "identity") return this.appCid;
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;
297
+ }
156
298
  if (operation === "read") {
157
299
  if (this.reads.size + this.writes.size + this.openingReads >= 32)
158
300
  throw new HostError("Context stream capacity exceeded.");
@@ -163,7 +305,22 @@ export class ContextServer {
163
305
  relativePath(data.path);
164
306
  this.openingReads++;
165
307
  try {
166
- await this.owner.retain([root], signal);
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
+ }
167
324
  signal.throwIfAborted();
168
325
  const directory = await this.store.read(root, { signal });
169
326
  signal.throwIfAborted();