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

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 (101) hide show
  1. package/README.md +26 -4
  2. package/dist/app-authoring.d.ts +11 -2
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +135 -20
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/app-definition.d.ts +0 -6
  7. package/dist/app-definition.d.ts.map +1 -1
  8. package/dist/app-definition.js +1 -7
  9. package/dist/app-definition.js.map +1 -1
  10. package/dist/app-templates.d.ts.map +1 -1
  11. package/dist/app-templates.js +8 -0
  12. package/dist/app-templates.js.map +1 -1
  13. package/dist/assembly.d.ts +7 -0
  14. package/dist/assembly.d.ts.map +1 -0
  15. package/dist/assembly.js +131 -0
  16. package/dist/assembly.js.map +1 -0
  17. package/dist/authoring.d.ts +6 -15
  18. package/dist/authoring.d.ts.map +1 -1
  19. package/dist/authoring.js +13 -42
  20. package/dist/authoring.js.map +1 -1
  21. package/dist/builtins/template.node.car +0 -0
  22. package/dist/builtins/template.web-app.car +0 -0
  23. package/dist/builtins.d.ts.map +1 -1
  24. package/dist/builtins.js +5 -10
  25. package/dist/builtins.js.map +1 -1
  26. package/dist/cli.d.ts +1 -0
  27. package/dist/cli.d.ts.map +1 -1
  28. package/dist/cli.js +639 -147
  29. package/dist/cli.js.map +1 -1
  30. package/dist/config-secret.d.ts +3 -0
  31. package/dist/config-secret.d.ts.map +1 -0
  32. package/dist/config-secret.js +83 -0
  33. package/dist/config-secret.js.map +1 -0
  34. package/dist/config.d.ts +7 -4
  35. package/dist/config.d.ts.map +1 -1
  36. package/dist/config.js +118 -22
  37. package/dist/config.js.map +1 -1
  38. package/dist/content-provider.d.ts +9 -0
  39. package/dist/content-provider.d.ts.map +1 -0
  40. package/dist/content-provider.js +90 -0
  41. package/dist/content-provider.js.map +1 -0
  42. package/dist/context.d.ts +1 -0
  43. package/dist/context.d.ts.map +1 -1
  44. package/dist/context.js +10 -0
  45. package/dist/context.js.map +1 -1
  46. package/dist/get.d.ts +3 -0
  47. package/dist/get.d.ts.map +1 -0
  48. package/dist/get.js +61 -0
  49. package/dist/get.js.map +1 -0
  50. package/dist/identity.d.ts +9 -0
  51. package/dist/identity.d.ts.map +1 -0
  52. package/dist/identity.js +89 -0
  53. package/dist/identity.js.map +1 -0
  54. package/dist/ipc.d.ts +2 -1
  55. package/dist/ipc.d.ts.map +1 -1
  56. package/dist/ipc.js +10 -1
  57. package/dist/ipc.js.map +1 -1
  58. package/dist/pins.d.ts +9 -9
  59. package/dist/pins.d.ts.map +1 -1
  60. package/dist/pins.js +10 -28
  61. package/dist/pins.js.map +1 -1
  62. package/dist/runtime.d.ts.map +1 -1
  63. package/dist/runtime.js +1 -1
  64. package/dist/runtime.js.map +1 -1
  65. package/dist/source-bundle.d.ts +0 -1
  66. package/dist/source-bundle.d.ts.map +1 -1
  67. package/dist/source-bundle.js +1 -2
  68. package/dist/source-bundle.js.map +1 -1
  69. package/dist/state.d.ts +35 -10
  70. package/dist/state.d.ts.map +1 -1
  71. package/dist/state.js +124 -14
  72. package/dist/state.js.map +1 -1
  73. package/dist/swarm.d.ts +113 -0
  74. package/dist/swarm.d.ts.map +1 -0
  75. package/dist/swarm.js +760 -0
  76. package/dist/swarm.js.map +1 -0
  77. package/dist/template-definition.d.ts +0 -17
  78. package/dist/template-definition.d.ts.map +1 -1
  79. package/dist/template-definition.js +1 -10
  80. package/dist/template-definition.js.map +1 -1
  81. package/package.json +15 -10
  82. package/src/app-authoring.ts +203 -29
  83. package/src/app-definition.ts +1 -7
  84. package/src/app-templates.ts +13 -0
  85. package/src/assembly.ts +182 -0
  86. package/src/authoring.ts +32 -58
  87. package/src/builtins.ts +5 -11
  88. package/src/cli.ts +785 -167
  89. package/src/config-secret.ts +96 -0
  90. package/src/config.ts +161 -25
  91. package/src/content-provider.ts +123 -0
  92. package/src/context.ts +10 -0
  93. package/src/get.ts +68 -0
  94. package/src/identity.ts +133 -0
  95. package/src/ipc.ts +8 -0
  96. package/src/pins.ts +14 -28
  97. package/src/runtime.ts +1 -0
  98. package/src/source-bundle.ts +1 -2
  99. package/src/state.ts +149 -24
  100. package/src/swarm.ts +1186 -0
  101. package/src/template-definition.ts +1 -10
@@ -0,0 +1,182 @@
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
+ !files.has(definition.entry.path) &&
158
+ !imports.some((item) => item.path === definition.entry.path)
159
+ )
160
+ throw new HostError("Recipe must explicitly select the Bundle entry.");
161
+ const output = options.output
162
+ ? resolve(options.output)
163
+ : join(root, options.outputDir ?? "dist", "bundle.car");
164
+ if (inputs.has(output))
165
+ throw new HostError("CAR output cannot replace recipe or source inputs.");
166
+ await outputAvailable(output, Boolean(options.replace));
167
+ const encoded = await encodeBundleCar(fileTree(files), {
168
+ ...options,
169
+ imports,
170
+ });
171
+ const result = {
172
+ kind: "content-bundle" as const,
173
+ cid: encoded.cid.toString(),
174
+ schemaCid: encoded.schema.cid.toString(),
175
+ output,
176
+ validation: encoded.validation,
177
+ };
178
+ return {
179
+ ...result,
180
+ bytes: await writeCar(encoded, output, Boolean(options.replace), options),
181
+ };
182
+ }
package/src/authoring.ts CHANGED
@@ -22,7 +22,6 @@ 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,
@@ -31,7 +30,11 @@ import {
31
30
  parseBundleManifest,
32
31
  ContentBundleError,
33
32
  } from "@ipab/bundle";
34
- import type { OpenBundleOptions, SchemaBlock } from "@ipab/bundle";
33
+ import type {
34
+ EntryValidation,
35
+ OpenBundleOptions,
36
+ SchemaBlock,
37
+ } from "@ipab/bundle";
35
38
  import {
36
39
  encodeBundleCar,
37
40
  encodeSchemaCar,
@@ -59,6 +62,7 @@ export interface BuildResult {
59
62
  schemaCid: string;
60
63
  output: string;
61
64
  bytes: number;
65
+ validation?: EntryValidation | null;
62
66
  }
63
67
  export interface InspectResult {
64
68
  kind: "content-bundle" | "bundle-schema";
@@ -68,15 +72,11 @@ export interface InspectResult {
68
72
  name: string;
69
73
  description: string;
70
74
  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[];
75
+ validation: {
76
+ definition: "valid";
77
+ storage: "verified" | "not-applicable";
78
+ entry: EntryValidation | null;
79
+ };
80
80
  }
81
81
  export interface ImportResult {
82
82
  kind: "bundle-import";
@@ -85,6 +85,7 @@ export interface ImportResult {
85
85
  source: string;
86
86
  blocks: number;
87
87
  bytes: number;
88
+ validation: EntryValidation | null;
88
89
  }
89
90
  export function nodeError(error: unknown, code: string): boolean {
90
91
  return error instanceof Error && "code" in error && error.code === code;
@@ -166,9 +167,16 @@ export async function selectedTree(
166
167
  );
167
168
  return filesystemTree(root, { exclude: excluded });
168
169
  }
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);
170
+ export async function readCar(
171
+ path: string,
172
+ options: AuthoringOptions,
173
+ directory = dirname(path),
174
+ ) {
175
+ const tree = await filesystemTree(directory);
176
+ const input = await tree.read(
177
+ relative(directory, path).split(sep).join("/"),
178
+ options,
179
+ );
172
180
  const chunks = streamChunks(input, options);
173
181
  let bytes = 0;
174
182
  try {
@@ -299,6 +307,7 @@ async function fromRoot(
299
307
  });
300
308
  return {
301
309
  schema: bundle.schema,
310
+ validation: bundle.validation,
302
311
  kind: "content-bundle" as const,
303
312
  sourceKind,
304
313
  cid: root.toString(),
@@ -334,7 +343,11 @@ export async function inspectBundle(
334
343
  sourceKind: "directory",
335
344
  cid: null,
336
345
  ...summary(encoded.schema),
337
- validation: { status: "valid", storage: "verified" },
346
+ validation: {
347
+ definition: "valid",
348
+ storage: "verified",
349
+ entry: encoded.validation,
350
+ },
338
351
  };
339
352
  } finally {
340
353
  await encoded.close();
@@ -347,8 +360,9 @@ export async function inspectBundle(
347
360
  cid: loaded.cid,
348
361
  ...summary(loaded.schema),
349
362
  validation: {
350
- status: "valid",
363
+ definition: "valid",
351
364
  storage: loaded.kind === "content-bundle" ? "verified" : "not-applicable",
365
+ entry: loaded.kind === "content-bundle" ? loaded.validation : null,
352
366
  },
353
367
  };
354
368
  }
@@ -378,6 +392,7 @@ export async function importBundle(
378
392
  cid,
379
393
  schemaCid: loaded.schema.cid.toString(),
380
394
  source: path,
395
+ validation: loaded.kind === "content-bundle" ? loaded.validation : null,
381
396
  blocks: car.blocks.size,
382
397
  bytes: car.bytes,
383
398
  };
@@ -490,54 +505,13 @@ export async function buildBundle(
490
505
  cid: encoded.cid.toString(),
491
506
  schemaCid: encoded.schema.cid.toString(),
492
507
  output,
508
+ validation: encoded.validation,
493
509
  };
494
510
  return {
495
511
  ...result,
496
512
  bytes: await writeCar(encoded, output, Boolean(options.replace), options),
497
513
  };
498
514
  }
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
515
  export async function scaffoldFiles(
542
516
  directory: string,
543
517
  files: ReadonlyMap<string, string | Uint8Array>,
package/src/builtins.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { appBuiltinAlias, builtinAppSchema } from "./app-definition.js";
1
+ import { builtinAppSchema } from "./app-definition.js";
2
2
  import {
3
3
  builtinTemplateSchema,
4
- templateBuiltinAlias,
5
4
  templateSchema,
6
5
  templateSchemaCid,
7
6
  } from "./template-definition.js";
@@ -25,17 +24,12 @@ async function loadBuiltins(): Promise<{
25
24
  builtin: true,
26
25
  });
27
26
  };
28
- for (const [name, schema] of [
29
- [appBuiltinAlias, await builtinAppSchema()],
30
- [templateBuiltinAlias, await builtinTemplateSchema()],
31
- ] as const) {
27
+ for (const schema of [
28
+ await builtinAppSchema(),
29
+ await builtinTemplateSchema(),
30
+ ]) {
32
31
  const cid = schema.cid.toString();
33
32
  blocks.set(cid, schema.bytes);
34
- add(name, cid, {
35
- kind: "schema",
36
- typeName: schema.manifest.name,
37
- schemaCid: cid,
38
- });
39
33
  }
40
34
  for (const [name, selector] of builtinTemplateNames) {
41
35
  const template = await builtinTemplateSource(selector);