@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
@@ -2,22 +2,15 @@ import { relative, isAbsolute, sep } from "node:path";
2
2
  import { createHash } from "node:crypto";
3
3
  import ignore from "ignore";
4
4
  import { stringify } from "yaml";
5
- import { bundleLimits } from "@ipab/bundle";
5
+ import { bundleLimits, parseBundleManifest } from "@ipab/bundle";
6
6
  import { listTree, readBytes } from "@ipab/bundle-builder";
7
7
  import { filesystemTree } from "@ipab/bundle-builder/node";
8
8
  import { nodeError, type AuthoringOptions } from "./authoring.js";
9
9
  import { HostError } from "./errors.js";
10
10
 
11
11
  export const sourceSchema = {
12
- name: "IPAB App Source",
13
- description:
14
- "App project source snapshot; build inputs are ordinary resources.",
15
- entry: {
16
- path: "package.json",
17
- contentType: "application/json",
18
- schema: { type: "object" },
19
- defaultValue: {},
20
- },
12
+ contentType: "application/json",
13
+ schema: { type: "object" },
21
14
  } as const;
22
15
 
23
16
  export async function captureSource(
@@ -42,7 +35,7 @@ export async function captureSource(
42
35
  if (
43
36
  normalized
44
37
  .split("/")
45
- .some((part) => [".git", "node_modules"].includes(part)) ||
38
+ .some((part) => [".git", ".ipab", "node_modules"].includes(part)) ||
46
39
  normalized === ".bundle-ignore" ||
47
40
  normalized === "dist" ||
48
41
  normalized.startsWith("dist/") ||
@@ -114,11 +107,30 @@ export async function captureSource(
114
107
  throw new HostError("Source .bundle.yaml is excluded by .gitignore.");
115
108
  files.set(
116
109
  ".bundle.yaml",
117
- new TextEncoder().encode(stringify(sourceSchema)),
110
+ new TextEncoder().encode(
111
+ stringify({
112
+ title: "IPAB App Source",
113
+ entry: "package.json",
114
+ type: ".bundle-type.yaml",
115
+ }),
116
+ ),
118
117
  );
118
+ if (excluded(".bundle-type.yaml", false))
119
+ throw new HostError(
120
+ "Source .bundle-type.yaml is excluded by .gitignore.",
121
+ );
122
+ if (!files.has(".bundle-type.yaml"))
123
+ files.set(
124
+ ".bundle-type.yaml",
125
+ new TextEncoder().encode(stringify(sourceSchema)),
126
+ );
119
127
  }
120
- if (!files.has("package.json"))
121
- throw new HostError("Source package.json is excluded by .gitignore.");
128
+ const manifest = parseBundleManifest(files.get(".bundle.yaml")!);
129
+ for (const required of [manifest.entry, manifest.type])
130
+ if (!files.has(required))
131
+ throw new HostError(
132
+ `Source required file is excluded or missing: ${required}.`,
133
+ );
122
134
  return files;
123
135
  }
124
136
 
@@ -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,21 @@ 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 type { Ownership } from "./history.js";
8
+ import { isSwarmRootSchema } from "./swarm-meta.js";
9
+ import { HostError, message, object } from "./errors.js";
10
+ import { Ownership } from "./history.js";
11
+ import { randomUUID } from "node:crypto";
12
+ import { ReadableStream as NodeReadableStream } from "node:stream/web";
10
13
  import { appDescription, appSchemaCid } from "./app-definition.js";
11
14
  import { readSupportedSchemas } from "./supported-schemas.js";
12
15
  export { appSchema, appSchemaCid } from "./app-definition.js";
13
- export const CHUNK_BYTES = 64 * 1024;
16
+ import { CHUNK_BYTES, relativePath } from "./space-path.js";
17
+ export { CHUNK_BYTES, relativePath } from "./space-path.js";
14
18
  export const OUTPUT_BYTES = 64 * 1024 * 1024;
15
19
  export const MAX_PATHS = 4096;
16
20
 
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
21
  export async function bytes(
45
- stream: ReadableStream<Uint8Array>,
22
+ stream: ReadableStream<Uint8Array> | NodeReadableStream<Uint8Array>,
46
23
  limit: number,
47
24
  ): Promise<Uint8Array> {
48
25
  const reader = stream.getReader();
@@ -83,12 +60,19 @@ export async function validatedBundle(
83
60
  owner: Ownership,
84
61
  signal?: AbortSignal,
85
62
  ): Promise<ContentBundle> {
63
+ const cid = CID.parse(root);
64
+ const shallow = await openBundle(cid, {
65
+ ...(signal ? { signal } : {}),
66
+ shallow: true,
67
+ resolveCid: (key, options) => store.block.get(key.toString(), options),
68
+ });
69
+ if (isSwarmRootSchema(shallow.schemaKey)) return shallow;
86
70
  await owner.retain([root], signal);
87
- return openBundle(CID.parse(root), {
71
+ return openBundle(cid, {
88
72
  ...(signal ? { signal } : {}),
89
- resolveCid: async (cid, options) => {
90
- await owner.retain([cid.toV1().toString()], options.signal);
91
- return store.block.get(cid.toString(), options);
73
+ resolveCid: async (key, options) => {
74
+ await owner.retain([key.toV1().toString()], options.signal);
75
+ return store.block.get(key.toString(), options);
92
76
  },
93
77
  });
94
78
  }
@@ -106,7 +90,7 @@ export async function loadApp(
106
90
  const bundle = await validatedBundle(root, store, owner, signal);
107
91
  if (
108
92
  bundle.schemaKey !== appSchemaCid.toString() ||
109
- bundle.manifest.entry.contentType !== "application/json"
93
+ bundle.schema.type.contentType !== "application/json"
110
94
  )
111
95
  throw new HostError(
112
96
  "App must use the documented exact builtin Node App type CID.",
@@ -117,10 +101,7 @@ export async function loadApp(
117
101
  const description = appDescription(
118
102
  JSON.parse(
119
103
  new TextDecoder("utf-8", { fatal: true }).decode(
120
- await bytes(
121
- await bundle.readFile(bundle.manifest.entry.path),
122
- 64 * 1024,
123
- ),
104
+ await bytes(await bundle.readFile(bundle.manifest.entry), 64 * 1024),
124
105
  ),
125
106
  ) as unknown,
126
107
  );
@@ -160,6 +141,75 @@ export async function loadApp(
160
141
  };
161
142
  }
162
143
 
144
+ /** Checkpoints are owned by the execution, not pins; only the final result is handed to history. */
145
+ export async function deriveBundle(
146
+ inputCid: string,
147
+ replacements: unknown,
148
+ store: IpfsStore,
149
+ owner: Ownership,
150
+ signal?: AbortSignal,
151
+ ): Promise<string> {
152
+ signal?.throwIfAborted();
153
+ const changes = Object.entries(object(replacements, "Bundle replacements"));
154
+ if (changes.length > MAX_PATHS)
155
+ throw new HostError("Bundle replacement path limit exceeded.");
156
+ let total = 0;
157
+ for (const [path, value] of changes) {
158
+ relativePath(path);
159
+ if (!(value instanceof Uint8Array))
160
+ throw new HostError("Bundle replacements must contain byte arrays.");
161
+ total += value.byteLength;
162
+ if (total > OUTPUT_BYTES)
163
+ throw new HostError("Bundle replacement byte limit exceeded.");
164
+ }
165
+ const input = CID.parse(inputCid).toV1().toString();
166
+ const original = await validatedBundle(input, store, owner, signal);
167
+ const id = randomUUID();
168
+ const session = `ipab-derive:${owner.state.identity}:${id}`;
169
+ const temporary = await Ownership.create(
170
+ owner.state,
171
+ store,
172
+ `derive:${id}`,
173
+ session,
174
+ );
175
+ try {
176
+ const output = new OutputSpace(store, temporary, session, signal);
177
+ const replaced = new Set(changes.map(([path]) => path));
178
+ for (const path of original.files) {
179
+ signal?.throwIfAborted();
180
+ if (replaced.has(path)) continue;
181
+ const file = await store.read(
182
+ `${input}/${path}`,
183
+ signal ? { signal } : {},
184
+ );
185
+ await output.link(path, file.cid.toString(), signal);
186
+ }
187
+ for (const [path, value] of changes) {
188
+ const content = value as Uint8Array;
189
+ const chunks = async function* () {
190
+ for (let offset = 0; offset < content.length; offset += CHUNK_BYTES) {
191
+ signal?.throwIfAborted();
192
+ yield await Promise.resolve(
193
+ content.subarray(offset, offset + CHUNK_BYTES),
194
+ );
195
+ }
196
+ };
197
+ await output.write(path, chunks(), signal);
198
+ }
199
+ const result = await output.finalize();
200
+ const derived = await validatedBundle(result, store, temporary, signal);
201
+ if (derived.schemaKey !== original.schemaKey)
202
+ throw new HostError(
203
+ "Derived Bundle must preserve the input's exact type CID.",
204
+ );
205
+ await owner.retain([result], signal);
206
+ signal?.throwIfAborted();
207
+ return result;
208
+ } finally {
209
+ await temporary.release();
210
+ }
211
+ }
212
+
163
213
  interface Entry {
164
214
  cid: CID;
165
215
  size: bigint;
@@ -178,6 +228,11 @@ export class OutputSpace {
178
228
  private readonly pending = new Set<string>();
179
229
  private totalBytes = 0;
180
230
  private fault: Error | undefined;
231
+ private bundleMode = false;
232
+ private bundleSchema: string | undefined;
233
+ private submittedBundle: string | undefined;
234
+ private selectingBundle = false;
235
+ private sessionStarted = false;
181
236
  constructor(
182
237
  readonly store: IpfsStore,
183
238
  readonly owner: Ownership,
@@ -187,6 +242,10 @@ export class OutputSpace {
187
242
  this.session = session;
188
243
  }
189
244
  private claim(path: string): void {
245
+ if (this.bundleMode)
246
+ throw new HostError(
247
+ "Explicit Bundle results cannot be mixed with output Space writes.",
248
+ );
190
249
  relativePath(path);
191
250
  if (this.claimed.size >= MAX_PATHS)
192
251
  throw new HostError("Output path limit exceeded.");
@@ -202,7 +261,125 @@ export class OutputSpace {
202
261
  this.pending.add(path);
203
262
  }
204
263
  fail(error: unknown): void {
205
- this.fault ??= new HostError(message(error));
264
+ this.fault ??=
265
+ error instanceof HostError ? error : new HostError(message(error));
266
+ }
267
+ private selectBundleMode(): void {
268
+ if (this.fault) throw this.fault;
269
+ if (this.claimed.size)
270
+ throw new HostError(
271
+ "Explicit Bundle results cannot be mixed with output Space writes.",
272
+ );
273
+ if (this.selectingBundle)
274
+ throw new HostError(
275
+ "Concurrent Bundle result selection is not supported.",
276
+ );
277
+ this.bundleMode = true;
278
+ }
279
+ async beginOpenSession(
280
+ inputCid: string,
281
+ signal?: AbortSignal,
282
+ supportedTypes?: ReadonlySet<string>,
283
+ expectedTypes?: unknown,
284
+ ): Promise<void> {
285
+ try {
286
+ this.selectBundleMode();
287
+ if (this.sessionStarted || this.submittedBundle)
288
+ throw new HostError(
289
+ "Open session has already started or submitted a result.",
290
+ );
291
+ this.sessionStarted = true;
292
+ this.selectingBundle = true;
293
+ if (expectedTypes !== undefined) {
294
+ if (
295
+ !Array.isArray(expectedTypes) ||
296
+ expectedTypes.length > MAX_PATHS ||
297
+ !expectedTypes.every(
298
+ (cid: unknown) =>
299
+ typeof cid === "string" &&
300
+ CID.parse(cid).toV1().toString() === cid,
301
+ )
302
+ )
303
+ throw new HostError(
304
+ "Open supportedTypes must be exact canonical CID strings.",
305
+ );
306
+ const expected = new Set<string>(expectedTypes as string[]);
307
+ if (expected.size !== expectedTypes.length)
308
+ throw new HostError(
309
+ "Open supportedTypes must not contain duplicate CIDs.",
310
+ );
311
+ if (
312
+ !supportedTypes ||
313
+ expected.size !== supportedTypes.size ||
314
+ [...expected].some((cid) => !supportedTypes.has(cid))
315
+ )
316
+ throw new HostError(
317
+ "Open supportedTypes must exactly match the App supportedSchemas declaration.",
318
+ );
319
+ }
320
+ const bundle = await validatedBundle(
321
+ inputCid,
322
+ this.store,
323
+ this.owner,
324
+ signal,
325
+ );
326
+ signal?.throwIfAborted();
327
+ if (supportedTypes && !supportedTypes.has(bundle.schemaKey))
328
+ throw new HostError(
329
+ "App does not declare support for the Open input's exact type CID.",
330
+ );
331
+ this.bundleSchema = bundle.schemaKey;
332
+ } catch (error) {
333
+ this.fail(error);
334
+ throw error;
335
+ } finally {
336
+ this.selectingBundle = false;
337
+ }
338
+ }
339
+ cancelOpenSession(): void {
340
+ this.bundleMode = true;
341
+ this.fail(
342
+ new HostError(
343
+ "Open session cancelled without a committed Bundle result.",
344
+ 130,
345
+ "CANCELLED",
346
+ "execution",
347
+ ),
348
+ );
349
+ }
350
+ discardBundleResult(): void {
351
+ this.submittedBundle = undefined;
352
+ }
353
+ async submitBundleResult(root: string, signal?: AbortSignal): Promise<void> {
354
+ try {
355
+ this.selectBundleMode();
356
+ } catch (error) {
357
+ this.fail(error);
358
+ throw error;
359
+ }
360
+ this.selectingBundle = true;
361
+ try {
362
+ const cid = CID.parse(root).toV1().toString();
363
+ if (this.submittedBundle && this.submittedBundle !== cid) {
364
+ const error = new HostError(
365
+ "A different Bundle result has already been submitted.",
366
+ );
367
+ this.fail(error);
368
+ throw error;
369
+ }
370
+ signal?.throwIfAborted();
371
+ if (this.submittedBundle === cid) return;
372
+ const bundle = await validatedBundle(cid, this.store, this.owner, signal);
373
+ signal?.throwIfAborted();
374
+ if (this.bundleSchema && bundle.schemaKey !== this.bundleSchema)
375
+ throw new HostError(
376
+ "Bundle result must preserve the Open session's exact type CID.",
377
+ );
378
+ this.submittedBundle = cid;
379
+ } finally {
380
+ // Validation/storage failures leave the explicit session open for retry, never a Space result.
381
+ this.selectingBundle = false;
382
+ }
206
383
  }
207
384
  async write(
208
385
  path: string,
@@ -238,7 +415,7 @@ export class OutputSpace {
238
415
  block instanceof Uint8Array
239
416
  ? block
240
417
  : await bytes(
241
- ReadableStream.from(block),
418
+ NodeReadableStream.from(block),
242
419
  this.store.limits.blockBytes,
243
420
  );
244
421
  await this.store.stageBlock(
@@ -315,7 +492,18 @@ export class OutputSpace {
315
492
  }
316
493
  }
317
494
  async finalize(): Promise<string> {
495
+ this.signal?.throwIfAborted();
318
496
  if (this.fault) throw this.fault;
497
+ if (this.bundleMode) {
498
+ if (this.selectingBundle || !this.submittedBundle)
499
+ throw new HostError(
500
+ "Open session ended without submitting a Bundle result.",
501
+ 130,
502
+ "CANCELLED",
503
+ "commit",
504
+ );
505
+ return this.submittedBundle;
506
+ }
319
507
  if (this.pending.size)
320
508
  throw new HostError(
321
509
  "Output has unfinished stream writes.",
@@ -0,0 +1,134 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { join } from "node:path";
3
+
4
+ // One process per preflight, not one per path or SQL statement. Use framework
5
+ // APIs directly: loading PowerShell's filesystem/ACL cmdlets dominates startup.
6
+ const script = `
7
+ $ErrorActionPreference = 'Stop'
8
+ $sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
9
+ $sidType = [Security.Principal.SecurityIdentifier]
10
+ $root = $env:IPAB_STATE_ROOT
11
+ $repair = $env:IPAB_STATE_ACTION -eq 'prepare'
12
+ $managed = $env:IPAB_STATE_MANAGED -eq '1'
13
+ function New-PrivateAcl($dir) {
14
+ if ($dir) {
15
+ $a = [Security.AccessControl.DirectorySecurity]::new()
16
+ $r = [Security.AccessControl.FileSystemAccessRule]::new($sid,'FullControl','ContainerInherit,ObjectInherit','None','Allow')
17
+ } else {
18
+ $a = [Security.AccessControl.FileSecurity]::new()
19
+ $r = [Security.AccessControl.FileSystemAccessRule]::new($sid,'FullControl','Allow')
20
+ }
21
+ $a.SetAccessRuleProtection($true,$false)
22
+ $a.AddAccessRule($r)
23
+ return $a
24
+ }
25
+ if ($repair -and ![IO.Directory]::Exists($root)) {
26
+ $new = New-PrivateAcl $true
27
+ $new.SetOwner($sid)
28
+ [void][IO.Directory]::CreateDirectory($root,$new)
29
+ }
30
+ $changes = @()
31
+ $paths = @($root)
32
+ foreach ($name in ([string]$env:IPAB_STATE_ENTRIES).Split('|',[StringSplitOptions]::RemoveEmptyEntries)) {
33
+ $paths += [IO.Path]::Combine($root,$name)
34
+ }
35
+ function Check-Path($path) {
36
+ try {
37
+ $attrs = [IO.File]::GetAttributes($path)
38
+ if ($attrs -band [IO.FileAttributes]::ReparsePoint) { throw 'Invalid state path' }
39
+ $dir = [bool]($attrs -band [IO.FileAttributes]::Directory)
40
+ $isDir = $path -eq $root -or [IO.Path]::GetFileName($path) -in 'runtime','logs','kubo-bin','kubo-cache','swarm-nodes','swarm-credentials','swarm-daemon','swarm-inspections','swarm-observations'
41
+ if ($dir -ne $isDir) { throw 'Invalid state type' }
42
+ if ($dir) { $acl = [IO.Directory]::GetAccessControl($path) }
43
+ else { $acl = [IO.File]::GetAccessControl($path) }
44
+ }
45
+ catch [IO.FileNotFoundException] {
46
+ if ($path -match 'state\\.sqlite-(wal|shm|journal)$') { return }
47
+ throw
48
+ }
49
+ if ($acl.GetOwner($sidType).Value -ne $sid.Value) { throw 'Invalid owner' }
50
+ if ($env:IPAB_STATE_ACTION -eq 'owners') { return }
51
+ if ($dir -and $path -ne $root -and [IO.Path]::GetFileName($path) -notin 'runtime','logs') { return }
52
+ $private = $false
53
+ $raw = [Security.AccessControl.RawSecurityDescriptor]::new($acl.GetSecurityDescriptorBinaryForm(),0)
54
+ foreach ($ace in $raw.DiscretionaryAcl) {
55
+ if ($ace.AceType -ne 'AccessAllowed' -or $ace.SecurityIdentifier.Value -ne $sid.Value) { $private = $false; break }
56
+ $flags = [int]$ace.AceFlags
57
+ if ($ace.AccessMask -eq [int][Security.AccessControl.FileSystemRights]::FullControl -and !($flags -band 8) -and (!$dir -or ($flags -band 15) -eq 3)) { $private = $true }
58
+ }
59
+ if (!$private -or ($path -eq $root -and !$acl.AreAccessRulesProtected)) {
60
+ if (!$repair -or (!$managed -and !$private)) { throw 'Not account-only' }
61
+ return @{path=$path; dir=$dir}
62
+ }
63
+ }
64
+ foreach ($path in $paths) { $change = Check-Path $path; if ($change) { $changes += $change } }
65
+ foreach ($c in $changes) {
66
+ $acl = New-PrivateAcl $c.dir
67
+ if ($c.dir) {
68
+ # SetKernelObjectSecurity only updates this directory; SetAccessControl
69
+ # would propagate inheritable ACEs through existing, uninspected children.
70
+ if (!('IpabDirectoryAcl' -as [type])) {
71
+ Add-Type -TypeDefinition @'
72
+ using System;
73
+ using System.Runtime.InteropServices;
74
+ using Microsoft.Win32.SafeHandles;
75
+ public static class IpabDirectoryAcl {
76
+ [DllImport("kernel32", CharSet=CharSet.Unicode, SetLastError=true)]
77
+ static extern SafeFileHandle CreateFile(string p, uint a, uint s, IntPtr sa, uint d, uint f, IntPtr t);
78
+ [DllImport("advapi32", SetLastError=true)]
79
+ static extern bool SetKernelObjectSecurity(SafeFileHandle h, uint i, byte[] sd);
80
+ public static void Set(string p, byte[] sd) {
81
+ using (var h = CreateFile(p, 0x40000, 3, IntPtr.Zero, 3, 0x02200000, IntPtr.Zero)) {
82
+ if (h.IsInvalid || !SetKernelObjectSecurity(h, 0x80000004, sd)) throw new System.ComponentModel.Win32Exception();
83
+ }
84
+ }
85
+ }
86
+ '@
87
+ }
88
+ [IpabDirectoryAcl]::Set($c.path,$acl.GetSecurityDescriptorBinaryForm())
89
+ } else { [IO.File]::SetAccessControl($c.path,$acl) }
90
+ if (Check-Path $c.path) { throw 'ACL did not persist' }
91
+ if ($c.path -eq $root) { 'sealed'; break }
92
+ }
93
+ `;
94
+
95
+ export function windowsStateAccess(
96
+ root: string,
97
+ entries: string[],
98
+ action: "owners" | "prepare" | "verify",
99
+ managed: boolean,
100
+ ): boolean {
101
+ const result = execFileSync(
102
+ join(
103
+ process.env.SystemRoot!,
104
+ "System32",
105
+ "WindowsPowerShell",
106
+ "v1.0",
107
+ "powershell.exe",
108
+ ),
109
+ [
110
+ "-NoLogo",
111
+ "-NoProfile",
112
+ "-NonInteractive",
113
+ "-EncodedCommand",
114
+ Buffer.from(script, "utf16le").toString("base64"),
115
+ ],
116
+ {
117
+ windowsHide: true,
118
+ encoding: "utf8",
119
+ env: {
120
+ ...Object.fromEntries(
121
+ Object.entries(process.env).filter(
122
+ ([key]) => key.toLowerCase() !== "psmodulepath",
123
+ ),
124
+ ),
125
+ IPAB_STATE_ROOT: root,
126
+ IPAB_STATE_ENTRIES: entries.join("|"),
127
+ IPAB_STATE_ACTION: action,
128
+ IPAB_STATE_MANAGED: managed ? "1" : "0",
129
+ },
130
+ stdio: ["ignore", "pipe", "pipe"],
131
+ },
132
+ );
133
+ return result.trim() === "sealed";
134
+ }