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

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 (107) hide show
  1. package/README.md +40 -13
  2. package/dist/app-authoring.d.ts.map +1 -1
  3. package/dist/app-authoring.js +11 -2
  4. package/dist/app-authoring.js.map +1 -1
  5. package/dist/app-definition.d.ts +52 -57
  6. package/dist/app-definition.d.ts.map +1 -1
  7. package/dist/app-definition.js +33 -38
  8. package/dist/app-definition.js.map +1 -1
  9. package/dist/app-templates.d.ts.map +1 -1
  10. package/dist/app-templates.js +2 -1
  11. package/dist/app-templates.js.map +1 -1
  12. package/dist/assembly.d.ts.map +1 -1
  13. package/dist/assembly.js +3 -4
  14. package/dist/assembly.js.map +1 -1
  15. package/dist/authoring.d.ts +3 -7
  16. package/dist/authoring.d.ts.map +1 -1
  17. package/dist/authoring.js +22 -12
  18. package/dist/authoring.js.map +1 -1
  19. package/dist/builtins/skill.ipab.car +0 -0
  20. package/dist/builtins/template.node.car +0 -0
  21. package/dist/builtins/template.web-app.car +0 -0
  22. package/dist/builtins.d.ts.map +1 -1
  23. package/dist/builtins.js +18 -2
  24. package/dist/builtins.js.map +1 -1
  25. package/dist/cli.d.ts.map +1 -1
  26. package/dist/cli.js +367 -124
  27. package/dist/cli.js.map +1 -1
  28. package/dist/config.d.ts +2 -1
  29. package/dist/config.d.ts.map +1 -1
  30. package/dist/config.js +29 -16
  31. package/dist/config.js.map +1 -1
  32. package/dist/content-provider.d.ts +1 -1
  33. package/dist/content-provider.d.ts.map +1 -1
  34. package/dist/content-provider.js +5 -2
  35. package/dist/content-provider.js.map +1 -1
  36. package/dist/get.d.ts.map +1 -1
  37. package/dist/get.js +34 -15
  38. package/dist/get.js.map +1 -1
  39. package/dist/identity.d.ts +5 -0
  40. package/dist/identity.d.ts.map +1 -1
  41. package/dist/identity.js +5 -0
  42. package/dist/identity.js.map +1 -1
  43. package/dist/kubo.d.ts +40 -0
  44. package/dist/kubo.d.ts.map +1 -0
  45. package/dist/kubo.js +555 -0
  46. package/dist/kubo.js.map +1 -0
  47. package/dist/pins.d.ts +3 -3
  48. package/dist/pins.js +1 -1
  49. package/dist/pins.js.map +1 -1
  50. package/dist/setup.d.ts +7 -0
  51. package/dist/setup.d.ts.map +1 -0
  52. package/dist/setup.js +22 -0
  53. package/dist/setup.js.map +1 -0
  54. package/dist/source-bundle.d.ts +3 -8
  55. package/dist/source-bundle.d.ts.map +1 -1
  56. package/dist/source-bundle.js +16 -11
  57. package/dist/source-bundle.js.map +1 -1
  58. package/dist/space.d.ts.map +1 -1
  59. package/dist/space.js +2 -2
  60. package/dist/space.js.map +1 -1
  61. package/dist/state.d.ts +7 -5
  62. package/dist/state.d.ts.map +1 -1
  63. package/dist/state.js +17 -14
  64. package/dist/state.js.map +1 -1
  65. package/dist/swarm-envelope.d.ts +36 -0
  66. package/dist/swarm-envelope.d.ts.map +1 -0
  67. package/dist/swarm-envelope.js +145 -0
  68. package/dist/swarm-envelope.js.map +1 -0
  69. package/dist/swarm-files.d.ts +6 -0
  70. package/dist/swarm-files.d.ts.map +1 -0
  71. package/dist/swarm-files.js +107 -0
  72. package/dist/swarm-files.js.map +1 -0
  73. package/dist/swarm.d.ts +120 -58
  74. package/dist/swarm.d.ts.map +1 -1
  75. package/dist/swarm.js +1065 -548
  76. package/dist/swarm.js.map +1 -1
  77. package/dist/template-definition.d.ts +47 -52
  78. package/dist/template-definition.d.ts.map +1 -1
  79. package/dist/template-definition.js +36 -41
  80. package/dist/template-definition.js.map +1 -1
  81. package/dist/template-source.d.ts +2 -0
  82. package/dist/template-source.d.ts.map +1 -1
  83. package/dist/template-source.js +4 -3
  84. package/dist/template-source.js.map +1 -1
  85. package/package.json +3 -3
  86. package/src/app-authoring.ts +22 -5
  87. package/src/app-definition.ts +33 -39
  88. package/src/app-templates.ts +9 -1
  89. package/src/assembly.ts +7 -4
  90. package/src/authoring.ts +31 -14
  91. package/src/builtins.ts +23 -2
  92. package/src/cli.ts +427 -147
  93. package/src/config.ts +32 -19
  94. package/src/content-provider.ts +5 -4
  95. package/src/get.ts +37 -15
  96. package/src/identity.ts +13 -0
  97. package/src/kubo.ts +732 -0
  98. package/src/pins.ts +1 -1
  99. package/src/setup.ts +21 -0
  100. package/src/source-bundle.ts +25 -12
  101. package/src/space.ts +2 -5
  102. package/src/state.ts +24 -15
  103. package/src/swarm-envelope.ts +255 -0
  104. package/src/swarm-files.ts +116 -0
  105. package/src/swarm.ts +1728 -836
  106. package/src/template-definition.ts +36 -41
  107. package/src/template-source.ts +10 -3
package/src/config.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { homedir } from "node:os";
2
- import { CID } from "multiformats/cid";
3
2
  import { join, resolve } from "node:path";
4
3
  import { readContentConfiguration } from "@ipab/ipfs-store/node";
5
4
  import type { StoreConfiguration } from "@ipab/ipfs-store";
@@ -7,8 +6,7 @@ import { HostError, object, syntax } from "./errors.js";
7
6
 
8
7
  export type ConfigKey =
9
8
  | "swarm.default"
10
- | "swarm.service"
11
- | "swarm.provider"
9
+ | "swarm.listen"
12
10
  | "swarm.cacheTtl"
13
11
  | "swarm.forceRefresh"
14
12
  | "store.gateway"
@@ -33,8 +31,7 @@ export interface ConfigItem {
33
31
  }
34
32
  export const defaults: Record<ConfigKey, ConfigValue> = {
35
33
  "swarm.default": null,
36
- "swarm.service": null,
37
- "swarm.provider": "http://127.0.0.1:5001",
34
+ "swarm.listen": "lan",
38
35
  "swarm.cacheTtl": 300,
39
36
  "swarm.forceRefresh": false,
40
37
  "store.gateway": null,
@@ -46,8 +43,7 @@ export const defaults: Record<ConfigKey, ConfigValue> = {
46
43
  "bundle.outputDir": "dist",
47
44
  };
48
45
  const environments: Partial<Record<ConfigKey, string>> = {
49
- "swarm.service": "IPAB_SWARM_SERVICE",
50
- "swarm.provider": "IPAB_SWARM_PROVIDER",
46
+ "swarm.listen": "IPAB_SWARM_LISTEN",
51
47
  "swarm.cacheTtl": "IPAB_SWARM_CACHE_TTL",
52
48
  "swarm.forceRefresh": "IPAB_SWARM_FORCE_REFRESH",
53
49
  "store.gateway": "IPAB_STORE_GATEWAY",
@@ -128,7 +124,9 @@ export function parseValue(
128
124
  ): ConfigValue {
129
125
  if (text && typeof value === "string") {
130
126
  if (key.startsWith("swarm.") && value === "default")
131
- return key === "swarm.cacheTtl" || key === "swarm.forceRefresh"
127
+ return key === "swarm.cacheTtl" ||
128
+ key === "swarm.forceRefresh" ||
129
+ key === "swarm.listen"
132
130
  ? defaults[key]
133
131
  : null;
134
132
  if (key === "history.enabled" || key === "swarm.forceRefresh") {
@@ -182,19 +180,14 @@ export function parseValue(
182
180
  if (typeof value === "boolean") return value;
183
181
  syntax("swarm.forceRefresh requires true or false.");
184
182
  }
183
+ if (key === "swarm.listen") {
184
+ if (value === "lan" || value === "loopback") return value;
185
+ syntax("swarm.listen requires lan or loopback.");
186
+ }
185
187
  if (key.startsWith("swarm.") && value === null) return null;
186
188
  if (key === "swarm.default" && typeof value === "string") {
187
- try {
188
- return CID.parse(value).toV1().toString();
189
- } catch {
190
- syntax("swarm.default requires an exact Swarm Bundle CID.");
191
- }
189
+ return swarmUrl(value);
192
190
  }
193
- if (
194
- (key === "swarm.service" || key === "swarm.provider") &&
195
- typeof value === "string"
196
- )
197
- return publicUrl(value);
198
191
  if (key === "store.gateway" && value === null) return null;
199
192
  if (
200
193
  key === "bundle.outputDir" &&
@@ -230,6 +223,26 @@ export function parseValue(
230
223
  );
231
224
  }
232
225
 
226
+ export function swarmUrl(value: string): string {
227
+ try {
228
+ const url = new URL(value);
229
+ pointerService(url.origin);
230
+ if (
231
+ url.username ||
232
+ url.password ||
233
+ url.search ||
234
+ url.hash ||
235
+ url.toString() !== value ||
236
+ !/^\/v1\/[a-f0-9]{16}\/[a-f0-9]{16}$/.test(url.pathname)
237
+ )
238
+ throw new Error();
239
+ return value;
240
+ } catch {
241
+ return syntax(
242
+ "Swarm requires an exact /v1/{scope}/{variable} configuration URL; old CID protocol is unsupported.",
243
+ );
244
+ }
245
+ }
233
246
  export function validateUserConfig(
234
247
  value: unknown,
235
248
  ): Partial<Record<ConfigKey, ConfigValue>> {
@@ -283,7 +296,7 @@ export function effectiveConfig(
283
296
  ): ConfigItem[] {
284
297
  if (env.IPAB_SWARM !== undefined)
285
298
  syntax(
286
- "IPAB_SWARM is no longer supported. Remove it and use --swarm CID_OR_ALIAS or ipab swarm use CID_OR_ALIAS.",
299
+ "IPAB_SWARM is no longer supported. Remove it and use --swarm URL_OR_ALIAS or ipab swarm use URL_OR_ALIAS.",
287
300
  );
288
301
  if (env.IPAB_STORE_RPC !== undefined)
289
302
  syntax(
@@ -59,6 +59,7 @@ export async function provideContent(
59
59
  roots: readonly string[],
60
60
  endpoint: string,
61
61
  fetcher: typeof fetch = fetch,
62
+ authorizationToken?: string,
62
63
  ): Promise<void> {
63
64
  const url = new URL(publicUrl(endpoint));
64
65
  if (
@@ -78,6 +79,9 @@ export async function provideContent(
78
79
  `${url.toString().replace(/\/$/, "")}/api/v0/dag/import?pin-roots=true`,
79
80
  {
80
81
  method: "POST",
82
+ ...(authorizationToken
83
+ ? { headers: { authorization: `Bearer ${authorizationToken}` } }
84
+ : {}),
81
85
  body: data,
82
86
  redirect: "error",
83
87
  signal: AbortSignal.timeout(30_000),
@@ -113,10 +117,7 @@ export async function bundleJson(store: IpfsStore, root: string) {
113
117
  });
114
118
  const value: unknown = JSON.parse(
115
119
  new TextDecoder("utf-8", { fatal: true }).decode(
116
- await bytes(
117
- await bundle.readFile(bundle.manifest.entry.path),
118
- 1024 * 1024,
119
- ),
120
+ await bytes(await bundle.readFile(bundle.manifest.entry), 1024 * 1024),
120
121
  ),
121
122
  );
122
123
  return { bundle, value };
package/src/get.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { dirname, resolve } from "node:path";
2
- import { lstat } from "node:fs/promises";
1
+ import { dirname, join, resolve } from "node:path";
2
+ import { lstat, mkdtemp, realpath, rmdir } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
3
4
  import { openBundle, bundleLimits } from "@ipab/bundle";
4
5
  import { CID } from "multiformats/cid";
5
6
  import type { IpfsStore } from "@ipab/ipfs-store";
@@ -10,7 +11,7 @@ import { HostError } from "./errors.js";
10
11
  export async function getBundle(
11
12
  store: IpfsStore,
12
13
  cid: string,
13
- output = cid,
14
+ output?: string,
14
15
  ): Promise<string> {
15
16
  await openBundle(CID.parse(cid), {
16
17
  resolveCid: (cid) => store.block.get(cid.toString()),
@@ -51,18 +52,39 @@ export async function getBundle(
51
52
  }
52
53
  };
53
54
  await visit(CID.parse(cid), "", 0);
54
- const directory = resolve(output);
55
- for (let current = directory; ; current = dirname(current)) {
56
- try {
57
- const stat = await lstat(current);
58
- if (stat.isSymbolicLink() || !stat.isDirectory())
59
- throw new HostError(
60
- "Download destination must not cross a symlink or file.",
61
- );
62
- } catch (cause) {
63
- if (!nodeError(cause, "ENOENT")) throw cause;
55
+ const directory =
56
+ output === undefined
57
+ ? await mkdtemp(join(await realpath(tmpdir()), "ipab-get-"))
58
+ : resolve(output);
59
+ try {
60
+ for (let current = directory; ; current = dirname(current)) {
61
+ try {
62
+ const stat = await lstat(current);
63
+ if (stat.isSymbolicLink() || !stat.isDirectory())
64
+ throw new HostError(
65
+ "Download destination must not cross a symlink or file.",
66
+ );
67
+ } catch (cause) {
68
+ if (!nodeError(cause, "ENOENT")) throw cause;
69
+ }
70
+ if (dirname(current) === current) break;
71
+ }
72
+ return await scaffoldFiles(directory, files);
73
+ } catch (cause) {
74
+ const failures: unknown[] = [cause];
75
+ if (output === undefined) {
76
+ try {
77
+ await rmdir(directory);
78
+ } catch (error) {
79
+ failures.push(error);
80
+ }
64
81
  }
65
- if (dirname(current) === current) break;
82
+ if (failures.length > 1)
83
+ throw new AggregateError(
84
+ failures,
85
+ "Download failed and temporary directory cleanup also failed.",
86
+ { cause },
87
+ );
88
+ throw cause;
66
89
  }
67
- return scaffoldFiles(directory, files);
68
90
  }
package/src/identity.ts CHANGED
@@ -29,6 +29,19 @@ export async function protectedPath(
29
29
  await requireAccountOnly(path);
30
30
  }
31
31
 
32
+ export interface ProtectedPath {
33
+ path: string;
34
+ directory?: boolean;
35
+ }
36
+
37
+ export async function protectedPaths(
38
+ paths: readonly ProtectedPath[],
39
+ ): Promise<void> {
40
+ for (const entry of paths)
41
+ await regularPath(entry.path, entry.directory ?? false);
42
+ await requireAccountOnlyMany(paths.map((entry) => entry.path));
43
+ }
44
+
32
45
  async function regularPath(path: string, directory: boolean): Promise<void> {
33
46
  const stat = await lstat(path);
34
47
  if (