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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/README.md +230 -7
  2. package/dist/app-authoring.d.ts +16 -2
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +151 -66
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/app-definition.d.ts +52 -63
  7. package/dist/app-definition.d.ts.map +1 -1
  8. package/dist/app-definition.js +33 -44
  9. package/dist/app-definition.js.map +1 -1
  10. package/dist/app-prebuild.d.ts +18 -0
  11. package/dist/app-prebuild.d.ts.map +1 -1
  12. package/dist/app-prebuild.js +46 -0
  13. package/dist/app-prebuild.js.map +1 -1
  14. package/dist/app-templates.d.ts +0 -1
  15. package/dist/app-templates.d.ts.map +1 -1
  16. package/dist/app-templates.js +15 -45
  17. package/dist/app-templates.js.map +1 -1
  18. package/dist/assembly.d.ts +7 -0
  19. package/dist/assembly.d.ts.map +1 -0
  20. package/dist/assembly.js +130 -0
  21. package/dist/assembly.js.map +1 -0
  22. package/dist/authoring.d.ts +8 -21
  23. package/dist/authoring.d.ts.map +1 -1
  24. package/dist/authoring.js +35 -54
  25. package/dist/authoring.js.map +1 -1
  26. package/dist/builtins/skill.ipab.car +0 -0
  27. package/dist/builtins/template.bundle-viewer.car +0 -0
  28. package/dist/builtins/template.check.car +0 -0
  29. package/dist/builtins/template.node.car +0 -0
  30. package/dist/builtins/template.web-app.car +0 -0
  31. package/dist/builtins.d.ts.map +1 -1
  32. package/dist/builtins.js +5 -32
  33. package/dist/builtins.js.map +1 -1
  34. package/dist/bundled-skills.d.ts +14 -0
  35. package/dist/bundled-skills.d.ts.map +1 -0
  36. package/dist/bundled-skills.js +34 -0
  37. package/dist/bundled-skills.js.map +1 -0
  38. package/dist/cli.d.ts +8 -2
  39. package/dist/cli.d.ts.map +1 -1
  40. package/dist/cli.js +2009 -308
  41. package/dist/cli.js.map +1 -1
  42. package/dist/config-secret.d.ts +3 -0
  43. package/dist/config-secret.d.ts.map +1 -0
  44. package/dist/config-secret.js +12 -0
  45. package/dist/config-secret.js.map +1 -0
  46. package/dist/config.d.ts +9 -4
  47. package/dist/config.d.ts.map +1 -1
  48. package/dist/config.js +164 -23
  49. package/dist/config.js.map +1 -1
  50. package/dist/content-provider.d.ts +32 -0
  51. package/dist/content-provider.d.ts.map +1 -0
  52. package/dist/content-provider.js +222 -0
  53. package/dist/content-provider.js.map +1 -0
  54. package/dist/context-types.d.ts +12 -0
  55. package/dist/context-types.d.ts.map +1 -0
  56. package/dist/context-types.js +2 -0
  57. package/dist/context-types.js.map +1 -0
  58. package/dist/context.d.ts +26 -0
  59. package/dist/context.d.ts.map +1 -1
  60. package/dist/context.js +56 -2
  61. package/dist/context.js.map +1 -1
  62. package/dist/daemon-ui.d.ts +93 -0
  63. package/dist/daemon-ui.d.ts.map +1 -0
  64. package/dist/daemon-ui.js +713 -0
  65. package/dist/daemon-ui.js.map +1 -0
  66. package/dist/daemon.d.ts +21 -0
  67. package/dist/daemon.d.ts.map +1 -0
  68. package/dist/daemon.js +317 -0
  69. package/dist/daemon.js.map +1 -0
  70. package/dist/doctor.d.ts +32 -0
  71. package/dist/doctor.d.ts.map +1 -0
  72. package/dist/doctor.js +684 -0
  73. package/dist/doctor.js.map +1 -0
  74. package/dist/errors.d.ts +1 -8
  75. package/dist/errors.d.ts.map +1 -1
  76. package/dist/errors.js +1 -20
  77. package/dist/errors.js.map +1 -1
  78. package/dist/get.d.ts +3 -0
  79. package/dist/get.d.ts.map +1 -0
  80. package/dist/get.js +80 -0
  81. package/dist/get.js.map +1 -0
  82. package/dist/history.d.ts.map +1 -1
  83. package/dist/history.js +6 -14
  84. package/dist/history.js.map +1 -1
  85. package/dist/host-error.d.ts +9 -0
  86. package/dist/host-error.d.ts.map +1 -0
  87. package/dist/host-error.js +21 -0
  88. package/dist/host-error.js.map +1 -0
  89. package/dist/identity.d.ts +16 -0
  90. package/dist/identity.d.ts.map +1 -0
  91. package/dist/identity.js +119 -0
  92. package/dist/identity.js.map +1 -0
  93. package/dist/ipc.d.ts +5 -1
  94. package/dist/ipc.d.ts.map +1 -1
  95. package/dist/ipc.js +125 -4
  96. package/dist/ipc.js.map +1 -1
  97. package/dist/kubo-install.d.ts +26 -0
  98. package/dist/kubo-install.d.ts.map +1 -0
  99. package/dist/kubo-install.js +724 -0
  100. package/dist/kubo-install.js.map +1 -0
  101. package/dist/kubo-peering.d.ts +11 -0
  102. package/dist/kubo-peering.d.ts.map +1 -0
  103. package/dist/kubo-peering.js +93 -0
  104. package/dist/kubo-peering.js.map +1 -0
  105. package/dist/kubo-peers.d.ts +12 -0
  106. package/dist/kubo-peers.d.ts.map +1 -0
  107. package/dist/kubo-peers.js +119 -0
  108. package/dist/kubo-peers.js.map +1 -0
  109. package/dist/kubo-release.d.ts +22 -0
  110. package/dist/kubo-release.d.ts.map +1 -0
  111. package/dist/kubo-release.js +176 -0
  112. package/dist/kubo-release.js.map +1 -0
  113. package/dist/kubo-topology.d.ts +19 -0
  114. package/dist/kubo-topology.d.ts.map +1 -0
  115. package/dist/kubo-topology.js +113 -0
  116. package/dist/kubo-topology.js.map +1 -0
  117. package/dist/kubo.d.ts +85 -0
  118. package/dist/kubo.d.ts.map +1 -0
  119. package/dist/kubo.js +755 -0
  120. package/dist/kubo.js.map +1 -0
  121. package/dist/pins.d.ts +18 -20
  122. package/dist/pins.d.ts.map +1 -1
  123. package/dist/pins.js +146 -126
  124. package/dist/pins.js.map +1 -1
  125. package/dist/pivot-rpc.d.ts +62 -0
  126. package/dist/pivot-rpc.d.ts.map +1 -0
  127. package/dist/pivot-rpc.js +407 -0
  128. package/dist/pivot-rpc.js.map +1 -0
  129. package/dist/pivot-service.d.ts +16 -0
  130. package/dist/pivot-service.d.ts.map +1 -0
  131. package/dist/pivot-service.js +50 -0
  132. package/dist/pivot-service.js.map +1 -0
  133. package/dist/publish-source.d.ts +3 -0
  134. package/dist/publish-source.d.ts.map +1 -0
  135. package/dist/publish-source.js +85 -0
  136. package/dist/publish-source.js.map +1 -0
  137. package/dist/runtime.d.ts +2 -0
  138. package/dist/runtime.d.ts.map +1 -1
  139. package/dist/runtime.js +9 -4
  140. package/dist/runtime.js.map +1 -1
  141. package/dist/search-index.d.ts +64 -0
  142. package/dist/search-index.d.ts.map +1 -0
  143. package/dist/search-index.js +409 -0
  144. package/dist/search-index.js.map +1 -0
  145. package/dist/search-service.d.ts +23 -0
  146. package/dist/search-service.d.ts.map +1 -0
  147. package/dist/search-service.js +116 -0
  148. package/dist/search-service.js.map +1 -0
  149. package/dist/skill-definition.d.ts +6 -0
  150. package/dist/skill-definition.d.ts.map +1 -0
  151. package/dist/skill-definition.js +38 -0
  152. package/dist/skill-definition.js.map +1 -0
  153. package/dist/skill-source.d.ts +9 -0
  154. package/dist/skill-source.d.ts.map +1 -0
  155. package/dist/skill-source.js +24 -0
  156. package/dist/skill-source.js.map +1 -0
  157. package/dist/skills.d.ts +104 -0
  158. package/dist/skills.d.ts.map +1 -0
  159. package/dist/skills.js +894 -0
  160. package/dist/skills.js.map +1 -0
  161. package/dist/source-bundle.d.ts +3 -9
  162. package/dist/source-bundle.d.ts.map +1 -1
  163. package/dist/source-bundle.js +17 -13
  164. package/dist/source-bundle.js.map +1 -1
  165. package/dist/space-path.d.ts +3 -0
  166. package/dist/space-path.d.ts.map +1 -0
  167. package/dist/space-path.js +17 -0
  168. package/dist/space-path.js.map +1 -0
  169. package/dist/space.d.ts +16 -4
  170. package/dist/space.d.ts.map +1 -1
  171. package/dist/space.js +170 -24
  172. package/dist/space.js.map +1 -1
  173. package/dist/state-storage-windows.d.ts +2 -0
  174. package/dist/state-storage-windows.d.ts.map +1 -0
  175. package/dist/state-storage-windows.js +114 -0
  176. package/dist/state-storage-windows.js.map +1 -0
  177. package/dist/state-storage.d.ts +5 -0
  178. package/dist/state-storage.d.ts.map +1 -0
  179. package/dist/state-storage.js +333 -0
  180. package/dist/state-storage.js.map +1 -0
  181. package/dist/state.d.ts +128 -16
  182. package/dist/state.d.ts.map +1 -1
  183. package/dist/state.js +506 -94
  184. package/dist/state.js.map +1 -1
  185. package/dist/swarm-envelope.d.ts +57 -0
  186. package/dist/swarm-envelope.d.ts.map +1 -0
  187. package/dist/swarm-envelope.js +299 -0
  188. package/dist/swarm-envelope.js.map +1 -0
  189. package/dist/swarm-files.d.ts +6 -0
  190. package/dist/swarm-files.d.ts.map +1 -0
  191. package/dist/swarm-files.js +116 -0
  192. package/dist/swarm-files.js.map +1 -0
  193. package/dist/swarm-meta.d.ts +15 -0
  194. package/dist/swarm-meta.d.ts.map +1 -0
  195. package/dist/swarm-meta.js +68 -0
  196. package/dist/swarm-meta.js.map +1 -0
  197. package/dist/swarm-observations.d.ts +40 -0
  198. package/dist/swarm-observations.d.ts.map +1 -0
  199. package/dist/swarm-observations.js +271 -0
  200. package/dist/swarm-observations.js.map +1 -0
  201. package/dist/swarm.d.ts +469 -0
  202. package/dist/swarm.d.ts.map +1 -0
  203. package/dist/swarm.js +4708 -0
  204. package/dist/swarm.js.map +1 -0
  205. package/dist/template-definition.d.ts +45 -67
  206. package/dist/template-definition.d.ts.map +1 -1
  207. package/dist/template-definition.js +35 -49
  208. package/dist/template-definition.js.map +1 -1
  209. package/dist/template-paths.d.ts +7 -0
  210. package/dist/template-paths.d.ts.map +1 -0
  211. package/dist/template-paths.js +23 -0
  212. package/dist/template-paths.js.map +1 -0
  213. package/dist/template-presentation.d.ts +20 -0
  214. package/dist/template-presentation.d.ts.map +1 -0
  215. package/dist/template-presentation.js +133 -0
  216. package/dist/template-presentation.js.map +1 -0
  217. package/dist/template-source.d.ts +4 -1
  218. package/dist/template-source.d.ts.map +1 -1
  219. package/dist/template-source.js +11 -4
  220. package/dist/template-source.js.map +1 -1
  221. package/dist/templates.d.ts +11 -0
  222. package/dist/templates.d.ts.map +1 -0
  223. package/dist/templates.js +53 -0
  224. package/dist/templates.js.map +1 -0
  225. package/dist/unixfs-tree.d.ts +67 -0
  226. package/dist/unixfs-tree.d.ts.map +1 -0
  227. package/dist/unixfs-tree.js +329 -0
  228. package/dist/unixfs-tree.js.map +1 -0
  229. package/package.json +18 -11
  230. package/src/app-authoring.ts +254 -88
  231. package/src/app-definition.ts +33 -45
  232. package/src/app-prebuild.ts +61 -0
  233. package/src/app-templates.ts +28 -71
  234. package/src/assembly.ts +185 -0
  235. package/src/authoring.ts +63 -72
  236. package/src/builtins.ts +7 -38
  237. package/src/bundled-skills.ts +60 -0
  238. package/src/cli.ts +2500 -376
  239. package/src/config-secret.ts +14 -0
  240. package/src/config.ts +215 -26
  241. package/src/content-provider.ts +299 -0
  242. package/src/context-types.ts +11 -0
  243. package/src/context.ts +100 -2
  244. package/src/daemon-ui.ts +812 -0
  245. package/src/daemon.ts +386 -0
  246. package/src/doctor.ts +1082 -0
  247. package/src/errors.ts +1 -25
  248. package/src/get.ts +90 -0
  249. package/src/history.ts +5 -15
  250. package/src/host-error.ts +26 -0
  251. package/src/identity.ts +175 -0
  252. package/src/ipc.ts +161 -4
  253. package/src/kubo-install.ts +936 -0
  254. package/src/kubo-peering.ts +126 -0
  255. package/src/kubo-peers.ts +144 -0
  256. package/src/kubo-release.ts +230 -0
  257. package/src/kubo-topology.ts +148 -0
  258. package/src/kubo.ts +1088 -0
  259. package/src/pins.ts +190 -142
  260. package/src/pivot-rpc.ts +601 -0
  261. package/src/pivot-service.ts +87 -0
  262. package/src/publish-source.ts +103 -0
  263. package/src/runtime.ts +16 -3
  264. package/src/search-index.ts +560 -0
  265. package/src/search-service.ts +152 -0
  266. package/src/skill-definition.ts +53 -0
  267. package/src/skill-source.ts +29 -0
  268. package/src/skills.ts +1236 -0
  269. package/src/source-bundle.ts +26 -14
  270. package/src/space-path.ts +30 -0
  271. package/src/space.ts +230 -42
  272. package/src/state-storage-windows.ts +134 -0
  273. package/src/state-storage.ts +378 -0
  274. package/src/state.ts +782 -116
  275. package/src/swarm-envelope.ts +461 -0
  276. package/src/swarm-files.ts +126 -0
  277. package/src/swarm-meta.ts +98 -0
  278. package/src/swarm-observations.ts +393 -0
  279. package/src/swarm.ts +7051 -0
  280. package/src/template-definition.ts +35 -49
  281. package/src/template-paths.ts +43 -0
  282. package/src/template-presentation.ts +151 -0
  283. package/src/template-source.ts +19 -5
  284. package/src/templates.ts +77 -0
  285. package/src/unixfs-tree.ts +401 -0
@@ -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 =
8
- | "store.rpc"
9
+ | "daemon.uiPort"
10
+ | "swarm.default"
11
+ | "swarm.listen"
12
+ | "swarm.pivot"
13
+ | "swarm.cacheTtl"
14
+ | "swarm.syncInterval"
15
+ | "swarm.forceRefresh"
16
+ | "store.gateway"
17
+ | "pointers.service"
18
+ | "pointers.apiKey"
9
19
  | "history.enabled"
10
20
  | "history.limit"
11
21
  | "output.retention"
@@ -13,8 +23,9 @@ 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
- storeRpc?: string;
28
+ storeGateway?: string;
18
29
  json: boolean;
19
30
  }
20
31
  export interface ConfigItem {
@@ -23,14 +34,31 @@ export interface ConfigItem {
23
34
  source: Source;
24
35
  }
25
36
  export const defaults: Record<ConfigKey, ConfigValue> = {
26
- "store.rpc": null,
37
+ "daemon.uiPort": 0,
38
+ "swarm.default": null,
39
+ "swarm.listen": "lan",
40
+ "swarm.pivot": false,
41
+ "swarm.cacheTtl": 300,
42
+ "swarm.syncInterval": 60,
43
+ "swarm.forceRefresh": false,
44
+ "store.gateway": null,
45
+ "pointers.service": null,
46
+ "pointers.apiKey": null,
27
47
  "history.enabled": true,
28
48
  "history.limit": 20,
29
49
  "output.retention": "history",
30
50
  "bundle.outputDir": "dist",
31
51
  };
32
- const environments: Record<ConfigKey, string> = {
33
- "store.rpc": "IPAB_STORE_RPC",
52
+ const environments: Partial<Record<ConfigKey, string>> = {
53
+ "daemon.uiPort": "IPAB_DAEMON_UI_PORT",
54
+ "swarm.listen": "IPAB_SWARM_LISTEN",
55
+ "swarm.pivot": "IPAB_SWARM_PIVOT",
56
+ "swarm.cacheTtl": "IPAB_SWARM_CACHE_TTL",
57
+ "swarm.syncInterval": "IPAB_SWARM_SYNC_INTERVAL",
58
+ "swarm.forceRefresh": "IPAB_SWARM_FORCE_REFRESH",
59
+ "store.gateway": "IPAB_STORE_GATEWAY",
60
+ "pointers.service": "IPAB_POINTERS_SERVICE",
61
+ "pointers.apiKey": "IPAB_POINTERS_API_KEY",
34
62
  "history.enabled": "IPAB_HISTORY_ENABLED",
35
63
  "history.limit": "IPAB_HISTORY_LIMIT",
36
64
  "output.retention": "IPAB_OUTPUT_RETENTION",
@@ -65,21 +93,134 @@ export function publicUrl(value: string): string {
65
93
  return url.toString();
66
94
  }
67
95
 
96
+ export function pointerService(value: string): string {
97
+ let url: URL;
98
+ try {
99
+ url = new URL(value);
100
+ } catch {
101
+ syntax(
102
+ "Pointer service must be a canonical HTTPS origin (HTTP loopback allowed).",
103
+ );
104
+ }
105
+ if (
106
+ url.username ||
107
+ url.password ||
108
+ url.search ||
109
+ url.hash ||
110
+ url.pathname !== "/" ||
111
+ (url.protocol !== "https:" &&
112
+ !(
113
+ url.protocol === "http:" &&
114
+ ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
115
+ )) ||
116
+ value !== url.origin
117
+ )
118
+ syntax(
119
+ "Pointer service must be a canonical HTTPS origin (HTTP loopback allowed).",
120
+ );
121
+ return url.origin;
122
+ }
123
+
124
+ export function visibleConfig(item: ConfigItem): ConfigItem {
125
+ return item.key === "pointers.apiKey" && item.value !== null
126
+ ? { ...item, value: "<redacted>" }
127
+ : item;
128
+ }
129
+
68
130
  export function parseValue(
69
131
  key: ConfigKey,
70
132
  value: unknown,
71
133
  text = false,
72
134
  ): ConfigValue {
73
135
  if (text && typeof value === "string") {
74
- if (key === "history.enabled") {
136
+ if (key === "daemon.uiPort" && value === "default") return defaults[key];
137
+ if (key.startsWith("swarm.") && value === "default")
138
+ return key === "swarm.cacheTtl" ||
139
+ key === "swarm.syncInterval" ||
140
+ key === "swarm.forceRefresh" ||
141
+ key === "swarm.pivot" ||
142
+ key === "swarm.listen"
143
+ ? defaults[key]
144
+ : null;
145
+ if (
146
+ key === "history.enabled" ||
147
+ key === "swarm.forceRefresh" ||
148
+ key === "swarm.pivot"
149
+ ) {
75
150
  if (value === "true") return true;
76
151
  if (value === "false") return false;
77
152
  }
78
153
  if (key === "history.limit" && /^[1-9]\d*$/.test(value))
79
154
  value = Number(value);
80
- if (key === "store.rpc" && value === "default") return null;
155
+ if (
156
+ (key === "daemon.uiPort" ||
157
+ key === "swarm.cacheTtl" ||
158
+ key === "swarm.syncInterval") &&
159
+ typeof value === "string" &&
160
+ /^(0|[1-9]\d*)$/.test(value)
161
+ )
162
+ value = Number(value);
163
+ if (
164
+ ["store.gateway", "pointers.service", "pointers.apiKey"].includes(key) &&
165
+ value === "default"
166
+ )
167
+ return null;
168
+ }
169
+ if (key === "pointers.service" || key === "pointers.apiKey") {
170
+ if (value === null) return null;
171
+ if (key === "pointers.service" && typeof value === "string")
172
+ return pointerService(value);
173
+ if (
174
+ key === "pointers.apiKey" &&
175
+ typeof value === "string" &&
176
+ /^[A-Za-z0-9_-]{43}$/.test(value) &&
177
+ Buffer.from(value, "base64url").toString("base64url") === value
178
+ )
179
+ return value;
180
+ syntax(
181
+ key === "pointers.apiKey"
182
+ ? "Invalid pointers.apiKey; expected a canonical base64url encoding of 32 bytes."
183
+ : "Invalid pointers.service; expected a canonical HTTPS origin or default.",
184
+ );
185
+ }
186
+ if (key === "daemon.uiPort") {
187
+ if (
188
+ typeof value === "number" &&
189
+ Number.isSafeInteger(value) &&
190
+ value >= 0 &&
191
+ value <= 65535
192
+ )
193
+ return value;
194
+ syntax(
195
+ "daemon.uiPort requires an integer port from 0 to 65535 (0 for automatic).",
196
+ );
197
+ }
198
+ if (key === "swarm.cacheTtl" || key === "swarm.syncInterval") {
199
+ const minimum = key === "swarm.syncInterval" ? 1 : 0;
200
+ if (
201
+ Number.isSafeInteger(value) &&
202
+ typeof value === "number" &&
203
+ value >= minimum &&
204
+ value <= 86400
205
+ )
206
+ return value;
207
+ syntax(
208
+ `${key} requires an integer number of seconds from ${minimum} to 86400.`,
209
+ );
210
+ }
211
+ if (key === "swarm.forceRefresh" || key === "swarm.pivot") {
212
+ if (typeof value === "boolean") return value;
213
+ syntax(`${key} requires true or false.`);
81
214
  }
82
- if (key === "store.rpc" && value === null) return null;
215
+ if (key === "swarm.listen") {
216
+ if (value === "lan" || value === "loopback") return value;
217
+ syntax("swarm.listen requires lan or loopback.");
218
+ }
219
+ if (key.startsWith("swarm.") && value === null) return null;
220
+ if (key === "swarm.default" && typeof value === "string") {
221
+ return swarmUrl(value);
222
+ }
223
+ if (key === "store.gateway" && value === null) return null;
83
224
  if (
84
225
  key === "bundle.outputDir" &&
85
226
  typeof value === "string" &&
@@ -87,7 +228,8 @@ export function parseValue(
87
228
  !value.includes("\0")
88
229
  )
89
230
  return value;
90
- if (key === "store.rpc" && typeof value === "string") return publicUrl(value);
231
+ if (key === "store.gateway" && typeof value === "string")
232
+ return publicUrl(value);
91
233
  if (key === "history.enabled" && typeof value === "boolean") return value;
92
234
  if (
93
235
  key === "history.limit" &&
@@ -113,6 +255,38 @@ export function parseValue(
113
255
  );
114
256
  }
115
257
 
258
+ export function rejectLegacySwarmCid(value: string): void {
259
+ try {
260
+ CID.parse(value);
261
+ } catch {
262
+ return;
263
+ }
264
+ throw new HostError(
265
+ "The old Swarm CID protocol is unsupported. Ask the Owner for the Swarm configuration URL.",
266
+ );
267
+ }
268
+
269
+ export function swarmUrl(value: string): string {
270
+ try {
271
+ const url = new URL(value);
272
+ pointerService(url.origin);
273
+ if (
274
+ url.username ||
275
+ url.password ||
276
+ url.search ||
277
+ url.hash ||
278
+ url.toString() !== value ||
279
+ !/^\/v1\/[a-f0-9]{16}\/[a-f0-9]{16}$/.test(url.pathname)
280
+ )
281
+ throw new Error();
282
+ return value;
283
+ } catch {
284
+ rejectLegacySwarmCid(value);
285
+ return syntax(
286
+ "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.",
287
+ );
288
+ }
289
+ }
116
290
  export function validateUserConfig(
117
291
  value: unknown,
118
292
  ): Partial<Record<ConfigKey, ConfigValue>> {
@@ -162,19 +336,38 @@ export function effectiveConfig(
162
336
  user: Partial<Record<ConfigKey, ConfigValue>>,
163
337
  flags: GlobalOptions,
164
338
  env: NodeJS.ProcessEnv = process.env,
339
+ resolveSwarm?: (reference: string) => string,
165
340
  ): ConfigItem[] {
341
+ if (env.IPAB_STORE_RPC !== undefined)
342
+ syntax(
343
+ "IPAB_STORE_RPC is no longer supported. Remove it and use IPAB_STORE_GATEWAY with an HTTP gateway URL.",
344
+ );
166
345
  const result = (Object.keys(defaults) as ConfigKey[]).map(
167
346
  (key): ConfigItem => {
168
- const variable = env[environments[key]];
169
- const flag = key === "store.rpc" ? flags.storeRpc : undefined;
347
+ const environment = environments[key];
348
+ const variable = environment === undefined ? undefined : env[environment];
349
+ const flag =
350
+ key === "store.gateway"
351
+ ? flags.storeGateway
352
+ : key === "swarm.default"
353
+ ? flags.swarm
354
+ : undefined;
170
355
  // Invalid lower layers are not hidden by a higher-priority override.
356
+ const parse = (value: string) =>
357
+ parseValue(
358
+ key,
359
+ key === "swarm.default" && value !== "default" && resolveSwarm
360
+ ? resolveSwarm(value)
361
+ : value,
362
+ true,
363
+ );
171
364
  const environmental =
172
- variable === undefined ? undefined : parseValue(key, variable, true);
365
+ variable === undefined ? undefined : parse(variable);
173
366
  return {
174
367
  key,
175
368
  value:
176
369
  flag !== undefined
177
- ? parseValue(key, flag, true)
370
+ ? parse(flag)
178
371
  : variable !== undefined
179
372
  ? environmental!
180
373
  : Object.hasOwn(user, key)
@@ -201,20 +394,16 @@ export function effectiveConfig(
201
394
  }
202
395
 
203
396
  export async function storeConfiguration(
204
- rpc: ConfigValue,
397
+ gateway: ConfigValue,
205
398
  ): Promise<StoreConfiguration> {
206
- const config =
207
- rpc === null
208
- ? await readContentConfiguration()
209
- : { backend: "kubo" as const, apiUrl: publicUrl(String(rpc)) };
210
- const urls =
211
- config.backend === "kubo"
212
- ? [config.apiUrl]
399
+ const config = await readContentConfiguration(
400
+ gateway === null ? {} : { gateway: publicUrl(String(gateway)) },
401
+ );
402
+ const urls = config.gateway
403
+ ? typeof config.gateway === "string"
404
+ ? [config.gateway]
213
405
  : config.gateway
214
- ? typeof config.gateway === "string"
215
- ? [config.gateway]
216
- : config.gateway
217
- : [];
406
+ : [];
218
407
  for (const url of urls) publicUrl(url);
219
408
  return config;
220
409
  }
@@ -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
+ }