@ipab/cli 0.1.0-rc.1 → 0.1.0-rc.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/README.md +234 -9
  2. package/dist/app-authoring.d.ts +15 -0
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +144 -58
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/app-definition.d.ts +52 -57
  7. package/dist/app-definition.d.ts.map +1 -1
  8. package/dist/app-definition.js +33 -38
  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 +8 -46
  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 +7 -8
  23. package/dist/authoring.d.ts.map +1 -1
  24. package/dist/authoring.js +59 -16
  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 +1 -23
  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 +1967 -258
  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 +11 -2
  47. package/dist/config.d.ts.map +1 -1
  48. package/dist/config.js +166 -8
  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 -1
  59. package/dist/context.d.ts.map +1 -1
  60. package/dist/context.js +54 -10
  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 +688 -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 -2
  94. package/dist/ipc.d.ts.map +1 -1
  95. package/dist/ipc.js +124 -12
  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 +16 -18
  122. package/dist/pins.d.ts.map +1 -1
  123. package/dist/pins.js +144 -106
  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 -8
  162. package/dist/source-bundle.d.ts.map +1 -1
  163. package/dist/source-bundle.js +17 -12
  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 +135 -13
  182. package/dist/state.d.ts.map +1 -1
  183. package/dist/state.js +577 -92
  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 +537 -0
  202. package/dist/swarm.d.ts.map +1 -0
  203. package/dist/swarm.js +4991 -0
  204. package/dist/swarm.js.map +1 -0
  205. package/dist/template-definition.d.ts +47 -52
  206. package/dist/template-definition.d.ts.map +1 -1
  207. package/dist/template-definition.js +36 -41
  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 +14 -7
  230. package/src/app-authoring.ts +240 -76
  231. package/src/app-definition.ts +33 -39
  232. package/src/app-prebuild.ts +61 -0
  233. package/src/app-templates.ts +17 -73
  234. package/src/assembly.ts +185 -0
  235. package/src/authoring.ts +99 -18
  236. package/src/builtins.ts +2 -27
  237. package/src/bundled-skills.ts +60 -0
  238. package/src/cli.ts +2461 -323
  239. package/src/config-secret.ts +14 -0
  240. package/src/config.ts +214 -8
  241. package/src/content-provider.ts +299 -0
  242. package/src/context-types.ts +11 -0
  243. package/src/context.ts +98 -10
  244. package/src/daemon-ui.ts +812 -0
  245. package/src/daemon.ts +386 -0
  246. package/src/doctor.ts +1092 -0
  247. package/src/errors.ts +1 -25
  248. package/src/get.ts +91 -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 +160 -11
  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 +188 -126
  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 -4
  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 -13
  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 +875 -108
  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 +7392 -0
  280. package/src/template-definition.ts +36 -41
  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,16 +1,23 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { lstat, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
- import { isBuiltin } from "node:module";
4
- import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
+ import {
4
+ basename,
5
+ dirname,
6
+ isAbsolute,
7
+ join,
8
+ relative,
9
+ resolve,
10
+ sep,
11
+ } from "node:path";
5
12
  import { StringDecoder } from "node:string_decoder";
6
- import { compileFunction } from "node:vm";
7
13
  import { stringify } from "yaml";
8
- import { parseData } from "@ipab/bundle";
14
+ import { parseData, type BundleType } from "@ipab/bundle";
9
15
  import { filesystemTree } from "@ipab/bundle-builder/node";
10
16
  import { encodeBundleCar, memoryTree, readBytes } from "@ipab/bundle-builder";
11
17
  import {
12
18
  writeCar,
13
19
  outputAvailable,
20
+ readCar,
14
21
  scaffoldFiles,
15
22
  nodeError,
16
23
  type AuthoringOptions,
@@ -28,10 +35,18 @@ import {
28
35
  builtinAppSchema,
29
36
  } from "./app-definition.js";
30
37
  import { appTemplate } from "./app-templates.js";
31
- import { buildChain, projectBuild, runPrebuild } from "./app-prebuild.js";
38
+ import {
39
+ buildChain,
40
+ compileAppProgram,
41
+ projectBuild,
42
+ runPrebuild,
43
+ } from "./app-prebuild.js";
32
44
  import { captureSource, sourceFingerprint } from "./source-bundle.js";
45
+ export { appSchema } from "./app-definition.js";
33
46
 
34
47
  const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
48
+ const sourceTarget = "source";
49
+ const sourceCarInput = "source.car";
35
50
 
36
51
  export async function initApp(
37
52
  name: string,
@@ -187,6 +202,9 @@ export async function buildApp(
187
202
  outputDir?: string;
188
203
  replace?: boolean;
189
204
  includeSource?: boolean;
205
+ /** Exact types from an author's Open App definition; exclusive with config paths. */
206
+ supportedTypes?: readonly BundleType[];
207
+ preparedDirectory?: string;
190
208
  } = {},
191
209
  ): Promise<
192
210
  Omit<BuildResult, "kind"> & {
@@ -327,7 +345,19 @@ export async function buildApp(
327
345
  throw new HostError(
328
346
  "App metadata cannot override the generated source declaration.",
329
347
  );
330
- const supportedSchemas = await buildSupportedSchemas(tree, schemas, options);
348
+ if (options.supportedTypes && schemas.size)
349
+ throw new HostError(
350
+ "Use either supportedTypes or supportedSchemas paths, not both.",
351
+ );
352
+ const declared = options.supportedTypes?.map(
353
+ (type, index) =>
354
+ [`schemas/${index}.json`, new TextEncoder().encode(json(type))] as const,
355
+ );
356
+ const supportedSchemas = await buildSupportedSchemas(
357
+ declared ? memoryTree(new Map(declared)) : tree,
358
+ declared ? new Set(declared.map(([path]) => path)) : schemas,
359
+ options,
360
+ );
331
361
  const description = appDescription({
332
362
  ...defaults,
333
363
  ...overrides,
@@ -412,7 +442,6 @@ export async function buildApp(
412
442
  options.signal?.throwIfAborted();
413
443
  const stage = await mkdtemp(join(root, ".ipab-app-"));
414
444
  try {
415
- const { build } = await import("esbuild");
416
445
  const bundleStage = join(stage, "bundle");
417
446
  const input = prebuild ? join(stage, "input") : root;
418
447
  if (prebuild) {
@@ -423,56 +452,13 @@ export async function buildApp(
423
452
  const prebuiltSource = prebuild
424
453
  ? await readBytes(inputTree, entry, 8 * 1024 * 1024, options)
425
454
  : undefined;
426
- if (prebuiltSource) {
427
- try {
428
- const source = new TextDecoder("utf-8", { fatal: true }).decode(
429
- prebuiltSource,
430
- );
431
- compileFunction(source.replace(/^#![^\r\n]*/, ""), [
432
- "exports",
433
- "require",
434
- "module",
435
- "__filename",
436
- "__dirname",
437
- ]);
438
- } catch (cause) {
439
- const error = new HostError(
440
- "Prebuilt App entry must be valid Node CommonJS; it is packaged without transformation.",
441
- );
442
- error.cause = cause;
443
- throw error;
444
- }
445
- }
446
455
  await mkdir(join(bundleStage, "resources"), { recursive: true });
447
- const program = await build({
448
- absWorkingDir: input,
449
- entryPoints: [entry],
450
- outfile: join(bundleStage, "resources", "app.cjs"),
451
- bundle: true,
452
- platform: "node",
453
- format: "cjs",
454
- target: "node22.15",
455
- packages: prebuild ? "external" : "bundle",
456
- metafile: true,
457
- write: false,
458
- logLevel: "silent",
459
- logOverride: {
460
- "unsupported-dynamic-import": "error",
461
- "unsupported-require-call": "error",
462
- "indirect-require": "error",
463
- },
464
- });
465
- if (
466
- program.outputFiles.length !== 1 ||
467
- program.outputFiles[0]!.contents.length > 8 * 1024 * 1024 ||
468
- (prebuild && Object.keys(program.metafile.inputs).length !== 1) ||
469
- Object.values(program.metafile.outputs).some((file) =>
470
- file.imports.some((item) => item.external && !isBuiltin(item.path)),
471
- )
472
- )
473
- throw new HostError(
474
- "App must bundle into one self-contained .cjs of at most 8 MiB; only Node builtins may remain external.",
475
- );
456
+ const program = await compileAppProgram(
457
+ input,
458
+ entry,
459
+ join(bundleStage, "resources", "app.cjs"),
460
+ prebuiltSource,
461
+ );
476
462
  if (sourceFiles && !prebuild)
477
463
  for (const path of Object.keys(program.metafile.inputs))
478
464
  if (
@@ -490,10 +476,27 @@ export async function buildApp(
490
476
  flag: "wx",
491
477
  signal: options.signal,
492
478
  });
493
- await writeFile(join(bundleStage, ".bundle.yaml"), stringify(appSchema), {
494
- flag: "wx",
495
- signal: options.signal,
496
- });
479
+ await writeFile(
480
+ join(bundleStage, ".bundle.yaml"),
481
+ stringify({
482
+ title: description.name,
483
+ description: description.description,
484
+ entry: "app.json",
485
+ type: ".bundle-type.yaml",
486
+ }),
487
+ {
488
+ flag: "wx",
489
+ signal: options.signal,
490
+ },
491
+ );
492
+ await writeFile(
493
+ join(bundleStage, ".bundle-type.yaml"),
494
+ stringify(appSchema),
495
+ {
496
+ flag: "wx",
497
+ signal: options.signal,
498
+ },
499
+ );
497
500
  if (supportedSchemas)
498
501
  await writeFile(
499
502
  join(bundleStage, ...supportedSchemasPath.split("/")),
@@ -512,7 +515,7 @@ export async function buildApp(
512
515
  (part, index, parts) =>
513
516
  part.startsWith(".") &&
514
517
  !(
515
- part === ".bundle.yaml" &&
518
+ [".bundle.yaml", ".bundle-type.yaml"].includes(part) &&
516
519
  index === parts.length - 1 &&
517
520
  index > 0
518
521
  ),
@@ -573,24 +576,100 @@ export async function buildApp(
573
576
  }),
574
577
  { flag: "wx", signal: options.signal },
575
578
  );
576
- const encoded = await encodeBundleCar(
577
- await filesystemTree(bundleStage),
578
- options,
579
- );
579
+ const source = encodedSource
580
+ ? {
581
+ cid: encodedSource.cid.toString(),
582
+ schemaCid: encodedSource.schema.cid.toString(),
583
+ output: sourceOutput,
584
+ bytes: await writeCar(
585
+ encodedSource,
586
+ sourceOutput,
587
+ Boolean(options.replace),
588
+ options,
589
+ ),
590
+ }
591
+ : undefined;
592
+ const sourceCar = source
593
+ ? await readCar(source.output, options)
594
+ : undefined;
595
+ const encoded = await encodeBundleCar(await filesystemTree(bundleStage), {
596
+ ...options,
597
+ imports:
598
+ encodedSource && sourceCar
599
+ ? [
600
+ {
601
+ path: sourceTarget,
602
+ cid: encodedSource.cid,
603
+ resolveCid: sourceCar.resolveCid,
604
+ },
605
+ ]
606
+ : [],
607
+ });
580
608
  try {
581
- const source = encodedSource
582
- ? {
583
- cid: encodedSource.cid.toString(),
584
- schemaCid: encodedSource.schema.cid.toString(),
585
- output: sourceOutput,
586
- bytes: await writeCar(
587
- encodedSource,
588
- sourceOutput,
589
- Boolean(options.replace),
609
+ if (options.preparedDirectory) {
610
+ const preparedFiles = new Map<string, Uint8Array>();
611
+ const preparedTree = await filesystemTree(bundleStage);
612
+ const collect = async (path: string): Promise<void> => {
613
+ for (const item of await preparedTree.list(path, options)) {
614
+ const child = path ? `${path}/${item.name}` : item.name;
615
+ if (item.kind === "directory") await collect(child);
616
+ else
617
+ preparedFiles.set(
618
+ child,
619
+ await readBytes(
620
+ preparedTree,
621
+ child,
622
+ 16 * 1024 * 1024,
623
+ options,
624
+ ),
625
+ );
626
+ }
627
+ };
628
+ await collect("");
629
+ preparedFiles.set(
630
+ "ipab.build.yaml",
631
+ new TextEncoder().encode(
632
+ stringify({
633
+ version: 1,
634
+ files: [...preparedFiles.keys()]
635
+ .filter((path) => path !== ".bundle.yaml")
636
+ .map((path) => ({ source: path, target: path })),
637
+ imports: source
638
+ ? [
639
+ {
640
+ cid: source.cid,
641
+ target: sourceTarget,
642
+ car: sourceCarInput,
643
+ },
644
+ ]
645
+ : [],
646
+ }),
647
+ ),
648
+ );
649
+ if (source)
650
+ preparedFiles.set(
651
+ sourceCarInput,
652
+ await readBytes(
653
+ await filesystemTree(dirname(source.output)),
654
+ basename(source.output),
655
+ source.bytes,
590
656
  options,
591
657
  ),
592
- }
593
- : undefined;
658
+ );
659
+ await scaffoldFiles(
660
+ options.preparedDirectory,
661
+ preparedFiles,
662
+ options,
663
+ );
664
+ return {
665
+ kind: "app-bundle",
666
+ cid: encoded.cid.toString(),
667
+ schemaCid: encoded.schema.cid.toString(),
668
+ output,
669
+ bytes: 0,
670
+ ...(source ? { source } : {}),
671
+ };
672
+ }
594
673
  return {
595
674
  kind: "app-bundle",
596
675
  cid: encoded.cid.toString(),
@@ -614,3 +693,88 @@ export async function buildApp(
614
693
  await rm(stage, { recursive: true, force: true });
615
694
  }
616
695
  }
696
+
697
+ /** Explicit project tool. The root CLI build never calls this helper. */
698
+ export async function prepareApp(
699
+ directory = ".",
700
+ options: AuthoringOptions & {
701
+ replace?: boolean;
702
+ includeSource?: boolean;
703
+ /** Exact types from an author's Open App definition; exclusive with config paths. */
704
+ supportedTypes?: readonly BundleType[];
705
+ } = {},
706
+ ) {
707
+ const root = resolve(directory);
708
+ const prepared = join(root, "dist", "prepared-app");
709
+ try {
710
+ const stat = await lstat(prepared);
711
+ if (stat.isSymbolicLink() || !stat.isDirectory() || !options.replace)
712
+ throw new HostError(
713
+ "Prepared output exists; explicit replacement requires a regular generated directory.",
714
+ );
715
+ const tree = await filesystemTree(prepared);
716
+ const recipe = object(
717
+ parseData(
718
+ await readBytes(tree, "ipab.build.yaml", 64 * 1024),
719
+ "application/yaml",
720
+ ),
721
+ "prepared recipe",
722
+ );
723
+ if (
724
+ recipe.version !== 1 ||
725
+ !Array.isArray(recipe.files) ||
726
+ !Array.isArray(recipe.imports) ||
727
+ recipe.imports.length > 1
728
+ )
729
+ throw new HostError(
730
+ "Existing prepared output is not owned by the App preparation tool.",
731
+ );
732
+ const allowed = new Set([
733
+ ".bundle.yaml",
734
+ "ipab.build.yaml",
735
+ ...recipe.files.map((item: unknown) => {
736
+ const mapping = object(item, "prepared mapping");
737
+ if (
738
+ typeof mapping.source !== "string" ||
739
+ mapping.source !== mapping.target
740
+ )
741
+ throw new HostError("Existing prepared mapping is not generated.");
742
+ return relativePath(mapping.source);
743
+ }),
744
+ ]);
745
+ if (recipe.imports.length) {
746
+ const sourceImport = object(recipe.imports[0], "prepared source import");
747
+ if (
748
+ sourceImport.target !== sourceTarget ||
749
+ sourceImport.car !== sourceCarInput ||
750
+ typeof sourceImport.cid !== "string"
751
+ )
752
+ throw new HostError(
753
+ "Existing prepared source import is not generated.",
754
+ );
755
+ allowed.add(sourceCarInput);
756
+ }
757
+ const verify = async (path: string): Promise<void> => {
758
+ for (const item of await tree.list(path)) {
759
+ const child = path ? `${path}/${item.name}` : item.name;
760
+ if (item.kind === "directory") await verify(child);
761
+ else if (!allowed.has(child))
762
+ throw new HostError(
763
+ "Prepared output contains custom files; choose a new output rather than deleting them.",
764
+ );
765
+ }
766
+ };
767
+ await verify("");
768
+ await rm(prepared, { recursive: true });
769
+ } catch (cause) {
770
+ if (!nodeError(cause, "ENOENT")) throw cause;
771
+ }
772
+ const result = await buildApp(root, {
773
+ ...options,
774
+ preparedDirectory: prepared,
775
+ });
776
+ return {
777
+ directory: prepared,
778
+ ...(result.source ? { source: result.source } : {}),
779
+ };
780
+ }
@@ -8,46 +8,40 @@ import {
8
8
 
9
9
  export const appBuiltinAlias = "builtin:node-app.v1";
10
10
  export const appSchema = {
11
- name: "IPAB Node App",
12
- description:
13
- "Self-contained Node CommonJS application and declarative metadata.",
14
- entry: {
15
- path: "app.json",
16
- contentType: "application/json",
17
- schema: {
18
- $id: "urn:ipab:node-app.v1",
19
- type: "object",
20
- additionalProperties: false,
21
- required: ["name", "version", "entry"],
22
- properties: {
23
- name: { type: "string" },
24
- version: { type: "string" },
25
- description: { type: "string" },
26
- entry: { type: "string" },
27
- source: {
28
- type: "object",
29
- additionalProperties: false,
30
- required: ["cid"],
31
- properties: { cid: { type: "string" } },
32
- },
33
- supportedSchemas: { type: "string" },
34
- author: {
11
+ contentType: "application/json",
12
+ schema: {
13
+ $id: "urn:ipab:node-app.v1",
14
+ type: "object",
15
+ additionalProperties: false,
16
+ required: ["name", "version", "entry"],
17
+ properties: {
18
+ name: { type: "string" },
19
+ version: { type: "string" },
20
+ description: { type: "string" },
21
+ entry: { type: "string" },
22
+ source: {
23
+ type: "object",
24
+ additionalProperties: false,
25
+ required: ["cid"],
26
+ properties: { cid: { type: "string" } },
27
+ },
28
+ supportedSchemas: { type: "string" },
29
+ author: {
30
+ type: "object",
31
+ additionalProperties: false,
32
+ required: ["name"],
33
+ properties: { name: { type: "string" }, email: { type: "string" } },
34
+ },
35
+ license: { type: "string" },
36
+ icon: { type: "string" },
37
+ i18n: {
38
+ type: "object",
39
+ additionalProperties: {
35
40
  type: "object",
36
41
  additionalProperties: false,
37
- required: ["name"],
38
- properties: { name: { type: "string" }, email: { type: "string" } },
39
- },
40
- license: { type: "string" },
41
- icon: { type: "string" },
42
- i18n: {
43
- type: "object",
44
- additionalProperties: {
45
- type: "object",
46
- additionalProperties: false,
47
- properties: {
48
- name: { type: "string" },
49
- description: { type: "string" },
50
- },
42
+ properties: {
43
+ name: { type: "string" },
44
+ description: { type: "string" },
51
45
  },
52
46
  },
53
47
  },
@@ -57,7 +51,7 @@ export const appSchema = {
57
51
 
58
52
  // This is the canonical full definition, not a separate identity for the alias.
59
53
  export const appSchemaCid = CID.parse(
60
- "bafyreid3qonq3i2rjekgz7mtarpu4zbfmgg4uhtcjtpijdgrrhdke5opzq",
54
+ "bafyreifualoac4ddlzl63jlfstz4xsq75cai5t6ywu3j3khhyukoccmybm",
61
55
  );
62
56
  export const builtinTypes: ReadonlyMap<string, string> = new Map([
63
57
  [appBuiltinAlias, appSchemaCid.toString()],
@@ -3,10 +3,71 @@ import { lstat, readFile, realpath } from "node:fs/promises";
3
3
  import { basename, delimiter, dirname, join, resolve } from "node:path";
4
4
  import { promisify } from "node:util";
5
5
  import { StringDecoder } from "node:string_decoder";
6
+ import { isBuiltin } from "node:module";
7
+ import { compileFunction } from "node:vm";
6
8
  import { HostError, object } from "./errors.js";
7
9
  import { nodeError, type AuthoringOptions } from "./authoring.js";
8
10
  import { relativePath } from "./space.js";
9
11
 
12
+ export async function compileAppProgram(
13
+ input: string,
14
+ entry: string,
15
+ outfile: string,
16
+ prebuiltSource?: Uint8Array,
17
+ ) {
18
+ if (prebuiltSource) {
19
+ try {
20
+ const source = new TextDecoder("utf-8", { fatal: true }).decode(
21
+ prebuiltSource,
22
+ );
23
+ compileFunction(source.replace(/^#![^\r\n]*/, ""), [
24
+ "exports",
25
+ "require",
26
+ "module",
27
+ "__filename",
28
+ "__dirname",
29
+ ]);
30
+ } catch (cause) {
31
+ const error = new HostError(
32
+ "Prebuilt App entry must be valid Node CommonJS; it is packaged without transformation.",
33
+ );
34
+ error.cause = cause;
35
+ throw error;
36
+ }
37
+ }
38
+ const { build } = await import("esbuild");
39
+ const program = await build({
40
+ absWorkingDir: input,
41
+ entryPoints: [entry],
42
+ outfile,
43
+ bundle: true,
44
+ platform: "node",
45
+ format: "cjs",
46
+ target: "node22.15",
47
+ packages: prebuiltSource ? "external" : "bundle",
48
+ metafile: true,
49
+ write: false,
50
+ logLevel: "silent",
51
+ logOverride: {
52
+ "unsupported-dynamic-import": "error",
53
+ "unsupported-require-call": "error",
54
+ "indirect-require": "error",
55
+ },
56
+ });
57
+ if (
58
+ program.outputFiles.length !== 1 ||
59
+ program.outputFiles[0]!.contents.length > 8 * 1024 * 1024 ||
60
+ (prebuiltSource && Object.keys(program.metafile.inputs).length !== 1) ||
61
+ Object.values(program.metafile.outputs).some((file) =>
62
+ file.imports.some((item) => item.external && !isBuiltin(item.path)),
63
+ )
64
+ )
65
+ throw new HostError(
66
+ "App must bundle into one self-contained .cjs of at most 8 MiB; only Node builtins may remain external.",
67
+ );
68
+ return program;
69
+ }
70
+
10
71
  export interface ProjectBuild {
11
72
  strategy: "ipab-prebuild";
12
73
  typecheck: { config: string; entry: string }[];
@@ -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,14 +16,10 @@ 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);
25
-
26
23
  function appName(name: string) {
27
24
  if (
28
25
  !/^[a-z0-9][a-z0-9._-]*$/.test(name) ||
@@ -248,11 +245,18 @@ export async function renderTemplate(
248
245
  options: AuthoringOptions & { parameters?: readonly string[] },
249
246
  ) {
250
247
  const definition = templateDefinition(
251
- 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
+ ),
252
256
  );
253
257
  const values = templateParameters(definition, name, options.parameters);
254
258
  const files = new Map<string, string | Uint8Array>();
255
- const paths = new Map<string, string>();
259
+ const paths = new Map<string, TreeEntry["kind"]>();
256
260
  let count = 0,
257
261
  total = 0;
258
262
  const collect = async (directory: string, depth: number): Promise<void> => {
@@ -262,35 +266,8 @@ export async function renderTemplate(
262
266
  if (++count > 256)
263
267
  throw new HostError("Template exceeds 256 project paths.");
264
268
  const source = `${directory}/${entry.name}`;
265
- relativePath(source);
266
- const original = source.slice("project/".length);
267
- const rendered = entry.kind === "file" && original.endsWith(".liquid");
268
- const target = relativePath(
269
- rendered ? original.slice(0, -".liquid".length) : original,
270
- );
271
- if (
272
- target
273
- .split("/")
274
- .some(
275
- (part) =>
276
- ["node_modules", ".git", "dist"].includes(part.toLowerCase()) ||
277
- /^(?:con|prn|aux|nul|com[0-9]|lpt[0-9])(?:\.|$)/i.test(part),
278
- )
279
- )
280
- throw new HostError(`Unsafe template output path: ${target}.`);
281
- const key = target.toLowerCase();
282
- if (
283
- paths.has(key) ||
284
- [...paths].some(
285
- ([existing, kind]) =>
286
- (kind === "file" && key.startsWith(`${existing}/`)) ||
287
- (entry.kind === "file" && existing.startsWith(`${key}/`)),
288
- )
289
- )
290
- throw new HostError(
291
- `Duplicate or overlapping template output path: ${target}.`,
292
- );
293
- paths.set(key, entry.kind);
269
+ const { target, rendered } = templateOutputPath(source, entry.kind);
270
+ addTemplateOutputPath(paths, target, entry.kind);
294
271
  if (entry.kind === "directory") {
295
272
  await collect(source, depth + 1);
296
273
  continue;
@@ -325,39 +302,6 @@ export async function appTemplate(
325
302
  options: AuthoringOptions & { parameters?: readonly string[] },
326
303
  ): Promise<{ files: Map<string, string | Uint8Array>; cid: string | null }> {
327
304
  options.signal?.throwIfAborted();
328
- for (const [alias, name] of builtinTemplateNames)
329
- if (selector === alias) selector = name;
330
- if (selector === "node" || selector === "web-app") {
331
- appName(name);
332
- const loaded = await builtinTemplateSource(selector, options);
333
- const parameters = [...(options.parameters ?? [])];
334
- if (
335
- selector === "web-app" &&
336
- !parameters.some((value) => value.startsWith("webAppDependency="))
337
- ) {
338
- const framework = object(
339
- JSON.parse(
340
- await readFile(
341
- fileURLToPath(import.meta.resolve("@ipab/web-app/package.json")),
342
- "utf8",
343
- ),
344
- ) as unknown,
345
- "Web App framework package",
346
- );
347
- if (typeof framework.version !== "string" || !framework.version.trim())
348
- throw new HostError(
349
- "Web App framework package is missing its version.",
350
- );
351
- parameters.push(`webAppDependency=${framework.version}`);
352
- }
353
- return {
354
- files: await renderTemplate(loaded.tree, name, {
355
- ...options,
356
- parameters,
357
- }),
358
- cid: loaded.cid,
359
- };
360
- }
361
305
  let legacy = false;
362
306
  try {
363
307
  await lstat(resolve(selector, ".bundle.yaml"));
@@ -388,7 +332,7 @@ export async function appTemplate(
388
332
  } catch (error) {
389
333
  if (nodeError(error, "ENOENT"))
390
334
  throw new HostError(
391
- `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.`,
392
336
  );
393
337
  throw error;
394
338
  }