@ipab/cli 0.1.0-rc.1 → 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.
- package/README.md +226 -9
- package/dist/app-authoring.d.ts +15 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +144 -58
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.js.map +1 -1
- package/dist/app-prebuild.d.ts +18 -0
- package/dist/app-prebuild.d.ts.map +1 -1
- package/dist/app-prebuild.js +46 -0
- package/dist/app-prebuild.js.map +1 -1
- package/dist/app-templates.d.ts +0 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +8 -46
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts +7 -0
- package/dist/assembly.d.ts.map +1 -0
- package/dist/assembly.js +130 -0
- package/dist/assembly.js.map +1 -0
- package/dist/authoring.d.ts +4 -8
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +25 -15
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.bundle-viewer.car +0 -0
- package/dist/builtins/template.check.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +1 -23
- package/dist/builtins.js.map +1 -1
- package/dist/bundled-skills.d.ts +14 -0
- package/dist/bundled-skills.d.ts.map +1 -0
- package/dist/bundled-skills.js +34 -0
- package/dist/bundled-skills.js.map +1 -0
- package/dist/cli.d.ts +8 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1919 -256
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +3 -0
- package/dist/config-secret.d.ts.map +1 -0
- package/dist/config-secret.js +12 -0
- package/dist/config-secret.js.map +1 -0
- package/dist/config.d.ts +7 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +151 -8
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +32 -0
- package/dist/content-provider.d.ts.map +1 -0
- package/dist/content-provider.js +222 -0
- package/dist/content-provider.js.map +1 -0
- 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 +26 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +54 -10
- package/dist/context.js.map +1 -1
- package/dist/daemon-ui.d.ts +93 -0
- package/dist/daemon-ui.d.ts.map +1 -0
- package/dist/daemon-ui.js +713 -0
- package/dist/daemon-ui.js.map +1 -0
- package/dist/daemon.d.ts +21 -0
- package/dist/daemon.d.ts.map +1 -0
- package/dist/daemon.js +317 -0
- package/dist/daemon.js.map +1 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +684 -0
- package/dist/doctor.js.map +1 -0
- package/dist/errors.d.ts +1 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -20
- package/dist/errors.js.map +1 -1
- package/dist/get.d.ts +3 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +80 -0
- package/dist/get.js.map +1 -0
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +6 -14
- package/dist/history.js.map +1 -1
- package/dist/host-error.d.ts +9 -0
- package/dist/host-error.d.ts.map +1 -0
- package/dist/host-error.js +21 -0
- package/dist/host-error.js.map +1 -0
- package/dist/identity.d.ts +16 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +119 -0
- package/dist/identity.js.map +1 -0
- package/dist/ipc.d.ts +5 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +124 -12
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -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 +119 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +113 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +85 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +755 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +16 -18
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/pivot-rpc.d.ts +62 -0
- package/dist/pivot-rpc.d.ts.map +1 -0
- package/dist/pivot-rpc.js +407 -0
- package/dist/pivot-rpc.js.map +1 -0
- package/dist/pivot-service.d.ts +16 -0
- package/dist/pivot-service.d.ts.map +1 -0
- package/dist/pivot-service.js +50 -0
- package/dist/pivot-service.js.map +1 -0
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +9 -4
- package/dist/runtime.js.map +1 -1
- package/dist/search-index.d.ts +64 -0
- package/dist/search-index.d.ts.map +1 -0
- package/dist/search-index.js +409 -0
- package/dist/search-index.js.map +1 -0
- package/dist/search-service.d.ts +23 -0
- package/dist/search-service.d.ts.map +1 -0
- package/dist/search-service.js +116 -0
- package/dist/search-service.js.map +1 -0
- package/dist/skill-definition.d.ts +6 -0
- package/dist/skill-definition.d.ts.map +1 -0
- package/dist/skill-definition.js +38 -0
- package/dist/skill-definition.js.map +1 -0
- package/dist/skill-source.d.ts +9 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +24 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +104 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +894 -0
- package/dist/skills.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +17 -12
- package/dist/source-bundle.js.map +1 -1
- package/dist/space-path.d.ts +3 -0
- package/dist/space-path.d.ts.map +1 -0
- package/dist/space-path.js +17 -0
- package/dist/space-path.js.map +1 -0
- package/dist/space.d.ts +16 -4
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +170 -24
- package/dist/space.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 +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +333 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +115 -13
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +492 -91
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +57 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +299 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +6 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +116 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm-meta.d.ts +15 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +68 -0
- package/dist/swarm-meta.js.map +1 -0
- package/dist/swarm-observations.d.ts +40 -0
- package/dist/swarm-observations.d.ts.map +1 -0
- package/dist/swarm-observations.js +271 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +469 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +4708 -0
- package/dist/swarm.js.map +1 -0
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.js.map +1 -1
- package/dist/template-paths.d.ts +7 -0
- package/dist/template-paths.d.ts.map +1 -0
- package/dist/template-paths.js +23 -0
- package/dist/template-paths.js.map +1 -0
- package/dist/template-presentation.d.ts +20 -0
- package/dist/template-presentation.d.ts.map +1 -0
- package/dist/template-presentation.js +133 -0
- package/dist/template-presentation.js.map +1 -0
- package/dist/template-source.d.ts +4 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +11 -4
- package/dist/template-source.js.map +1 -1
- package/dist/templates.d.ts +11 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +53 -0
- package/dist/templates.js.map +1 -0
- package/dist/unixfs-tree.d.ts +67 -0
- package/dist/unixfs-tree.d.ts.map +1 -0
- package/dist/unixfs-tree.js +329 -0
- package/dist/unixfs-tree.js.map +1 -0
- package/package.json +14 -7
- package/src/app-authoring.ts +240 -76
- package/src/app-definition.ts +33 -39
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +17 -73
- package/src/assembly.ts +185 -0
- package/src/authoring.ts +41 -17
- package/src/builtins.ts +2 -27
- package/src/bundled-skills.ts +60 -0
- package/src/cli.ts +2399 -320
- package/src/config-secret.ts +14 -0
- package/src/config.ts +196 -8
- package/src/content-provider.ts +299 -0
- package/src/context-types.ts +11 -0
- package/src/context.ts +98 -10
- package/src/daemon-ui.ts +812 -0
- package/src/daemon.ts +386 -0
- package/src/doctor.ts +1082 -0
- package/src/errors.ts +1 -25
- package/src/get.ts +90 -0
- package/src/history.ts +5 -15
- package/src/host-error.ts +26 -0
- package/src/identity.ts +175 -0
- package/src/ipc.ts +160 -11
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +144 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo-topology.ts +148 -0
- package/src/kubo.ts +1088 -0
- package/src/pins.ts +188 -126
- package/src/pivot-rpc.ts +601 -0
- package/src/pivot-service.ts +87 -0
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +16 -4
- package/src/search-index.ts +560 -0
- package/src/search-service.ts +152 -0
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +29 -0
- package/src/skills.ts +1236 -0
- package/src/source-bundle.ts +26 -13
- package/src/space-path.ts +30 -0
- package/src/space.ts +230 -42
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +378 -0
- package/src/state.ts +759 -107
- package/src/swarm-envelope.ts +461 -0
- package/src/swarm-files.ts +126 -0
- package/src/swarm-meta.ts +98 -0
- package/src/swarm-observations.ts +393 -0
- package/src/swarm.ts +7051 -0
- package/src/template-definition.ts +36 -41
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +19 -5
- package/src/templates.ts +77 -0
- package/src/unixfs-tree.ts +401 -0
package/src/state.ts
CHANGED
|
@@ -1,21 +1,102 @@
|
|
|
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
|
+
swarmUrl,
|
|
9
10
|
type ConfigKey,
|
|
10
11
|
type ConfigValue,
|
|
11
12
|
} from "./config.js";
|
|
12
|
-
import { HostError, object, syntax } from "./
|
|
13
|
+
import { HostError, object, syntax } from "./host-error.js";
|
|
14
|
+
import { openStateStorage } from "./state-storage.js";
|
|
13
15
|
|
|
14
16
|
export interface Association {
|
|
15
17
|
schemaCid: string;
|
|
16
18
|
appCid: string;
|
|
17
19
|
argv: string[];
|
|
18
20
|
}
|
|
21
|
+
export function createAssociation(
|
|
22
|
+
schemaCid: string,
|
|
23
|
+
appCid: string,
|
|
24
|
+
argv: string[],
|
|
25
|
+
): Association {
|
|
26
|
+
if (!argv.length || !argv.includes("%"))
|
|
27
|
+
syntax('Argument template must contain a whole "%" argument.');
|
|
28
|
+
return {
|
|
29
|
+
schemaCid: exactCid(schemaCid, true),
|
|
30
|
+
appCid: exactCid(appCid),
|
|
31
|
+
argv: [...argv],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface RegisteredSwarm {
|
|
35
|
+
// Keep the storage column name, but values are configuration URLs; old CID rows are rejected.
|
|
36
|
+
cid: string;
|
|
37
|
+
alias: string | null;
|
|
38
|
+
}
|
|
39
|
+
export interface SwarmProjection {
|
|
40
|
+
account: string;
|
|
41
|
+
swarm: string;
|
|
42
|
+
stateCid: string;
|
|
43
|
+
root: string;
|
|
44
|
+
valueCid: string;
|
|
45
|
+
verifiedAt: number;
|
|
46
|
+
headId: string;
|
|
47
|
+
epoch: number;
|
|
48
|
+
membership: string;
|
|
49
|
+
}
|
|
50
|
+
export type SearchEntryKind = "bundle" | "dir" | "file";
|
|
51
|
+
export type SearchFilterKind = "all" | SearchEntryKind;
|
|
52
|
+
export interface SearchIndexState {
|
|
53
|
+
headCid: string;
|
|
54
|
+
indexedAt: string;
|
|
55
|
+
}
|
|
56
|
+
export interface SearchIndexRow {
|
|
57
|
+
path: string;
|
|
58
|
+
cid: string;
|
|
59
|
+
kind: SearchEntryKind;
|
|
60
|
+
title: string | null;
|
|
61
|
+
description: string | null;
|
|
62
|
+
schemaCid: string | null;
|
|
63
|
+
}
|
|
64
|
+
export interface SearchIndexDelta {
|
|
65
|
+
headCid: string;
|
|
66
|
+
indexedAt: string;
|
|
67
|
+
fullReset: boolean;
|
|
68
|
+
deleteSubtrees: readonly string[];
|
|
69
|
+
deleteChildrenOnly: readonly string[];
|
|
70
|
+
upserts: readonly SearchIndexRow[];
|
|
71
|
+
}
|
|
72
|
+
export interface SearchIndexQuery {
|
|
73
|
+
q: string;
|
|
74
|
+
prefix?: string;
|
|
75
|
+
kind: SearchFilterKind;
|
|
76
|
+
schemaCid?: string;
|
|
77
|
+
limit: number;
|
|
78
|
+
offset: number;
|
|
79
|
+
}
|
|
80
|
+
export interface SearchIndexMatch {
|
|
81
|
+
path: string;
|
|
82
|
+
cid: string;
|
|
83
|
+
kind: SearchEntryKind;
|
|
84
|
+
bundle?: {
|
|
85
|
+
title: string;
|
|
86
|
+
description: string;
|
|
87
|
+
schemaCid: string;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export interface SearchIndexQueryResult {
|
|
91
|
+
headCid: string;
|
|
92
|
+
indexedAt: string;
|
|
93
|
+
total: number;
|
|
94
|
+
matches: SearchIndexMatch[];
|
|
95
|
+
}
|
|
96
|
+
const projectionVersion = 2;
|
|
97
|
+
export function swarmAlias(value: string): void {
|
|
98
|
+
pinName(value);
|
|
99
|
+
}
|
|
19
100
|
export interface RunRecord {
|
|
20
101
|
id: string;
|
|
21
102
|
origin: "exec" | "open";
|
|
@@ -43,9 +124,9 @@ export interface Hold {
|
|
|
43
124
|
recordId: string | null;
|
|
44
125
|
}
|
|
45
126
|
export type PinMetadata =
|
|
46
|
-
| { kind: "bundle";
|
|
47
|
-
| { kind: "schema";
|
|
48
|
-
| { kind: "content";
|
|
127
|
+
| { kind: "bundle"; title: string; schemaCid: string }
|
|
128
|
+
| { kind: "schema"; title: string; schemaCid: string }
|
|
129
|
+
| { kind: "content"; title: null; schemaCid: null };
|
|
49
130
|
export interface NamedPin {
|
|
50
131
|
name: string;
|
|
51
132
|
cid: string;
|
|
@@ -54,15 +135,24 @@ export interface NamedPin {
|
|
|
54
135
|
metadata: PinMetadata | null;
|
|
55
136
|
builtin?: true;
|
|
56
137
|
}
|
|
138
|
+
export interface ContentPin {
|
|
139
|
+
reference: string;
|
|
140
|
+
owner: string;
|
|
141
|
+
cid: string | null;
|
|
142
|
+
targetCid: string | null;
|
|
143
|
+
swarm: string | null;
|
|
144
|
+
name: string | null;
|
|
145
|
+
status: "pending" | "ready" | "deleting";
|
|
146
|
+
freshness: "current" | "stale" | "missing";
|
|
147
|
+
error: string | null;
|
|
148
|
+
}
|
|
57
149
|
export function bundlePinMetadata(pin: NamedPin) {
|
|
58
150
|
if (pin.status !== "ready" || !pin.metadata)
|
|
59
151
|
throw new HostError(
|
|
60
|
-
`
|
|
152
|
+
`Packaged reference "${pin.name}" is ${pin.status}; reinstall a verified CLI package before using it.`,
|
|
61
153
|
);
|
|
62
154
|
if (pin.metadata.kind !== "bundle")
|
|
63
|
-
throw new HostError(
|
|
64
|
-
`Pin "${pin.name}" is not a Bundle pin. Its retention is preserved; run ipab pin delete ${pin.name}, then wrap the content in a Bundle before adding a new pin.`,
|
|
65
|
-
);
|
|
155
|
+
throw new HostError(`Packaged reference "${pin.name}" is not a Bundle.`);
|
|
66
156
|
return pin.metadata;
|
|
67
157
|
}
|
|
68
158
|
|
|
@@ -128,31 +218,132 @@ function persistedHold(value: string, owner: string): Hold {
|
|
|
128
218
|
}
|
|
129
219
|
}
|
|
130
220
|
|
|
221
|
+
// SQLite can report BUSY immediately when concurrent first opens both try to
|
|
222
|
+
// upgrade their read lock to switch journal mode, even with a busy timeout.
|
|
223
|
+
function enableWal(db: DatabaseSync): void {
|
|
224
|
+
const until = performance.now() + 5000;
|
|
225
|
+
for (;;) {
|
|
226
|
+
try {
|
|
227
|
+
db.exec("PRAGMA journal_mode=WAL");
|
|
228
|
+
if (db.prepare("PRAGMA journal_mode").get()?.journal_mode !== "wal")
|
|
229
|
+
throw new HostError(
|
|
230
|
+
"Cannot enable WAL for the private state database.",
|
|
231
|
+
);
|
|
232
|
+
return;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (
|
|
235
|
+
(((error as { errcode?: number }).errcode ?? 0) & 0xff) !== 5 ||
|
|
236
|
+
performance.now() >= until
|
|
237
|
+
)
|
|
238
|
+
throw error;
|
|
239
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
131
244
|
export class State {
|
|
132
245
|
readonly db: DatabaseSync;
|
|
133
246
|
readonly identity: string;
|
|
247
|
+
readonly directory: string;
|
|
248
|
+
readonly legacyRegistrationCount: number;
|
|
249
|
+
readonly legacyPinCount: number;
|
|
134
250
|
constructor(
|
|
135
|
-
|
|
251
|
+
directory: string,
|
|
136
252
|
private readonly builtins: readonly NamedPin[] = [],
|
|
253
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
137
254
|
) {
|
|
138
|
-
|
|
139
|
-
this.db = new DatabaseSync(
|
|
140
|
-
|
|
141
|
-
.
|
|
255
|
+
this.directory = resolve(directory);
|
|
256
|
+
this.db = new DatabaseSync(openStateStorage(this.directory, env));
|
|
257
|
+
try {
|
|
258
|
+
enableWal(this.db);
|
|
259
|
+
this.db.exec(`PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;
|
|
142
260
|
CREATE TABLE IF NOT EXISTS identity (id TEXT NOT NULL);
|
|
143
261
|
CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
144
262
|
DROP TABLE IF EXISTS aliases;
|
|
145
|
-
CREATE TABLE IF NOT EXISTS
|
|
146
|
-
CREATE TABLE IF NOT EXISTS
|
|
263
|
+
CREATE TABLE IF NOT EXISTS content_pins (reference TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL);
|
|
264
|
+
CREATE TABLE IF NOT EXISTS swarms (cid TEXT PRIMARY KEY, alias TEXT UNIQUE);
|
|
265
|
+
CREATE TABLE IF NOT EXISTS swarm_headers (account TEXT NOT NULL, swarm TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (account,swarm));
|
|
266
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_checkpoints (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, state_cid TEXT NOT NULL, binding TEXT NOT NULL, PRIMARY KEY (account,swarm));
|
|
267
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_names (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, name TEXT NOT NULL, normalized TEXT NOT NULL, target_cid TEXT NOT NULL, PRIMARY KEY (account,swarm,name));
|
|
268
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_registrations (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, schema_cid TEXT NOT NULL, app_cid TEXT NOT NULL, argv TEXT NOT NULL, PRIMARY KEY (account,swarm,schema_cid));
|
|
269
|
+
CREATE TABLE IF NOT EXISTS swarm_search_state (swarm TEXT PRIMARY KEY, head_cid TEXT NOT NULL, indexed_at TEXT NOT NULL);
|
|
270
|
+
CREATE TABLE IF NOT EXISTS swarm_search_entries (swarm TEXT NOT NULL, path TEXT NOT NULL, cid TEXT NOT NULL, kind TEXT NOT NULL, title TEXT, description TEXT, schema_cid TEXT, path_lower TEXT NOT NULL, title_lower TEXT, description_lower TEXT, PRIMARY KEY (swarm,path));
|
|
271
|
+
CREATE INDEX IF NOT EXISTS idx_swarm_search_entries_swarm_kind ON swarm_search_entries (swarm,kind);
|
|
272
|
+
CREATE INDEX IF NOT EXISTS idx_swarm_search_entries_swarm_schema ON swarm_search_entries (swarm,schema_cid);
|
|
147
273
|
CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
148
274
|
CREATE TABLE IF NOT EXISTS holds (owner TEXT PRIMARY KEY, value TEXT NOT NULL);`);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
275
|
+
this.transaction(() => {
|
|
276
|
+
if (
|
|
277
|
+
this.db
|
|
278
|
+
.prepare(
|
|
279
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='named_pins'",
|
|
280
|
+
)
|
|
281
|
+
.get()
|
|
282
|
+
) {
|
|
283
|
+
if (
|
|
284
|
+
this.db
|
|
285
|
+
.prepare(
|
|
286
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_named_pins'",
|
|
287
|
+
)
|
|
288
|
+
.get()
|
|
289
|
+
)
|
|
290
|
+
throw new HostError(
|
|
291
|
+
"Both named_pins and legacy_named_pins exist; preserve both and resolve the archive conflict before retrying.",
|
|
292
|
+
);
|
|
293
|
+
this.db.exec("ALTER TABLE named_pins RENAME TO legacy_named_pins");
|
|
294
|
+
}
|
|
295
|
+
this.db.exec(
|
|
296
|
+
"CREATE TABLE IF NOT EXISTS legacy_named_pins (name TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL)",
|
|
297
|
+
);
|
|
298
|
+
if (
|
|
299
|
+
this.db
|
|
300
|
+
.prepare(
|
|
301
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='registrations'",
|
|
302
|
+
)
|
|
303
|
+
.get()
|
|
304
|
+
) {
|
|
305
|
+
if (
|
|
306
|
+
this.db
|
|
307
|
+
.prepare(
|
|
308
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_user_registrations'",
|
|
309
|
+
)
|
|
310
|
+
.get()
|
|
311
|
+
)
|
|
312
|
+
throw new HostError(
|
|
313
|
+
"Both registrations and legacy_user_registrations exist in the state database. Preserve both tables and resolve the archive conflict before retrying; no User registrations were uploaded or deleted.",
|
|
314
|
+
);
|
|
315
|
+
this.db.exec(
|
|
316
|
+
"ALTER TABLE registrations RENAME TO legacy_user_registrations",
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
if (!this.db.prepare("SELECT id FROM identity").get())
|
|
320
|
+
this.db.prepare("INSERT INTO identity VALUES (?)").run(randomUUID());
|
|
321
|
+
});
|
|
322
|
+
this.legacyPinCount = Number(
|
|
323
|
+
this.db
|
|
324
|
+
.prepare("SELECT COUNT(*) AS count FROM legacy_named_pins")
|
|
325
|
+
.get()!.count,
|
|
326
|
+
);
|
|
327
|
+
this.legacyRegistrationCount = this.db
|
|
328
|
+
.prepare(
|
|
329
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_user_registrations'",
|
|
330
|
+
)
|
|
331
|
+
.get()
|
|
332
|
+
? Number(
|
|
333
|
+
this.db
|
|
334
|
+
.prepare(
|
|
335
|
+
"SELECT COUNT(*) AS count FROM legacy_user_registrations",
|
|
336
|
+
)
|
|
337
|
+
.get()!.count,
|
|
338
|
+
)
|
|
339
|
+
: 0;
|
|
340
|
+
this.identity = String(
|
|
341
|
+
this.db.prepare("SELECT id FROM identity").get()!.id,
|
|
342
|
+
);
|
|
343
|
+
} catch (error) {
|
|
344
|
+
this.db.close();
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
156
347
|
}
|
|
157
348
|
close(): void {
|
|
158
349
|
this.db.close();
|
|
@@ -168,8 +359,16 @@ export class State {
|
|
|
168
359
|
throw error;
|
|
169
360
|
}
|
|
170
361
|
}
|
|
171
|
-
config(): Partial<
|
|
172
|
-
|
|
362
|
+
config({ includeSecrets = true }: { includeSecrets?: boolean } = {}): Partial<
|
|
363
|
+
Record<ConfigKey, ConfigValue>
|
|
364
|
+
> {
|
|
365
|
+
const entries = this.db
|
|
366
|
+
.prepare(
|
|
367
|
+
includeSecrets
|
|
368
|
+
? "SELECT key,value FROM config"
|
|
369
|
+
: "SELECT key,value FROM config WHERE key != 'pointers.apiKey'",
|
|
370
|
+
)
|
|
371
|
+
.all();
|
|
173
372
|
if (entries.some((row) => row.key === "store.rpc"))
|
|
174
373
|
throw new HostError(
|
|
175
374
|
"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.",
|
|
@@ -197,6 +396,463 @@ export class State {
|
|
|
197
396
|
resetConfig(key: ConfigKey | "store.rpc"): void {
|
|
198
397
|
this.db.prepare("DELETE FROM config WHERE key=?").run(key);
|
|
199
398
|
}
|
|
399
|
+
swarms(): RegisteredSwarm[] {
|
|
400
|
+
return this.db
|
|
401
|
+
.prepare("SELECT cid,alias FROM swarms ORDER BY cid")
|
|
402
|
+
.all()
|
|
403
|
+
.map((row) => {
|
|
404
|
+
if (
|
|
405
|
+
typeof row.cid !== "string" ||
|
|
406
|
+
(row.alias !== null && typeof row.alias !== "string")
|
|
407
|
+
)
|
|
408
|
+
throw new HostError("Invalid persisted swarm registration.");
|
|
409
|
+
const cid = swarmUrl(row.cid);
|
|
410
|
+
if (row.alias !== null) swarmAlias(row.alias);
|
|
411
|
+
return { cid, alias: row.alias };
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
resolveSwarm(reference: string): string {
|
|
415
|
+
if (/^https?:/.test(reference)) return swarmUrl(reference);
|
|
416
|
+
const entry = this.swarms().find((entry) => entry.alias === reference);
|
|
417
|
+
if (entry) return entry.cid;
|
|
418
|
+
rejectLegacySwarmCid(reference);
|
|
419
|
+
throw new HostError(
|
|
420
|
+
`Unknown swarm alias "${reference}". Run ipab swarm list.`,
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
registeredSwarm(reference: string): RegisteredSwarm {
|
|
424
|
+
const cid = this.resolveSwarm(reference);
|
|
425
|
+
const entry = this.swarms().find((entry) => entry.cid === cid);
|
|
426
|
+
if (!entry)
|
|
427
|
+
throw new HostError(
|
|
428
|
+
`Swarm "${reference}" is not registered. Run ipab swarm set ALIAS ${cid}.`,
|
|
429
|
+
);
|
|
430
|
+
return entry;
|
|
431
|
+
}
|
|
432
|
+
checkSwarmAlias(alias: string, cid?: string): void {
|
|
433
|
+
swarmAlias(alias);
|
|
434
|
+
const existing = this.swarms().find((entry) => entry.alias === alias);
|
|
435
|
+
if (existing && existing.cid !== cid)
|
|
436
|
+
throw new HostError(
|
|
437
|
+
`Swarm alias "${alias}" is already assigned to "${existing.cid}". Choose a different alias, or explicitly retarget it with ipab swarm set ${alias} CONFIG_URL.`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
441
|
+
cid = swarmUrl(cid);
|
|
442
|
+
return this.transaction(() => {
|
|
443
|
+
if (alias !== undefined) this.checkSwarmAlias(alias, cid);
|
|
444
|
+
return this.saveSwarmRegistration(cid, alias);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
setSwarm(
|
|
448
|
+
alias: string,
|
|
449
|
+
url: string,
|
|
450
|
+
expectedTarget: string | null,
|
|
451
|
+
): RegisteredSwarm {
|
|
452
|
+
swarmAlias(alias);
|
|
453
|
+
url = swarmUrl(url);
|
|
454
|
+
return this.transaction(() => {
|
|
455
|
+
const current = this.swarms().find((entry) => entry.alias === alias);
|
|
456
|
+
if ((current?.cid ?? null) !== expectedTarget)
|
|
457
|
+
throw new HostError(
|
|
458
|
+
`Swarm alias "${alias}" changed while setting it. Run ipab swarm list and retry.`,
|
|
459
|
+
);
|
|
460
|
+
if (current && current.cid !== url)
|
|
461
|
+
this.db
|
|
462
|
+
.prepare("UPDATE swarms SET alias=NULL WHERE cid=?")
|
|
463
|
+
.run(current.cid);
|
|
464
|
+
return this.saveSwarmRegistration(url, alias);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
private saveSwarmRegistration(cid: string, alias?: string): RegisteredSwarm {
|
|
468
|
+
const previous = this.swarms().find((entry) => entry.cid === cid);
|
|
469
|
+
const entry = { cid, alias: alias ?? previous?.alias ?? null };
|
|
470
|
+
this.db
|
|
471
|
+
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
472
|
+
.run(cid, entry.alias);
|
|
473
|
+
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
474
|
+
this.setConfig("swarm.default", cid);
|
|
475
|
+
return entry;
|
|
476
|
+
}
|
|
477
|
+
useSwarm(reference: string): string {
|
|
478
|
+
return this.transaction(() => {
|
|
479
|
+
const entry = this.registeredSwarm(reference);
|
|
480
|
+
this.setConfig("swarm.default", entry.cid);
|
|
481
|
+
return entry.cid;
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
removeSwarm(reference: string): string {
|
|
485
|
+
return this.transaction(() => {
|
|
486
|
+
const entry = this.registeredSwarm(reference);
|
|
487
|
+
this.db.prepare("DELETE FROM swarms WHERE cid=?").run(entry.cid);
|
|
488
|
+
this.deleteSwarmCache(entry.cid);
|
|
489
|
+
if (this.config({ includeSecrets: false })["swarm.default"] === entry.cid)
|
|
490
|
+
this.resetConfig("swarm.default");
|
|
491
|
+
return entry.cid;
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
swarmHeader(account: string, swarm: string): unknown {
|
|
495
|
+
const row = this.db
|
|
496
|
+
.prepare("SELECT value FROM swarm_headers WHERE account=? AND swarm=?")
|
|
497
|
+
.get(account, swarm);
|
|
498
|
+
if (!row) return undefined;
|
|
499
|
+
try {
|
|
500
|
+
return JSON.parse(String(row.value)) as unknown;
|
|
501
|
+
} catch {
|
|
502
|
+
throw new HostError("Malformed persisted swarm header cache.");
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
saveSwarmHeader(account: string, swarm: string, value: unknown): void {
|
|
506
|
+
this.db
|
|
507
|
+
.prepare("INSERT OR REPLACE INTO swarm_headers VALUES (?,?,?)")
|
|
508
|
+
.run(account, swarm, JSON.stringify(value));
|
|
509
|
+
}
|
|
510
|
+
invalidateSwarmHeader(swarm: string): void {
|
|
511
|
+
this.transaction(() => {
|
|
512
|
+
this.deleteSwarmCache(swarm);
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
private deleteSwarmCache(swarm: string): void {
|
|
516
|
+
this.db.prepare("DELETE FROM swarm_headers WHERE swarm=?").run(swarm);
|
|
517
|
+
for (const table of [
|
|
518
|
+
"swarm_projection_checkpoints",
|
|
519
|
+
"swarm_projection_names",
|
|
520
|
+
"swarm_projection_registrations",
|
|
521
|
+
])
|
|
522
|
+
this.db.prepare(`DELETE FROM ${table} WHERE swarm=?`).run(swarm);
|
|
523
|
+
}
|
|
524
|
+
private checkProjectionHeader(binding: SwarmProjection): void {
|
|
525
|
+
const cached = this.swarmHeader(binding.account, binding.swarm) as
|
|
526
|
+
| {
|
|
527
|
+
head: { root: string; cid: string };
|
|
528
|
+
stateCid?: string;
|
|
529
|
+
verifiedAt: number;
|
|
530
|
+
headId: string;
|
|
531
|
+
epoch?: number;
|
|
532
|
+
}
|
|
533
|
+
| undefined;
|
|
534
|
+
if (
|
|
535
|
+
!cached ||
|
|
536
|
+
cached.head.root !== binding.root ||
|
|
537
|
+
cached.head.cid !== binding.valueCid ||
|
|
538
|
+
cached.verifiedAt !== binding.verifiedAt ||
|
|
539
|
+
cached.headId !== binding.headId ||
|
|
540
|
+
(cached.epoch !== undefined && cached.epoch !== binding.epoch) ||
|
|
541
|
+
(cached.stateCid !== undefined && cached.stateCid !== binding.stateCid)
|
|
542
|
+
)
|
|
543
|
+
throw new HostError("Swarm header changed; retry the verified read.");
|
|
544
|
+
}
|
|
545
|
+
projectSwarm(
|
|
546
|
+
binding: SwarmProjection,
|
|
547
|
+
names: Readonly<Record<string, CID>>,
|
|
548
|
+
registrations: () => readonly Association[],
|
|
549
|
+
): void {
|
|
550
|
+
this.transaction(() => {
|
|
551
|
+
this.checkProjectionHeader(binding);
|
|
552
|
+
const cached = this.swarmHeader(binding.account, binding.swarm) as Record<
|
|
553
|
+
string,
|
|
554
|
+
unknown
|
|
555
|
+
>;
|
|
556
|
+
if (cached.stateCid === undefined)
|
|
557
|
+
this.saveSwarmHeader(binding.account, binding.swarm, {
|
|
558
|
+
...cached,
|
|
559
|
+
stateCid: binding.stateCid,
|
|
560
|
+
});
|
|
561
|
+
const old = this.db
|
|
562
|
+
.prepare(
|
|
563
|
+
"SELECT version,state_cid,binding FROM swarm_projection_checkpoints WHERE account=? AND swarm=?",
|
|
564
|
+
)
|
|
565
|
+
.get(binding.account, binding.swarm);
|
|
566
|
+
const encoded = JSON.stringify(binding);
|
|
567
|
+
if (old?.version === projectionVersion && old.binding === encoded) return;
|
|
568
|
+
// A new Publication may reference the same State: only provenance changes.
|
|
569
|
+
if (
|
|
570
|
+
old?.version !== projectionVersion ||
|
|
571
|
+
old.state_cid !== binding.stateCid
|
|
572
|
+
) {
|
|
573
|
+
for (const table of [
|
|
574
|
+
"swarm_projection_names",
|
|
575
|
+
"swarm_projection_registrations",
|
|
576
|
+
])
|
|
577
|
+
this.db
|
|
578
|
+
.prepare(`DELETE FROM ${table} WHERE account=? AND swarm=?`)
|
|
579
|
+
.run(binding.account, binding.swarm);
|
|
580
|
+
const insertName = this.db.prepare(
|
|
581
|
+
"INSERT INTO swarm_projection_names VALUES (?,?,?,?,?,?)",
|
|
582
|
+
);
|
|
583
|
+
for (const [name, target] of Object.entries(names))
|
|
584
|
+
insertName.run(
|
|
585
|
+
binding.account,
|
|
586
|
+
binding.swarm,
|
|
587
|
+
projectionVersion,
|
|
588
|
+
name,
|
|
589
|
+
name.toLowerCase(),
|
|
590
|
+
target.toString(),
|
|
591
|
+
);
|
|
592
|
+
const insertRegistration = this.db.prepare(
|
|
593
|
+
"INSERT INTO swarm_projection_registrations VALUES (?,?,?,?,?,?)",
|
|
594
|
+
);
|
|
595
|
+
for (const item of registrations())
|
|
596
|
+
insertRegistration.run(
|
|
597
|
+
binding.account,
|
|
598
|
+
binding.swarm,
|
|
599
|
+
projectionVersion,
|
|
600
|
+
item.schemaCid,
|
|
601
|
+
item.appCid,
|
|
602
|
+
JSON.stringify(item.argv),
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
this.db
|
|
606
|
+
.prepare(
|
|
607
|
+
"INSERT OR REPLACE INTO swarm_projection_checkpoints VALUES (?,?,?,?,?)",
|
|
608
|
+
)
|
|
609
|
+
.run(
|
|
610
|
+
binding.account,
|
|
611
|
+
binding.swarm,
|
|
612
|
+
projectionVersion,
|
|
613
|
+
binding.stateCid,
|
|
614
|
+
encoded,
|
|
615
|
+
);
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
private checkProjection(binding: SwarmProjection): void {
|
|
619
|
+
this.checkProjectionHeader(binding);
|
|
620
|
+
if (
|
|
621
|
+
!this.db
|
|
622
|
+
.prepare(
|
|
623
|
+
"SELECT 1 FROM swarm_projection_checkpoints WHERE account=? AND swarm=? AND version=? AND binding=?",
|
|
624
|
+
)
|
|
625
|
+
.get(
|
|
626
|
+
binding.account,
|
|
627
|
+
binding.swarm,
|
|
628
|
+
projectionVersion,
|
|
629
|
+
JSON.stringify(binding),
|
|
630
|
+
)
|
|
631
|
+
)
|
|
632
|
+
throw new HostError("Swarm projection changed; retry the verified read.");
|
|
633
|
+
}
|
|
634
|
+
searchSwarmNames(
|
|
635
|
+
binding: SwarmProjection,
|
|
636
|
+
query: string,
|
|
637
|
+
): { name: string; targetCid: string }[] {
|
|
638
|
+
return this.transaction(() => {
|
|
639
|
+
this.checkProjection(binding);
|
|
640
|
+
return this.db
|
|
641
|
+
.prepare(
|
|
642
|
+
"SELECT name,target_cid FROM swarm_projection_names WHERE account=? AND swarm=? AND version=? AND instr(normalized,?)>0",
|
|
643
|
+
)
|
|
644
|
+
.all(
|
|
645
|
+
binding.account,
|
|
646
|
+
binding.swarm,
|
|
647
|
+
projectionVersion,
|
|
648
|
+
query.toLowerCase(),
|
|
649
|
+
)
|
|
650
|
+
.map((row) => ({
|
|
651
|
+
name: String(row.name),
|
|
652
|
+
targetCid: String(row.target_cid),
|
|
653
|
+
}))
|
|
654
|
+
.sort((left, right) =>
|
|
655
|
+
left.name < right.name ? -1 : left.name > right.name ? 1 : 0,
|
|
656
|
+
);
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
searchIndexState(swarm: string): SearchIndexState | null {
|
|
660
|
+
const row = this.db
|
|
661
|
+
.prepare(
|
|
662
|
+
"SELECT head_cid, indexed_at FROM swarm_search_state WHERE swarm = ?",
|
|
663
|
+
)
|
|
664
|
+
.get(swarm);
|
|
665
|
+
if (!row) return null;
|
|
666
|
+
return {
|
|
667
|
+
headCid: String(row.head_cid),
|
|
668
|
+
indexedAt: String(row.indexed_at),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
applySearchIndexDelta(swarm: string, delta: SearchIndexDelta): void {
|
|
672
|
+
this.transaction(() => {
|
|
673
|
+
if (delta.fullReset) {
|
|
674
|
+
this.db
|
|
675
|
+
.prepare("DELETE FROM swarm_search_entries WHERE swarm = ?")
|
|
676
|
+
.run(swarm);
|
|
677
|
+
} else {
|
|
678
|
+
const deleteSubtree = this.db.prepare(
|
|
679
|
+
"DELETE FROM swarm_search_entries WHERE swarm = ? AND (path = ? OR (path >= ? AND path < ?))",
|
|
680
|
+
);
|
|
681
|
+
for (const prefix of delta.deleteSubtrees) {
|
|
682
|
+
deleteSubtree.run(swarm, prefix, `${prefix}/`, `${prefix}0`);
|
|
683
|
+
}
|
|
684
|
+
const deleteChildren = this.db.prepare(
|
|
685
|
+
"DELETE FROM swarm_search_entries WHERE swarm = ? AND path >= ? AND path < ?",
|
|
686
|
+
);
|
|
687
|
+
for (const prefix of delta.deleteChildrenOnly) {
|
|
688
|
+
deleteChildren.run(swarm, `${prefix}/`, `${prefix}0`);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
const upsert = this.db.prepare(
|
|
692
|
+
"INSERT OR REPLACE INTO swarm_search_entries (swarm, path, cid, kind, title, description, schema_cid, path_lower, title_lower, description_lower) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
693
|
+
);
|
|
694
|
+
for (const entry of delta.upserts) {
|
|
695
|
+
upsert.run(
|
|
696
|
+
swarm,
|
|
697
|
+
entry.path,
|
|
698
|
+
entry.cid,
|
|
699
|
+
entry.kind,
|
|
700
|
+
entry.title,
|
|
701
|
+
entry.description,
|
|
702
|
+
entry.schemaCid,
|
|
703
|
+
entry.path.toLowerCase(),
|
|
704
|
+
entry.title !== null ? entry.title.toLowerCase() : null,
|
|
705
|
+
entry.description !== null ? entry.description.toLowerCase() : null,
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
this.db
|
|
709
|
+
.prepare(
|
|
710
|
+
"INSERT OR REPLACE INTO swarm_search_state (swarm, head_cid, indexed_at) VALUES (?, ?, ?)",
|
|
711
|
+
)
|
|
712
|
+
.run(swarm, delta.headCid, delta.indexedAt);
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
querySearchIndex(
|
|
716
|
+
swarm: string,
|
|
717
|
+
params: SearchIndexQuery,
|
|
718
|
+
): SearchIndexQueryResult | null {
|
|
719
|
+
this.db.exec("BEGIN");
|
|
720
|
+
try {
|
|
721
|
+
const stateRow = this.db
|
|
722
|
+
.prepare(
|
|
723
|
+
"SELECT head_cid, indexed_at FROM swarm_search_state WHERE swarm = ?",
|
|
724
|
+
)
|
|
725
|
+
.get(swarm);
|
|
726
|
+
if (!stateRow) {
|
|
727
|
+
this.db.exec("COMMIT");
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
const needle = params.q.toLowerCase();
|
|
731
|
+
const conditions: string[] = [
|
|
732
|
+
"swarm = ?",
|
|
733
|
+
"(instr(path_lower, ?) > 0 OR (title_lower IS NOT NULL AND instr(title_lower, ?) > 0) OR (description_lower IS NOT NULL AND instr(description_lower, ?) > 0))",
|
|
734
|
+
];
|
|
735
|
+
const whereValues: (string | number)[] = [swarm, needle, needle, needle];
|
|
736
|
+
if (params.prefix !== undefined) {
|
|
737
|
+
conditions.push("(path = ? OR (path >= ? AND path < ?))");
|
|
738
|
+
whereValues.push(
|
|
739
|
+
params.prefix,
|
|
740
|
+
`${params.prefix}/`,
|
|
741
|
+
`${params.prefix}0`,
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
if (params.kind !== "all") {
|
|
745
|
+
conditions.push("kind = ?");
|
|
746
|
+
whereValues.push(params.kind);
|
|
747
|
+
}
|
|
748
|
+
if (params.schemaCid !== undefined) {
|
|
749
|
+
conditions.push("schema_cid = ?");
|
|
750
|
+
whereValues.push(params.schemaCid);
|
|
751
|
+
}
|
|
752
|
+
const whereSql = conditions.join(" AND ");
|
|
753
|
+
const countRow = this.db
|
|
754
|
+
.prepare(
|
|
755
|
+
`SELECT COUNT(*) AS total FROM swarm_search_entries WHERE ${whereSql}`,
|
|
756
|
+
)
|
|
757
|
+
.get(...whereValues);
|
|
758
|
+
const total = Number(countRow?.total ?? 0);
|
|
759
|
+
const needleLen = needle.length;
|
|
760
|
+
const rows = this.db
|
|
761
|
+
.prepare(
|
|
762
|
+
`SELECT path, cid, kind, title, description, schema_cid
|
|
763
|
+
FROM swarm_search_entries
|
|
764
|
+
WHERE ${whereSql}
|
|
765
|
+
ORDER BY
|
|
766
|
+
CASE
|
|
767
|
+
WHEN path_lower = ? THEN 0
|
|
768
|
+
WHEN substr(path_lower, 1, ?) = ? THEN 1
|
|
769
|
+
WHEN title_lower IS NOT NULL AND title_lower = ? THEN 2
|
|
770
|
+
WHEN title_lower IS NOT NULL AND substr(title_lower, 1, ?) = ? THEN 3
|
|
771
|
+
WHEN instr(path_lower, ?) > 0 THEN 4
|
|
772
|
+
WHEN title_lower IS NOT NULL AND instr(title_lower, ?) > 0 THEN 5
|
|
773
|
+
ELSE 6
|
|
774
|
+
END ASC,
|
|
775
|
+
path ASC
|
|
776
|
+
LIMIT ? OFFSET ?`,
|
|
777
|
+
)
|
|
778
|
+
.all(
|
|
779
|
+
...whereValues,
|
|
780
|
+
needle,
|
|
781
|
+
needleLen,
|
|
782
|
+
needle,
|
|
783
|
+
needle,
|
|
784
|
+
needleLen,
|
|
785
|
+
needle,
|
|
786
|
+
needle,
|
|
787
|
+
needle,
|
|
788
|
+
params.limit,
|
|
789
|
+
params.offset,
|
|
790
|
+
);
|
|
791
|
+
this.db.exec("COMMIT");
|
|
792
|
+
const matches: SearchIndexMatch[] = rows.map((row) => {
|
|
793
|
+
const kind = String(row.kind) as SearchEntryKind;
|
|
794
|
+
const match: SearchIndexMatch = {
|
|
795
|
+
path: String(row.path),
|
|
796
|
+
cid: String(row.cid),
|
|
797
|
+
kind,
|
|
798
|
+
};
|
|
799
|
+
if (
|
|
800
|
+
kind === "bundle" &&
|
|
801
|
+
typeof row.title === "string" &&
|
|
802
|
+
typeof row.description === "string" &&
|
|
803
|
+
typeof row.schema_cid === "string"
|
|
804
|
+
) {
|
|
805
|
+
match.bundle = {
|
|
806
|
+
title: row.title,
|
|
807
|
+
description: row.description,
|
|
808
|
+
schemaCid: row.schema_cid,
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
return match;
|
|
812
|
+
});
|
|
813
|
+
return {
|
|
814
|
+
headCid: String(stateRow.head_cid),
|
|
815
|
+
indexedAt: String(stateRow.indexed_at),
|
|
816
|
+
total,
|
|
817
|
+
matches,
|
|
818
|
+
};
|
|
819
|
+
} catch (error) {
|
|
820
|
+
this.db.exec("ROLLBACK");
|
|
821
|
+
throw error;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
projectedSwarmRegistrations(
|
|
825
|
+
binding: SwarmProjection,
|
|
826
|
+
schemaCid?: string,
|
|
827
|
+
): Association[] {
|
|
828
|
+
return this.transaction(() => {
|
|
829
|
+
this.checkProjection(binding);
|
|
830
|
+
const rows =
|
|
831
|
+
schemaCid === undefined
|
|
832
|
+
? this.db
|
|
833
|
+
.prepare(
|
|
834
|
+
"SELECT schema_cid,app_cid,argv FROM swarm_projection_registrations WHERE account=? AND swarm=? AND version=?",
|
|
835
|
+
)
|
|
836
|
+
.all(binding.account, binding.swarm, projectionVersion)
|
|
837
|
+
: this.db
|
|
838
|
+
.prepare(
|
|
839
|
+
"SELECT schema_cid,app_cid,argv FROM swarm_projection_registrations WHERE account=? AND swarm=? AND version=? AND schema_cid=?",
|
|
840
|
+
)
|
|
841
|
+
.all(
|
|
842
|
+
binding.account,
|
|
843
|
+
binding.swarm,
|
|
844
|
+
projectionVersion,
|
|
845
|
+
schemaCid,
|
|
846
|
+
);
|
|
847
|
+
return rows
|
|
848
|
+
.map((row) => ({
|
|
849
|
+
schemaCid: String(row.schema_cid),
|
|
850
|
+
appCid: String(row.app_cid),
|
|
851
|
+
argv: JSON.parse(String(row.argv)) as string[],
|
|
852
|
+
}))
|
|
853
|
+
.sort((left, right) => left.schemaCid.localeCompare(right.schemaCid));
|
|
854
|
+
});
|
|
855
|
+
}
|
|
200
856
|
resolve(value: string, schema = false): string {
|
|
201
857
|
try {
|
|
202
858
|
return exactCid(value, schema);
|
|
@@ -204,43 +860,26 @@ export class State {
|
|
|
204
860
|
try {
|
|
205
861
|
CID.parse(value);
|
|
206
862
|
} catch {
|
|
207
|
-
const pin = this.pin(value);
|
|
863
|
+
const pin = value.startsWith("builtin:") ? this.pin(value) : undefined;
|
|
208
864
|
if (pin) {
|
|
209
865
|
const metadata = bundlePinMetadata(pin);
|
|
210
866
|
if (schema) return metadata.schemaCid;
|
|
211
867
|
return exactCid(pin.cid, schema);
|
|
212
868
|
}
|
|
213
869
|
throw new HostError(
|
|
214
|
-
`Unknown CID or
|
|
870
|
+
`Unknown CID or packaged builtin reference "${value}". Local content aliases are unsupported; use an exact CID or a Swarm IPAB URI.`,
|
|
215
871
|
);
|
|
216
872
|
}
|
|
217
873
|
throw error;
|
|
218
874
|
}
|
|
219
875
|
}
|
|
220
|
-
resolvePin(name: string): string | undefined {
|
|
221
|
-
const pin = this.pin(name);
|
|
222
|
-
if (!pin) return undefined;
|
|
223
|
-
bundlePinMetadata(pin);
|
|
224
|
-
return exactCid(pin.cid);
|
|
225
|
-
}
|
|
226
876
|
pins(): NamedPin[] {
|
|
227
|
-
|
|
228
|
-
.prepare("SELECT name,owner,value FROM named_pins ORDER BY name")
|
|
229
|
-
.all()
|
|
230
|
-
.map((row) =>
|
|
231
|
-
this.readPin(String(row.name), String(row.owner), String(row.value)),
|
|
232
|
-
);
|
|
233
|
-
return [...user, ...this.builtins];
|
|
877
|
+
return [...this.builtins];
|
|
234
878
|
}
|
|
235
879
|
pin(name: string): NamedPin | undefined {
|
|
236
880
|
const builtin = this.builtins.find((pin) => pin.name === name);
|
|
237
881
|
if (builtin) return builtin;
|
|
238
|
-
|
|
239
|
-
.prepare("SELECT owner,value FROM named_pins WHERE name=?")
|
|
240
|
-
.get(name);
|
|
241
|
-
return row
|
|
242
|
-
? this.readPin(name, String(row.owner), String(row.value))
|
|
243
|
-
: undefined;
|
|
882
|
+
return undefined;
|
|
244
883
|
}
|
|
245
884
|
private readPin(name: string, owner: string, value: string): NamedPin {
|
|
246
885
|
const data = object(JSON.parse(value) as unknown, "named pin");
|
|
@@ -258,20 +897,20 @@ export class State {
|
|
|
258
897
|
if (data.metadata !== null) {
|
|
259
898
|
const value = object(data.metadata, "pin type metadata");
|
|
260
899
|
if (value.kind === "content") {
|
|
261
|
-
if (value.
|
|
900
|
+
if (value.title !== null || value.schemaCid !== null)
|
|
262
901
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
263
|
-
metadata = { kind: "content",
|
|
902
|
+
metadata = { kind: "content", title: null, schemaCid: null };
|
|
264
903
|
} else {
|
|
265
904
|
if (
|
|
266
905
|
(value.kind !== "bundle" && value.kind !== "schema") ||
|
|
267
|
-
typeof value.
|
|
268
|
-
!value.
|
|
906
|
+
typeof value.title !== "string" ||
|
|
907
|
+
!value.title.trim() ||
|
|
269
908
|
typeof value.schemaCid !== "string"
|
|
270
909
|
)
|
|
271
910
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
272
911
|
metadata = {
|
|
273
912
|
kind: value.kind,
|
|
274
|
-
|
|
913
|
+
title: value.title,
|
|
275
914
|
schemaCid: exactCid(value.schemaCid, true),
|
|
276
915
|
};
|
|
277
916
|
}
|
|
@@ -285,73 +924,86 @@ export class State {
|
|
|
285
924
|
metadata,
|
|
286
925
|
};
|
|
287
926
|
}
|
|
288
|
-
|
|
289
|
-
|
|
927
|
+
archivedPins(): NamedPin[] {
|
|
928
|
+
return this.db
|
|
929
|
+
.prepare("SELECT name,owner,value FROM legacy_named_pins ORDER BY name")
|
|
930
|
+
.all()
|
|
931
|
+
.map((row) =>
|
|
932
|
+
this.readPin(String(row.name), String(row.owner), String(row.value)),
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
contentPins(): ContentPin[] {
|
|
936
|
+
return this.db
|
|
937
|
+
.prepare(
|
|
938
|
+
"SELECT reference,owner,value FROM content_pins ORDER BY reference",
|
|
939
|
+
)
|
|
940
|
+
.all()
|
|
941
|
+
.map((row) => {
|
|
942
|
+
const pin = JSON.parse(String(row.value)) as ContentPin;
|
|
943
|
+
if (
|
|
944
|
+
pin.reference !== row.reference ||
|
|
945
|
+
pin.owner !== row.owner ||
|
|
946
|
+
!["pending", "ready", "deleting"].includes(pin.status) ||
|
|
947
|
+
!["current", "stale", "missing"].includes(pin.freshness) ||
|
|
948
|
+
(pin.error !== null && typeof pin.error !== "string") ||
|
|
949
|
+
(pin.cid !== null && typeof pin.cid !== "string") ||
|
|
950
|
+
(pin.targetCid !== null && typeof pin.targetCid !== "string") ||
|
|
951
|
+
(pin.swarm !== null && typeof pin.swarm !== "string") ||
|
|
952
|
+
(pin.name !== null && typeof pin.name !== "string")
|
|
953
|
+
)
|
|
954
|
+
throw new HostError(
|
|
955
|
+
"Invalid persisted content pin; recovery is blocked.",
|
|
956
|
+
);
|
|
957
|
+
if (pin.cid) exactCid(pin.cid);
|
|
958
|
+
if (pin.targetCid) exactCid(pin.targetCid);
|
|
959
|
+
if (pin.swarm) swarmUrl(pin.swarm);
|
|
960
|
+
if (
|
|
961
|
+
(pin.swarm === null) !== (pin.name === null) ||
|
|
962
|
+
(pin.swarm === null &&
|
|
963
|
+
(exactCid(pin.reference) !== pin.reference ||
|
|
964
|
+
(pin.cid !== null && pin.cid !== pin.reference))) ||
|
|
965
|
+
(pin.swarm !== null &&
|
|
966
|
+
(!pin.reference.startsWith("ipab://") ||
|
|
967
|
+
(pin.name === "" &&
|
|
968
|
+
!/^ipab:\/\/[^/]+\/$/u.test(pin.reference)))) ||
|
|
969
|
+
(pin.status === "ready" && (!pin.cid || !pin.targetCid))
|
|
970
|
+
)
|
|
971
|
+
throw new HostError(
|
|
972
|
+
"Invalid persisted pin reference/version binding; recovery is blocked.",
|
|
973
|
+
);
|
|
974
|
+
return pin;
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
contentPin(reference: string): ContentPin | undefined {
|
|
978
|
+
return this.contentPins().find((pin) => pin.reference === reference);
|
|
979
|
+
}
|
|
980
|
+
saveContentPin(pin: ContentPin): void {
|
|
290
981
|
if (
|
|
291
982
|
!this.db
|
|
292
983
|
.prepare(
|
|
293
|
-
"INSERT INTO
|
|
984
|
+
"INSERT INTO content_pins VALUES (?,?,?) ON CONFLICT(reference) DO UPDATE SET value=excluded.value WHERE owner=excluded.owner",
|
|
294
985
|
)
|
|
295
|
-
.run(pin.
|
|
986
|
+
.run(pin.reference, pin.owner, JSON.stringify(pin)).changes
|
|
296
987
|
)
|
|
297
|
-
throw new HostError(
|
|
988
|
+
throw new HostError("Content pin reference is occupied.");
|
|
989
|
+
}
|
|
990
|
+
deleteContentPin(reference: string): void {
|
|
991
|
+
this.db
|
|
992
|
+
.prepare("DELETE FROM content_pins WHERE reference=?")
|
|
993
|
+
.run(reference);
|
|
298
994
|
}
|
|
299
995
|
isPinOwner(owner: string): boolean {
|
|
300
996
|
return Boolean(
|
|
301
|
-
this.db
|
|
997
|
+
this.db
|
|
998
|
+
.prepare(
|
|
999
|
+
"SELECT 1 FROM content_pins WHERE owner=? UNION ALL SELECT 1 FROM legacy_named_pins WHERE owner=?",
|
|
1000
|
+
)
|
|
1001
|
+
.get(owner, owner),
|
|
302
1002
|
);
|
|
303
1003
|
}
|
|
304
1004
|
deletePin(name: string): void {
|
|
305
1005
|
pinName(name);
|
|
306
|
-
this.db.prepare("DELETE FROM
|
|
307
|
-
}
|
|
308
|
-
registrations(): Association[] {
|
|
309
|
-
return this.db
|
|
310
|
-
.prepare("SELECT value FROM registrations ORDER BY schema_cid")
|
|
311
|
-
.all()
|
|
312
|
-
.map((row) => JSON.parse(String(row.value)) as Association);
|
|
313
|
-
}
|
|
314
|
-
association(schemaCid: string): Association | undefined {
|
|
315
|
-
const row = this.db
|
|
316
|
-
.prepare("SELECT value FROM registrations WHERE schema_cid=?")
|
|
317
|
-
.get(schemaCid);
|
|
318
|
-
return row ? (JSON.parse(String(row.value)) as Association) : undefined;
|
|
319
|
-
}
|
|
320
|
-
setAssociation(
|
|
321
|
-
schema: string,
|
|
322
|
-
app: string,
|
|
323
|
-
argv: string[],
|
|
324
|
-
replace: boolean,
|
|
325
|
-
): Association {
|
|
326
|
-
const value: Association = {
|
|
327
|
-
schemaCid: this.resolve(schema, true),
|
|
328
|
-
appCid: this.resolve(app),
|
|
329
|
-
argv,
|
|
330
|
-
};
|
|
331
|
-
if (!argv.length || !argv.includes("%"))
|
|
332
|
-
syntax('Argument template must contain a whole "%" argument.');
|
|
333
|
-
this.transaction(() => {
|
|
334
|
-
const old = this.association(value.schemaCid);
|
|
335
|
-
if (old && JSON.stringify(old) !== JSON.stringify(value) && !replace)
|
|
336
|
-
throw new HostError(
|
|
337
|
-
"Schema has another association; use --replace before --.",
|
|
338
|
-
);
|
|
339
|
-
this.db
|
|
340
|
-
.prepare("INSERT OR REPLACE INTO registrations VALUES (?,?)")
|
|
341
|
-
.run(value.schemaCid, JSON.stringify(value));
|
|
342
|
-
});
|
|
343
|
-
return value;
|
|
344
|
-
}
|
|
345
|
-
removeAssociation(schema: string): string {
|
|
346
|
-
const cid = this.resolve(schema, true);
|
|
347
|
-
if (
|
|
348
|
-
!this.db.prepare("DELETE FROM registrations WHERE schema_cid=?").run(cid)
|
|
349
|
-
.changes
|
|
350
|
-
)
|
|
351
|
-
throw new HostError(
|
|
352
|
-
"No association exists for this Schema. Run ipab register list.",
|
|
353
|
-
);
|
|
354
|
-
return cid;
|
|
1006
|
+
this.db.prepare("DELETE FROM legacy_named_pins WHERE name=?").run(name);
|
|
355
1007
|
}
|
|
356
1008
|
runs(): RunRecord[] {
|
|
357
1009
|
return this.db
|