@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
@@ -1,11 +1,12 @@
1
- import { fileURLToPath } from "node:url";
2
1
  import { resolve } from "node:path";
3
- import { lstat, readFile } from "node:fs/promises";
2
+ import { lstat } from "node:fs/promises";
4
3
  import { Liquid, type Template } from "liquidjs";
4
+ import { parseBundleManifest } from "@ipab/bundle";
5
5
  import {
6
6
  listTree,
7
7
  readBytes,
8
8
  type ReadonlyFileTree,
9
+ type TreeEntry,
9
10
  } from "@ipab/bundle-builder";
10
11
  import { filesystemTree } from "@ipab/bundle-builder/node";
11
12
  import { HostError, object } from "./errors.js";
@@ -15,13 +16,20 @@ import {
15
16
  templateDefinition,
16
17
  templateParameters,
17
18
  } from "./template-definition.js";
18
- import { builtinTemplateSource, templateSource } from "./template-source.js";
19
+ import { templateSource } from "./template-source.js";
20
+ import { addTemplateOutputPath, templateOutputPath } from "./template-paths.js";
19
21
  import { nodeError } from "./authoring.js";
20
22
 
21
- export const builtinTemplateNames = new Map([
22
- ["builtin:template.node", "node"],
23
- ["builtin:template.web-app", "web-app"],
24
- ] as const);
23
+ function appName(name: string) {
24
+ if (
25
+ !/^[a-z0-9][a-z0-9._-]*$/.test(name) ||
26
+ name.length > 214 ||
27
+ ["node_modules", "favicon.ico"].includes(name)
28
+ )
29
+ throw new HostError(
30
+ "App NAME must be an unscoped lowercase npm package name.",
31
+ );
32
+ }
25
33
 
26
34
  async function legacyTemplate(
27
35
  selector: string,
@@ -237,11 +245,18 @@ export async function renderTemplate(
237
245
  options: AuthoringOptions & { parameters?: readonly string[] },
238
246
  ) {
239
247
  const definition = templateDefinition(
240
- await readBytes(tree, "template.yaml", 64 * 1024, options),
248
+ await readBytes(
249
+ tree,
250
+ parseBundleManifest(
251
+ await readBytes(tree, ".bundle.yaml", 1024 * 1024, options),
252
+ ).entry,
253
+ 64 * 1024,
254
+ options,
255
+ ),
241
256
  );
242
257
  const values = templateParameters(definition, name, options.parameters);
243
258
  const files = new Map<string, string | Uint8Array>();
244
- const paths = new Map<string, string>();
259
+ const paths = new Map<string, TreeEntry["kind"]>();
245
260
  let count = 0,
246
261
  total = 0;
247
262
  const collect = async (directory: string, depth: number): Promise<void> => {
@@ -251,35 +266,8 @@ export async function renderTemplate(
251
266
  if (++count > 256)
252
267
  throw new HostError("Template exceeds 256 project paths.");
253
268
  const source = `${directory}/${entry.name}`;
254
- relativePath(source);
255
- const original = source.slice("project/".length);
256
- const rendered = entry.kind === "file" && original.endsWith(".liquid");
257
- const target = relativePath(
258
- rendered ? original.slice(0, -".liquid".length) : original,
259
- );
260
- if (
261
- target
262
- .split("/")
263
- .some(
264
- (part) =>
265
- ["node_modules", ".git", "dist"].includes(part.toLowerCase()) ||
266
- /^(?:con|prn|aux|nul|com[0-9]|lpt[0-9])(?:\.|$)/i.test(part),
267
- )
268
- )
269
- throw new HostError(`Unsafe template output path: ${target}.`);
270
- const key = target.toLowerCase();
271
- if (
272
- paths.has(key) ||
273
- [...paths].some(
274
- ([existing, kind]) =>
275
- (kind === "file" && key.startsWith(`${existing}/`)) ||
276
- (entry.kind === "file" && existing.startsWith(`${key}/`)),
277
- )
278
- )
279
- throw new HostError(
280
- `Duplicate or overlapping template output path: ${target}.`,
281
- );
282
- paths.set(key, entry.kind);
269
+ const { target, rendered } = templateOutputPath(source, entry.kind);
270
+ addTemplateOutputPath(paths, target, entry.kind);
283
271
  if (entry.kind === "directory") {
284
272
  await collect(source, depth + 1);
285
273
  continue;
@@ -314,38 +302,6 @@ export async function appTemplate(
314
302
  options: AuthoringOptions & { parameters?: readonly string[] },
315
303
  ): Promise<{ files: Map<string, string | Uint8Array>; cid: string | null }> {
316
304
  options.signal?.throwIfAborted();
317
- for (const [alias, name] of builtinTemplateNames)
318
- if (selector === alias) selector = name;
319
- if (selector === "node" || selector === "web-app") {
320
- const loaded = await builtinTemplateSource(selector, options);
321
- const parameters = [...(options.parameters ?? [])];
322
- if (
323
- selector === "web-app" &&
324
- !parameters.some((value) => value.startsWith("webAppDependency="))
325
- ) {
326
- const framework = object(
327
- JSON.parse(
328
- await readFile(
329
- fileURLToPath(import.meta.resolve("@ipab/web-app/package.json")),
330
- "utf8",
331
- ),
332
- ) as unknown,
333
- "Web App framework package",
334
- );
335
- if (typeof framework.version !== "string" || !framework.version.trim())
336
- throw new HostError(
337
- "Web App framework package is missing its version.",
338
- );
339
- parameters.push(`webAppDependency=${framework.version}`);
340
- }
341
- return {
342
- files: await renderTemplate(loaded.tree, name, {
343
- ...options,
344
- parameters,
345
- }),
346
- cid: loaded.cid,
347
- };
348
- }
349
305
  let legacy = false;
350
306
  try {
351
307
  await lstat(resolve(selector, ".bundle.yaml"));
@@ -360,6 +316,7 @@ export async function appTemplate(
360
316
  }
361
317
  }
362
318
  if (legacy) {
319
+ appName(name);
363
320
  if (options.parameters?.length)
364
321
  throw new HostError(
365
322
  "--param requires a Template Bundle, not a legacy mapping.",
@@ -375,7 +332,7 @@ export async function appTemplate(
375
332
  } catch (error) {
376
333
  if (nodeError(error, "ENOENT"))
377
334
  throw new HostError(
378
- `Unknown template "${selector}". Use node, web-app, a valid Bundle directory, single-root CAR, exact CID or named pin.`,
335
+ `Unknown template "${selector}". Use a Swarm template selector, valid Bundle directory, single-root CAR, exact CID or IPAB URI. Builtin templates and local aliases are unsupported.`,
379
336
  );
380
337
  throw error;
381
338
  }
@@ -0,0 +1,185 @@
1
+ import { lstat } from "node:fs/promises";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
+ import { CID } from "multiformats/cid";
4
+ import { parseData, parseBundleManifest } from "@ipab/bundle";
5
+ import {
6
+ encodeBundleCar,
7
+ fileTree,
8
+ readBytes,
9
+ type FileReader,
10
+ } from "@ipab/bundle-builder";
11
+ import { filesystemTree } from "@ipab/bundle-builder/node";
12
+ import { HostError, object } from "./errors.js";
13
+ import { relativePath } from "./space.js";
14
+ import {
15
+ nodeError,
16
+ outputAvailable,
17
+ readCar,
18
+ writeCar,
19
+ type AuthoringOptions,
20
+ type BuildResult,
21
+ } from "./authoring.js";
22
+
23
+ function keys(value: Record<string, unknown>, allowed: string[]): void {
24
+ if (Object.keys(value).some((key) => !allowed.includes(key)))
25
+ throw new HostError(
26
+ "Unknown assembly recipe field. Legacy App ipab.yaml/ipab.json is not a generic assembly recipe; run project preparation separately.",
27
+ );
28
+ }
29
+
30
+ export async function assembleBundle(
31
+ source = ".",
32
+ options: AuthoringOptions & {
33
+ output?: string;
34
+ outputDir?: string;
35
+ replace?: boolean;
36
+ } = {},
37
+ ): Promise<BuildResult> {
38
+ const input = resolve(source);
39
+ const stat = await lstat(input);
40
+ if (stat.isSymbolicLink())
41
+ throw new HostError("Assembly input must not be a symlink.");
42
+ const recipePath = stat.isDirectory()
43
+ ? join(input, "ipab.build.yaml")
44
+ : input;
45
+ const root = dirname(recipePath);
46
+ const tree = await filesystemTree(root);
47
+ let recipeBytes: Uint8Array;
48
+ try {
49
+ recipeBytes = await readBytes(
50
+ tree,
51
+ basename(recipePath),
52
+ 64 * 1024,
53
+ options,
54
+ );
55
+ } catch (cause) {
56
+ if (stat.isDirectory()) {
57
+ for (const legacy of ["ipab.yaml", "ipab.json"]) {
58
+ try {
59
+ await lstat(join(root, legacy));
60
+ throw new HostError(
61
+ "Legacy App build configuration is unsupported by generic build. Run the project preparation script, then supply ipab.build.yaml.",
62
+ );
63
+ } catch (error) {
64
+ if (!nodeError(error, "ENOENT")) throw error;
65
+ }
66
+ }
67
+ }
68
+ throw cause;
69
+ }
70
+ const recipe = object(
71
+ parseData(recipeBytes, "application/yaml"),
72
+ "assembly recipe",
73
+ );
74
+ keys(recipe, ["version", "files", "imports"]);
75
+ if (
76
+ recipe.version !== 1 ||
77
+ !Array.isArray(recipe.files) ||
78
+ !Array.isArray(recipe.imports ?? [])
79
+ )
80
+ throw new HostError(
81
+ "Assembly recipe requires version: 1, files: [{source,target}], and optional imports: [{cid,target}].",
82
+ );
83
+ const files = new Map<string, FileReader>();
84
+ const paths: string[] = [];
85
+ const claim = (target: string) => {
86
+ relativePath(target);
87
+ const key = target.toLowerCase();
88
+ if (
89
+ paths.some(
90
+ (previous) =>
91
+ previous === key ||
92
+ previous.startsWith(`${key}/`) ||
93
+ key.startsWith(`${previous}/`),
94
+ )
95
+ )
96
+ throw new HostError(`Assembly target collision: ${target}.`);
97
+ paths.push(key);
98
+ };
99
+ files.set(".bundle.yaml", () => tree.read(".bundle.yaml", options));
100
+ claim(".bundle.yaml");
101
+ const definition = parseBundleManifest(
102
+ await readBytes(tree, ".bundle.yaml", 1024 * 1024, options),
103
+ );
104
+ const inputs = new Set<string>([resolve(root, ".bundle.yaml"), recipePath]);
105
+ for (const item of recipe.files) {
106
+ const mapping = object(item, "file mapping");
107
+ keys(mapping, ["source", "target"]);
108
+ if (
109
+ typeof mapping.source !== "string" ||
110
+ typeof mapping.target !== "string"
111
+ )
112
+ throw new HostError(
113
+ "Recipe file mappings require source and target strings.",
114
+ );
115
+ relativePath(mapping.source);
116
+ claim(mapping.target);
117
+ inputs.add(resolve(root, ...mapping.source.split("/")));
118
+ const sourcePath = mapping.source;
119
+ files.set(mapping.target, () => tree.read(sourcePath, options));
120
+ }
121
+ // CID grafts are processed sequentially; keep at most one local CAR in memory.
122
+ let localCar:
123
+ { path: string; data: Awaited<ReturnType<typeof readCar>> } | undefined;
124
+ const imports = ((recipe.imports as unknown[] | undefined) ?? []).map(
125
+ (item) => {
126
+ const mapping = object(item, "CID import");
127
+ keys(mapping, ["cid", "target", "car"]);
128
+ if (typeof mapping.cid !== "string" || typeof mapping.target !== "string")
129
+ throw new HostError("CID imports require cid and target strings.");
130
+ const cid = CID.parse(mapping.cid);
131
+ claim(mapping.target);
132
+ if (mapping.car !== undefined) {
133
+ if (typeof mapping.car !== "string")
134
+ throw new HostError("CID import car must be a relative file path.");
135
+ const path = resolve(root, relativePath(mapping.car));
136
+ inputs.add(path);
137
+ return {
138
+ path: mapping.target,
139
+ cid,
140
+ resolveCid: async (block: CID) => {
141
+ if (localCar?.path !== path)
142
+ localCar = { path, data: await readCar(path, options, root) };
143
+ if (!localCar.data.root.equals(cid))
144
+ throw new HostError(
145
+ "CID import does not match the local CAR root.",
146
+ );
147
+ return localCar.data.resolveCid(block);
148
+ },
149
+ };
150
+ }
151
+ if (!options.resolveCid)
152
+ throw new HostError("CID imports require a Store.");
153
+ return { path: mapping.target, cid, resolveCid: options.resolveCid };
154
+ },
155
+ );
156
+ if (
157
+ [definition.entry, definition.type].some(
158
+ (path) => !files.has(path) && !imports.some((item) => item.path === path),
159
+ )
160
+ )
161
+ throw new HostError(
162
+ "Recipe must explicitly select the Bundle entry and type files.",
163
+ );
164
+ const output = options.output
165
+ ? resolve(options.output)
166
+ : join(root, options.outputDir ?? "dist", "bundle.car");
167
+ if ([...inputs].some((input) => input.toLowerCase() === output.toLowerCase()))
168
+ throw new HostError("CAR output cannot replace recipe or source inputs.");
169
+ await outputAvailable(output, Boolean(options.replace));
170
+ const encoded = await encodeBundleCar(fileTree(files), {
171
+ ...options,
172
+ imports,
173
+ });
174
+ const result = {
175
+ kind: "content-bundle" as const,
176
+ cid: encoded.cid.toString(),
177
+ schemaCid: encoded.schema.cid.toString(),
178
+ output,
179
+ validation: encoded.validation,
180
+ };
181
+ return {
182
+ ...result,
183
+ bytes: await writeCar(encoded, output, Boolean(options.replace), options),
184
+ };
185
+ }
package/src/authoring.ts CHANGED
@@ -22,16 +22,21 @@ import { CarBlockIterator } from "@ipld/car/iterator";
22
22
  import { CID } from "multiformats/cid";
23
23
  import { sha256 } from "multiformats/hashes/sha2";
24
24
  import ignore from "ignore";
25
- import { stringify } from "yaml";
26
25
  import {
27
26
  bundleLimits,
28
27
  encodeSchema,
29
28
  openBundle,
30
29
  openSchema,
31
30
  parseBundleManifest,
31
+ parseBundleType,
32
32
  ContentBundleError,
33
33
  } from "@ipab/bundle";
34
- import type { OpenBundleOptions, SchemaBlock } from "@ipab/bundle";
34
+ import type {
35
+ EntryValidation,
36
+ OpenBundleOptions,
37
+ SchemaBlock,
38
+ BundleManifest,
39
+ } from "@ipab/bundle";
35
40
  import {
36
41
  encodeBundleCar,
37
42
  encodeSchemaCar,
@@ -59,24 +64,20 @@ export interface BuildResult {
59
64
  schemaCid: string;
60
65
  output: string;
61
66
  bytes: number;
67
+ validation?: EntryValidation | null;
62
68
  }
63
69
  export interface InspectResult {
64
70
  kind: "content-bundle" | "bundle-schema";
65
71
  sourceKind: "directory" | "definition" | "car" | "cid";
66
72
  cid: string | null;
67
73
  schemaCid: string;
68
- name: string;
69
- description: string;
70
- entry: { path: string; contentType: string };
71
- validation: { status: "valid"; storage: "verified" | "not-applicable" };
72
- }
73
- export interface InitResult {
74
- kind: "bundle-init";
75
- directory: string;
76
- schemaCid: string;
77
- name: string;
78
- entry: { path: string; contentType: string };
79
- files: string[];
74
+ manifest?: BundleManifest;
75
+ type: SchemaBlock["type"];
76
+ validation: {
77
+ definition: "valid";
78
+ storage: "verified" | "not-applicable";
79
+ entry: EntryValidation | null;
80
+ };
80
81
  }
81
82
  export interface ImportResult {
82
83
  kind: "bundle-import";
@@ -85,6 +86,7 @@ export interface ImportResult {
85
86
  source: string;
86
87
  blocks: number;
87
88
  bytes: number;
89
+ validation: EntryValidation | null;
88
90
  }
89
91
  export function nodeError(error: unknown, code: string): boolean {
90
92
  return error instanceof Error && "code" in error && error.code === code;
@@ -109,7 +111,7 @@ async function localFile(
109
111
  return readBytes(tree, basename(path), maximum, options);
110
112
  }
111
113
  async function definitionAt(path: string, options: AuthoringOptions) {
112
- return parseBundleManifest(
114
+ return parseBundleType(
113
115
  await localFile(path, bundleLimits.parsingBytes, options),
114
116
  );
115
117
  }
@@ -159,16 +161,23 @@ export async function selectedTree(
159
161
  pathKey(path).startsWith(`${pathKey(outputDir)}/`)),
160
162
  ) ||
161
163
  matcher.ignores(directory ? `${path}/` : path);
162
- for (const required of [".bundle.yaml", definition.entry.path])
164
+ for (const required of [".bundle.yaml", definition.entry, definition.type])
163
165
  if (excluded(required, false))
164
166
  throw new HostError(
165
167
  `Required bundle file is excluded: ${required}. Fix .bundle-ignore or choose an output outside the entry tree.`,
166
168
  );
167
169
  return filesystemTree(root, { exclude: excluded });
168
170
  }
169
- export async function readCar(path: string, options: AuthoringOptions) {
170
- const tree = await filesystemTree(dirname(path));
171
- const input = await tree.read(basename(path), options);
171
+ export async function readCar(
172
+ path: string,
173
+ options: AuthoringOptions,
174
+ directory = dirname(path),
175
+ ) {
176
+ const tree = await filesystemTree(directory);
177
+ const input = await tree.read(
178
+ relative(directory, path).split(sep).join("/"),
179
+ options,
180
+ );
172
181
  const chunks = streamChunks(input, options);
173
182
  let bytes = 0;
174
183
  try {
@@ -299,18 +308,18 @@ async function fromRoot(
299
308
  });
300
309
  return {
301
310
  schema: bundle.schema,
311
+ manifest: bundle.manifest,
312
+ validation: bundle.validation,
302
313
  kind: "content-bundle" as const,
303
314
  sourceKind,
304
315
  cid: root.toString(),
305
316
  };
306
317
  }
307
- function summary(schema: SchemaBlock) {
308
- const { name, description, entry } = schema.manifest;
318
+ function summary(schema: SchemaBlock, manifest?: BundleManifest) {
309
319
  return {
310
320
  schemaCid: schema.cid.toString(),
311
- name,
312
- description,
313
- entry: { path: entry.path, contentType: entry.contentType },
321
+ type: schema.type,
322
+ ...(manifest ? { manifest } : {}),
314
323
  };
315
324
  }
316
325
  export async function inspectBundle(
@@ -333,8 +342,12 @@ export async function inspectBundle(
333
342
  kind: "content-bundle",
334
343
  sourceKind: "directory",
335
344
  cid: null,
336
- ...summary(encoded.schema),
337
- validation: { status: "valid", storage: "verified" },
345
+ ...summary(encoded.schema, encoded.manifest ?? undefined),
346
+ validation: {
347
+ definition: "valid",
348
+ storage: "verified",
349
+ entry: encoded.validation,
350
+ },
338
351
  };
339
352
  } finally {
340
353
  await encoded.close();
@@ -345,10 +358,14 @@ export async function inspectBundle(
345
358
  kind: loaded.kind,
346
359
  sourceKind: loaded.sourceKind,
347
360
  cid: loaded.cid,
348
- ...summary(loaded.schema),
361
+ ...summary(
362
+ loaded.schema,
363
+ "manifest" in loaded ? loaded.manifest : undefined,
364
+ ),
349
365
  validation: {
350
- status: "valid",
366
+ definition: "valid",
351
367
  storage: loaded.kind === "content-bundle" ? "verified" : "not-applicable",
368
+ entry: loaded.kind === "content-bundle" ? loaded.validation : null,
352
369
  },
353
370
  };
354
371
  }
@@ -378,6 +395,7 @@ export async function importBundle(
378
395
  cid,
379
396
  schemaCid: loaded.schema.cid.toString(),
380
397
  source: path,
398
+ validation: loaded.kind === "content-bundle" ? loaded.validation : null,
381
399
  blocks: car.blocks.size,
382
400
  bytes: car.bytes,
383
401
  };
@@ -458,14 +476,28 @@ export async function buildBundle(
458
476
  const output = options.output
459
477
  ? resolve(options.output)
460
478
  : join(outputDirectory, options.schemaOnly ? "schema.car" : "bundle.car");
461
- const definitionPath = stat.isDirectory() ? join(root, ".bundle.yaml") : path;
479
+ const manifestPath = join(root, ".bundle.yaml");
480
+ const manifest =
481
+ !options.schemaOnly || (await existing(manifestPath))
482
+ ? parseBundleManifest(
483
+ await localFile(manifestPath, bundleLimits.manifestBytes, options),
484
+ )
485
+ : undefined;
486
+ const definitionPath = stat.isDirectory()
487
+ ? join(root, manifest?.type ?? ".bundle-type.yaml")
488
+ : path;
462
489
  const definition = await definitionAt(definitionPath, options);
463
490
  if (
464
491
  [
465
492
  definitionPath,
466
493
  join(root, ".bundle.yaml"),
467
494
  join(root, ".bundle-ignore"),
468
- resolve(root, ...definition.entry.path.split("/")),
495
+ ...(manifest
496
+ ? [
497
+ resolve(root, ...manifest.entry.split("/")),
498
+ resolve(root, ...manifest.type.split("/")),
499
+ ]
500
+ : []),
469
501
  ].some((input) => pathKey(output) === pathKey(input))
470
502
  )
471
503
  throw new HostError(
@@ -490,54 +522,13 @@ export async function buildBundle(
490
522
  cid: encoded.cid.toString(),
491
523
  schemaCid: encoded.schema.cid.toString(),
492
524
  output,
525
+ validation: encoded.validation,
493
526
  };
494
527
  return {
495
528
  ...result,
496
529
  bytes: await writeCar(encoded, output, Boolean(options.replace), options),
497
530
  };
498
531
  }
499
- export async function initBundle(
500
- source: string,
501
- directory: string,
502
- options: AuthoringOptions = {},
503
- ): Promise<InitResult> {
504
- const loaded = await artifact(source, options);
505
- if (loaded.kind !== "bundle-schema")
506
- throw new HostError(
507
- "Initialization requires a type definition, not a content bundle.",
508
- );
509
- const schema = loaded.schema;
510
- const { entry } = schema.manifest;
511
- if (ignore().add("/dist/").ignores(entry.path))
512
- throw new HostError(
513
- "Default entry conflicts with the generated /dist/ ignore rule.",
514
- );
515
- let value: string;
516
- if (entry.contentType === "application/jsonl") {
517
- if (!Array.isArray(entry.defaultValue))
518
- throw new HostError("JSONL default must be an array.");
519
- value = entry.defaultValue.map((item) => JSON.stringify(item)).join("\n");
520
- if (value) value += "\n";
521
- } else
522
- value =
523
- entry.contentType === "application/yaml"
524
- ? stringify(entry.defaultValue)
525
- : `${JSON.stringify(entry.defaultValue, null, 2)}\n`;
526
- const files = new Map([
527
- [".bundle.yaml", stringify(schema.manifest)],
528
- [".bundle-ignore", "/dist/\n"],
529
- [entry.path, value],
530
- ]);
531
- const root = await scaffoldFiles(directory, files, options);
532
- return {
533
- kind: "bundle-init",
534
- directory: root,
535
- schemaCid: schema.cid.toString(),
536
- name: schema.manifest.name,
537
- entry: { path: entry.path, contentType: entry.contentType },
538
- files: [...files.keys()],
539
- };
540
- }
541
532
  export async function scaffoldFiles(
542
533
  directory: string,
543
534
  files: ReadonlyMap<string, string | Uint8Array>,
package/src/builtins.ts CHANGED
@@ -1,13 +1,6 @@
1
- import { appBuiltinAlias, builtinAppSchema } from "./app-definition.js";
2
- import {
3
- builtinTemplateSchema,
4
- templateBuiltinAlias,
5
- templateSchema,
6
- templateSchemaCid,
7
- } from "./template-definition.js";
8
- import { builtinTemplateNames } from "./app-templates.js";
9
- import { builtinTemplateSource } from "./template-source.js";
10
- import type { NamedPin, PinMetadata } from "./state.js";
1
+ import { builtinAppSchema } from "./app-definition.js";
2
+ import { builtinTemplateSchema } from "./template-definition.js";
3
+ import type { NamedPin } from "./state.js";
11
4
 
12
5
  async function loadBuiltins(): Promise<{
13
6
  pins: readonly NamedPin[];
@@ -15,36 +8,12 @@ async function loadBuiltins(): Promise<{
15
8
  }> {
16
9
  const pins: NamedPin[] = [];
17
10
  const blocks = new Map<string, Uint8Array>();
18
- const add = (name: string, cid: string, metadata: PinMetadata) => {
19
- pins.push({
20
- name,
21
- cid,
22
- owner: name,
23
- status: "ready",
24
- metadata,
25
- builtin: true,
26
- });
27
- };
28
- for (const [name, schema] of [
29
- [appBuiltinAlias, await builtinAppSchema()],
30
- [templateBuiltinAlias, await builtinTemplateSchema()],
31
- ] as const) {
11
+ for (const schema of [
12
+ await builtinAppSchema(),
13
+ await builtinTemplateSchema(),
14
+ ]) {
32
15
  const cid = schema.cid.toString();
33
16
  blocks.set(cid, schema.bytes);
34
- add(name, cid, {
35
- kind: "schema",
36
- typeName: schema.manifest.name,
37
- schemaCid: cid,
38
- });
39
- }
40
- for (const [name, selector] of builtinTemplateNames) {
41
- const template = await builtinTemplateSource(selector);
42
- for (const [cid, bytes] of template.blocks) blocks.set(cid, bytes);
43
- add(name, template.cid, {
44
- kind: "bundle",
45
- typeName: templateSchema.name,
46
- schemaCid: templateSchemaCid.toString(),
47
- });
48
17
  }
49
18
  return { pins, blocks };
50
19
  }