@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,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,
@@ -41,28 +56,18 @@ export async function initApp(
41
56
  parameters?: readonly string[];
42
57
  } = {},
43
58
  ) {
44
- if (
45
- !/^[a-z0-9][a-z0-9._-]*$/.test(name) ||
46
- name.length > 214 ||
47
- ["node_modules", "favicon.ico"].includes(name)
48
- )
49
- throw new HostError(
50
- "App NAME must be an unscoped lowercase npm package name.",
51
- );
52
59
  const { files, cid: templateCid } = await appTemplate(
53
60
  options.template ?? "node",
54
61
  name,
55
62
  options,
56
63
  );
57
- const schemaCid = (await builtinAppSchema()).cid.toString();
58
64
  const root = await scaffoldFiles(directory, files, options);
59
65
  return {
60
- kind: "app-init" as const,
66
+ kind: "project-init" as const,
61
67
  directory: root,
62
68
  name,
63
69
  template: options.template ?? "node",
64
70
  templateCid,
65
- schemaCid,
66
71
  files: [...files.keys()],
67
72
  };
68
73
  }
@@ -197,6 +202,9 @@ export async function buildApp(
197
202
  outputDir?: string;
198
203
  replace?: boolean;
199
204
  includeSource?: boolean;
205
+ /** Exact types from an author's Open App definition; exclusive with config paths. */
206
+ supportedTypes?: readonly BundleType[];
207
+ preparedDirectory?: string;
200
208
  } = {},
201
209
  ): Promise<
202
210
  Omit<BuildResult, "kind"> & {
@@ -337,11 +345,23 @@ export async function buildApp(
337
345
  throw new HostError(
338
346
  "App metadata cannot override the generated source declaration.",
339
347
  );
340
- 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
+ );
341
361
  const description = appDescription({
342
362
  ...defaults,
343
363
  ...overrides,
344
- entry: appSchema.entry.defaultValue.entry,
364
+ entry: "resources/app.cjs",
345
365
  ...(supportedSchemas ? { supportedSchemas: supportedSchemasPath } : {}),
346
366
  });
347
367
  if (description.icon !== undefined) {
@@ -422,7 +442,6 @@ export async function buildApp(
422
442
  options.signal?.throwIfAborted();
423
443
  const stage = await mkdtemp(join(root, ".ipab-app-"));
424
444
  try {
425
- const { build } = await import("esbuild");
426
445
  const bundleStage = join(stage, "bundle");
427
446
  const input = prebuild ? join(stage, "input") : root;
428
447
  if (prebuild) {
@@ -433,56 +452,13 @@ export async function buildApp(
433
452
  const prebuiltSource = prebuild
434
453
  ? await readBytes(inputTree, entry, 8 * 1024 * 1024, options)
435
454
  : undefined;
436
- if (prebuiltSource) {
437
- try {
438
- const source = new TextDecoder("utf-8", { fatal: true }).decode(
439
- prebuiltSource,
440
- );
441
- compileFunction(source.replace(/^#![^\r\n]*/, ""), [
442
- "exports",
443
- "require",
444
- "module",
445
- "__filename",
446
- "__dirname",
447
- ]);
448
- } catch (cause) {
449
- const error = new HostError(
450
- "Prebuilt App entry must be valid Node CommonJS; it is packaged without transformation.",
451
- );
452
- error.cause = cause;
453
- throw error;
454
- }
455
- }
456
455
  await mkdir(join(bundleStage, "resources"), { recursive: true });
457
- const program = await build({
458
- absWorkingDir: input,
459
- entryPoints: [entry],
460
- outfile: join(bundleStage, "resources", "app.cjs"),
461
- bundle: true,
462
- platform: "node",
463
- format: "cjs",
464
- target: "node22.15",
465
- packages: prebuild ? "external" : "bundle",
466
- metafile: true,
467
- write: false,
468
- logLevel: "silent",
469
- logOverride: {
470
- "unsupported-dynamic-import": "error",
471
- "unsupported-require-call": "error",
472
- "indirect-require": "error",
473
- },
474
- });
475
- if (
476
- program.outputFiles.length !== 1 ||
477
- program.outputFiles[0]!.contents.length > 8 * 1024 * 1024 ||
478
- (prebuild && Object.keys(program.metafile.inputs).length !== 1) ||
479
- Object.values(program.metafile.outputs).some((file) =>
480
- file.imports.some((item) => item.external && !isBuiltin(item.path)),
481
- )
482
- )
483
- throw new HostError(
484
- "App must bundle into one self-contained .cjs of at most 8 MiB; only Node builtins may remain external.",
485
- );
456
+ const program = await compileAppProgram(
457
+ input,
458
+ entry,
459
+ join(bundleStage, "resources", "app.cjs"),
460
+ prebuiltSource,
461
+ );
486
462
  if (sourceFiles && !prebuild)
487
463
  for (const path of Object.keys(program.metafile.inputs))
488
464
  if (
@@ -500,10 +476,27 @@ export async function buildApp(
500
476
  flag: "wx",
501
477
  signal: options.signal,
502
478
  });
503
- await writeFile(join(bundleStage, ".bundle.yaml"), stringify(appSchema), {
504
- flag: "wx",
505
- signal: options.signal,
506
- });
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
+ );
507
500
  if (supportedSchemas)
508
501
  await writeFile(
509
502
  join(bundleStage, ...supportedSchemasPath.split("/")),
@@ -515,7 +508,19 @@ export async function buildApp(
515
508
  if (++resourceCount > 4096)
516
509
  throw new HostError("App resource file count exceeds 4096.");
517
510
  relativePath(path);
518
- if (path.split("/").some((part) => part.startsWith(".")))
511
+ if (
512
+ path
513
+ .split("/")
514
+ .some(
515
+ (part, index, parts) =>
516
+ part.startsWith(".") &&
517
+ !(
518
+ [".bundle.yaml", ".bundle-type.yaml"].includes(part) &&
519
+ index === parts.length - 1 &&
520
+ index > 0
521
+ ),
522
+ )
523
+ )
519
524
  throw new HostError("Hidden prebuilt resources are not supported.");
520
525
  const bytes = await readBytes(inputTree, path, 16 * 1024 * 1024, options);
521
526
  const target = join(bundleStage, "resources", ...path.split("/"));
@@ -571,24 +576,100 @@ export async function buildApp(
571
576
  }),
572
577
  { flag: "wx", signal: options.signal },
573
578
  );
574
- const encoded = await encodeBundleCar(
575
- await filesystemTree(bundleStage),
576
- options,
577
- );
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
+ });
578
608
  try {
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),
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,
588
656
  options,
589
657
  ),
590
- }
591
- : 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
+ }
592
673
  return {
593
674
  kind: "app-bundle",
594
675
  cid: encoded.cid.toString(),
@@ -612,3 +693,88 @@ export async function buildApp(
612
693
  await rm(stage, { recursive: true, force: true });
613
694
  }
614
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,52 +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
- defaultValue: {
18
- name: "Untitled App",
19
- version: "0.0.0",
20
- description: "",
21
- entry: "resources/app.cjs",
22
- },
23
- schema: {
24
- $id: "urn:ipab:node-app.v1",
25
- type: "object",
26
- additionalProperties: false,
27
- required: ["name", "version", "entry"],
28
- properties: {
29
- name: { type: "string" },
30
- version: { type: "string" },
31
- description: { type: "string" },
32
- entry: { type: "string" },
33
- source: {
34
- type: "object",
35
- additionalProperties: false,
36
- required: ["cid"],
37
- properties: { cid: { type: "string" } },
38
- },
39
- supportedSchemas: { type: "string" },
40
- 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: {
41
40
  type: "object",
42
41
  additionalProperties: false,
43
- required: ["name"],
44
- properties: { name: { type: "string" }, email: { type: "string" } },
45
- },
46
- license: { type: "string" },
47
- icon: { type: "string" },
48
- i18n: {
49
- type: "object",
50
- additionalProperties: {
51
- type: "object",
52
- additionalProperties: false,
53
- properties: {
54
- name: { type: "string" },
55
- description: { type: "string" },
56
- },
42
+ properties: {
43
+ name: { type: "string" },
44
+ description: { type: "string" },
57
45
  },
58
46
  },
59
47
  },
@@ -63,7 +51,7 @@ export const appSchema = {
63
51
 
64
52
  // This is the canonical full definition, not a separate identity for the alias.
65
53
  export const appSchemaCid = CID.parse(
66
- "bafyreif6sc6dlltoddzfwdqcfgfaaquhrmjccu7wsmkqf6m62ruoxy43pq",
54
+ "bafyreifualoac4ddlzl63jlfstz4xsq75cai5t6ywu3j3khhyukoccmybm",
67
55
  );
68
56
  export const builtinTypes: ReadonlyMap<string, string> = new Map([
69
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 }[];