@ipab/cli 0.1.0-rc.3 → 0.1.0-rc.5

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 (97) hide show
  1. package/README.md +21 -10
  2. package/dist/cli.d.ts +3 -1
  3. package/dist/cli.d.ts.map +1 -1
  4. package/dist/cli.js +188 -46
  5. package/dist/cli.js.map +1 -1
  6. package/dist/config-secret.d.ts +1 -1
  7. package/dist/config-secret.d.ts.map +1 -1
  8. package/dist/config-secret.js +2 -73
  9. package/dist/config-secret.js.map +1 -1
  10. package/dist/config.d.ts +2 -1
  11. package/dist/config.d.ts.map +1 -1
  12. package/dist/config.js +23 -3
  13. package/dist/config.js.map +1 -1
  14. package/dist/context-types.d.ts +12 -0
  15. package/dist/context-types.d.ts.map +1 -0
  16. package/dist/context-types.js +2 -0
  17. package/dist/context-types.js.map +1 -0
  18. package/dist/context.d.ts +6 -0
  19. package/dist/context.d.ts.map +1 -1
  20. package/dist/context.js +25 -0
  21. package/dist/context.js.map +1 -1
  22. package/dist/identity.d.ts +5 -0
  23. package/dist/identity.d.ts.map +1 -1
  24. package/dist/identity.js +5 -0
  25. package/dist/identity.js.map +1 -1
  26. package/dist/ipc.d.ts +3 -1
  27. package/dist/ipc.d.ts.map +1 -1
  28. package/dist/ipc.js +34 -1
  29. package/dist/ipc.js.map +1 -1
  30. package/dist/kubo-install.d.ts +9 -0
  31. package/dist/kubo-install.d.ts.map +1 -0
  32. package/dist/kubo-install.js +361 -0
  33. package/dist/kubo-install.js.map +1 -0
  34. package/dist/kubo-peering.d.ts +11 -0
  35. package/dist/kubo-peering.d.ts.map +1 -0
  36. package/dist/kubo-peering.js +93 -0
  37. package/dist/kubo-peering.js.map +1 -0
  38. package/dist/kubo-peers.d.ts +12 -0
  39. package/dist/kubo-peers.d.ts.map +1 -0
  40. package/dist/kubo-peers.js +118 -0
  41. package/dist/kubo-peers.js.map +1 -0
  42. package/dist/kubo-release.d.ts +3 -0
  43. package/dist/kubo-release.d.ts.map +1 -0
  44. package/dist/kubo-release.js +12 -0
  45. package/dist/kubo-release.js.map +1 -0
  46. package/dist/kubo.d.ts +14 -5
  47. package/dist/kubo.d.ts.map +1 -1
  48. package/dist/kubo.js +186 -206
  49. package/dist/kubo.js.map +1 -1
  50. package/dist/runtime.d.ts +2 -0
  51. package/dist/runtime.d.ts.map +1 -1
  52. package/dist/runtime.js +6 -2
  53. package/dist/runtime.js.map +1 -1
  54. package/dist/setup.d.ts +2 -1
  55. package/dist/setup.d.ts.map +1 -1
  56. package/dist/setup.js +23 -18
  57. package/dist/setup.js.map +1 -1
  58. package/dist/state-storage-windows.d.ts +2 -0
  59. package/dist/state-storage-windows.d.ts.map +1 -0
  60. package/dist/state-storage-windows.js +114 -0
  61. package/dist/state-storage-windows.js.map +1 -0
  62. package/dist/state-storage.d.ts +3 -0
  63. package/dist/state-storage.d.ts.map +1 -0
  64. package/dist/state-storage.js +299 -0
  65. package/dist/state-storage.js.map +1 -0
  66. package/dist/state.d.ts +5 -3
  67. package/dist/state.d.ts.map +1 -1
  68. package/dist/state.js +64 -30
  69. package/dist/state.js.map +1 -1
  70. package/dist/swarm-files.d.ts +3 -2
  71. package/dist/swarm-files.d.ts.map +1 -1
  72. package/dist/swarm-files.js +25 -9
  73. package/dist/swarm-files.js.map +1 -1
  74. package/dist/swarm.d.ts +10 -5
  75. package/dist/swarm.d.ts.map +1 -1
  76. package/dist/swarm.js +225 -79
  77. package/dist/swarm.js.map +1 -1
  78. package/package.json +4 -4
  79. package/src/cli.ts +224 -60
  80. package/src/config-secret.ts +2 -84
  81. package/src/config.ts +25 -3
  82. package/src/context-types.ts +11 -0
  83. package/src/context.ts +40 -0
  84. package/src/identity.ts +13 -0
  85. package/src/ipc.ts +50 -1
  86. package/src/kubo-install.ts +452 -0
  87. package/src/kubo-peering.ts +126 -0
  88. package/src/kubo-peers.ts +145 -0
  89. package/src/kubo-release.ts +17 -0
  90. package/src/kubo.ts +254 -275
  91. package/src/runtime.ts +12 -1
  92. package/src/setup.ts +30 -17
  93. package/src/state-storage-windows.ts +134 -0
  94. package/src/state-storage.ts +329 -0
  95. package/src/state.ts +76 -35
  96. package/src/swarm-files.ts +26 -6
  97. package/src/swarm.ts +316 -97
package/src/state.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { mkdirSync } from "node:fs";
2
1
  import { randomUUID } from "node:crypto";
3
- import { join } from "node:path";
2
+ import { resolve } from "node:path";
4
3
  import { DatabaseSync } from "node:sqlite";
5
4
  import { CID } from "multiformats/cid";
6
5
  import type { StoreConfiguration } from "@ipab/ipfs-store";
7
6
  import {
7
+ rejectLegacySwarmCid,
8
8
  validateUserConfig,
9
9
  swarmUrl,
10
10
  type ConfigKey,
@@ -12,6 +12,7 @@ import {
12
12
  } from "./config.js";
13
13
  import { HostError, object, syntax } from "./errors.js";
14
14
  import { secureConfigurationStorage } from "./config-secret.js";
15
+ import { prepareStateStorage } from "./state-storage.js";
15
16
 
16
17
  export interface Association {
17
18
  schemaCid: string;
@@ -138,17 +139,43 @@ function persistedHold(value: string, owner: string): Hold {
138
139
  }
139
140
  }
140
141
 
142
+ // SQLite can report BUSY immediately when concurrent first opens both try to
143
+ // upgrade their read lock to switch journal mode, even with a busy timeout.
144
+ function enableWal(db: DatabaseSync): void {
145
+ const until = performance.now() + 5000;
146
+ for (;;) {
147
+ try {
148
+ db.exec("PRAGMA journal_mode=WAL");
149
+ if (db.prepare("PRAGMA journal_mode").get()?.journal_mode !== "wal")
150
+ throw new HostError(
151
+ "Cannot enable WAL for the private state database.",
152
+ );
153
+ return;
154
+ } catch (error) {
155
+ if (
156
+ (((error as { errcode?: number }).errcode ?? 0) & 0xff) !== 5 ||
157
+ performance.now() >= until
158
+ )
159
+ throw error;
160
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
161
+ }
162
+ }
163
+ }
164
+
141
165
  export class State {
142
166
  readonly db: DatabaseSync;
143
167
  readonly identity: string;
168
+ readonly directory: string;
144
169
  constructor(
145
- readonly directory: string,
170
+ directory: string,
146
171
  private readonly builtins: readonly NamedPin[] = [],
172
+ env: NodeJS.ProcessEnv = process.env,
147
173
  ) {
148
- mkdirSync(directory, { recursive: true, mode: 0o700 });
149
- this.db = new DatabaseSync(join(directory, "state.sqlite"));
150
- this.db
151
- .exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
174
+ this.directory = resolve(directory);
175
+ this.db = new DatabaseSync(prepareStateStorage(this.directory, env));
176
+ try {
177
+ enableWal(this.db);
178
+ this.db.exec(`PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;
152
179
  CREATE TABLE IF NOT EXISTS identity (id TEXT NOT NULL);
153
180
  CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT NOT NULL);
154
181
  DROP TABLE IF EXISTS aliases;
@@ -158,13 +185,17 @@ export class State {
158
185
  CREATE TABLE IF NOT EXISTS swarm_headers (account TEXT NOT NULL, swarm TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (account,swarm));
159
186
  CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, value TEXT NOT NULL);
160
187
  CREATE TABLE IF NOT EXISTS holds (owner TEXT PRIMARY KEY, value TEXT NOT NULL);`);
161
- this.transaction(() => {
162
- if (!this.db.prepare("SELECT id FROM identity").get())
163
- this.db.prepare("INSERT INTO identity VALUES (?)").run(randomUUID());
164
- });
165
- this.identity = String(
166
- this.db.prepare("SELECT id FROM identity").get()!.id,
167
- );
188
+ this.transaction(() => {
189
+ if (!this.db.prepare("SELECT id FROM identity").get())
190
+ this.db.prepare("INSERT INTO identity VALUES (?)").run(randomUUID());
191
+ });
192
+ this.identity = String(
193
+ this.db.prepare("SELECT id FROM identity").get()!.id,
194
+ );
195
+ } catch (error) {
196
+ this.db.close();
197
+ throw error;
198
+ }
168
199
  }
169
200
  close(): void {
170
201
  this.db.close();
@@ -180,14 +211,26 @@ export class State {
180
211
  throw error;
181
212
  }
182
213
  }
183
- config(): Partial<Record<ConfigKey, ConfigValue>> {
184
- const entries = this.db.prepare("SELECT key,value FROM config").all();
185
- if (
186
- entries.some(
187
- (row) => row.key === "pointers.apiKey" && row.value !== "null",
214
+ config({ includeSecrets = true }: { includeSecrets?: boolean } = {}): Partial<
215
+ Record<ConfigKey, ConfigValue>
216
+ > {
217
+ const entries = this.db
218
+ .prepare(
219
+ includeSecrets
220
+ ? "SELECT key, CASE WHEN key='pointers.apiKey' AND value!='null' THEN NULL ELSE value END AS value FROM config"
221
+ : "SELECT key,value FROM config WHERE key != 'pointers.apiKey'",
188
222
  )
189
- )
190
- secureConfigurationStorage(this.directory, false);
223
+ .all();
224
+ const secret = entries.find(
225
+ (row) => row.key === "pointers.apiKey" && row.value === null,
226
+ );
227
+ if (secret) {
228
+ secureConfigurationStorage(this.directory);
229
+ secret.value =
230
+ this.db
231
+ .prepare("SELECT value FROM config WHERE key='pointers.apiKey'")
232
+ .get()?.value ?? "null";
233
+ }
191
234
  if (entries.some((row) => row.key === "store.rpc"))
192
235
  throw new HostError(
193
236
  "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.",
@@ -209,7 +252,7 @@ export class State {
209
252
  }
210
253
  setConfig(key: ConfigKey, value: ConfigValue): void {
211
254
  if (key === "pointers.apiKey" && value !== null)
212
- secureConfigurationStorage(this.directory, true);
255
+ secureConfigurationStorage(this.directory);
213
256
  this.db
214
257
  .prepare("INSERT OR REPLACE INTO config VALUES (?,?)")
215
258
  .run(key, JSON.stringify(value));
@@ -234,13 +277,12 @@ export class State {
234
277
  }
235
278
  resolveSwarm(reference: string): string {
236
279
  if (/^https?:/.test(reference)) return swarmUrl(reference);
237
- {
238
- const entry = this.swarms().find((entry) => entry.alias === reference);
239
- if (entry) return entry.cid;
240
- throw new HostError(
241
- `Unknown swarm alias "${reference}". Run ipab swarm list.`,
242
- );
243
- }
280
+ const entry = this.swarms().find((entry) => entry.alias === reference);
281
+ if (entry) return entry.cid;
282
+ rejectLegacySwarmCid(reference);
283
+ throw new HostError(
284
+ `Unknown swarm alias "${reference}". Run ipab swarm list.`,
285
+ );
244
286
  }
245
287
  registeredSwarm(reference: string): RegisteredSwarm {
246
288
  const cid = this.resolveSwarm(reference);
@@ -253,11 +295,10 @@ export class State {
253
295
  }
254
296
  checkSwarmAlias(alias: string, cid?: string): void {
255
297
  swarmAlias(alias);
256
- if (
257
- this.swarms().some((entry) => entry.alias === alias && entry.cid !== cid)
258
- )
298
+ const existing = this.swarms().find((entry) => entry.alias === alias);
299
+ if (existing && existing.cid !== cid)
259
300
  throw new HostError(
260
- `Swarm alias "${alias}" is already assigned. Choose another alias.`,
301
+ `Swarm alias "${alias}" is already assigned to "${existing.cid}". To keep both Swarms, retry with a different --alias. To replace only this local registration, run ipab swarm remove ${alias} and retry. Removal clears a matching default but preserves credentials and remote membership.`,
261
302
  );
262
303
  }
263
304
  registerSwarm(cid: string, alias?: string): RegisteredSwarm {
@@ -269,7 +310,7 @@ export class State {
269
310
  this.db
270
311
  .prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
271
312
  .run(cid, entry.alias);
272
- if (this.config()["swarm.default"] == null)
313
+ if (this.config({ includeSecrets: false })["swarm.default"] == null)
273
314
  this.setConfig("swarm.default", cid);
274
315
  return entry;
275
316
  });
@@ -285,7 +326,7 @@ export class State {
285
326
  return this.transaction(() => {
286
327
  const entry = this.registeredSwarm(reference);
287
328
  this.db.prepare("DELETE FROM swarms WHERE cid=?").run(entry.cid);
288
- if (this.config()["swarm.default"] === entry.cid)
329
+ if (this.config({ includeSecrets: false })["swarm.default"] === entry.cid)
289
330
  this.resetConfig("swarm.default");
290
331
  return entry.cid;
291
332
  });
@@ -2,7 +2,12 @@ import { lstat, mkdir, open, readFile, rename, unlink } from "node:fs/promises";
2
2
  import { join } from "node:path";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { DatabaseSync } from "node:sqlite";
5
- import { protectedPath, secureNew } from "./identity.js";
5
+ import {
6
+ protectedPath,
7
+ protectedPaths,
8
+ secureNew,
9
+ type ProtectedPath,
10
+ } from "./identity.js";
6
11
  import { nodeError } from "./authoring.js";
7
12
  import { HostError } from "./errors.js";
8
13
 
@@ -24,13 +29,24 @@ export async function privateDirectory(
24
29
  export async function readPrivate<T>(
25
30
  directory: string,
26
31
  name: string,
32
+ additionalPaths: readonly ProtectedPath[] = [],
27
33
  ): Promise<T | undefined> {
28
34
  const path = join(directory, name);
29
- try {
30
- await protectedPath(path);
31
- } catch (cause) {
32
- if (nodeError(cause, "ENOENT")) return undefined;
33
- throw cause;
35
+ if (additionalPaths.length) {
36
+ try {
37
+ await lstat(path);
38
+ } catch (cause) {
39
+ if (nodeError(cause, "ENOENT")) return undefined;
40
+ throw cause;
41
+ }
42
+ await protectedPaths([{ path }, ...additionalPaths]);
43
+ } else {
44
+ try {
45
+ await protectedPath(path);
46
+ } catch (cause) {
47
+ if (nodeError(cause, "ENOENT")) return undefined;
48
+ throw cause;
49
+ }
34
50
  }
35
51
  if ((await lstat(path)).size > 2 * 1024 * 1024)
36
52
  throw new HostError("Protected state is oversized.");
@@ -60,7 +76,9 @@ export async function writePrivate(
60
76
  export async function withPrivateLock<T>(
61
77
  directory: string,
62
78
  operation: () => Promise<T>,
79
+ signal?: AbortSignal,
63
80
  ): Promise<T> {
81
+ signal?.throwIfAborted();
64
82
  const path = join(directory, "operation.sqlite");
65
83
  try {
66
84
  const handle = await open(path, "wx", 0o600);
@@ -75,6 +93,7 @@ export async function withPrivateLock<T>(
75
93
  try {
76
94
  db.exec("PRAGMA busy_timeout=0");
77
95
  for (let attempt = 0; attempt < 300; attempt++) {
96
+ signal?.throwIfAborted();
78
97
  try {
79
98
  db.exec("BEGIN IMMEDIATE");
80
99
  locked = true;
@@ -92,6 +111,7 @@ export async function withPrivateLock<T>(
92
111
  throw new HostError(
93
112
  "Another local Swarm operation is still running; retry later.",
94
113
  );
114
+ signal?.throwIfAborted();
95
115
  return await operation();
96
116
  } finally {
97
117
  if (locked) db.exec("ROLLBACK");