p2p-fret 0.2.0 → 0.3.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 CHANGED
@@ -92,6 +92,71 @@ if (fretService.detectPartition()) {
92
92
  }
93
93
  ```
94
94
 
95
+ ## Routing Table Persistence
96
+
97
+ FRET's routing table is in-memory by default. The `exportTable` / `importTable` API lets you snapshot and restore it across restarts, avoiding cold-start bootstrap latency.
98
+
99
+ ### API
100
+
101
+ ```typescript
102
+ interface FretService {
103
+ // Export the full routing table as a JSON-serializable snapshot
104
+ exportTable(): SerializedTable;
105
+
106
+ // Import a previously exported snapshot; returns number of entries loaded
107
+ importTable(table: SerializedTable): number;
108
+ }
109
+
110
+ interface SerializedTable {
111
+ v: 1;
112
+ peerId: string; // exporter's PeerId
113
+ timestamp: number; // unix ms at export time
114
+ entries: SerializedPeerEntry[];
115
+ }
116
+
117
+ interface SerializedPeerEntry {
118
+ id: string; // PeerId
119
+ coord: string; // base64url ring coordinate
120
+ relevance: number;
121
+ lastAccess: number;
122
+ state: PeerState;
123
+ accessCount: number;
124
+ successCount: number;
125
+ failureCount: number;
126
+ avgLatencyMs: number;
127
+ metadata?: Record<string, any>;
128
+ }
129
+ ```
130
+
131
+ ### Usage
132
+
133
+ ```typescript
134
+ import { createFret, type SerializedTable } from 'p2p-fret';
135
+ import fs from 'node:fs/promises';
136
+
137
+ const fret = createFret(libp2pNode, { capacity: 2048 });
138
+
139
+ // Save before shutdown
140
+ const table = fret.exportTable();
141
+ await fs.writeFile('fret-table.json', JSON.stringify(table));
142
+
143
+ // Restore on startup (before or after start())
144
+ const saved: SerializedTable = JSON.parse(
145
+ await fs.readFile('fret-table.json', 'utf-8')
146
+ );
147
+ const count = fret.importTable(saved);
148
+ console.log(`Restored ${count} routing entries`);
149
+
150
+ await fret.start();
151
+ ```
152
+
153
+ ### Behavior
154
+
155
+ - **State reset**: All imported entries get `state: 'disconnected'` regardless of their exported state — connection liveness must be re-established through pings and stabilization.
156
+ - **Capacity enforcement**: `importTable` enforces the configured capacity after loading. A table exported from a node with a larger capacity won't exceed the importer's limit.
157
+ - **Stale data safety**: After import, the normal stabilization loop probes peers to update connection states and relevance scores. Unreachable peers are decayed and eventually evicted.
158
+ - **JSON round-trip safe**: `SerializedTable` survives `JSON.stringify` / `JSON.parse`.
159
+
95
160
  ## Test harness (local meshes)
96
161
 
97
162
  A minimal harness will spin up a small libp2p mesh in-process and exercise: