@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
@@ -0,0 +1,14 @@
1
+ import { syntax } from "./errors.js";
2
+ export { verifyStateStorage as secureConfigurationStorage } from "./state-storage.js";
3
+
4
+ export async function readSecretStream(
5
+ input: AsyncIterable<string>,
6
+ ): Promise<string> {
7
+ let text = "";
8
+ for await (const chunk of input) {
9
+ if (Buffer.byteLength(text) + Buffer.byteLength(chunk) > 64)
10
+ syntax("API key input exceeds 64 bytes.");
11
+ text += chunk;
12
+ }
13
+ return text.trim();
14
+ }
package/src/config.ts CHANGED
@@ -1,11 +1,21 @@
1
1
  import { homedir } from "node:os";
2
2
  import { join, resolve } from "node:path";
3
+ import { CID } from "multiformats/cid";
3
4
  import { readContentConfiguration } from "@ipab/ipfs-store/node";
4
5
  import type { StoreConfiguration } from "@ipab/ipfs-store";
5
- import { HostError, object, syntax } from "./errors.js";
6
+ import { HostError, object, syntax } from "./host-error.js";
6
7
 
7
8
  export type ConfigKey =
9
+ | "daemon.uiPort"
10
+ | "swarm.default"
11
+ | "swarm.listen"
12
+ | "swarm.pivot"
13
+ | "swarm.cacheTtl"
14
+ | "swarm.syncInterval"
15
+ | "swarm.forceRefresh"
8
16
  | "store.gateway"
17
+ | "pointers.service"
18
+ | "pointers.apiKey"
9
19
  | "history.enabled"
10
20
  | "history.limit"
11
21
  | "output.retention"
@@ -13,6 +23,7 @@ export type ConfigKey =
13
23
  export type ConfigValue = string | boolean | number | null;
14
24
  export type Source = "flag" | "env" | "user" | "default";
15
25
  export interface GlobalOptions {
26
+ swarm?: string;
16
27
  stateDir?: string;
17
28
  storeGateway?: string;
18
29
  json: boolean;
@@ -22,15 +33,49 @@ export interface ConfigItem {
22
33
  value: ConfigValue;
23
34
  source: Source;
24
35
  }
36
+ export const LOCAL_SWARM_ALIAS = "local";
37
+ export const LOCAL_SWARM_URL =
38
+ "https://example.invalid/v1/0000000000000000/0000000000000000";
39
+
40
+ export function isLocalSwarm(value: string): boolean {
41
+ return value === LOCAL_SWARM_URL;
42
+ }
43
+
44
+ export function isReservedLocalHost(value: string): boolean {
45
+ try {
46
+ const host = new URL(value).hostname.toLowerCase();
47
+ return host === "invalid" || host.endsWith(".invalid");
48
+ } catch {
49
+ return false;
50
+ }
51
+ }
52
+
25
53
  export const defaults: Record<ConfigKey, ConfigValue> = {
54
+ "daemon.uiPort": 0,
55
+ "swarm.default": LOCAL_SWARM_URL,
56
+ "swarm.listen": "lan",
57
+ "swarm.pivot": false,
58
+ "swarm.cacheTtl": 300,
59
+ "swarm.syncInterval": 60,
60
+ "swarm.forceRefresh": false,
26
61
  "store.gateway": null,
62
+ "pointers.service": null,
63
+ "pointers.apiKey": null,
27
64
  "history.enabled": true,
28
65
  "history.limit": 20,
29
66
  "output.retention": "history",
30
67
  "bundle.outputDir": "dist",
31
68
  };
32
- const environments: Record<ConfigKey, string> = {
69
+ const environments: Partial<Record<ConfigKey, string>> = {
70
+ "daemon.uiPort": "IPAB_DAEMON_UI_PORT",
71
+ "swarm.listen": "IPAB_SWARM_LISTEN",
72
+ "swarm.pivot": "IPAB_SWARM_PIVOT",
73
+ "swarm.cacheTtl": "IPAB_SWARM_CACHE_TTL",
74
+ "swarm.syncInterval": "IPAB_SWARM_SYNC_INTERVAL",
75
+ "swarm.forceRefresh": "IPAB_SWARM_FORCE_REFRESH",
33
76
  "store.gateway": "IPAB_STORE_GATEWAY",
77
+ "pointers.service": "IPAB_POINTERS_SERVICE",
78
+ "pointers.apiKey": "IPAB_POINTERS_API_KEY",
34
79
  "history.enabled": "IPAB_HISTORY_ENABLED",
35
80
  "history.limit": "IPAB_HISTORY_LIMIT",
36
81
  "output.retention": "IPAB_OUTPUT_RETENTION",
@@ -65,19 +110,133 @@ export function publicUrl(value: string): string {
65
110
  return url.toString();
66
111
  }
67
112
 
113
+ export function pointerService(value: string): string {
114
+ let url: URL;
115
+ try {
116
+ url = new URL(value);
117
+ } catch {
118
+ syntax(
119
+ "Pointer service must be a canonical HTTPS origin (HTTP loopback allowed).",
120
+ );
121
+ }
122
+ if (
123
+ url.username ||
124
+ url.password ||
125
+ url.search ||
126
+ url.hash ||
127
+ url.pathname !== "/" ||
128
+ (url.protocol !== "https:" &&
129
+ !(
130
+ url.protocol === "http:" &&
131
+ ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
132
+ )) ||
133
+ value !== url.origin
134
+ )
135
+ syntax(
136
+ "Pointer service must be a canonical HTTPS origin (HTTP loopback allowed).",
137
+ );
138
+ return url.origin;
139
+ }
140
+
141
+ export function visibleConfig(item: ConfigItem): ConfigItem {
142
+ return item.key === "pointers.apiKey" && item.value !== null
143
+ ? { ...item, value: "<redacted>" }
144
+ : item;
145
+ }
146
+
68
147
  export function parseValue(
69
148
  key: ConfigKey,
70
149
  value: unknown,
71
150
  text = false,
72
151
  ): ConfigValue {
73
152
  if (text && typeof value === "string") {
74
- if (key === "history.enabled") {
153
+ if (key === "daemon.uiPort" && value === "default") return defaults[key];
154
+ if (key.startsWith("swarm.") && value === "default")
155
+ return key === "swarm.default" ||
156
+ key === "swarm.cacheTtl" ||
157
+ key === "swarm.syncInterval" ||
158
+ key === "swarm.forceRefresh" ||
159
+ key === "swarm.pivot" ||
160
+ key === "swarm.listen"
161
+ ? defaults[key]
162
+ : null;
163
+ if (
164
+ key === "history.enabled" ||
165
+ key === "swarm.forceRefresh" ||
166
+ key === "swarm.pivot"
167
+ ) {
75
168
  if (value === "true") return true;
76
169
  if (value === "false") return false;
77
170
  }
78
171
  if (key === "history.limit" && /^[1-9]\d*$/.test(value))
79
172
  value = Number(value);
80
- if (key === "store.gateway" && value === "default") return null;
173
+ if (
174
+ (key === "daemon.uiPort" ||
175
+ key === "swarm.cacheTtl" ||
176
+ key === "swarm.syncInterval") &&
177
+ typeof value === "string" &&
178
+ /^(0|[1-9]\d*)$/.test(value)
179
+ )
180
+ value = Number(value);
181
+ if (
182
+ ["store.gateway", "pointers.service", "pointers.apiKey"].includes(key) &&
183
+ value === "default"
184
+ )
185
+ return null;
186
+ }
187
+ if (key === "pointers.service" || key === "pointers.apiKey") {
188
+ if (value === null) return null;
189
+ if (key === "pointers.service" && typeof value === "string")
190
+ return pointerService(value);
191
+ if (
192
+ key === "pointers.apiKey" &&
193
+ typeof value === "string" &&
194
+ /^[A-Za-z0-9_-]{43}$/.test(value) &&
195
+ Buffer.from(value, "base64url").toString("base64url") === value
196
+ )
197
+ return value;
198
+ syntax(
199
+ key === "pointers.apiKey"
200
+ ? "Invalid pointers.apiKey; expected a canonical base64url encoding of 32 bytes."
201
+ : "Invalid pointers.service; expected a canonical HTTPS origin or default.",
202
+ );
203
+ }
204
+ if (key === "daemon.uiPort") {
205
+ if (
206
+ typeof value === "number" &&
207
+ Number.isSafeInteger(value) &&
208
+ value >= 0 &&
209
+ value <= 65535
210
+ )
211
+ return value;
212
+ syntax(
213
+ "daemon.uiPort requires an integer port from 0 to 65535 (0 for automatic).",
214
+ );
215
+ }
216
+ if (key === "swarm.cacheTtl" || key === "swarm.syncInterval") {
217
+ const minimum = key === "swarm.syncInterval" ? 1 : 0;
218
+ if (
219
+ Number.isSafeInteger(value) &&
220
+ typeof value === "number" &&
221
+ value >= minimum &&
222
+ value <= 86400
223
+ )
224
+ return value;
225
+ syntax(
226
+ `${key} requires an integer number of seconds from ${minimum} to 86400.`,
227
+ );
228
+ }
229
+ if (key === "swarm.forceRefresh" || key === "swarm.pivot") {
230
+ if (typeof value === "boolean") return value;
231
+ syntax(`${key} requires true or false.`);
232
+ }
233
+ if (key === "swarm.listen") {
234
+ if (value === "lan" || value === "loopback") return value;
235
+ syntax("swarm.listen requires lan or loopback.");
236
+ }
237
+ if (key.startsWith("swarm.") && value === null) return null;
238
+ if (key === "swarm.default" && typeof value === "string") {
239
+ return swarmUrl(value);
81
240
  }
82
241
  if (key === "store.gateway" && value === null) return null;
83
242
  if (
@@ -114,6 +273,38 @@ export function parseValue(
114
273
  );
115
274
  }
116
275
 
276
+ export function rejectLegacySwarmCid(value: string): void {
277
+ try {
278
+ CID.parse(value);
279
+ } catch {
280
+ return;
281
+ }
282
+ throw new HostError(
283
+ "The old Swarm CID protocol is unsupported. Ask the Owner for the Swarm configuration URL.",
284
+ );
285
+ }
286
+
287
+ export function swarmUrl(value: string): string {
288
+ try {
289
+ const url = new URL(value);
290
+ pointerService(url.origin);
291
+ if (
292
+ url.username ||
293
+ url.password ||
294
+ url.search ||
295
+ url.hash ||
296
+ url.toString() !== value ||
297
+ !/^\/v1\/[a-f0-9]{16}\/[a-f0-9]{16}$/.test(url.pathname)
298
+ )
299
+ throw new Error();
300
+ return value;
301
+ } catch {
302
+ rejectLegacySwarmCid(value);
303
+ return syntax(
304
+ "Swarm requires an exact /v1/{scope}/{variable} configuration URL (HTTPS or HTTP loopback), with two 16-character lowercase hex IDs and no credentials, query or fragment.",
305
+ );
306
+ }
307
+ }
117
308
  export function validateUserConfig(
118
309
  value: unknown,
119
310
  ): Partial<Record<ConfigKey, ConfigValue>> {
@@ -163,6 +354,7 @@ export function effectiveConfig(
163
354
  user: Partial<Record<ConfigKey, ConfigValue>>,
164
355
  flags: GlobalOptions,
165
356
  env: NodeJS.ProcessEnv = process.env,
357
+ resolveSwarm?: (reference: string) => string,
166
358
  ): ConfigItem[] {
167
359
  if (env.IPAB_STORE_RPC !== undefined)
168
360
  syntax(
@@ -170,16 +362,30 @@ export function effectiveConfig(
170
362
  );
171
363
  const result = (Object.keys(defaults) as ConfigKey[]).map(
172
364
  (key): ConfigItem => {
173
- const variable = env[environments[key]];
174
- const flag = key === "store.gateway" ? flags.storeGateway : undefined;
365
+ const environment = environments[key];
366
+ const variable = environment === undefined ? undefined : env[environment];
367
+ const flag =
368
+ key === "store.gateway"
369
+ ? flags.storeGateway
370
+ : key === "swarm.default"
371
+ ? flags.swarm
372
+ : undefined;
175
373
  // Invalid lower layers are not hidden by a higher-priority override.
374
+ const parse = (value: string) =>
375
+ parseValue(
376
+ key,
377
+ key === "swarm.default" && value !== "default" && resolveSwarm
378
+ ? resolveSwarm(value)
379
+ : value,
380
+ true,
381
+ );
176
382
  const environmental =
177
- variable === undefined ? undefined : parseValue(key, variable, true);
383
+ variable === undefined ? undefined : parse(variable);
178
384
  return {
179
385
  key,
180
386
  value:
181
387
  flag !== undefined
182
- ? parseValue(key, flag, true)
388
+ ? parse(flag)
183
389
  : variable !== undefined
184
390
  ? environmental!
185
391
  : Object.hasOwn(user, key)
@@ -0,0 +1,299 @@
1
+ import {
2
+ createWriter,
3
+ headerLength,
4
+ blockLength,
5
+ } from "@ipld/car/buffer-writer";
6
+ import * as dagPb from "@ipld/dag-pb";
7
+ import * as dagCbor from "@ipld/dag-cbor";
8
+ import { CID } from "multiformats/cid";
9
+ import type { IpfsStore } from "@ipab/ipfs-store";
10
+ import { bundleLimits, openBundle } from "@ipab/bundle";
11
+ import { bytes } from "./space.js";
12
+ import { HostError } from "./errors.js";
13
+ import { publicUrl } from "./config.js";
14
+
15
+ export async function contentCar(
16
+ store: IpfsStore,
17
+ roots: readonly string[],
18
+ signal?: AbortSignal,
19
+ ): Promise<Uint8Array> {
20
+ const blocks = new Map<string, { cid: CID; bytes: Uint8Array }>();
21
+ let total = 0;
22
+ const visit = async (cid: CID, depth: number): Promise<void> => {
23
+ const key = cid.toV1().toString();
24
+ if (blocks.has(key)) return;
25
+ if (depth > bundleLimits.depth || blocks.size >= bundleLimits.blocks)
26
+ throw new HostError("Publication DAG exceeds limits.");
27
+ const value = await store.block.get(key, signal ? { signal } : {});
28
+ total += value.length;
29
+ if (total > bundleLimits.bytes)
30
+ throw new HostError("Publication exceeds byte budget.");
31
+ blocks.set(key, { cid, bytes: value });
32
+ if (cid.code === dagPb.code)
33
+ for (const link of dagPb.decode(value).Links)
34
+ await visit(link.Hash, depth + 1);
35
+ else if (cid.code === dagCbor.code) {
36
+ const scan = async (value: unknown): Promise<void> => {
37
+ const link = CID.asCID(value);
38
+ if (link) await visit(link, depth + 1);
39
+ else if (Array.isArray(value))
40
+ for (const item of value) await scan(item);
41
+ else if (value && typeof value === "object")
42
+ for (const item of Object.values(value)) await scan(item);
43
+ };
44
+ await scan(dagCbor.decode(value));
45
+ }
46
+ };
47
+ const cids = [...new Set(roots)].map((root) => CID.parse(root));
48
+ for (const cid of cids) await visit(cid, 0);
49
+ const size =
50
+ headerLength({ roots: cids }) +
51
+ [...blocks.values()].reduce((sum, block) => sum + blockLength(block), 0);
52
+ const writer = createWriter(new ArrayBuffer(size), { roots: cids });
53
+ for (const block of blocks.values()) writer.write(block);
54
+ return writer.close();
55
+ }
56
+
57
+ /**
58
+ * Builds a CAR containing only the specified single blocks (non-recursive, e.g. v7 path
59
+ * directory blocks) plus the full DAG closure of `recursiveRoots` (e.g. published target).
60
+ */
61
+ export async function shallowContentCar(
62
+ store: IpfsStore,
63
+ directBlocks: readonly string[],
64
+ recursiveRoots: readonly string[],
65
+ signal?: AbortSignal,
66
+ ): Promise<Uint8Array> {
67
+ const blocks = new Map<string, { cid: CID; bytes: Uint8Array }>();
68
+ let total = 0;
69
+ const recordSingle = async (cid: CID): Promise<Uint8Array> => {
70
+ const key = cid.toV1().toString();
71
+ const existing = blocks.get(key);
72
+ if (existing) return existing.bytes;
73
+ if (blocks.size >= bundleLimits.blocks)
74
+ throw new HostError("Publication DAG exceeds limits.");
75
+ const value = await store.block.get(key, signal ? { signal } : {});
76
+ total += value.length;
77
+ if (total > bundleLimits.bytes)
78
+ throw new HostError("Publication exceeds byte budget.");
79
+ blocks.set(key, { cid: cid.toV1(), bytes: value });
80
+ return value;
81
+ };
82
+ const visit = async (cid: CID, depth: number): Promise<void> => {
83
+ const key = cid.toV1().toString();
84
+ if (blocks.has(key)) return;
85
+ if (depth > bundleLimits.depth || blocks.size >= bundleLimits.blocks)
86
+ throw new HostError("Publication DAG exceeds limits.");
87
+ const value = await recordSingle(cid);
88
+ if (cid.code === dagPb.code)
89
+ for (const link of dagPb.decode(value).Links)
90
+ await visit(link.Hash, depth + 1);
91
+ else if (cid.code === dagCbor.code) {
92
+ const scan = async (item: unknown): Promise<void> => {
93
+ const link = CID.asCID(item);
94
+ if (link) await visit(link, depth + 1);
95
+ else if (Array.isArray(item))
96
+ for (const entry of item) await scan(entry);
97
+ else if (item && typeof item === "object")
98
+ for (const entry of Object.values(item)) await scan(entry);
99
+ };
100
+ await scan(dagCbor.decode(value));
101
+ }
102
+ };
103
+ for (const root of [...new Set(recursiveRoots)])
104
+ await visit(CID.parse(root), 0);
105
+ for (const root of [...new Set(directBlocks)])
106
+ await recordSingle(CID.parse(root));
107
+ const cids = [...new Set([...directBlocks, ...recursiveRoots])].map((root) =>
108
+ CID.parse(root).toV1(),
109
+ );
110
+ const size =
111
+ headerLength({ roots: cids }) +
112
+ [...blocks.values()].reduce((sum, block) => sum + blockLength(block), 0);
113
+ const writer = createWriter(new ArrayBuffer(size), { roots: cids });
114
+ for (const block of blocks.values()) writer.write(block);
115
+ return writer.close();
116
+ }
117
+
118
+ /** Explicit upload bridge, not a Store backend or daemon lifecycle controller. */
119
+ export async function provideContent(
120
+ store: IpfsStore,
121
+ roots: readonly string[],
122
+ endpoint: string,
123
+ fetcher: typeof fetch = fetch,
124
+ authorizationToken?: string,
125
+ retainedRoots: readonly string[] = roots,
126
+ signal?: AbortSignal,
127
+ ): Promise<void> {
128
+ const url = new URL(publicUrl(endpoint));
129
+ if (
130
+ url.protocol !== "https:" &&
131
+ !["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
132
+ )
133
+ throw new HostError(
134
+ "Kubo provider must use HTTPS or account-local loopback HTTP.",
135
+ );
136
+ const car = await contentCar(store, roots, signal);
137
+ const owner = `publication:${roots[0]!}`;
138
+ await store.retain(owner, retainedRoots, signal ? { signal } : {});
139
+ await store.confirmRetained(owner, retainedRoots, signal ? { signal } : {});
140
+ const data = new FormData();
141
+ data.append("file", new Blob([Uint8Array.from(car)]), "content.car");
142
+ const response = await fetcher(
143
+ `${url.toString().replace(/\/$/, "")}/api/v0/dag/import?pin-roots=true`,
144
+ {
145
+ method: "POST",
146
+ ...(authorizationToken
147
+ ? { headers: { authorization: `Bearer ${authorizationToken}` } }
148
+ : {}),
149
+ body: data,
150
+ redirect: "error",
151
+ signal: signal
152
+ ? AbortSignal.any([signal, AbortSignal.timeout(30_000)])
153
+ : AbortSignal.timeout(30_000),
154
+ },
155
+ );
156
+ if (!response.ok || !response.body)
157
+ throw new HostError(
158
+ `Content provider import failed (HTTP ${response.status}); HEAD was not changed.`,
159
+ );
160
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(
161
+ await bytes(response.body, 256 * 1024),
162
+ );
163
+ const accepted = new Set<string>();
164
+ for (const line of text.trim().split(/\r?\n/)) {
165
+ const item = JSON.parse(line) as {
166
+ Root?: { Cid?: { "/"?: string }; PinErrorMsg?: string };
167
+ ErrMsg?: string;
168
+ };
169
+ if (item.ErrMsg || item.Root?.PinErrorMsg)
170
+ throw new HostError("Content provider failed to pin imported roots.");
171
+ if (item.Root?.Cid?.["/"])
172
+ accepted.add(CID.parse(item.Root.Cid["/"]).toV1().toString());
173
+ }
174
+ if (roots.some((root) => !accepted.has(CID.parse(root).toV1().toString())))
175
+ throw new HostError(
176
+ "Content provider did not confirm every imported/pinned root; HEAD was not changed.",
177
+ );
178
+ }
179
+
180
+ /**
181
+ * Shallow provider bridge for v7 UnixFS root updates:
182
+ * - Retains `directBlocks` directly (non-recursively) and `recursiveRoots` recursively in local Store.
183
+ * - Imports a shallow CAR into Kubo with `pin-roots=false`.
184
+ * - Calls `/api/v0/pin/add?recursive=false` for each direct path directory block,
185
+ * and `/api/v0/pin/add?recursive=true` for each recursive content root.
186
+ */
187
+ export async function provideShallowContent(
188
+ store: IpfsStore,
189
+ options: {
190
+ directBlocks: readonly string[];
191
+ recursiveRoots: readonly string[];
192
+ retainedDirect?: readonly string[];
193
+ retainedRecursive?: readonly string[];
194
+ endpoint: string;
195
+ fetcher?: typeof fetch;
196
+ authorizationToken?: string;
197
+ owner?: string;
198
+ signal?: AbortSignal;
199
+ },
200
+ ): Promise<void> {
201
+ const {
202
+ directBlocks,
203
+ recursiveRoots,
204
+ endpoint,
205
+ fetcher = fetch,
206
+ authorizationToken,
207
+ signal,
208
+ } = options;
209
+ const retainedDirect = options.retainedDirect ?? directBlocks;
210
+ const retainedRecursive = options.retainedRecursive ?? recursiveRoots;
211
+ const url = new URL(publicUrl(endpoint));
212
+ if (
213
+ url.protocol !== "https:" &&
214
+ !["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
215
+ )
216
+ throw new HostError(
217
+ "Kubo provider must use HTTPS or account-local loopback HTTP.",
218
+ );
219
+ const car = await shallowContentCar(
220
+ store,
221
+ directBlocks,
222
+ recursiveRoots,
223
+ signal,
224
+ );
225
+ const owner =
226
+ options.owner ??
227
+ `publication:${directBlocks[0] ?? recursiveRoots[0] ?? "empty"}`;
228
+ const retainOptions = signal ? { signal } : {};
229
+ if (retainedDirect.length) {
230
+ await store.retainDirect(owner, retainedDirect, retainOptions);
231
+ await store.confirmDirectRetained(owner, retainedDirect, retainOptions);
232
+ }
233
+ if (retainedRecursive.length) {
234
+ await store.retain(owner, retainedRecursive, retainOptions);
235
+ await store.confirmRetained(owner, retainedRecursive, retainOptions);
236
+ }
237
+ const base = url.toString().replace(/\/$/, "");
238
+ const headers = authorizationToken
239
+ ? { authorization: `Bearer ${authorizationToken}` }
240
+ : undefined;
241
+ const requestSignal = () =>
242
+ signal
243
+ ? AbortSignal.any([signal, AbortSignal.timeout(30_000)])
244
+ : AbortSignal.timeout(30_000);
245
+ const data = new FormData();
246
+ data.append("file", new Blob([Uint8Array.from(car)]), "content.car");
247
+ const response = await fetcher(`${base}/api/v0/dag/import?pin-roots=false`, {
248
+ method: "POST",
249
+ ...(headers ? { headers } : {}),
250
+ body: data,
251
+ redirect: "error",
252
+ signal: requestSignal(),
253
+ });
254
+ if (!response.ok || !response.body)
255
+ throw new HostError(
256
+ `Content provider import failed (HTTP ${response.status}); HEAD was not changed.`,
257
+ );
258
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(
259
+ await bytes(response.body, 256 * 1024),
260
+ );
261
+ for (const line of text.trim().split(/\r?\n/)) {
262
+ if (!line.trim()) continue;
263
+ const item = JSON.parse(line) as { ErrMsg?: string };
264
+ if (item.ErrMsg)
265
+ throw new HostError("Content provider failed to import shallow blocks.");
266
+ }
267
+ const pinOne = async (cid: string, recursive: boolean) => {
268
+ const pinResponse = await fetcher(
269
+ `${base}/api/v0/pin/add?arg=${encodeURIComponent(cid)}&recursive=${recursive ? "true" : "false"}`,
270
+ {
271
+ method: "POST",
272
+ ...(headers ? { headers } : {}),
273
+ redirect: "error",
274
+ signal: requestSignal(),
275
+ },
276
+ );
277
+ if (!pinResponse.ok || !pinResponse.body)
278
+ throw new HostError(
279
+ `Content provider pin failed for ${cid} (HTTP ${pinResponse.status}); HEAD was not changed.`,
280
+ );
281
+ await bytes(pinResponse.body, 64 * 1024);
282
+ };
283
+ for (const cid of [...new Set(directBlocks)])
284
+ await pinOne(CID.parse(cid).toV1().toString(), false);
285
+ for (const cid of [...new Set(recursiveRoots)])
286
+ await pinOne(CID.parse(cid).toV1().toString(), true);
287
+ }
288
+
289
+ export async function bundleJson(store: IpfsStore, root: string) {
290
+ const bundle = await openBundle(CID.parse(root), {
291
+ resolveCid: (cid) => store.block.get(cid.toString()),
292
+ });
293
+ const value: unknown = JSON.parse(
294
+ new TextDecoder("utf-8", { fatal: true }).decode(
295
+ await bytes(await bundle.readFile(bundle.manifest.entry), 1024 * 1024),
296
+ ),
297
+ );
298
+ return { bundle, value };
299
+ }
@@ -0,0 +1,11 @@
1
+ export interface BundleInspection {
2
+ targetCid: string;
3
+ cid: string;
4
+ title: string;
5
+ description: string;
6
+ entry: string;
7
+ contentType: string;
8
+ schemaCid: string;
9
+ files: string[];
10
+ swarm?: string;
11
+ }