@ipab/cli 0.1.0-rc.1 → 0.1.0-rc.3
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.
- package/README.md +43 -6
- package/dist/app-authoring.d.ts +10 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +138 -13
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.js.map +1 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +2 -1
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts +7 -0
- package/dist/assembly.d.ts.map +1 -0
- package/dist/assembly.js +130 -0
- package/dist/assembly.js.map +1 -0
- package/dist/authoring.d.ts +4 -8
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +25 -15
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +18 -2
- package/dist/builtins.js.map +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +720 -76
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +3 -0
- package/dist/config-secret.d.ts.map +1 -0
- package/dist/config-secret.js +83 -0
- package/dist/config-secret.js.map +1 -0
- package/dist/config.d.ts +6 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +109 -7
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +9 -0
- package/dist/content-provider.d.ts.map +1 -0
- package/dist/content-provider.js +93 -0
- package/dist/content-provider.js.map +1 -0
- package/dist/get.d.ts +3 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +80 -0
- package/dist/get.js.map +1 -0
- package/dist/identity.d.ts +9 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +89 -0
- package/dist/identity.js.map +1 -0
- package/dist/kubo.d.ts +38 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +528 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +3 -3
- package/dist/pins.js +1 -1
- package/dist/pins.js.map +1 -1
- package/dist/setup.d.ts +7 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +22 -0
- package/dist/setup.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +17 -12
- package/dist/source-bundle.js.map +1 -1
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +2 -2
- package/dist/space.js.map +1 -1
- package/dist/state.d.ts +19 -4
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +105 -7
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +36 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +145 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +5 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +94 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm.d.ts +173 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +1245 -0
- package/dist/swarm.js.map +1 -0
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.js.map +1 -1
- package/dist/template-source.d.ts +2 -0
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +4 -3
- package/dist/template-source.js.map +1 -1
- package/package.json +10 -5
- package/src/app-authoring.ts +210 -21
- package/src/app-definition.ts +33 -39
- package/src/app-templates.ts +9 -1
- package/src/assembly.ts +185 -0
- package/src/authoring.ts +41 -17
- package/src/builtins.ts +23 -2
- package/src/cli.ts +875 -79
- package/src/config-secret.ts +96 -0
- package/src/config.ts +146 -7
- package/src/content-provider.ts +124 -0
- package/src/get.ts +90 -0
- package/src/identity.ts +133 -0
- package/src/kubo.ts +691 -0
- package/src/pins.ts +1 -1
- package/src/setup.ts +21 -0
- package/src/source-bundle.ts +26 -13
- package/src/space.ts +2 -5
- package/src/state.ts +121 -9
- package/src/swarm-envelope.ts +255 -0
- package/src/swarm-files.ts +100 -0
- package/src/swarm.ts +2025 -0
- package/src/template-definition.ts +36 -41
- package/src/template-source.ts +10 -3
package/src/app-authoring.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { lstat, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { isBuiltin } from "node:module";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
basename,
|
|
6
|
+
dirname,
|
|
7
|
+
isAbsolute,
|
|
8
|
+
join,
|
|
9
|
+
relative,
|
|
10
|
+
resolve,
|
|
11
|
+
sep,
|
|
12
|
+
} from "node:path";
|
|
5
13
|
import { StringDecoder } from "node:string_decoder";
|
|
6
14
|
import { compileFunction } from "node:vm";
|
|
7
15
|
import { stringify } from "yaml";
|
|
@@ -11,6 +19,7 @@ import { encodeBundleCar, memoryTree, readBytes } from "@ipab/bundle-builder";
|
|
|
11
19
|
import {
|
|
12
20
|
writeCar,
|
|
13
21
|
outputAvailable,
|
|
22
|
+
readCar,
|
|
14
23
|
scaffoldFiles,
|
|
15
24
|
nodeError,
|
|
16
25
|
type AuthoringOptions,
|
|
@@ -30,8 +39,11 @@ import {
|
|
|
30
39
|
import { appTemplate } from "./app-templates.js";
|
|
31
40
|
import { buildChain, projectBuild, runPrebuild } from "./app-prebuild.js";
|
|
32
41
|
import { captureSource, sourceFingerprint } from "./source-bundle.js";
|
|
42
|
+
export { appSchema } from "./app-definition.js";
|
|
33
43
|
|
|
34
44
|
const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
|
|
45
|
+
const sourceTarget = "source";
|
|
46
|
+
const sourceCarInput = "source.car";
|
|
35
47
|
|
|
36
48
|
export async function initApp(
|
|
37
49
|
name: string,
|
|
@@ -187,6 +199,7 @@ export async function buildApp(
|
|
|
187
199
|
outputDir?: string;
|
|
188
200
|
replace?: boolean;
|
|
189
201
|
includeSource?: boolean;
|
|
202
|
+
preparedDirectory?: string;
|
|
190
203
|
} = {},
|
|
191
204
|
): Promise<
|
|
192
205
|
Omit<BuildResult, "kind"> & {
|
|
@@ -490,10 +503,27 @@ export async function buildApp(
|
|
|
490
503
|
flag: "wx",
|
|
491
504
|
signal: options.signal,
|
|
492
505
|
});
|
|
493
|
-
await writeFile(
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
506
|
+
await writeFile(
|
|
507
|
+
join(bundleStage, ".bundle.yaml"),
|
|
508
|
+
stringify({
|
|
509
|
+
title: description.name,
|
|
510
|
+
description: description.description,
|
|
511
|
+
entry: "app.json",
|
|
512
|
+
type: ".bundle-type.yaml",
|
|
513
|
+
}),
|
|
514
|
+
{
|
|
515
|
+
flag: "wx",
|
|
516
|
+
signal: options.signal,
|
|
517
|
+
},
|
|
518
|
+
);
|
|
519
|
+
await writeFile(
|
|
520
|
+
join(bundleStage, ".bundle-type.yaml"),
|
|
521
|
+
stringify(appSchema),
|
|
522
|
+
{
|
|
523
|
+
flag: "wx",
|
|
524
|
+
signal: options.signal,
|
|
525
|
+
},
|
|
526
|
+
);
|
|
497
527
|
if (supportedSchemas)
|
|
498
528
|
await writeFile(
|
|
499
529
|
join(bundleStage, ...supportedSchemasPath.split("/")),
|
|
@@ -512,7 +542,7 @@ export async function buildApp(
|
|
|
512
542
|
(part, index, parts) =>
|
|
513
543
|
part.startsWith(".") &&
|
|
514
544
|
!(
|
|
515
|
-
|
|
545
|
+
[".bundle.yaml", ".bundle-type.yaml"].includes(part) &&
|
|
516
546
|
index === parts.length - 1 &&
|
|
517
547
|
index > 0
|
|
518
548
|
),
|
|
@@ -573,24 +603,100 @@ export async function buildApp(
|
|
|
573
603
|
}),
|
|
574
604
|
{ flag: "wx", signal: options.signal },
|
|
575
605
|
);
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
606
|
+
const source = encodedSource
|
|
607
|
+
? {
|
|
608
|
+
cid: encodedSource.cid.toString(),
|
|
609
|
+
schemaCid: encodedSource.schema.cid.toString(),
|
|
610
|
+
output: sourceOutput,
|
|
611
|
+
bytes: await writeCar(
|
|
612
|
+
encodedSource,
|
|
613
|
+
sourceOutput,
|
|
614
|
+
Boolean(options.replace),
|
|
615
|
+
options,
|
|
616
|
+
),
|
|
617
|
+
}
|
|
618
|
+
: undefined;
|
|
619
|
+
const sourceCar = source
|
|
620
|
+
? await readCar(source.output, options)
|
|
621
|
+
: undefined;
|
|
622
|
+
const encoded = await encodeBundleCar(await filesystemTree(bundleStage), {
|
|
623
|
+
...options,
|
|
624
|
+
imports:
|
|
625
|
+
encodedSource && sourceCar
|
|
626
|
+
? [
|
|
627
|
+
{
|
|
628
|
+
path: sourceTarget,
|
|
629
|
+
cid: encodedSource.cid,
|
|
630
|
+
resolveCid: sourceCar.resolveCid,
|
|
631
|
+
},
|
|
632
|
+
]
|
|
633
|
+
: [],
|
|
634
|
+
});
|
|
580
635
|
try {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
636
|
+
if (options.preparedDirectory) {
|
|
637
|
+
const preparedFiles = new Map<string, Uint8Array>();
|
|
638
|
+
const preparedTree = await filesystemTree(bundleStage);
|
|
639
|
+
const collect = async (path: string): Promise<void> => {
|
|
640
|
+
for (const item of await preparedTree.list(path, options)) {
|
|
641
|
+
const child = path ? `${path}/${item.name}` : item.name;
|
|
642
|
+
if (item.kind === "directory") await collect(child);
|
|
643
|
+
else
|
|
644
|
+
preparedFiles.set(
|
|
645
|
+
child,
|
|
646
|
+
await readBytes(
|
|
647
|
+
preparedTree,
|
|
648
|
+
child,
|
|
649
|
+
16 * 1024 * 1024,
|
|
650
|
+
options,
|
|
651
|
+
),
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
await collect("");
|
|
656
|
+
preparedFiles.set(
|
|
657
|
+
"ipab.build.yaml",
|
|
658
|
+
new TextEncoder().encode(
|
|
659
|
+
stringify({
|
|
660
|
+
version: 1,
|
|
661
|
+
files: [...preparedFiles.keys()]
|
|
662
|
+
.filter((path) => path !== ".bundle.yaml")
|
|
663
|
+
.map((path) => ({ source: path, target: path })),
|
|
664
|
+
imports: source
|
|
665
|
+
? [
|
|
666
|
+
{
|
|
667
|
+
cid: source.cid,
|
|
668
|
+
target: sourceTarget,
|
|
669
|
+
car: sourceCarInput,
|
|
670
|
+
},
|
|
671
|
+
]
|
|
672
|
+
: [],
|
|
673
|
+
}),
|
|
674
|
+
),
|
|
675
|
+
);
|
|
676
|
+
if (source)
|
|
677
|
+
preparedFiles.set(
|
|
678
|
+
sourceCarInput,
|
|
679
|
+
await readBytes(
|
|
680
|
+
await filesystemTree(dirname(source.output)),
|
|
681
|
+
basename(source.output),
|
|
682
|
+
source.bytes,
|
|
590
683
|
options,
|
|
591
684
|
),
|
|
592
|
-
|
|
593
|
-
|
|
685
|
+
);
|
|
686
|
+
await scaffoldFiles(
|
|
687
|
+
options.preparedDirectory,
|
|
688
|
+
preparedFiles,
|
|
689
|
+
options,
|
|
690
|
+
);
|
|
691
|
+
return {
|
|
692
|
+
kind: "app-bundle",
|
|
693
|
+
cid: encoded.cid.toString(),
|
|
694
|
+
schemaCid: encoded.schema.cid.toString(),
|
|
695
|
+
output,
|
|
696
|
+
bytes: 0,
|
|
697
|
+
...(source ? { source } : {}),
|
|
698
|
+
};
|
|
699
|
+
}
|
|
594
700
|
return {
|
|
595
701
|
kind: "app-bundle",
|
|
596
702
|
cid: encoded.cid.toString(),
|
|
@@ -614,3 +720,86 @@ export async function buildApp(
|
|
|
614
720
|
await rm(stage, { recursive: true, force: true });
|
|
615
721
|
}
|
|
616
722
|
}
|
|
723
|
+
|
|
724
|
+
/** Explicit project tool. The root CLI build never calls this helper. */
|
|
725
|
+
export async function prepareApp(
|
|
726
|
+
directory = ".",
|
|
727
|
+
options: AuthoringOptions & {
|
|
728
|
+
replace?: boolean;
|
|
729
|
+
includeSource?: boolean;
|
|
730
|
+
} = {},
|
|
731
|
+
) {
|
|
732
|
+
const root = resolve(directory);
|
|
733
|
+
const prepared = join(root, "dist", "prepared-app");
|
|
734
|
+
try {
|
|
735
|
+
const stat = await lstat(prepared);
|
|
736
|
+
if (stat.isSymbolicLink() || !stat.isDirectory() || !options.replace)
|
|
737
|
+
throw new HostError(
|
|
738
|
+
"Prepared output exists; explicit replacement requires a regular generated directory.",
|
|
739
|
+
);
|
|
740
|
+
const tree = await filesystemTree(prepared);
|
|
741
|
+
const recipe = object(
|
|
742
|
+
parseData(
|
|
743
|
+
await readBytes(tree, "ipab.build.yaml", 64 * 1024),
|
|
744
|
+
"application/yaml",
|
|
745
|
+
),
|
|
746
|
+
"prepared recipe",
|
|
747
|
+
);
|
|
748
|
+
if (
|
|
749
|
+
recipe.version !== 1 ||
|
|
750
|
+
!Array.isArray(recipe.files) ||
|
|
751
|
+
!Array.isArray(recipe.imports) ||
|
|
752
|
+
recipe.imports.length > 1
|
|
753
|
+
)
|
|
754
|
+
throw new HostError(
|
|
755
|
+
"Existing prepared output is not owned by the App preparation tool.",
|
|
756
|
+
);
|
|
757
|
+
const allowed = new Set([
|
|
758
|
+
".bundle.yaml",
|
|
759
|
+
"ipab.build.yaml",
|
|
760
|
+
...recipe.files.map((item: unknown) => {
|
|
761
|
+
const mapping = object(item, "prepared mapping");
|
|
762
|
+
if (
|
|
763
|
+
typeof mapping.source !== "string" ||
|
|
764
|
+
mapping.source !== mapping.target
|
|
765
|
+
)
|
|
766
|
+
throw new HostError("Existing prepared mapping is not generated.");
|
|
767
|
+
return relativePath(mapping.source);
|
|
768
|
+
}),
|
|
769
|
+
]);
|
|
770
|
+
if (recipe.imports.length) {
|
|
771
|
+
const sourceImport = object(recipe.imports[0], "prepared source import");
|
|
772
|
+
if (
|
|
773
|
+
sourceImport.target !== sourceTarget ||
|
|
774
|
+
sourceImport.car !== sourceCarInput ||
|
|
775
|
+
typeof sourceImport.cid !== "string"
|
|
776
|
+
)
|
|
777
|
+
throw new HostError(
|
|
778
|
+
"Existing prepared source import is not generated.",
|
|
779
|
+
);
|
|
780
|
+
allowed.add(sourceCarInput);
|
|
781
|
+
}
|
|
782
|
+
const verify = async (path: string): Promise<void> => {
|
|
783
|
+
for (const item of await tree.list(path)) {
|
|
784
|
+
const child = path ? `${path}/${item.name}` : item.name;
|
|
785
|
+
if (item.kind === "directory") await verify(child);
|
|
786
|
+
else if (!allowed.has(child))
|
|
787
|
+
throw new HostError(
|
|
788
|
+
"Prepared output contains custom files; choose a new output rather than deleting them.",
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
await verify("");
|
|
793
|
+
await rm(prepared, { recursive: true });
|
|
794
|
+
} catch (cause) {
|
|
795
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
796
|
+
}
|
|
797
|
+
const result = await buildApp(root, {
|
|
798
|
+
...options,
|
|
799
|
+
preparedDirectory: prepared,
|
|
800
|
+
});
|
|
801
|
+
return {
|
|
802
|
+
directory: prepared,
|
|
803
|
+
...(result.source ? { source: result.source } : {}),
|
|
804
|
+
};
|
|
805
|
+
}
|
package/src/app-definition.ts
CHANGED
|
@@ -8,46 +8,40 @@ import {
|
|
|
8
8
|
|
|
9
9
|
export const appBuiltinAlias = "builtin:node-app.v1";
|
|
10
10
|
export const appSchema = {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
type: "
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
"
|
|
54
|
+
"bafyreifualoac4ddlzl63jlfstz4xsq75cai5t6ywu3j3khhyukoccmybm",
|
|
61
55
|
);
|
|
62
56
|
export const builtinTypes: ReadonlyMap<string, string> = new Map([
|
|
63
57
|
[appBuiltinAlias, appSchemaCid.toString()],
|
package/src/app-templates.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { fileURLToPath } from "node:url";
|
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { lstat, readFile } from "node:fs/promises";
|
|
4
4
|
import { Liquid, type Template } from "liquidjs";
|
|
5
|
+
import { parseBundleManifest } from "@ipab/bundle";
|
|
5
6
|
import {
|
|
6
7
|
listTree,
|
|
7
8
|
readBytes,
|
|
@@ -248,7 +249,14 @@ export async function renderTemplate(
|
|
|
248
249
|
options: AuthoringOptions & { parameters?: readonly string[] },
|
|
249
250
|
) {
|
|
250
251
|
const definition = templateDefinition(
|
|
251
|
-
await readBytes(
|
|
252
|
+
await readBytes(
|
|
253
|
+
tree,
|
|
254
|
+
parseBundleManifest(
|
|
255
|
+
await readBytes(tree, ".bundle.yaml", 1024 * 1024, options),
|
|
256
|
+
).entry,
|
|
257
|
+
64 * 1024,
|
|
258
|
+
options,
|
|
259
|
+
),
|
|
252
260
|
);
|
|
253
261
|
const values = templateParameters(definition, name, options.parameters);
|
|
254
262
|
const files = new Map<string, string | Uint8Array>();
|
package/src/assembly.ts
ADDED
|
@@ -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
|
+
}
|