@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.
- package/README.md +21 -10
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +188 -46
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +1 -1
- package/dist/config-secret.d.ts.map +1 -1
- package/dist/config-secret.js +2 -73
- package/dist/config-secret.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +23 -3
- package/dist/config.js.map +1 -1
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +25 -0
- package/dist/context.js.map +1 -1
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +5 -0
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +3 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +34 -1
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +9 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +361 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +118 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +3 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +12 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo.d.ts +14 -5
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +186 -206
- package/dist/kubo.js.map +1 -1
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +6 -2
- package/dist/runtime.js.map +1 -1
- package/dist/setup.d.ts +2 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +23 -18
- package/dist/setup.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +3 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +299 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +5 -3
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +64 -30
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +3 -2
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +25 -9
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +10 -5
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +225 -79
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli.ts +224 -60
- package/src/config-secret.ts +2 -84
- package/src/config.ts +25 -3
- package/src/context-types.ts +11 -0
- package/src/context.ts +40 -0
- package/src/identity.ts +13 -0
- package/src/ipc.ts +50 -1
- package/src/kubo-install.ts +452 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +145 -0
- package/src/kubo-release.ts +17 -0
- package/src/kubo.ts +254 -275
- package/src/runtime.ts +12 -1
- package/src/setup.ts +30 -17
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +329 -0
- package/src/state.ts +76 -35
- package/src/swarm-files.ts +26 -6
- 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 {
|
|
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
|
-
|
|
170
|
+
directory: string,
|
|
146
171
|
private readonly builtins: readonly NamedPin[] = [],
|
|
172
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
147
173
|
) {
|
|
148
|
-
|
|
149
|
-
this.db = new DatabaseSync(
|
|
150
|
-
|
|
151
|
-
.
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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<
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
257
|
-
|
|
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.
|
|
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
|
});
|
package/src/swarm-files.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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");
|