@fangorn-network/sdk 2026.7.12 → 2026.7.13
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 +81 -67
- package/lib/cli/cli.js +9 -9
- package/lib/engine/car.d.ts +10 -0
- package/lib/engine/car.js +37 -0
- package/lib/engine/graph.d.ts +70 -0
- package/lib/engine/graph.js +73 -0
- package/lib/engine/index.d.ts +109 -70
- package/lib/engine/index.js +293 -226
- package/lib/engine/test-helpers.d.ts +43 -0
- package/lib/engine/test-helpers.js +69 -0
- package/lib/fangorn.d.ts +25 -22
- package/lib/fangorn.js +105 -76
- package/lib/index.d.ts +3 -2
- package/lib/index.js +2 -1
- package/lib/providers/storage/pinata.d.ts +16 -9
- package/lib/providers/storage/pinata.js +29 -21
- package/lib/providers/storage/types.d.ts +18 -8
- package/lib/test/testbed.d.ts +7 -5
- package/lib/test/testbed.js +2 -1
- package/package.json +2 -8
- package/lib/engine/blockstore.d.ts +0 -24
- package/lib/engine/blockstore.js +0 -85
package/lib/engine/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DataRegistryClient } from "../contracts/data-registry/index.js";
|
|
2
2
|
import "../contracts/index.js";
|
|
3
|
+
import { MetadataStorage } from "../providers/storage/types.js";
|
|
3
4
|
import { Address, Hash } from "viem";
|
|
4
5
|
import { CID } from "multiformats/cid";
|
|
5
|
-
import { Blockstore } from "interface-blockstore";
|
|
6
6
|
|
|
7
7
|
//#region src/engine/index.d.ts
|
|
8
8
|
type SchemaID = string;
|
|
@@ -10,7 +10,7 @@ type RelationLabel = string;
|
|
|
10
10
|
type NamespaceID = string;
|
|
11
11
|
interface Vertex {
|
|
12
12
|
schemaId: SchemaID;
|
|
13
|
-
payload: Record<string,
|
|
13
|
+
payload: Record<string, unknown>;
|
|
14
14
|
}
|
|
15
15
|
interface Edge {
|
|
16
16
|
sourceCid: string;
|
|
@@ -26,24 +26,28 @@ interface EdgeSchema {
|
|
|
26
26
|
relation: RelationLabel;
|
|
27
27
|
targetSchema: SchemaID;
|
|
28
28
|
}
|
|
29
|
+
/** A decoded commit — the git-like history object the on-chain digest points at. */
|
|
29
30
|
interface CommitObject {
|
|
30
|
-
|
|
31
|
+
/** Link to the publisher's root map (namespace → tree). */
|
|
32
|
+
root: CID;
|
|
31
33
|
parents: CID[];
|
|
32
34
|
timestamp: number;
|
|
33
35
|
message: string;
|
|
36
|
+
/** Opaque storage URI of the CAR file holding the blocks this commit introduced. */
|
|
37
|
+
car: string;
|
|
34
38
|
}
|
|
35
39
|
interface NamespaceContents {
|
|
36
40
|
vertices: {
|
|
37
41
|
cid: string;
|
|
38
42
|
schemaId: SchemaID;
|
|
39
|
-
payload: Record<string,
|
|
43
|
+
payload: Record<string, unknown>;
|
|
40
44
|
}[];
|
|
41
45
|
edges: Edge[];
|
|
42
46
|
}
|
|
43
47
|
/** Net change to one namespace between two on-chain roots (see `namespaceDiff`). */
|
|
44
48
|
interface NamespaceDiff {
|
|
45
|
-
/** Vertices new
|
|
46
|
-
addedVertices: NamespaceContents[
|
|
49
|
+
/** Vertices new vs. the old root, decoded to their payloads. */
|
|
50
|
+
addedVertices: NamespaceContents["vertices"];
|
|
47
51
|
/** Edges new vs. the old root. */
|
|
48
52
|
addedEdges: Edge[];
|
|
49
53
|
/** CIDs of vertices present at the old root but gone at the new one. */
|
|
@@ -58,78 +62,114 @@ declare class MetagraphRegistry {
|
|
|
58
62
|
hasVertexSchema(namespace: NamespaceID, schemaId: SchemaID): boolean;
|
|
59
63
|
registerEdge(namespace: NamespaceID, rule: EdgeSchema): void;
|
|
60
64
|
hasEdgeSchema(namespace: NamespaceID, sourceSchema: SchemaID, relation: RelationLabel, targetSchema: SchemaID): boolean;
|
|
61
|
-
validateVertex(namespace: NamespaceID, schemaId: SchemaID, payload: Record<string,
|
|
65
|
+
validateVertex(namespace: NamespaceID, schemaId: SchemaID, payload: Record<string, unknown>): boolean;
|
|
62
66
|
validateEdge(namespace: NamespaceID, sourceSchema: SchemaID, relation: RelationLabel, targetSchema: SchemaID): boolean;
|
|
63
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Upper bound on a namespace name's length. Namespaces are keys of the root
|
|
70
|
+
* map block, so this is just a sanity bound keeping that block small and
|
|
71
|
+
* rejecting garbage input early.
|
|
72
|
+
*/
|
|
73
|
+
declare const MAX_NAMESPACE_LENGTH = 256;
|
|
74
|
+
/** Reject invalid namespace names up front with a clear error. */
|
|
75
|
+
declare function assertValidNamespace(namespace: NamespaceID): void;
|
|
64
76
|
/** One line of a commit's diff against its first parent. */
|
|
65
77
|
interface CommitDiff {
|
|
66
78
|
message: string;
|
|
67
79
|
parents: string[];
|
|
68
80
|
timestamp: number;
|
|
69
|
-
|
|
70
|
-
/** namespaced
|
|
81
|
+
root: string;
|
|
82
|
+
/** namespaced entries (`<ns>/v/<cid>`, `<ns>/e/<cid>`) new vs. the parent. */
|
|
71
83
|
added: string[];
|
|
72
|
-
/** namespaced
|
|
84
|
+
/** namespaced entries present in the parent but gone in this commit. */
|
|
73
85
|
removed: string[];
|
|
74
86
|
}
|
|
87
|
+
/** Input to `createCommit`: a graph staged under one namespace. */
|
|
88
|
+
interface CreateCommitOptions {
|
|
89
|
+
namespace: NamespaceID;
|
|
90
|
+
/** Parent commit this builds on (local HEAD or on-chain tip), or null for a root commit. */
|
|
91
|
+
base: CID | null;
|
|
92
|
+
/** Vertices keyed by a caller-local id, so edges can reference them. */
|
|
93
|
+
vertices: {
|
|
94
|
+
id: string;
|
|
95
|
+
schemaId: SchemaID;
|
|
96
|
+
payload: Record<string, unknown>;
|
|
97
|
+
}[];
|
|
98
|
+
/**
|
|
99
|
+
* Edges between vertices. Endpoints are local ids of vertices staged in
|
|
100
|
+
* this same call, or full CIDs of vertices committed earlier (CIDs are
|
|
101
|
+
* content-addressed and stable across commits).
|
|
102
|
+
*/
|
|
103
|
+
edges: {
|
|
104
|
+
relation: RelationLabel;
|
|
105
|
+
from: string;
|
|
106
|
+
to: string;
|
|
107
|
+
}[];
|
|
108
|
+
message: string;
|
|
109
|
+
/**
|
|
110
|
+
* When true, the staged graph REPLACES the namespace's previous contents
|
|
111
|
+
* (a snapshot commit — anything omitted is removed). Default is additive:
|
|
112
|
+
* new state = parent state ∪ staged.
|
|
113
|
+
*/
|
|
114
|
+
replace?: boolean;
|
|
115
|
+
}
|
|
75
116
|
declare class FangornEngine {
|
|
76
|
-
private
|
|
117
|
+
private storage;
|
|
77
118
|
private metagraph;
|
|
78
119
|
private registryClient;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Local block cache. Written blocks land here immediately; remote blocks
|
|
122
|
+
* are pulled in whole-CAR-at-a-time by `loadCar`. Content addressing makes
|
|
123
|
+
* the cache trivially coherent.
|
|
124
|
+
*/
|
|
125
|
+
private cache;
|
|
126
|
+
/** Commits whose CAR has already been loaded into the cache. */
|
|
127
|
+
private loadedCars;
|
|
128
|
+
constructor(storage: MetadataStorage, metagraph: MetagraphRegistry, registryClient: DataRegistryClient);
|
|
82
129
|
/**
|
|
83
130
|
* Reconstructs the CID a commit's raw digest hex refers to. The contract only
|
|
84
|
-
* stores the bare 32-byte sha256 digest (see
|
|
85
|
-
* CID must wrap that digest directly via Digest.create — hashing it again
|
|
131
|
+
* stores the bare 32-byte sha256 digest (see commitStateRoot), so rebuilding
|
|
132
|
+
* the CID must wrap that digest directly via Digest.create — hashing it again
|
|
86
133
|
* with sha256.digest() would produce an unrelated CID pointing at nothing.
|
|
87
134
|
*/
|
|
88
135
|
private hexToRootCid;
|
|
89
|
-
/**
|
|
90
|
-
resolveHeadCommit(publisher: Address): Promise<CID | null>;
|
|
91
|
-
/** Read and decode a commit object by CID. */
|
|
136
|
+
/** Read and decode a commit object by CID (directly addressable — no CAR needed). */
|
|
92
137
|
getCommit(commitCid: CID): Promise<CommitObject>;
|
|
93
|
-
/**
|
|
94
|
-
private
|
|
95
|
-
/**
|
|
96
|
-
* Open a Pail write batch over `basePailRoot` (or a fresh empty tree when
|
|
97
|
-
* null). Pure/local — never reads the chain, so callers control exactly which
|
|
98
|
-
* historical root a new commit builds on (on-chain tip vs. a local HEAD).
|
|
99
|
-
*/
|
|
100
|
-
openBatch(basePailRoot: CID | null): Promise<any>;
|
|
138
|
+
/** Pull every block of `commit`'s CAR into the local cache. */
|
|
139
|
+
private loadCar;
|
|
101
140
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* the next commit and pass the expected old root to the on-chain CAS.
|
|
141
|
+
* Resolve a data block: local cache first, then walk the commit chain from
|
|
142
|
+
* `hint`, loading one ancestor CAR at a time until the block turns up.
|
|
105
143
|
*/
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
144
|
+
private getBlock;
|
|
145
|
+
private putLocal;
|
|
146
|
+
/** Reconstruct the publisher's current on-chain head as a commit CID, or null if none. */
|
|
147
|
+
resolveHeadCommit(publisher: Address): Promise<CID | null>;
|
|
148
|
+
/** Decode the root map behind a commit. */
|
|
149
|
+
private rootMapAt;
|
|
150
|
+
/** The full vertex/edge link sets one commit records for `namespace`. */
|
|
151
|
+
private treeLinksAt;
|
|
113
152
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
153
|
+
* Build a new commit on top of `base` — entirely in memory, then persisted
|
|
154
|
+
* as exactly TWO uploads: one CAR file holding every block this commit
|
|
155
|
+
* introduced (git's packfile), and the small commit block itself (stored
|
|
156
|
+
* individually so the on-chain digest resolves without any index).
|
|
157
|
+
*
|
|
158
|
+
* The new namespace state is the parent state plus the staged graph;
|
|
159
|
+
* unchanged blocks are shared byte-for-byte with ancestors and are NOT
|
|
160
|
+
* re-uploaded. Does not touch the chain — `pushCommit` settles it.
|
|
118
161
|
*/
|
|
119
|
-
|
|
162
|
+
createCommit(opts: CreateCommitOptions): Promise<{
|
|
120
163
|
commitCid: CID;
|
|
121
|
-
|
|
164
|
+
root: CID;
|
|
165
|
+
vertexCids: Record<string, string>;
|
|
122
166
|
}>;
|
|
123
|
-
/** Build, encode and store a CommitObject block; returns its CID. */
|
|
124
|
-
private writeCommit;
|
|
125
|
-
/** Upload every block written locally since the last flush to the remote backend. */
|
|
126
|
-
flush(): Promise<void>;
|
|
127
167
|
/**
|
|
128
168
|
* Settle a local commit as the publisher's on-chain head — the permissioned,
|
|
129
|
-
* git `push` step.
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
169
|
+
* git `push` step. The commit and its CAR are already durable in storage
|
|
170
|
+
* (createCommit uploads them), so this is just the compare-and-swap against
|
|
171
|
+
* the current head. Unless `force`, the on-chain head must be an ancestor of
|
|
172
|
+
* `commitCid` (fast-forward only).
|
|
133
173
|
*/
|
|
134
174
|
pushCommit(publisher: Address, commitCid: CID, opts?: {
|
|
135
175
|
force?: boolean;
|
|
@@ -146,35 +186,34 @@ declare class FangornEngine {
|
|
|
146
186
|
private isAncestor;
|
|
147
187
|
/** The commit CID string a raw on-chain root hex points at. */
|
|
148
188
|
commitCidFromRootHex(hex: string): string;
|
|
189
|
+
/** Decode a vertex block into the public read shape. */
|
|
190
|
+
private decodeVertex;
|
|
191
|
+
/** Decode an edge block into the public read shape. */
|
|
192
|
+
private decodeEdge;
|
|
149
193
|
/**
|
|
150
194
|
* Net change to a single namespace between two on-chain roots — the core of
|
|
151
|
-
* the subscription light-client.
|
|
152
|
-
*
|
|
153
|
-
*
|
|
195
|
+
* the subscription light-client. Namespace trees are explicit link sets, so
|
|
196
|
+
* the diff is a plain set difference; only blocks that actually changed are
|
|
197
|
+
* decoded (each side resolves through its own commit's CAR chain).
|
|
154
198
|
*
|
|
155
199
|
* Diffing root-to-root (rather than walking commits) is deliberately robust:
|
|
156
200
|
* it yields the correct net delta even across a force-push / non-fast-forward
|
|
157
201
|
* reorg — exactly what an index builder needs (which vertices to (re)embed,
|
|
158
|
-
* which to evict).
|
|
159
|
-
* and all edges are recovered from the pail key alone (no extra fetch), since
|
|
160
|
-
* an edge key already encodes `source|relation|target`.
|
|
202
|
+
* which to evict).
|
|
161
203
|
*/
|
|
162
204
|
namespaceDiff(oldRootHex: string | null, newRootHex: string, namespace: NamespaceID): Promise<NamespaceDiff>;
|
|
163
|
-
/**
|
|
164
|
-
private edgeFromKey;
|
|
165
|
-
/** Diff a commit against its first parent, by namespaced pail key. */
|
|
205
|
+
/** Diff a commit against its first parent, across every namespace. */
|
|
166
206
|
diffCommit(commitCid: CID): Promise<CommitDiff>;
|
|
167
|
-
/**
|
|
168
|
-
private entriesAt;
|
|
169
|
-
/**
|
|
170
|
-
* Cheap existence check: does anything (including the bare `sys/init`
|
|
171
|
-
* marker) exist under this namespace prefix yet? Short-circuits on the
|
|
172
|
-
* first entry found instead of decoding the whole namespace, unlike
|
|
173
|
-
* listNamespace (which also skips non-CID values like the marker itself).
|
|
174
|
-
*/
|
|
207
|
+
/** Does this namespace exist under the publisher's on-chain root yet? */
|
|
175
208
|
namespaceExists(namespace: NamespaceID, publisher: Address): Promise<boolean>;
|
|
176
|
-
/** Lists every vertex and edge currently
|
|
209
|
+
/** Lists every vertex and edge currently committed under a namespace, resolved from the on-chain root. */
|
|
177
210
|
listNamespace(namespace: NamespaceID, publisher: Address): Promise<NamespaceContents>;
|
|
211
|
+
/**
|
|
212
|
+
* Fetch a single vertex by CID, resolving through the publisher's commit
|
|
213
|
+
* CAR chain (vertex blocks are not individually pinned — they live inside
|
|
214
|
+
* commit CARs).
|
|
215
|
+
*/
|
|
216
|
+
readVertex(cid: string, publisher: Address): Promise<NamespaceContents["vertices"][number]>;
|
|
178
217
|
}
|
|
179
218
|
//#endregion
|
|
180
|
-
export { CommitDiff, CommitObject, Edge, EdgeSchema, FangornEngine, MetagraphRegistry, NamespaceContents, NamespaceDiff, NamespaceID, RelationLabel, SchemaID, Vertex, VertexSchema };
|
|
219
|
+
export { CommitDiff, CommitObject, CreateCommitOptions, Edge, EdgeSchema, FangornEngine, MAX_NAMESPACE_LENGTH, MetagraphRegistry, NamespaceContents, NamespaceDiff, NamespaceID, RelationLabel, SchemaID, Vertex, VertexSchema, assertValidNamespace };
|