@ipab/cli 0.0.0 → 0.1.0-rc.1
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/LICENSE +21 -0
- package/README.md +24 -1
- package/dist/app-authoring.d.ts +22 -0
- package/dist/app-authoring.d.ts.map +1 -0
- package/dist/app-authoring.js +448 -0
- package/dist/app-authoring.js.map +1 -0
- package/dist/app-definition.d.ts +103 -0
- package/dist/app-definition.d.ts.map +1 -0
- package/dist/app-definition.js +201 -0
- package/dist/app-definition.js.map +1 -0
- package/dist/app-prebuild.d.ts +12 -0
- package/dist/app-prebuild.d.ts.map +1 -0
- package/dist/app-prebuild.js +231 -0
- package/dist/app-prebuild.js.map +1 -0
- package/dist/app-templates.d.ts +13 -0
- package/dist/app-templates.d.ts.map +1 -0
- package/dist/app-templates.js +295 -0
- package/dist/app-templates.js.map +1 -0
- package/dist/authoring.d.ts +63 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +411 -0
- package/dist/authoring.js.map +1 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts +6 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +41 -0
- package/dist/builtins.js.map +1 -0
- package/dist/child.d.ts +5 -0
- package/dist/child.d.ts.map +1 -0
- package/dist/child.js +28 -0
- package/dist/child.js.map +1 -0
- package/dist/cli.d.ts +24 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1110 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +154 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +43 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +174 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +40 -0
- package/dist/errors.js.map +1 -0
- package/dist/history.d.ts +34 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +286 -0
- package/dist/history.js.map +1 -0
- package/dist/ipc.d.ts +27 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +341 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logs.d.ts +15 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +118 -0
- package/dist/logs.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +4 -0
- package/dist/main.js.map +1 -0
- package/dist/pins.d.ts +33 -0
- package/dist/pins.d.ts.map +1 -0
- package/dist/pins.js +165 -0
- package/dist/pins.js.map +1 -0
- package/dist/runtime.d.ts +20 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +237 -0
- package/dist/runtime.js.map +1 -0
- package/dist/source-bundle.d.ts +15 -0
- package/dist/source-bundle.d.ts.map +1 -0
- package/dist/source-bundle.js +102 -0
- package/dist/source-bundle.js.map +1 -0
- package/dist/space.d.ts +33 -0
- package/dist/space.d.ts.map +1 -0
- package/dist/space.js +269 -0
- package/dist/space.js.map +1 -0
- package/dist/state.d.ts +102 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +335 -0
- package/dist/state.js.map +1 -0
- package/dist/supported-schemas.d.ts +5 -0
- package/dist/supported-schemas.d.ts.map +1 -0
- package/dist/supported-schemas.js +90 -0
- package/dist/supported-schemas.js.map +1 -0
- package/dist/template-definition.d.ts +98 -0
- package/dist/template-definition.d.ts.map +1 -0
- package/dist/template-definition.js +179 -0
- package/dist/template-definition.js.map +1 -0
- package/dist/template-source.d.ts +12 -0
- package/dist/template-source.d.ts.map +1 -0
- package/dist/template-source.js +97 -0
- package/dist/template-source.js.map +1 -0
- package/package.json +60 -3
- package/src/app-authoring.ts +616 -0
- package/src/app-definition.ts +250 -0
- package/src/app-prebuild.ts +289 -0
- package/src/app-templates.ts +395 -0
- package/src/authoring.ts +577 -0
- package/src/builtins.ts +49 -0
- package/src/child.ts +32 -0
- package/src/cli.ts +1330 -0
- package/src/config.ts +221 -0
- package/src/context.ts +242 -0
- package/src/errors.ts +46 -0
- package/src/history.ts +391 -0
- package/src/ipc.ts +366 -0
- package/src/logs.ts +132 -0
- package/src/main.ts +4 -0
- package/src/pins.ts +200 -0
- package/src/runtime.ts +301 -0
- package/src/source-bundle.ts +135 -0
- package/src/space.ts +379 -0
- package/src/state.ts +430 -0
- package/src/supported-schemas.ts +151 -0
- package/src/template-definition.ts +233 -0
- package/src/template-source.ts +128 -0
package/src/state.ts
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { CID } from "multiformats/cid";
|
|
6
|
+
import type { StoreConfiguration } from "@ipab/ipfs-store";
|
|
7
|
+
import {
|
|
8
|
+
validateUserConfig,
|
|
9
|
+
type ConfigKey,
|
|
10
|
+
type ConfigValue,
|
|
11
|
+
} from "./config.js";
|
|
12
|
+
import { HostError, object, syntax } from "./errors.js";
|
|
13
|
+
|
|
14
|
+
export interface Association {
|
|
15
|
+
schemaCid: string;
|
|
16
|
+
appCid: string;
|
|
17
|
+
argv: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface RunRecord {
|
|
20
|
+
id: string;
|
|
21
|
+
origin: "exec" | "open";
|
|
22
|
+
appCid: string;
|
|
23
|
+
argv: string[];
|
|
24
|
+
startedAt: string;
|
|
25
|
+
endedAt: string;
|
|
26
|
+
status: "succeeded" | "failed" | "cancelled";
|
|
27
|
+
outputCid: string | null;
|
|
28
|
+
appExitCode: number | null;
|
|
29
|
+
log?: "complete" | "incomplete";
|
|
30
|
+
error: { phase: string; code: string; message: string } | null;
|
|
31
|
+
retention: {
|
|
32
|
+
owned: boolean;
|
|
33
|
+
state: "none" | "retained" | "releasing" | "pending";
|
|
34
|
+
owner?: string;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface Hold {
|
|
38
|
+
owner: string;
|
|
39
|
+
backend: StoreConfiguration;
|
|
40
|
+
roots: string[];
|
|
41
|
+
pid: number | null;
|
|
42
|
+
session: string | null;
|
|
43
|
+
recordId: string | null;
|
|
44
|
+
}
|
|
45
|
+
export type PinMetadata =
|
|
46
|
+
| { kind: "bundle"; typeName: string; schemaCid: string }
|
|
47
|
+
| { kind: "schema"; typeName: string; schemaCid: string }
|
|
48
|
+
| { kind: "content"; typeName: null; schemaCid: null };
|
|
49
|
+
export interface NamedPin {
|
|
50
|
+
name: string;
|
|
51
|
+
cid: string;
|
|
52
|
+
owner: string;
|
|
53
|
+
status: "pending" | "ready" | "deleting";
|
|
54
|
+
metadata: PinMetadata | null;
|
|
55
|
+
builtin?: true;
|
|
56
|
+
}
|
|
57
|
+
export function bundlePinMetadata(pin: NamedPin) {
|
|
58
|
+
if (pin.status !== "ready" || !pin.metadata)
|
|
59
|
+
throw new HostError(
|
|
60
|
+
`Pin "${pin.name}" is ${pin.status}; retry pin add/delete before using it.`,
|
|
61
|
+
);
|
|
62
|
+
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
|
+
);
|
|
66
|
+
return pin.metadata;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function exactCid(value: string, schema = false): string {
|
|
70
|
+
let cid: CID;
|
|
71
|
+
try {
|
|
72
|
+
cid = CID.parse(value).toV1();
|
|
73
|
+
} catch {
|
|
74
|
+
syntax("Expected a valid CID.");
|
|
75
|
+
}
|
|
76
|
+
if (schema && (cid.code !== 0x71 || cid.multihash.code !== 0x12))
|
|
77
|
+
syntax("Schema must be a DAG-CBOR SHA-256 block CID.");
|
|
78
|
+
return cid.toString();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function pinName(value: string, builtin = false): void {
|
|
82
|
+
if (value.startsWith("builtin:")) {
|
|
83
|
+
if (builtin && /^builtin:[A-Za-z][A-Za-z0-9._-]*$/.test(value)) return;
|
|
84
|
+
syntax(
|
|
85
|
+
"Built-in pins are read-only and cannot be added, deleted or renamed.",
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (!/^[A-Za-z][A-Za-z0-9._-]*$/.test(value))
|
|
89
|
+
syntax("Invalid pin name; builtin names are reserved.");
|
|
90
|
+
try {
|
|
91
|
+
CID.parse(value);
|
|
92
|
+
} catch {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
syntax("A pin name must not itself be a CID.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function persistedHold(value: string, owner: string): Hold {
|
|
99
|
+
try {
|
|
100
|
+
const data = object(JSON.parse(value) as unknown, "retention metadata");
|
|
101
|
+
const backend = object(data.backend, "original Store configuration");
|
|
102
|
+
if (backend.backend === "kubo")
|
|
103
|
+
throw new HostError(
|
|
104
|
+
"This retention record belongs to the removed Kubo RPC Store. It is not migrated or released. Create a new local pin and manage the old daemon pin independently.",
|
|
105
|
+
1,
|
|
106
|
+
"LEGACY_STORE",
|
|
107
|
+
);
|
|
108
|
+
if (
|
|
109
|
+
data.owner !== owner ||
|
|
110
|
+
!Array.isArray(data.roots) ||
|
|
111
|
+
data.roots.some((root: unknown) => typeof root !== "string") ||
|
|
112
|
+
(data.pid !== null &&
|
|
113
|
+
(!Number.isSafeInteger(data.pid) || Number(data.pid) <= 0)) ||
|
|
114
|
+
(data.session !== null && typeof data.session !== "string") ||
|
|
115
|
+
(data.recordId !== null && typeof data.recordId !== "string")
|
|
116
|
+
)
|
|
117
|
+
throw new Error("Invalid retention fields.");
|
|
118
|
+
for (const root of data.roots as string[]) exactCid(root);
|
|
119
|
+
return data as unknown as Hold;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (error instanceof HostError && error.code === "LEGACY_STORE")
|
|
122
|
+
throw error;
|
|
123
|
+
throw new HostError(
|
|
124
|
+
"Invalid persisted retention metadata; cleanup is blocked.",
|
|
125
|
+
1,
|
|
126
|
+
"STATE_INVALID",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export class State {
|
|
132
|
+
readonly db: DatabaseSync;
|
|
133
|
+
readonly identity: string;
|
|
134
|
+
constructor(
|
|
135
|
+
readonly directory: string,
|
|
136
|
+
private readonly builtins: readonly NamedPin[] = [],
|
|
137
|
+
) {
|
|
138
|
+
mkdirSync(directory, { recursive: true });
|
|
139
|
+
this.db = new DatabaseSync(join(directory, "state.sqlite"));
|
|
140
|
+
this.db
|
|
141
|
+
.exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
|
|
142
|
+
CREATE TABLE IF NOT EXISTS identity (id TEXT NOT NULL);
|
|
143
|
+
CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
144
|
+
DROP TABLE IF EXISTS aliases;
|
|
145
|
+
CREATE TABLE IF NOT EXISTS named_pins (name TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL);
|
|
146
|
+
CREATE TABLE IF NOT EXISTS registrations (schema_cid TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
147
|
+
CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
148
|
+
CREATE TABLE IF NOT EXISTS holds (owner TEXT PRIMARY KEY, value TEXT NOT NULL);`);
|
|
149
|
+
this.transaction(() => {
|
|
150
|
+
if (!this.db.prepare("SELECT id FROM identity").get())
|
|
151
|
+
this.db.prepare("INSERT INTO identity VALUES (?)").run(randomUUID());
|
|
152
|
+
});
|
|
153
|
+
this.identity = String(
|
|
154
|
+
this.db.prepare("SELECT id FROM identity").get()!.id,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
close(): void {
|
|
158
|
+
this.db.close();
|
|
159
|
+
}
|
|
160
|
+
transaction<T>(operation: () => T): T {
|
|
161
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
162
|
+
try {
|
|
163
|
+
const result = operation();
|
|
164
|
+
this.db.exec("COMMIT");
|
|
165
|
+
return result;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
this.db.exec("ROLLBACK");
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
config(): Partial<Record<ConfigKey, ConfigValue>> {
|
|
172
|
+
const entries = this.db.prepare("SELECT key,value FROM config").all();
|
|
173
|
+
if (entries.some((row) => row.key === "store.rpc"))
|
|
174
|
+
throw new HostError(
|
|
175
|
+
"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.",
|
|
176
|
+
);
|
|
177
|
+
try {
|
|
178
|
+
return validateUserConfig(
|
|
179
|
+
Object.fromEntries(
|
|
180
|
+
entries.map((row) => [
|
|
181
|
+
row.key,
|
|
182
|
+
JSON.parse(String(row.value)) as unknown,
|
|
183
|
+
]),
|
|
184
|
+
),
|
|
185
|
+
);
|
|
186
|
+
} catch {
|
|
187
|
+
throw new HostError(
|
|
188
|
+
"Malformed user configuration; inspect the state database configuration.",
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
setConfig(key: ConfigKey, value: ConfigValue): void {
|
|
193
|
+
this.db
|
|
194
|
+
.prepare("INSERT OR REPLACE INTO config VALUES (?,?)")
|
|
195
|
+
.run(key, JSON.stringify(value));
|
|
196
|
+
}
|
|
197
|
+
resetConfig(key: ConfigKey | "store.rpc"): void {
|
|
198
|
+
this.db.prepare("DELETE FROM config WHERE key=?").run(key);
|
|
199
|
+
}
|
|
200
|
+
resolve(value: string, schema = false): string {
|
|
201
|
+
try {
|
|
202
|
+
return exactCid(value, schema);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
try {
|
|
205
|
+
CID.parse(value);
|
|
206
|
+
} catch {
|
|
207
|
+
const pin = this.pin(value);
|
|
208
|
+
if (pin) {
|
|
209
|
+
const metadata = bundlePinMetadata(pin);
|
|
210
|
+
if (schema) return metadata.schemaCid;
|
|
211
|
+
return exactCid(pin.cid, schema);
|
|
212
|
+
}
|
|
213
|
+
throw new HostError(
|
|
214
|
+
`Unknown CID or pin "${value}". Run ipab pin list.`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
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
|
+
pins(): NamedPin[] {
|
|
227
|
+
const user = this.db
|
|
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];
|
|
234
|
+
}
|
|
235
|
+
pin(name: string): NamedPin | undefined {
|
|
236
|
+
const builtin = this.builtins.find((pin) => pin.name === name);
|
|
237
|
+
if (builtin) return builtin;
|
|
238
|
+
const row = this.db
|
|
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;
|
|
244
|
+
}
|
|
245
|
+
private readPin(name: string, owner: string, value: string): NamedPin {
|
|
246
|
+
const data = object(JSON.parse(value) as unknown, "named pin");
|
|
247
|
+
if (
|
|
248
|
+
data.name !== name ||
|
|
249
|
+
data.owner !== owner ||
|
|
250
|
+
typeof data.cid !== "string" ||
|
|
251
|
+
(data.status !== "pending" &&
|
|
252
|
+
data.status !== "ready" &&
|
|
253
|
+
data.status !== "deleting")
|
|
254
|
+
)
|
|
255
|
+
throw new HostError("Invalid persisted named pin; recovery is blocked.");
|
|
256
|
+
exactCid(data.cid);
|
|
257
|
+
let metadata: PinMetadata | null = null;
|
|
258
|
+
if (data.metadata !== null) {
|
|
259
|
+
const value = object(data.metadata, "pin type metadata");
|
|
260
|
+
if (value.kind === "content") {
|
|
261
|
+
if (value.typeName !== null || value.schemaCid !== null)
|
|
262
|
+
throw new HostError("Invalid persisted pin type metadata.");
|
|
263
|
+
metadata = { kind: "content", typeName: null, schemaCid: null };
|
|
264
|
+
} else {
|
|
265
|
+
if (
|
|
266
|
+
(value.kind !== "bundle" && value.kind !== "schema") ||
|
|
267
|
+
typeof value.typeName !== "string" ||
|
|
268
|
+
!value.typeName.trim() ||
|
|
269
|
+
typeof value.schemaCid !== "string"
|
|
270
|
+
)
|
|
271
|
+
throw new HostError("Invalid persisted pin type metadata.");
|
|
272
|
+
metadata = {
|
|
273
|
+
kind: value.kind,
|
|
274
|
+
typeName: value.typeName,
|
|
275
|
+
schemaCid: exactCid(value.schemaCid, true),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
} else if (data.status === "ready")
|
|
279
|
+
throw new HostError("Ready pin is missing type metadata.");
|
|
280
|
+
return {
|
|
281
|
+
name,
|
|
282
|
+
cid: data.cid,
|
|
283
|
+
owner,
|
|
284
|
+
status: data.status,
|
|
285
|
+
metadata,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
savePin(pin: NamedPin): void {
|
|
289
|
+
pinName(pin.name);
|
|
290
|
+
if (
|
|
291
|
+
!this.db
|
|
292
|
+
.prepare(
|
|
293
|
+
"INSERT INTO named_pins VALUES (?,?,?) ON CONFLICT(name) DO UPDATE SET value=excluded.value WHERE owner=excluded.owner",
|
|
294
|
+
)
|
|
295
|
+
.run(pin.name, pin.owner, JSON.stringify(pin)).changes
|
|
296
|
+
)
|
|
297
|
+
throw new HostError(`Pin name "${pin.name}" is occupied.`);
|
|
298
|
+
}
|
|
299
|
+
isPinOwner(owner: string): boolean {
|
|
300
|
+
return Boolean(
|
|
301
|
+
this.db.prepare("SELECT 1 FROM named_pins WHERE owner=?").get(owner),
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
deletePin(name: string): void {
|
|
305
|
+
pinName(name);
|
|
306
|
+
this.db.prepare("DELETE FROM named_pins WHERE name=?").run(name);
|
|
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;
|
|
355
|
+
}
|
|
356
|
+
runs(): RunRecord[] {
|
|
357
|
+
return this.db
|
|
358
|
+
.prepare("SELECT value FROM runs")
|
|
359
|
+
.all()
|
|
360
|
+
.map((row) => JSON.parse(String(row.value)) as RunRecord);
|
|
361
|
+
}
|
|
362
|
+
private matchingRunIds(prefix: string): string[] {
|
|
363
|
+
// Prefixes are literal identifiers, not GLOB patterns.
|
|
364
|
+
const pattern = `${prefix.replaceAll("[", "[[]").replaceAll("*", "[*]").replaceAll("?", "[?]")}*`;
|
|
365
|
+
return this.db
|
|
366
|
+
.prepare("SELECT id FROM runs WHERE id GLOB ? ORDER BY id LIMIT 2")
|
|
367
|
+
.all(pattern)
|
|
368
|
+
.map((row) => String(row.id));
|
|
369
|
+
}
|
|
370
|
+
shortRunId(id: string): string {
|
|
371
|
+
let length = Math.min(8, id.length);
|
|
372
|
+
while (
|
|
373
|
+
length < id.length &&
|
|
374
|
+
this.matchingRunIds(id.slice(0, length)).length > 1
|
|
375
|
+
)
|
|
376
|
+
length++;
|
|
377
|
+
return id.slice(0, length);
|
|
378
|
+
}
|
|
379
|
+
resolveRunId(value: string): string {
|
|
380
|
+
if (this.db.prepare("SELECT id FROM runs WHERE id=?").get(value))
|
|
381
|
+
return value;
|
|
382
|
+
if (value.length >= 8) {
|
|
383
|
+
const matches = this.matchingRunIds(value);
|
|
384
|
+
if (matches.length === 1) return matches[0]!;
|
|
385
|
+
if (matches.length > 1)
|
|
386
|
+
throw new HostError(
|
|
387
|
+
`Ambiguous run ID prefix "${value}"; use more characters or the full ID.`,
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
throw new HostError(
|
|
391
|
+
`Execution record "${value}" does not exist. Run ipab history list.`,
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
run(id: string): RunRecord {
|
|
395
|
+
const row = this.db.prepare("SELECT value FROM runs WHERE id=?").get(id);
|
|
396
|
+
if (!row)
|
|
397
|
+
throw new HostError(
|
|
398
|
+
`Execution record "${id}" does not exist. Run ipab history list.`,
|
|
399
|
+
);
|
|
400
|
+
return JSON.parse(String(row.value)) as RunRecord;
|
|
401
|
+
}
|
|
402
|
+
saveRun(record: RunRecord): void {
|
|
403
|
+
this.db
|
|
404
|
+
.prepare("INSERT OR REPLACE INTO runs VALUES (?,?)")
|
|
405
|
+
.run(record.id, JSON.stringify(record));
|
|
406
|
+
}
|
|
407
|
+
deleteRun(id: string): void {
|
|
408
|
+
this.db.prepare("DELETE FROM runs WHERE id=?").run(id);
|
|
409
|
+
}
|
|
410
|
+
holds(): Hold[] {
|
|
411
|
+
return this.db
|
|
412
|
+
.prepare("SELECT owner,value FROM holds")
|
|
413
|
+
.all()
|
|
414
|
+
.map((row) => persistedHold(String(row.value), String(row.owner)));
|
|
415
|
+
}
|
|
416
|
+
hold(owner: string): Hold | undefined {
|
|
417
|
+
const row = this.db
|
|
418
|
+
.prepare("SELECT value FROM holds WHERE owner=?")
|
|
419
|
+
.get(owner);
|
|
420
|
+
return row ? persistedHold(String(row.value), owner) : undefined;
|
|
421
|
+
}
|
|
422
|
+
saveHold(hold: Hold): void {
|
|
423
|
+
this.db
|
|
424
|
+
.prepare("INSERT OR REPLACE INTO holds VALUES (?,?)")
|
|
425
|
+
.run(hold.owner, JSON.stringify(hold));
|
|
426
|
+
}
|
|
427
|
+
deleteHold(owner: string): void {
|
|
428
|
+
this.db.prepare("DELETE FROM holds WHERE owner=?").run(owner);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { CarBlockIterator } from "@ipld/car/iterator";
|
|
2
|
+
import {
|
|
3
|
+
blockLength,
|
|
4
|
+
createWriter,
|
|
5
|
+
headerLength,
|
|
6
|
+
} from "@ipld/car/buffer-writer";
|
|
7
|
+
import {
|
|
8
|
+
bundleLimits,
|
|
9
|
+
ContentBundleError,
|
|
10
|
+
decodeSchema,
|
|
11
|
+
encodeSchema,
|
|
12
|
+
parseData,
|
|
13
|
+
type SchemaBlock,
|
|
14
|
+
} from "@ipab/bundle";
|
|
15
|
+
import {
|
|
16
|
+
readBytes,
|
|
17
|
+
streamChunks,
|
|
18
|
+
type ReadonlyFileTree,
|
|
19
|
+
type ReadOptions,
|
|
20
|
+
} from "@ipab/bundle-builder";
|
|
21
|
+
import { HostError } from "./errors.js";
|
|
22
|
+
|
|
23
|
+
export const supportedSchemasPath = "resources/supported-schemas.car";
|
|
24
|
+
const carBytes = bundleLimits.bytes + bundleLimits.files * 128;
|
|
25
|
+
|
|
26
|
+
function budget(signal?: AbortSignal) {
|
|
27
|
+
let work = 0;
|
|
28
|
+
return {
|
|
29
|
+
nodes: bundleLimits.nodes,
|
|
30
|
+
depth: bundleLimits.validationDepth,
|
|
31
|
+
jsonDepth: bundleLimits.jsonDepth,
|
|
32
|
+
schemaBytes: bundleLimits.schemaBytes,
|
|
33
|
+
charge(this: void) {
|
|
34
|
+
signal?.throwIfAborted();
|
|
35
|
+
if (++work > bundleLimits.nodes)
|
|
36
|
+
throw new ContentBundleError(
|
|
37
|
+
"LIMIT_EXCEEDED",
|
|
38
|
+
"Supported Schemas aggregate work budget exceeded.",
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function buildSupportedSchemas(
|
|
45
|
+
tree: ReadonlyFileTree,
|
|
46
|
+
paths: ReadonlySet<string>,
|
|
47
|
+
options: ReadOptions = {},
|
|
48
|
+
): Promise<Uint8Array | undefined> {
|
|
49
|
+
if (!paths.size) return undefined;
|
|
50
|
+
if (paths.size > bundleLimits.files)
|
|
51
|
+
throw new HostError("Too many supported Schema source files.");
|
|
52
|
+
const limits = budget(options.signal);
|
|
53
|
+
const blocks = new Map<string, SchemaBlock>();
|
|
54
|
+
let total = 0;
|
|
55
|
+
for (const path of paths) {
|
|
56
|
+
const bytes = await readBytes(
|
|
57
|
+
tree,
|
|
58
|
+
path,
|
|
59
|
+
bundleLimits.schemaBytes,
|
|
60
|
+
options,
|
|
61
|
+
);
|
|
62
|
+
total += bytes.length;
|
|
63
|
+
if (total > bundleLimits.parsingBytes)
|
|
64
|
+
throw new ContentBundleError(
|
|
65
|
+
"LIMIT_EXCEEDED",
|
|
66
|
+
"Supported Schemas source byte budget exceeded.",
|
|
67
|
+
);
|
|
68
|
+
const block = await encodeSchema(
|
|
69
|
+
parseData(
|
|
70
|
+
bytes,
|
|
71
|
+
"application/json",
|
|
72
|
+
limits.nodes,
|
|
73
|
+
limits.jsonDepth,
|
|
74
|
+
limits.charge,
|
|
75
|
+
),
|
|
76
|
+
limits,
|
|
77
|
+
);
|
|
78
|
+
blocks.set(block.cid.toString(), block);
|
|
79
|
+
}
|
|
80
|
+
const ordered = [...blocks.entries()]
|
|
81
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
82
|
+
.map(([, block]) => block);
|
|
83
|
+
const roots = ordered.map((block) => block.cid);
|
|
84
|
+
const size =
|
|
85
|
+
headerLength({ roots }) +
|
|
86
|
+
ordered.reduce((size, block) => size + blockLength(block), 0);
|
|
87
|
+
if (size > carBytes)
|
|
88
|
+
throw new ContentBundleError(
|
|
89
|
+
"LIMIT_EXCEEDED",
|
|
90
|
+
"Supported Schemas CAR byte budget exceeded.",
|
|
91
|
+
);
|
|
92
|
+
const writer = createWriter(new ArrayBuffer(size), { roots });
|
|
93
|
+
for (const block of ordered) writer.write(block);
|
|
94
|
+
return writer.close();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function readSupportedSchemas(
|
|
98
|
+
stream: ReadableStream<Uint8Array>,
|
|
99
|
+
signal?: AbortSignal,
|
|
100
|
+
): Promise<ReadonlySet<string>> {
|
|
101
|
+
const chunks = streamChunks(stream, signal ? { signal } : {});
|
|
102
|
+
const limits = budget(signal);
|
|
103
|
+
let total = 0;
|
|
104
|
+
const bounded = async function* () {
|
|
105
|
+
for await (const chunk of chunks) {
|
|
106
|
+
total += chunk.length;
|
|
107
|
+
if (total > carBytes)
|
|
108
|
+
throw new ContentBundleError(
|
|
109
|
+
"LIMIT_EXCEEDED",
|
|
110
|
+
"Supported Schemas CAR byte budget exceeded.",
|
|
111
|
+
);
|
|
112
|
+
yield chunk;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
try {
|
|
116
|
+
const iterator = await CarBlockIterator.fromIterable(bounded());
|
|
117
|
+
const roots = await iterator.getRoots();
|
|
118
|
+
if (!roots.length || roots.length > bundleLimits.files)
|
|
119
|
+
throw new HostError(
|
|
120
|
+
"Supported Schemas CAR requires a bounded nonempty root list.",
|
|
121
|
+
);
|
|
122
|
+
const supported = new Set(roots.map((root) => root.toString()));
|
|
123
|
+
if (
|
|
124
|
+
supported.size !== roots.length ||
|
|
125
|
+
roots.some(
|
|
126
|
+
(root) =>
|
|
127
|
+
root.version !== 1 ||
|
|
128
|
+
root.code !== 0x71 ||
|
|
129
|
+
root.multihash.code !== 0x12,
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
throw new HostError(
|
|
133
|
+
"Supported Schemas CAR requires unique CIDv1 DAG-CBOR SHA-256 roots.",
|
|
134
|
+
);
|
|
135
|
+
const seen = new Set<string>();
|
|
136
|
+
for await (const block of iterator) {
|
|
137
|
+
const key = block.cid.toString();
|
|
138
|
+
if (!supported.has(key) || seen.has(key))
|
|
139
|
+
throw new HostError(
|
|
140
|
+
"Supported Schemas CAR has an unrelated or duplicate block record.",
|
|
141
|
+
);
|
|
142
|
+
await decodeSchema(block.cid, block.bytes, limits);
|
|
143
|
+
seen.add(key);
|
|
144
|
+
}
|
|
145
|
+
if (seen.size !== supported.size)
|
|
146
|
+
throw new HostError("Supported Schemas CAR is missing a root block.");
|
|
147
|
+
return supported;
|
|
148
|
+
} finally {
|
|
149
|
+
await chunks.return(undefined);
|
|
150
|
+
}
|
|
151
|
+
}
|