-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-server.mjs
More file actions
36 lines (30 loc) · 1.21 KB
/
test-server.mjs
File metadata and controls
36 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Local test: run a standalone P2P peer server (Node B).
* Usage: NODE_ROLE=server P2P_PORT=8099 node test-server.mjs
*/
import { loadOrCreateIdentity } from "./dist/identity.js";
import { initDb } from "./dist/peer-db.js";
import { startPeerServer, getInbox } from "./dist/peer-server.js";
import { mkdirSync } from "fs";
import { join } from "path";
const PORT = parseInt(process.env.P2P_PORT ?? "8099");
const DATA_DIR = join("/tmp", "p2p-node-b");
mkdirSync(DATA_DIR, { recursive: true });
const identity = loadOrCreateIdentity(DATA_DIR);
initDb(DATA_DIR);
console.log(`[node-b] Agent ID : ${identity.agentId.slice(0, 8)}...`);
console.log(`[node-b] Starting peer server on [::]:${PORT} (test mode)...`);
await startPeerServer(PORT, { testMode: true });
console.log(`[node-b] Ready. Waiting for messages... (Ctrl+C to stop)`);
// Print inbox every 3s
setInterval(() => {
const msgs = getInbox();
if (msgs.length > 0) {
const m = msgs[0];
console.log(`\n[node-b] *** RECEIVED MESSAGE ***`);
console.log(`[node-b] from : ${m.from}`);
console.log(`[node-b] content: "${m.content}"`);
console.log(`[node-b] event : ${m.event}`);
console.log(`[node-b] verified: ${m.verified}`);
}
}, 3000);