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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +21 -5
  2. package/dist/app-authoring.d.ts +10 -0
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +127 -11
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/assembly.d.ts +7 -0
  7. package/dist/assembly.d.ts.map +1 -0
  8. package/dist/assembly.js +131 -0
  9. package/dist/assembly.js.map +1 -0
  10. package/dist/authoring.d.ts +1 -1
  11. package/dist/authoring.d.ts.map +1 -1
  12. package/dist/authoring.js +3 -3
  13. package/dist/authoring.js.map +1 -1
  14. package/dist/builtins/template.node.car +0 -0
  15. package/dist/builtins/template.web-app.car +0 -0
  16. package/dist/cli.d.ts +1 -0
  17. package/dist/cli.d.ts.map +1 -1
  18. package/dist/cli.js +520 -66
  19. package/dist/cli.js.map +1 -1
  20. package/dist/config-secret.d.ts +3 -0
  21. package/dist/config-secret.d.ts.map +1 -0
  22. package/dist/config-secret.js +83 -0
  23. package/dist/config-secret.js.map +1 -0
  24. package/dist/config.d.ts +5 -2
  25. package/dist/config.d.ts.map +1 -1
  26. package/dist/config.js +105 -7
  27. package/dist/config.js.map +1 -1
  28. package/dist/content-provider.d.ts +9 -0
  29. package/dist/content-provider.d.ts.map +1 -0
  30. package/dist/content-provider.js +90 -0
  31. package/dist/content-provider.js.map +1 -0
  32. package/dist/get.d.ts +3 -0
  33. package/dist/get.d.ts.map +1 -0
  34. package/dist/get.js +61 -0
  35. package/dist/get.js.map +1 -0
  36. package/dist/identity.d.ts +9 -0
  37. package/dist/identity.d.ts.map +1 -0
  38. package/dist/identity.js +89 -0
  39. package/dist/identity.js.map +1 -0
  40. package/dist/source-bundle.js +1 -1
  41. package/dist/source-bundle.js.map +1 -1
  42. package/dist/state.d.ts +15 -0
  43. package/dist/state.d.ts.map +1 -1
  44. package/dist/state.js +100 -1
  45. package/dist/state.js.map +1 -1
  46. package/dist/swarm.d.ts +113 -0
  47. package/dist/swarm.d.ts.map +1 -0
  48. package/dist/swarm.js +760 -0
  49. package/dist/swarm.js.map +1 -0
  50. package/package.json +10 -5
  51. package/src/app-authoring.ts +188 -16
  52. package/src/assembly.ts +182 -0
  53. package/src/authoring.ts +10 -3
  54. package/src/cli.ts +641 -68
  55. package/src/config-secret.ts +96 -0
  56. package/src/config.ts +142 -7
  57. package/src/content-provider.ts +123 -0
  58. package/src/get.ts +68 -0
  59. package/src/identity.ts +133 -0
  60. package/src/source-bundle.ts +1 -1
  61. package/src/state.ts +112 -1
  62. package/src/swarm.ts +1186 -0
package/src/state.ts CHANGED
@@ -10,12 +10,20 @@ import {
10
10
  type ConfigValue,
11
11
  } from "./config.js";
12
12
  import { HostError, object, syntax } from "./errors.js";
13
+ import { secureConfigurationStorage } from "./config-secret.js";
13
14
 
14
15
  export interface Association {
15
16
  schemaCid: string;
16
17
  appCid: string;
17
18
  argv: string[];
18
19
  }
20
+ export interface RegisteredSwarm {
21
+ cid: string;
22
+ alias: string | null;
23
+ }
24
+ export function swarmAlias(value: string): void {
25
+ pinName(value);
26
+ }
19
27
  export interface RunRecord {
20
28
  id: string;
21
29
  origin: "exec" | "open";
@@ -135,7 +143,7 @@ export class State {
135
143
  readonly directory: string,
136
144
  private readonly builtins: readonly NamedPin[] = [],
137
145
  ) {
138
- mkdirSync(directory, { recursive: true });
146
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
139
147
  this.db = new DatabaseSync(join(directory, "state.sqlite"));
140
148
  this.db
141
149
  .exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
@@ -144,6 +152,8 @@ export class State {
144
152
  DROP TABLE IF EXISTS aliases;
145
153
  CREATE TABLE IF NOT EXISTS named_pins (name TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL);
146
154
  CREATE TABLE IF NOT EXISTS registrations (schema_cid TEXT PRIMARY KEY, value TEXT NOT NULL);
155
+ CREATE TABLE IF NOT EXISTS swarms (cid TEXT PRIMARY KEY, alias TEXT UNIQUE);
156
+ CREATE TABLE IF NOT EXISTS swarm_headers (account TEXT NOT NULL, swarm TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (account,swarm));
147
157
  CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, value TEXT NOT NULL);
148
158
  CREATE TABLE IF NOT EXISTS holds (owner TEXT PRIMARY KEY, value TEXT NOT NULL);`);
149
159
  this.transaction(() => {
@@ -170,6 +180,12 @@ export class State {
170
180
  }
171
181
  config(): Partial<Record<ConfigKey, ConfigValue>> {
172
182
  const entries = this.db.prepare("SELECT key,value FROM config").all();
183
+ if (
184
+ entries.some(
185
+ (row) => row.key === "pointers.apiKey" && row.value !== "null",
186
+ )
187
+ )
188
+ secureConfigurationStorage(this.directory, false);
173
189
  if (entries.some((row) => row.key === "store.rpc"))
174
190
  throw new HostError(
175
191
  "store.rpc is no longer supported. Run ipab config reset store.rpc, then configure store.gateway with an HTTP gateway URL. Existing Kubo pins are not migrated or released.",
@@ -190,6 +206,8 @@ export class State {
190
206
  }
191
207
  }
192
208
  setConfig(key: ConfigKey, value: ConfigValue): void {
209
+ if (key === "pointers.apiKey" && value !== null)
210
+ secureConfigurationStorage(this.directory, true);
193
211
  this.db
194
212
  .prepare("INSERT OR REPLACE INTO config VALUES (?,?)")
195
213
  .run(key, JSON.stringify(value));
@@ -197,6 +215,99 @@ export class State {
197
215
  resetConfig(key: ConfigKey | "store.rpc"): void {
198
216
  this.db.prepare("DELETE FROM config WHERE key=?").run(key);
199
217
  }
218
+ swarms(): RegisteredSwarm[] {
219
+ return this.db
220
+ .prepare("SELECT cid,alias FROM swarms ORDER BY cid")
221
+ .all()
222
+ .map((row) => {
223
+ if (
224
+ typeof row.cid !== "string" ||
225
+ (row.alias !== null && typeof row.alias !== "string")
226
+ )
227
+ throw new HostError("Invalid persisted swarm registration.");
228
+ const cid = exactCid(row.cid);
229
+ if (row.alias !== null) swarmAlias(row.alias);
230
+ return { cid, alias: row.alias };
231
+ });
232
+ }
233
+ resolveSwarm(reference: string): string {
234
+ try {
235
+ return CID.parse(reference).toV1().toString();
236
+ } catch {
237
+ const entry = this.swarms().find((entry) => entry.alias === reference);
238
+ if (entry) return entry.cid;
239
+ throw new HostError(
240
+ `Unknown swarm alias "${reference}". Run ipab swarm list.`,
241
+ );
242
+ }
243
+ }
244
+ registeredSwarm(reference: string): RegisteredSwarm {
245
+ const cid = this.resolveSwarm(reference);
246
+ const entry = this.swarms().find((entry) => entry.cid === cid);
247
+ if (!entry)
248
+ throw new HostError(
249
+ `Swarm "${reference}" is not registered. Run ipab swarm add ${cid}.`,
250
+ );
251
+ return entry;
252
+ }
253
+ checkSwarmAlias(alias: string, cid?: string): void {
254
+ swarmAlias(alias);
255
+ if (
256
+ this.swarms().some((entry) => entry.alias === alias && entry.cid !== cid)
257
+ )
258
+ throw new HostError(
259
+ `Swarm alias "${alias}" is already assigned. Choose another alias.`,
260
+ );
261
+ }
262
+ registerSwarm(cid: string, alias?: string): RegisteredSwarm {
263
+ cid = exactCid(cid);
264
+ return this.transaction(() => {
265
+ if (alias !== undefined) this.checkSwarmAlias(alias, cid);
266
+ const previous = this.swarms().find((entry) => entry.cid === cid);
267
+ const entry = { cid, alias: alias ?? previous?.alias ?? null };
268
+ this.db
269
+ .prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
270
+ .run(cid, entry.alias);
271
+ if (this.config()["swarm.default"] == null)
272
+ this.setConfig("swarm.default", cid);
273
+ return entry;
274
+ });
275
+ }
276
+ useSwarm(reference: string): string {
277
+ return this.transaction(() => {
278
+ const entry = this.registeredSwarm(reference);
279
+ this.setConfig("swarm.default", entry.cid);
280
+ return entry.cid;
281
+ });
282
+ }
283
+ removeSwarm(reference: string): string {
284
+ return this.transaction(() => {
285
+ const entry = this.registeredSwarm(reference);
286
+ this.db.prepare("DELETE FROM swarms WHERE cid=?").run(entry.cid);
287
+ if (this.config()["swarm.default"] === entry.cid)
288
+ this.resetConfig("swarm.default");
289
+ return entry.cid;
290
+ });
291
+ }
292
+ swarmHeader(account: string, swarm: string): unknown {
293
+ const row = this.db
294
+ .prepare("SELECT value FROM swarm_headers WHERE account=? AND swarm=?")
295
+ .get(account, swarm);
296
+ if (!row) return undefined;
297
+ try {
298
+ return JSON.parse(String(row.value)) as unknown;
299
+ } catch {
300
+ throw new HostError("Malformed persisted swarm header cache.");
301
+ }
302
+ }
303
+ saveSwarmHeader(account: string, swarm: string, value: unknown): void {
304
+ this.db
305
+ .prepare("INSERT OR REPLACE INTO swarm_headers VALUES (?,?,?)")
306
+ .run(account, swarm, JSON.stringify(value));
307
+ }
308
+ invalidateSwarmHeader(swarm: string): void {
309
+ this.db.prepare("DELETE FROM swarm_headers WHERE swarm=?").run(swarm);
310
+ }
200
311
  resolve(value: string, schema = false): string {
201
312
  try {
202
313
  return exactCid(value, schema);