@serfab/cadre-core 0.10.0 → 0.11.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.
Files changed (105) hide show
  1. package/README.md +255 -255
  2. package/dist/cadre-node.d.ts +203 -28
  3. package/dist/cadre-node.js +445 -84
  4. package/dist/cadre-node.js.map +1 -1
  5. package/dist/control-cohort.d.ts +26 -0
  6. package/dist/control-cohort.js +26 -0
  7. package/dist/control-cohort.js.map +1 -1
  8. package/dist/control-database.d.ts +82 -117
  9. package/dist/control-database.js +217 -295
  10. package/dist/control-database.js.map +1 -1
  11. package/dist/control-formation-recorder.d.ts +8 -7
  12. package/dist/control-formation-recorder.js +8 -7
  13. package/dist/control-formation-recorder.js.map +1 -1
  14. package/dist/control-schema.d.ts +1 -1
  15. package/dist/control-schema.js +789 -774
  16. package/dist/control-schema.js.map +1 -1
  17. package/dist/control-write-retry.d.ts +15 -5
  18. package/dist/control-write-retry.js +19 -18
  19. package/dist/control-write-retry.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.js +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/peer-addr-book.d.ts +92 -0
  24. package/dist/peer-addr-book.js +220 -0
  25. package/dist/peer-addr-book.js.map +1 -0
  26. package/dist/seed-bootstrap.d.ts +7 -0
  27. package/dist/seed-bootstrap.js +15 -13
  28. package/dist/seed-bootstrap.js.map +1 -1
  29. package/dist/strand-cohort.d.ts +4 -37
  30. package/dist/strand-cohort.js +4 -15
  31. package/dist/strand-cohort.js.map +1 -1
  32. package/dist/strand-database.d.ts +11 -16
  33. package/dist/strand-database.js +19 -7
  34. package/dist/strand-database.js.map +1 -1
  35. package/dist/strand-formation-manager.d.ts +8 -8
  36. package/dist/strand-formation-manager.js +15 -18
  37. package/dist/strand-formation-manager.js.map +1 -1
  38. package/dist/strand-instance-manager.d.ts +52 -21
  39. package/dist/strand-instance-manager.js +100 -34
  40. package/dist/strand-instance-manager.js.map +1 -1
  41. package/dist/strand-membership-writer.d.ts +6 -4
  42. package/dist/strand-membership-writer.js +38 -36
  43. package/dist/strand-membership-writer.js.map +1 -1
  44. package/dist/types.d.ts +39 -31
  45. package/dist/types.js.map +1 -1
  46. package/package.json +8 -7
  47. package/src/arachnode-stub.ts +159 -159
  48. package/src/bootstrap-peer-store-file.ts +44 -44
  49. package/src/bootstrap-peer-store.ts +172 -172
  50. package/src/cadre-node.ts +5158 -4771
  51. package/src/canonical-json.ts +30 -30
  52. package/src/control-authorization.ts +114 -114
  53. package/src/control-cohort.ts +130 -103
  54. package/src/control-database.ts +2406 -2506
  55. package/src/control-formation-recorder.ts +305 -304
  56. package/src/control-schema.ts +801 -786
  57. package/src/control-stream.ts +205 -205
  58. package/src/control-write-retry.ts +483 -473
  59. package/src/delegate-admission.ts +302 -302
  60. package/src/device-token.ts +94 -94
  61. package/src/diagnostics/connection-path.ts +277 -277
  62. package/src/diagnostics/webrtc-turn-tracker.ts +222 -222
  63. package/src/ed25519-key.ts +134 -134
  64. package/src/enrollment.ts +199 -199
  65. package/src/file-durable-slot.ts +61 -61
  66. package/src/formation-approval.ts +604 -604
  67. package/src/fs-atomic.ts +107 -107
  68. package/src/hibernation-manager.ts +437 -437
  69. package/src/identity-key.ts +109 -109
  70. package/src/index.ts +1 -0
  71. package/src/key-store-file.ts +132 -132
  72. package/src/key-store.ts +99 -99
  73. package/src/membership-connection-gater.ts +204 -204
  74. package/src/node-local-snapshot.ts +260 -260
  75. package/src/peer-addr-book.ts +250 -0
  76. package/src/peer-authorization.ts +259 -259
  77. package/src/peer-record.ts +164 -164
  78. package/src/push-credentials.ts +84 -84
  79. package/src/push-node.ts +64 -64
  80. package/src/push-notifier-apns.ts +247 -247
  81. package/src/push-notifier-fcm.ts +220 -220
  82. package/src/push-notifier-shared.ts +28 -28
  83. package/src/push-notifier.ts +52 -52
  84. package/src/relay-addrs.ts +111 -111
  85. package/src/relay-reservation.ts +823 -823
  86. package/src/seed-bootstrap.ts +15 -15
  87. package/src/seed-trust-policy.ts +140 -140
  88. package/src/strand-addr-protocol.ts +441 -441
  89. package/src/strand-backfill.ts +366 -366
  90. package/src/strand-cohort.ts +38 -77
  91. package/src/strand-database.ts +202 -204
  92. package/src/strand-formation-manager.ts +491 -494
  93. package/src/strand-formation-protocol.ts +692 -692
  94. package/src/strand-instance-manager.ts +108 -50
  95. package/src/strand-member-key.ts +39 -39
  96. package/src/strand-member-registry.ts +182 -182
  97. package/src/strand-membership-writer.ts +1432 -1430
  98. package/src/strand-solicitation.ts +472 -472
  99. package/src/strand-transport-key.ts +58 -58
  100. package/src/strand-wake-payload.ts +32 -32
  101. package/src/strand-wake-protocol.ts +314 -314
  102. package/src/strand-watcher.ts +314 -314
  103. package/src/trusted-owner-store-file.ts +41 -41
  104. package/src/trusted-owner-store.ts +171 -171
  105. package/src/types.ts +1309 -1303
@@ -1,172 +1,172 @@
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, type DurableSlot, type NodeLocalSnapshotSpec } from './node-local-snapshot.js';
42
-
43
- const log = debug('sereus:cadre:bootstrap-peer-store');
44
-
45
- /** One retained cold-start dial target: an owner peer a seed nominated. */
46
- export interface BootstrapPeerEntry {
47
- /** Multiaddr strings exactly as the seed carried them (parsed at dial time). */
48
- addrs: string[];
49
- /** Wall-clock ms the entry was last recorded (diagnostics / future eviction). */
50
- recordedAt: number;
51
- }
52
-
53
- export interface BootstrapPeerStore {
54
- /** Party this store is scoped to. */
55
- readonly partyId: string;
56
-
57
- /**
58
- * Every retained target, peerId -> entry. Every backend copies the map per call,
59
- * so the result is a snapshot decoupled from later {@link record} calls and is
60
- * safe to iterate while recording. The entry objects need no copy: `record`
61
- * REPLACES an entry rather than mutating it in place.
62
- */
63
- all(): ReadonlyMap<string, BootstrapPeerEntry>;
64
-
65
- /**
66
- * Retain (or REPLACE) a peer's dial addresses. Replace, not merge, so a
67
- * re-seed after an owner's address changed drops the stale address instead of
68
- * accumulating dead ones.
69
- *
70
- * Implementations MUST reflect the entry in {@link all} SYNCHRONOUSLY; the
71
- * returned promise tracks durability only. That is what lets the synchronous
72
- * `CadreNode.recordSeedBootstrapPeers` keep its signature while a file
73
- * backend persists in the background.
74
- *
75
- * NOTE: entries are never evicted, and a persistent backend's file therefore
76
- * grows across the node's whole lifetime rather than one process. Fine while a
77
- * seed nominates one or a few owners and entries are keyed by peer id; if a
78
- * node ever applies seeds naming many distinct owners, add eviction (oldest
79
- * {@link BootstrapPeerEntry.recordedAt} first, or a cap) rather than letting
80
- * the file grow unbounded — `recordedAt` exists so eviction has something to
81
- * sort by.
82
- */
83
- record(peerId: string, addrs: readonly string[]): Promise<void>;
84
- }
85
-
86
- /**
87
- * Ephemeral in-memory store for nodes without durable storage (tests, browser
88
- * demos, not-yet-persisted mobile). Same contract, no disk: a node using this
89
- * loses its retry targets on restart and must be re-seeded to rejoin.
90
- */
91
- export class MemoryBootstrapPeerStore implements BootstrapPeerStore {
92
- private readonly peers = new Map<string, BootstrapPeerEntry>();
93
-
94
- constructor(readonly partyId: string) {}
95
-
96
- all(): ReadonlyMap<string, BootstrapPeerEntry> {
97
- return new Map(this.peers);
98
- }
99
-
100
- async record(peerId: string, addrs: readonly string[]): Promise<void> {
101
- this.peers.set(peerId, { addrs: [...addrs], recordedAt: Date.now() });
102
- log('bootstrap peer retained (party=%s, peer=%s, addrs=%d)', this.partyId, peerId, addrs.length);
103
- }
104
- }
105
-
106
- /**
107
- * What the store persists: `peers` maps peerId -> {@link BootstrapPeerEntry}.
108
- *
109
- * A structurally junk entry is DROPPED and its siblings retained — nothing here
110
- * is trust-bearing (see the module comment) and the record is a stranded node's
111
- * only way back into its party, so discarding the whole set over one bad entry
112
- * would be strictly worse. What "junk" means: an unparseable peer id (the dial
113
- * path binds every address to this id, so an id that cannot parse is not a dial
114
- * target at all), an empty address list, or a non-string address — each would
115
- * otherwise log a parse failure once per reconcile pass forever.
116
- */
117
- const BOOTSTRAP_PEER_SNAPSHOT: NodeLocalSnapshotSpec<BootstrapPeerEntry> = {
118
- label: 'bootstrap-peer store',
119
- payloadKey: 'peers',
120
- unusableEntry: 'drop-entry',
121
- acceptEntry: (peerId, entry) => {
122
- if (typeof entry !== 'object' || entry === null) return undefined;
123
- const { addrs, recordedAt } = entry as { addrs?: unknown; recordedAt?: unknown };
124
- if (!Array.isArray(addrs) || addrs.length === 0) return undefined;
125
- if (addrs.some((addr) => typeof addr !== 'string' || addr.length === 0)) return undefined;
126
- if (typeof recordedAt !== 'number') return undefined;
127
- try {
128
- peerIdFromString(peerId);
129
- } catch {
130
- return undefined;
131
- }
132
- return { addrs: [...(addrs as string[])], recordedAt };
133
- }
134
- };
135
-
136
- /**
137
- * Durable {@link BootstrapPeerStore} over an app-supplied {@link DurableSlot} —
138
- * the cross-platform half of every persistent backend (the Node
139
- * `FileBootstrapPeerStore` is this class over a file slot).
140
- *
141
- * Construct via {@link open}. Load and persist policy — what an absent, corrupt,
142
- * foreign-party or unreadable slot does, and what a failed persist does — is
143
- * documented once on `NodeLocalSnapshot`; this class only supplies the payload
144
- * shape and the drop-the-bad-entry policy above.
145
- */
146
- export class PersistentBootstrapPeerStore implements BootstrapPeerStore {
147
- private constructor(private readonly snapshot: NodeLocalSnapshot<BootstrapPeerEntry>) {}
148
-
149
- /** Load (or cold-start) the party's retained dial targets from `slot`. */
150
- static async open(slot: DurableSlot, partyId: string): Promise<PersistentBootstrapPeerStore> {
151
- return new PersistentBootstrapPeerStore(
152
- await NodeLocalSnapshot.open(slot, partyId, BOOTSTRAP_PEER_SNAPSHOT)
153
- );
154
- }
155
-
156
- get partyId(): string {
157
- return this.snapshot.partyId;
158
- }
159
-
160
- all(): ReadonlyMap<string, BootstrapPeerEntry> {
161
- return this.snapshot.entrySnapshot();
162
- }
163
-
164
- /**
165
- * Retain a peer's dial addresses: visible via {@link all} synchronously, then
166
- * the full snapshot is persisted (see `NodeLocalSnapshot.put`).
167
- */
168
- record(peerId: string, addrs: readonly string[]): Promise<void> {
169
- log('bootstrap peer retained (party=%s, peer=%s, addrs=%d); persisting', this.partyId, peerId, addrs.length);
170
- return this.snapshot.put(peerId, { addrs: [...addrs], recordedAt: Date.now() });
171
- }
172
- }
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, type DurableSlot, type NodeLocalSnapshotSpec } from './node-local-snapshot.js';
42
+
43
+ const log = debug('sereus:cadre:bootstrap-peer-store');
44
+
45
+ /** One retained cold-start dial target: an owner peer a seed nominated. */
46
+ export interface BootstrapPeerEntry {
47
+ /** Multiaddr strings exactly as the seed carried them (parsed at dial time). */
48
+ addrs: string[];
49
+ /** Wall-clock ms the entry was last recorded (diagnostics / future eviction). */
50
+ recordedAt: number;
51
+ }
52
+
53
+ export interface BootstrapPeerStore {
54
+ /** Party this store is scoped to. */
55
+ readonly partyId: string;
56
+
57
+ /**
58
+ * Every retained target, peerId -> entry. Every backend copies the map per call,
59
+ * so the result is a snapshot decoupled from later {@link record} calls and is
60
+ * safe to iterate while recording. The entry objects need no copy: `record`
61
+ * REPLACES an entry rather than mutating it in place.
62
+ */
63
+ all(): ReadonlyMap<string, BootstrapPeerEntry>;
64
+
65
+ /**
66
+ * Retain (or REPLACE) a peer's dial addresses. Replace, not merge, so a
67
+ * re-seed after an owner's address changed drops the stale address instead of
68
+ * accumulating dead ones.
69
+ *
70
+ * Implementations MUST reflect the entry in {@link all} SYNCHRONOUSLY; the
71
+ * returned promise tracks durability only. That is what lets the synchronous
72
+ * `CadreNode.recordSeedBootstrapPeers` keep its signature while a file
73
+ * backend persists in the background.
74
+ *
75
+ * NOTE: entries are never evicted, and a persistent backend's file therefore
76
+ * grows across the node's whole lifetime rather than one process. Fine while a
77
+ * seed nominates one or a few owners and entries are keyed by peer id; if a
78
+ * node ever applies seeds naming many distinct owners, add eviction (oldest
79
+ * {@link BootstrapPeerEntry.recordedAt} first, or a cap) rather than letting
80
+ * the file grow unbounded — `recordedAt` exists so eviction has something to
81
+ * sort by.
82
+ */
83
+ record(peerId: string, addrs: readonly string[]): Promise<void>;
84
+ }
85
+
86
+ /**
87
+ * Ephemeral in-memory store for nodes without durable storage (tests, browser
88
+ * demos, not-yet-persisted mobile). Same contract, no disk: a node using this
89
+ * loses its retry targets on restart and must be re-seeded to rejoin.
90
+ */
91
+ export class MemoryBootstrapPeerStore implements BootstrapPeerStore {
92
+ private readonly peers = new Map<string, BootstrapPeerEntry>();
93
+
94
+ constructor(readonly partyId: string) {}
95
+
96
+ all(): ReadonlyMap<string, BootstrapPeerEntry> {
97
+ return new Map(this.peers);
98
+ }
99
+
100
+ async record(peerId: string, addrs: readonly string[]): Promise<void> {
101
+ this.peers.set(peerId, { addrs: [...addrs], recordedAt: Date.now() });
102
+ log('bootstrap peer retained (party=%s, peer=%s, addrs=%d)', this.partyId, peerId, addrs.length);
103
+ }
104
+ }
105
+
106
+ /**
107
+ * What the store persists: `peers` maps peerId -> {@link BootstrapPeerEntry}.
108
+ *
109
+ * A structurally junk entry is DROPPED and its siblings retained — nothing here
110
+ * is trust-bearing (see the module comment) and the record is a stranded node's
111
+ * only way back into its party, so discarding the whole set over one bad entry
112
+ * would be strictly worse. What "junk" means: an unparseable peer id (the dial
113
+ * path binds every address to this id, so an id that cannot parse is not a dial
114
+ * target at all), an empty address list, or a non-string address — each would
115
+ * otherwise log a parse failure once per reconcile pass forever.
116
+ */
117
+ const BOOTSTRAP_PEER_SNAPSHOT: NodeLocalSnapshotSpec<BootstrapPeerEntry> = {
118
+ label: 'bootstrap-peer store',
119
+ payloadKey: 'peers',
120
+ unusableEntry: 'drop-entry',
121
+ acceptEntry: (peerId, entry) => {
122
+ if (typeof entry !== 'object' || entry === null) return undefined;
123
+ const { addrs, recordedAt } = entry as { addrs?: unknown; recordedAt?: unknown };
124
+ if (!Array.isArray(addrs) || addrs.length === 0) return undefined;
125
+ if (addrs.some((addr) => typeof addr !== 'string' || addr.length === 0)) return undefined;
126
+ if (typeof recordedAt !== 'number') return undefined;
127
+ try {
128
+ peerIdFromString(peerId);
129
+ } catch {
130
+ return undefined;
131
+ }
132
+ return { addrs: [...(addrs as string[])], recordedAt };
133
+ }
134
+ };
135
+
136
+ /**
137
+ * Durable {@link BootstrapPeerStore} over an app-supplied {@link DurableSlot} —
138
+ * the cross-platform half of every persistent backend (the Node
139
+ * `FileBootstrapPeerStore` is this class over a file slot).
140
+ *
141
+ * Construct via {@link open}. Load and persist policy — what an absent, corrupt,
142
+ * foreign-party or unreadable slot does, and what a failed persist does — is
143
+ * documented once on `NodeLocalSnapshot`; this class only supplies the payload
144
+ * shape and the drop-the-bad-entry policy above.
145
+ */
146
+ export class PersistentBootstrapPeerStore implements BootstrapPeerStore {
147
+ private constructor(private readonly snapshot: NodeLocalSnapshot<BootstrapPeerEntry>) {}
148
+
149
+ /** Load (or cold-start) the party's retained dial targets from `slot`. */
150
+ static async open(slot: DurableSlot, partyId: string): Promise<PersistentBootstrapPeerStore> {
151
+ return new PersistentBootstrapPeerStore(
152
+ await NodeLocalSnapshot.open(slot, partyId, BOOTSTRAP_PEER_SNAPSHOT)
153
+ );
154
+ }
155
+
156
+ get partyId(): string {
157
+ return this.snapshot.partyId;
158
+ }
159
+
160
+ all(): ReadonlyMap<string, BootstrapPeerEntry> {
161
+ return this.snapshot.entrySnapshot();
162
+ }
163
+
164
+ /**
165
+ * Retain a peer's dial addresses: visible via {@link all} synchronously, then
166
+ * the full snapshot is persisted (see `NodeLocalSnapshot.put`).
167
+ */
168
+ record(peerId: string, addrs: readonly string[]): Promise<void> {
169
+ log('bootstrap peer retained (party=%s, peer=%s, addrs=%d); persisting', this.partyId, peerId, addrs.length);
170
+ return this.snapshot.put(peerId, { addrs: [...addrs], recordedAt: Date.now() });
171
+ }
172
+ }