@serfab/cadre-core 0.8.1 → 0.10.0
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 +6 -4
- package/dist/bootstrap-peer-store-file.d.ts +13 -0
- package/dist/bootstrap-peer-store-file.js +42 -0
- package/dist/bootstrap-peer-store-file.js.map +1 -0
- package/dist/bootstrap-peer-store.d.ts +73 -0
- package/dist/bootstrap-peer-store.js +128 -0
- package/dist/bootstrap-peer-store.js.map +1 -0
- package/dist/cadre-node.d.ts +1006 -101
- package/dist/cadre-node.js +1977 -283
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +93 -0
- package/dist/control-authorization.js +57 -0
- package/dist/control-authorization.js.map +1 -0
- package/dist/control-cohort.d.ts +15 -15
- package/dist/control-cohort.js +14 -14
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +1033 -76
- package/dist/control-database.js +1520 -153
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +91 -12
- package/dist/control-formation-recorder.js +131 -16
- package/dist/control-formation-recorder.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +774 -186
- package/dist/control-schema.js.map +1 -1
- package/dist/control-stream.d.ts +38 -4
- package/dist/control-stream.js +70 -4
- package/dist/control-stream.js.map +1 -1
- package/dist/control-write-retry.d.ts +151 -0
- package/dist/control-write-retry.js +411 -0
- package/dist/control-write-retry.js.map +1 -0
- package/dist/delegate-admission.d.ts +134 -0
- package/dist/delegate-admission.js +244 -0
- package/dist/delegate-admission.js.map +1 -0
- package/dist/device-token.d.ts +12 -12
- package/dist/device-token.js +15 -14
- package/dist/device-token.js.map +1 -1
- package/dist/ed25519-key.d.ts +68 -0
- package/dist/ed25519-key.js +106 -0
- package/dist/ed25519-key.js.map +1 -0
- package/dist/file-durable-slot.d.ts +20 -0
- package/dist/file-durable-slot.js +53 -0
- package/dist/file-durable-slot.js.map +1 -0
- package/dist/formation-approval.d.ts +157 -0
- package/dist/formation-approval.js +375 -0
- package/dist/formation-approval.js.map +1 -0
- package/dist/fs-atomic.d.ts +38 -0
- package/dist/fs-atomic.js +97 -0
- package/dist/fs-atomic.js.map +1 -0
- package/dist/identity-key.d.ts +49 -0
- package/dist/identity-key.js +81 -0
- package/dist/identity-key.js.map +1 -0
- package/dist/index.d.ts +16 -6
- package/dist/index.js +44 -10
- package/dist/index.js.map +1 -1
- package/dist/key-store-file.d.ts +2 -24
- package/dist/key-store-file.js +10 -77
- package/dist/key-store-file.js.map +1 -1
- package/dist/key-store.d.ts +1 -1
- package/dist/key-store.js +1 -1
- package/dist/membership-connection-gater.d.ts +146 -0
- package/dist/membership-connection-gater.js +180 -0
- package/dist/membership-connection-gater.js.map +1 -0
- package/dist/node-local-snapshot.d.ts +109 -0
- package/dist/node-local-snapshot.js +182 -0
- package/dist/node-local-snapshot.js.map +1 -0
- package/dist/peer-authorization.d.ts +150 -11
- package/dist/peer-authorization.js +170 -13
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-record.d.ts +24 -11
- package/dist/peer-record.js +29 -13
- package/dist/peer-record.js.map +1 -1
- package/dist/push-node.d.ts +38 -0
- package/dist/push-node.js +48 -0
- package/dist/push-node.js.map +1 -0
- package/dist/push-notifier-apns.d.ts +10 -2
- package/dist/push-notifier-apns.js +14 -10
- package/dist/push-notifier-apns.js.map +1 -1
- package/dist/push-notifier-fcm.d.ts +7 -1
- package/dist/push-notifier-fcm.js +9 -10
- package/dist/push-notifier-fcm.js.map +1 -1
- package/dist/push-notifier.d.ts +16 -35
- package/dist/push-notifier.js +16 -44
- package/dist/push-notifier.js.map +1 -1
- package/dist/relay-addrs.d.ts +45 -0
- package/dist/relay-addrs.js +103 -0
- package/dist/relay-addrs.js.map +1 -0
- package/dist/relay-reservation.d.ts +266 -0
- package/dist/relay-reservation.js +613 -0
- package/dist/relay-reservation.js.map +1 -0
- package/dist/seed-bootstrap.d.ts +263 -79
- package/dist/seed-bootstrap.js +413 -211
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +50 -24
- package/dist/seed-trust-policy.js +28 -19
- package/dist/seed-trust-policy.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +42 -13
- package/dist/strand-addr-protocol.js +58 -52
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-backfill.d.ts +109 -0
- package/dist/strand-backfill.js +279 -0
- package/dist/strand-backfill.js.map +1 -0
- package/dist/strand-database.d.ts +5 -5
- package/dist/strand-database.js +4 -4
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +38 -9
- package/dist/strand-formation-manager.js +120 -17
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +104 -5
- package/dist/strand-formation-protocol.js +233 -5
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +40 -2
- package/dist/strand-instance-manager.js +93 -15
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +6 -6
- package/dist/strand-member-key.js +6 -6
- package/dist/strand-member-key.js.map +1 -1
- package/dist/strand-member-registry.d.ts +12 -12
- package/dist/strand-member-registry.js +22 -14
- package/dist/strand-member-registry.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +532 -138
- package/dist/strand-membership-writer.js +800 -179
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-solicitation.d.ts +99 -33
- package/dist/strand-solicitation.js +100 -52
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-transport-key.d.ts +40 -0
- package/dist/strand-transport-key.js +56 -0
- package/dist/strand-transport-key.js.map +1 -0
- package/dist/strand-wake-protocol.d.ts +9 -7
- package/dist/strand-wake-protocol.js +22 -46
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.d.ts +17 -2
- package/dist/strand-watcher.js +47 -3
- package/dist/strand-watcher.js.map +1 -1
- package/dist/trusted-owner-store-file.d.ts +12 -0
- package/dist/trusted-owner-store-file.js +39 -0
- package/dist/trusted-owner-store-file.js.map +1 -0
- package/dist/trusted-owner-store.d.ts +64 -0
- package/dist/trusted-owner-store.js +126 -0
- package/dist/trusted-owner-store.js.map +1 -0
- package/dist/types.d.ts +325 -50
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -1
- package/package.json +23 -8
- package/src/bootstrap-peer-store-file.ts +44 -0
- package/src/bootstrap-peer-store.ts +172 -0
- package/src/cadre-node.ts +2129 -295
- package/src/control-authorization.ts +114 -0
- package/src/control-cohort.ts +17 -17
- package/src/control-database.ts +1766 -191
- package/src/control-formation-recorder.ts +174 -21
- package/src/control-schema.ts +786 -198
- package/src/control-stream.ts +81 -4
- package/src/control-write-retry.ts +473 -0
- package/src/delegate-admission.ts +302 -0
- package/src/device-token.ts +19 -14
- package/src/ed25519-key.ts +134 -0
- package/src/file-durable-slot.ts +61 -0
- package/src/formation-approval.ts +604 -0
- package/src/fs-atomic.ts +107 -0
- package/src/identity-key.ts +109 -0
- package/src/index.ts +157 -24
- package/src/key-store-file.ts +10 -81
- package/src/key-store.ts +1 -1
- package/src/membership-connection-gater.ts +204 -0
- package/src/node-local-snapshot.ts +260 -0
- package/src/peer-authorization.ts +219 -13
- package/src/peer-record.ts +31 -13
- package/src/push-node.ts +64 -0
- package/src/push-notifier-apns.ts +16 -12
- package/src/push-notifier-fcm.ts +9 -10
- package/src/push-notifier.ts +16 -57
- package/src/relay-addrs.ts +111 -0
- package/src/relay-reservation.ts +823 -0
- package/src/seed-bootstrap.ts +490 -226
- package/src/seed-trust-policy.ts +65 -32
- package/src/strand-addr-protocol.ts +84 -53
- package/src/strand-backfill.ts +366 -0
- package/src/strand-database.ts +6 -6
- package/src/strand-formation-manager.ts +145 -19
- package/src/strand-formation-protocol.ts +310 -9
- package/src/strand-instance-manager.ts +113 -16
- package/src/strand-member-key.ts +7 -7
- package/src/strand-member-registry.ts +28 -23
- package/src/strand-membership-writer.ts +1430 -640
- package/src/strand-solicitation.ts +155 -90
- package/src/strand-transport-key.ts +58 -0
- package/src/strand-wake-protocol.ts +29 -46
- package/src/strand-watcher.ts +66 -3
- package/src/trusted-owner-store-file.ts +41 -0
- package/src/trusted-owner-store.ts +171 -0
- package/src/types.ts +342 -51
- package/dist/authority-key.d.ts +0 -41
- package/dist/authority-key.js +0 -52
- package/dist/authority-key.js.map +0 -1
- package/src/authority-key.ts +0 -68
package/README.md
CHANGED
|
@@ -126,9 +126,9 @@ Adding a new device to your cadre uses the Seed Bootstrap API:
|
|
|
126
126
|
// On the new device: generate identity
|
|
127
127
|
const enrollment = new EnrollmentService();
|
|
128
128
|
const { peerId, privateKey } = await enrollment.createCadrePeer();
|
|
129
|
-
// Store privateKey securely, send peerId + multiaddrs to
|
|
129
|
+
// Store privateKey securely, send peerId + multiaddrs to owner
|
|
130
130
|
|
|
131
|
-
// On
|
|
131
|
+
// On owner device: authorize the new peer and create seed
|
|
132
132
|
await node.authorizePeer(newDevicePeerId, newDeviceMultiaddrs);
|
|
133
133
|
const seed = await node.createSeed();
|
|
134
134
|
|
|
@@ -169,7 +169,9 @@ The main entry point for cadre participation.
|
|
|
169
169
|
| `getStrands()` | Get all active strand instances |
|
|
170
170
|
| `getStrand(id)` | Get a specific strand instance |
|
|
171
171
|
| `addStrand(row)` | Manually add a strand (testing/direct API) |
|
|
172
|
-
| `
|
|
172
|
+
| `stopStrand(id)` | Stop a strand on this node only (the shared `Strand` row stays; rediscovered on restart) |
|
|
173
|
+
| `publishStrand(id, type?, memberKey?)` | Owner-signed `Strand` row insert — makes the strand visible cadre-wide |
|
|
174
|
+
| `unpublishStrand(id)` | Owner-signed party-wide removal — deletes this party's `Strand` row; every node stops the strand. Destroys a closed strand's `MemberPrivateKey` |
|
|
173
175
|
| `getEnrollmentService()` | Access peer enrollment API |
|
|
174
176
|
|
|
175
177
|
### Events
|
|
@@ -180,7 +182,7 @@ The main entry point for cadre participation.
|
|
|
180
182
|
| `control:disconnected` | - | Disconnected from control network |
|
|
181
183
|
| `strand:started` | `{ strandId }` | Strand instance started |
|
|
182
184
|
| `strand:stopped` | `{ strandId }` | Strand instance stopped |
|
|
183
|
-
| `strand:error` | `{ strandId, error }` | Error in strand instance |
|
|
185
|
+
| `strand:error` | `{ strandId, error }` | Error in strand instance. A failed launch re-emits this on every retry (backing off up to 5 min), so treat it as a repeating signal, not a one-shot |
|
|
184
186
|
|
|
185
187
|
## Related Documentation
|
|
186
188
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BootstrapPeerStore } from './bootstrap-peer-store.js';
|
|
2
|
+
/**
|
|
3
|
+
* File-backed {@link BootstrapPeerStore}. Open via {@link open}, which loads the
|
|
4
|
+
* existing file and returns the cross-platform store over it; an absent, corrupt, or wrong-party file is a cold
|
|
5
|
+
* start (empty store) and structurally junk entries are dropped, while a
|
|
6
|
+
* present-but-unreadable file throws. Those rules and their reasoning live on
|
|
7
|
+
* `NodeLocalSnapshot.open` — including the in-process-only write serialisation,
|
|
8
|
+
* which two nodes sharing one directory for one party would defeat.
|
|
9
|
+
*/
|
|
10
|
+
export declare const FileBootstrapPeerStore: {
|
|
11
|
+
/** Load (or cold-start) the party's retained dial targets from a file in `dir`. */
|
|
12
|
+
open(dir: string, partyId: string): Promise<BootstrapPeerStore>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-only file-backed {@link BootstrapPeerStore}: one JSON file per party under
|
|
3
|
+
* a configured directory (the node's state directory — `cadre-cli` passes
|
|
4
|
+
* `ResolvedConfig.nodeStateDir`), holding the node-local, non-replicated
|
|
5
|
+
* cold-start dial targets a seed nominated.
|
|
6
|
+
*
|
|
7
|
+
* Nothing but the file itself lives here: the envelope, load policy, per-entry
|
|
8
|
+
* validation (shape only — never signatures; see the `bootstrap-peer-store.ts`
|
|
9
|
+
* module comment for why nothing persisted here is trust-bearing) and the
|
|
10
|
+
* serialised snapshot writes are cross-platform in
|
|
11
|
+
* `PersistentBootstrapPeerStore` / `node-local-snapshot.ts`, and the file is a
|
|
12
|
+
* `FileDurableSlot`. This module exists only so the `node:fs` edge stays out of
|
|
13
|
+
* the package's cross-platform default entry (`./index.js`) — import it from the
|
|
14
|
+
* dedicated subpath instead:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { FileBootstrapPeerStore } from '@serfab/cadre-core/bootstrap-peer-store-file';
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* A React Native / browser entry graph never resolves this path, so the
|
|
21
|
+
* `node:fs` edge never reaches a bundler that cannot satisfy it (same isolation
|
|
22
|
+
* pattern as `trusted-owner-store-file.ts` / `key-store-file.ts`).
|
|
23
|
+
*/
|
|
24
|
+
import { FileDurableSlot } from './file-durable-slot.js';
|
|
25
|
+
import { PersistentBootstrapPeerStore } from './bootstrap-peer-store.js';
|
|
26
|
+
/** Base name of the store file: `<dir>/bootstrap-peers.<encoded partyId>.json`. */
|
|
27
|
+
const SLOT_NAME = 'bootstrap-peers';
|
|
28
|
+
/**
|
|
29
|
+
* File-backed {@link BootstrapPeerStore}. Open via {@link open}, which loads the
|
|
30
|
+
* existing file and returns the cross-platform store over it; an absent, corrupt, or wrong-party file is a cold
|
|
31
|
+
* start (empty store) and structurally junk entries are dropped, while a
|
|
32
|
+
* present-but-unreadable file throws. Those rules and their reasoning live on
|
|
33
|
+
* `NodeLocalSnapshot.open` — including the in-process-only write serialisation,
|
|
34
|
+
* which two nodes sharing one directory for one party would defeat.
|
|
35
|
+
*/
|
|
36
|
+
export const FileBootstrapPeerStore = {
|
|
37
|
+
/** Load (or cold-start) the party's retained dial targets from a file in `dir`. */
|
|
38
|
+
async open(dir, partyId) {
|
|
39
|
+
return PersistentBootstrapPeerStore.open(new FileDurableSlot(dir, SLOT_NAME, partyId), partyId);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=bootstrap-peer-store-file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap-peer-store-file.js","sourceRoot":"","sources":["../src/bootstrap-peer-store-file.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAGzE,mFAAmF;AACnF,MAAM,SAAS,GAAG,iBAAiB,CAAC;AAEpC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACrC,mFAAmF;IACnF,KAAK,CAAC,IAAI,CAAC,GAAW,EAAE,OAAe;QACtC,OAAO,4BAA4B,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;IACjG,CAAC;CACD,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type DurableSlot } from './node-local-snapshot.js';
|
|
2
|
+
/** One retained cold-start dial target: an owner peer a seed nominated. */
|
|
3
|
+
export interface BootstrapPeerEntry {
|
|
4
|
+
/** Multiaddr strings exactly as the seed carried them (parsed at dial time). */
|
|
5
|
+
addrs: string[];
|
|
6
|
+
/** Wall-clock ms the entry was last recorded (diagnostics / future eviction). */
|
|
7
|
+
recordedAt: number;
|
|
8
|
+
}
|
|
9
|
+
export interface BootstrapPeerStore {
|
|
10
|
+
/** Party this store is scoped to. */
|
|
11
|
+
readonly partyId: string;
|
|
12
|
+
/**
|
|
13
|
+
* Every retained target, peerId -> entry. Every backend copies the map per call,
|
|
14
|
+
* so the result is a snapshot decoupled from later {@link record} calls and is
|
|
15
|
+
* safe to iterate while recording. The entry objects need no copy: `record`
|
|
16
|
+
* REPLACES an entry rather than mutating it in place.
|
|
17
|
+
*/
|
|
18
|
+
all(): ReadonlyMap<string, BootstrapPeerEntry>;
|
|
19
|
+
/**
|
|
20
|
+
* Retain (or REPLACE) a peer's dial addresses. Replace, not merge, so a
|
|
21
|
+
* re-seed after an owner's address changed drops the stale address instead of
|
|
22
|
+
* accumulating dead ones.
|
|
23
|
+
*
|
|
24
|
+
* Implementations MUST reflect the entry in {@link all} SYNCHRONOUSLY; the
|
|
25
|
+
* returned promise tracks durability only. That is what lets the synchronous
|
|
26
|
+
* `CadreNode.recordSeedBootstrapPeers` keep its signature while a file
|
|
27
|
+
* backend persists in the background.
|
|
28
|
+
*
|
|
29
|
+
* NOTE: entries are never evicted, and a persistent backend's file therefore
|
|
30
|
+
* grows across the node's whole lifetime rather than one process. Fine while a
|
|
31
|
+
* seed nominates one or a few owners and entries are keyed by peer id; if a
|
|
32
|
+
* node ever applies seeds naming many distinct owners, add eviction (oldest
|
|
33
|
+
* {@link BootstrapPeerEntry.recordedAt} first, or a cap) rather than letting
|
|
34
|
+
* the file grow unbounded — `recordedAt` exists so eviction has something to
|
|
35
|
+
* sort by.
|
|
36
|
+
*/
|
|
37
|
+
record(peerId: string, addrs: readonly string[]): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Ephemeral in-memory store for nodes without durable storage (tests, browser
|
|
41
|
+
* demos, not-yet-persisted mobile). Same contract, no disk: a node using this
|
|
42
|
+
* loses its retry targets on restart and must be re-seeded to rejoin.
|
|
43
|
+
*/
|
|
44
|
+
export declare class MemoryBootstrapPeerStore implements BootstrapPeerStore {
|
|
45
|
+
readonly partyId: string;
|
|
46
|
+
private readonly peers;
|
|
47
|
+
constructor(partyId: string);
|
|
48
|
+
all(): ReadonlyMap<string, BootstrapPeerEntry>;
|
|
49
|
+
record(peerId: string, addrs: readonly string[]): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Durable {@link BootstrapPeerStore} over an app-supplied {@link DurableSlot} —
|
|
53
|
+
* the cross-platform half of every persistent backend (the Node
|
|
54
|
+
* `FileBootstrapPeerStore` is this class over a file slot).
|
|
55
|
+
*
|
|
56
|
+
* Construct via {@link open}. Load and persist policy — what an absent, corrupt,
|
|
57
|
+
* foreign-party or unreadable slot does, and what a failed persist does — is
|
|
58
|
+
* documented once on `NodeLocalSnapshot`; this class only supplies the payload
|
|
59
|
+
* shape and the drop-the-bad-entry policy above.
|
|
60
|
+
*/
|
|
61
|
+
export declare class PersistentBootstrapPeerStore implements BootstrapPeerStore {
|
|
62
|
+
private readonly snapshot;
|
|
63
|
+
private constructor();
|
|
64
|
+
/** Load (or cold-start) the party's retained dial targets from `slot`. */
|
|
65
|
+
static open(slot: DurableSlot, partyId: string): Promise<PersistentBootstrapPeerStore>;
|
|
66
|
+
get partyId(): string;
|
|
67
|
+
all(): ReadonlyMap<string, BootstrapPeerEntry>;
|
|
68
|
+
/**
|
|
69
|
+
* Retain a peer's dial addresses: visible via {@link all} synchronously, then
|
|
70
|
+
* the full snapshot is persisted (see `NodeLocalSnapshot.put`).
|
|
71
|
+
*/
|
|
72
|
+
record(peerId: string, addrs: readonly string[]): Promise<void>;
|
|
73
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-local, NON-replicated cold-start bootstrap-peer store: the dial targets a
|
|
3
|
+
* node keeps so it can retry its way back into a party it has been seeded into
|
|
4
|
+
* but never managed to connect to.
|
|
5
|
+
*
|
|
6
|
+
* A newcomer applies a signed seed nominating the party's owner machines and the
|
|
7
|
+
* addresses they answer on. `SeedBootstrapService.applySeed` dials those owners
|
|
8
|
+
* exactly once, best-effort; when that dial fails (owner briefly down, relay
|
|
9
|
+
* reservation not up, not yet vouched) the node has an empty `CadrePeer` table
|
|
10
|
+
* and no connection, and `CadreNode.reconcileControlCohort`'s cold-start branch
|
|
11
|
+
* is the only way back — it re-dials these retained addresses every pass. Held
|
|
12
|
+
* only in memory, they die with the process and the node is stranded for good,
|
|
13
|
+
* which is what this store exists to prevent.
|
|
14
|
+
*
|
|
15
|
+
* Three implementations, mirroring `trusted-owner-store.ts` (read that first —
|
|
16
|
+
* it solves the same "must outlive the process, storage differs per platform"
|
|
17
|
+
* problem, the two stores are deliberately symmetrical, and both persistent
|
|
18
|
+
* forms share `node-local-snapshot.ts`):
|
|
19
|
+
* - {@link MemoryBootstrapPeerStore} (this module, cross-platform) — ephemeral;
|
|
20
|
+
* the default when no store is injected via `CadreNodeConfig.bootstrapPeers`.
|
|
21
|
+
* - {@link PersistentBootstrapPeerStore} (this module, cross-platform) — durable
|
|
22
|
+
* over any `DurableSlot` the embedding app supplies (IndexedDB, a LevelDB KV,
|
|
23
|
+
* SQLite, …).
|
|
24
|
+
* - `FileBootstrapPeerStore` — the above over a Node file in the node's state
|
|
25
|
+
* directory; Node-only, behind the subpath
|
|
26
|
+
* `@serfab/cadre-core/bootstrap-peer-store-file` (same isolation pattern as
|
|
27
|
+
* `key-store-file`) so `node:fs` never lands in the RN/browser entry graph.
|
|
28
|
+
*
|
|
29
|
+
* **Nothing here is trust-bearing, and a loader must not try to re-verify it.**
|
|
30
|
+
* Only *dial targets* are retained — never a seed, a signature, or an authority
|
|
31
|
+
* claim. The seed was signature-checked against the node-local trusted-owner
|
|
32
|
+
* anchor before its addresses were retained, and a dial grants no authority:
|
|
33
|
+
* `CadreNode.bootstrapDialAddrs` binds every address to the peer id it was
|
|
34
|
+
* retained under, so a dial cannot be redirected to whoever answers. What a
|
|
35
|
+
* persistent backend's loader DOES owe is dropping structurally junk entries
|
|
36
|
+
* (unparseable peer id, empty address list, non-string address) rather than
|
|
37
|
+
* carrying them into the dial loop.
|
|
38
|
+
*/
|
|
39
|
+
import debug from 'debug';
|
|
40
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
41
|
+
import { NodeLocalSnapshot } from './node-local-snapshot.js';
|
|
42
|
+
const log = debug('sereus:cadre:bootstrap-peer-store');
|
|
43
|
+
/**
|
|
44
|
+
* Ephemeral in-memory store for nodes without durable storage (tests, browser
|
|
45
|
+
* demos, not-yet-persisted mobile). Same contract, no disk: a node using this
|
|
46
|
+
* loses its retry targets on restart and must be re-seeded to rejoin.
|
|
47
|
+
*/
|
|
48
|
+
export class MemoryBootstrapPeerStore {
|
|
49
|
+
constructor(partyId) {
|
|
50
|
+
this.partyId = partyId;
|
|
51
|
+
this.peers = new Map();
|
|
52
|
+
}
|
|
53
|
+
all() {
|
|
54
|
+
return new Map(this.peers);
|
|
55
|
+
}
|
|
56
|
+
async record(peerId, addrs) {
|
|
57
|
+
this.peers.set(peerId, { addrs: [...addrs], recordedAt: Date.now() });
|
|
58
|
+
log('bootstrap peer retained (party=%s, peer=%s, addrs=%d)', this.partyId, peerId, addrs.length);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* What the store persists: `peers` maps peerId -> {@link BootstrapPeerEntry}.
|
|
63
|
+
*
|
|
64
|
+
* A structurally junk entry is DROPPED and its siblings retained — nothing here
|
|
65
|
+
* is trust-bearing (see the module comment) and the record is a stranded node's
|
|
66
|
+
* only way back into its party, so discarding the whole set over one bad entry
|
|
67
|
+
* would be strictly worse. What "junk" means: an unparseable peer id (the dial
|
|
68
|
+
* path binds every address to this id, so an id that cannot parse is not a dial
|
|
69
|
+
* target at all), an empty address list, or a non-string address — each would
|
|
70
|
+
* otherwise log a parse failure once per reconcile pass forever.
|
|
71
|
+
*/
|
|
72
|
+
const BOOTSTRAP_PEER_SNAPSHOT = {
|
|
73
|
+
label: 'bootstrap-peer store',
|
|
74
|
+
payloadKey: 'peers',
|
|
75
|
+
unusableEntry: 'drop-entry',
|
|
76
|
+
acceptEntry: (peerId, entry) => {
|
|
77
|
+
if (typeof entry !== 'object' || entry === null)
|
|
78
|
+
return undefined;
|
|
79
|
+
const { addrs, recordedAt } = entry;
|
|
80
|
+
if (!Array.isArray(addrs) || addrs.length === 0)
|
|
81
|
+
return undefined;
|
|
82
|
+
if (addrs.some((addr) => typeof addr !== 'string' || addr.length === 0))
|
|
83
|
+
return undefined;
|
|
84
|
+
if (typeof recordedAt !== 'number')
|
|
85
|
+
return undefined;
|
|
86
|
+
try {
|
|
87
|
+
peerIdFromString(peerId);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return { addrs: [...addrs], recordedAt };
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Durable {@link BootstrapPeerStore} over an app-supplied {@link DurableSlot} —
|
|
97
|
+
* the cross-platform half of every persistent backend (the Node
|
|
98
|
+
* `FileBootstrapPeerStore` is this class over a file slot).
|
|
99
|
+
*
|
|
100
|
+
* Construct via {@link open}. Load and persist policy — what an absent, corrupt,
|
|
101
|
+
* foreign-party or unreadable slot does, and what a failed persist does — is
|
|
102
|
+
* documented once on `NodeLocalSnapshot`; this class only supplies the payload
|
|
103
|
+
* shape and the drop-the-bad-entry policy above.
|
|
104
|
+
*/
|
|
105
|
+
export class PersistentBootstrapPeerStore {
|
|
106
|
+
constructor(snapshot) {
|
|
107
|
+
this.snapshot = snapshot;
|
|
108
|
+
}
|
|
109
|
+
/** Load (or cold-start) the party's retained dial targets from `slot`. */
|
|
110
|
+
static async open(slot, partyId) {
|
|
111
|
+
return new PersistentBootstrapPeerStore(await NodeLocalSnapshot.open(slot, partyId, BOOTSTRAP_PEER_SNAPSHOT));
|
|
112
|
+
}
|
|
113
|
+
get partyId() {
|
|
114
|
+
return this.snapshot.partyId;
|
|
115
|
+
}
|
|
116
|
+
all() {
|
|
117
|
+
return this.snapshot.entrySnapshot();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Retain a peer's dial addresses: visible via {@link all} synchronously, then
|
|
121
|
+
* the full snapshot is persisted (see `NodeLocalSnapshot.put`).
|
|
122
|
+
*/
|
|
123
|
+
record(peerId, addrs) {
|
|
124
|
+
log('bootstrap peer retained (party=%s, peer=%s, addrs=%d); persisting', this.partyId, peerId, addrs.length);
|
|
125
|
+
return this.snapshot.put(peerId, { addrs: [...addrs], recordedAt: Date.now() });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL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