peerpigeon 0.1.0 → 1.0.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 +141 -1365
- package/cli.js +1382 -0
- package/dist/peerpigeon-browser.js +12290 -0
- package/dist/peerpigeon-browser.js.map +7 -0
- package/docs/API_DOCUMENTATION.md +1945 -0
- package/docs/BOOTSTRAP_HUBS.md +319 -0
- package/docs/CLI_README.md +371 -0
- package/docs/DOCUMENTATION_REORGANIZATION.md +172 -0
- package/docs/HUB_QUICK_REF.md +107 -0
- package/docs/HUB_SCRIPTS.md +331 -0
- package/docs/HUB_SYSTEM.md +552 -0
- package/docs/MIGRATION_COMPLETE.md +93 -0
- package/docs/NETWORK_NAMESPACES.md +212 -0
- package/docs/README.md +107 -0
- package/docs/SELECTIVE_STREAMING_GUIDE.md +427 -0
- package/docs/TEST_CLEANUP_COMPLETE.md +80 -0
- package/examples/bootstrap-hub-example.js +292 -0
- package/examples/browser/README.md +278 -0
- package/examples/browser/app.js +2426 -49
- package/examples/browser/index.html +482 -543
- package/examples/browser/minimal/README.md +55 -0
- package/examples/browser/minimal/index.html +75 -0
- package/examples/browser/minimal/minimal.js +234 -0
- package/examples/browser/styles.css +687 -1425
- package/examples/browser/test-bundle.html +35 -0
- package/examples/browser-2/index.html +30 -0
- package/examples/browser-2/package.json +21 -0
- package/examples/browser-2/src/App-simple.vue +21 -0
- package/examples/browser-2/src/App.vue +420 -0
- package/examples/browser-2/src/components/CryptoView.vue +1661 -0
- package/examples/browser-2/src/components/DHTView.vue +1281 -0
- package/examples/browser-2/src/components/DebugView.vue +1773 -0
- package/examples/browser-2/src/components/MediaView.vue +1048 -0
- package/examples/browser-2/src/components/MessagingView.vue +775 -0
- package/examples/browser-2/src/components/NetworkView.vue +930 -0
- package/examples/browser-2/src/components/StorageView.vue +1376 -0
- package/examples/browser-2/src/main.js +5 -0
- package/examples/browser-2/src/stores/peerpigeon.js +895 -0
- package/examples/browser-2/vite.config.js +6 -0
- package/examples/hub-example.js +186 -0
- package/examples/multi-hub-test.js +186 -0
- package/examples/node/README.md +2 -2
- package/examples/node/storage-test.js +141 -6
- package/examples/selective-streaming-demo.html +698 -0
- package/examples/standard-events-demo.js +126 -0
- package/package.json +33 -16
- package/server/index.js +1032 -98
- package/server/start.js +49 -0
- package/src/ConnectionManager.js +239 -225
- package/src/CryptoManager.js +147 -18
- package/src/DebugLogger.js +0 -7
- package/src/DistributedStorageManager.js +245 -205
- package/src/EnvironmentDetector.js +109 -2
- package/src/EventEmitter.js +79 -1
- package/src/EvictionManager.js +10 -9
- package/src/GossipManager.js +163 -11
- package/src/LexicalStorageInterface.js +332 -0
- package/src/PeerConnection.js +333 -310
- package/src/PeerPigeonMesh.js +577 -135
- package/src/SignalingClient.js +14 -243
- package/src/SignalingHandler.js +31 -10
- package/src/StorageManager.js +0 -39
- package/src/WebDHT-Simple.js +389 -0
- package/src/WebDHT.js +363 -1165
- package/src/browser-entry.js +40 -0
- package/examples/browser/assets/images/favicon.png +0 -0
- package/examples/browser/assets/images/pigeon.png +0 -0
- package/examples/browser/crypto-test.html +0 -427
- package/examples/browser/ui-old.js +0 -1926
- package/examples/browser/ui.js +0 -3010
- package/websocket-server/README.md +0 -247
- package/websocket-server/server.js +0 -391
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PeerPigeon Hub Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to set up and use PeerPigeon hubs.
|
|
5
|
+
* Hubs are long-standing servers that:
|
|
6
|
+
* 1. Connect to each other on the 'pigeonhub-mesh' namespace
|
|
7
|
+
* 2. Still serve regular peers in their own networks
|
|
8
|
+
* 3. Can be identified and tracked separately from regular peers
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { PeerPigeonServer } from '../server/index.js';
|
|
12
|
+
|
|
13
|
+
// Example 1: Create a regular signaling server
|
|
14
|
+
async function createRegularServer() {
|
|
15
|
+
const server = new PeerPigeonServer({
|
|
16
|
+
port: 3000,
|
|
17
|
+
host: 'localhost',
|
|
18
|
+
isHub: false // Regular server
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
server.on('peerConnected', ({ peerId, totalConnections }) => {
|
|
22
|
+
console.log(`Peer connected: ${peerId.substring(0, 8)}... (Total: ${totalConnections})`);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
server.on('peerAnnounced', ({ peerId, networkName, isHub }) => {
|
|
26
|
+
if (isHub) {
|
|
27
|
+
console.log(`🏢 Hub announced: ${peerId.substring(0, 8)}... on network: ${networkName}`);
|
|
28
|
+
} else {
|
|
29
|
+
console.log(`📱 Peer announced: ${peerId.substring(0, 8)}... on network: ${networkName}`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
server.on('peerDisconnected', ({ peerId, totalConnections }) => {
|
|
34
|
+
console.log(`Peer disconnected: ${peerId.substring(0, 8)}... (Remaining: ${totalConnections})`);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
await server.start();
|
|
38
|
+
console.log('Regular server started');
|
|
39
|
+
return server;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Example 2: Create a hub server
|
|
43
|
+
async function createHubServer() {
|
|
44
|
+
const hubServer = new PeerPigeonServer({
|
|
45
|
+
port: 3001,
|
|
46
|
+
host: 'localhost',
|
|
47
|
+
isHub: true // This is a hub server
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Listen for hub registration events
|
|
51
|
+
hubServer.on('hubRegistered', ({ peerId, totalHubs }) => {
|
|
52
|
+
console.log(`🏢 New hub registered: ${peerId.substring(0, 8)}... (Total hubs: ${totalHubs})`);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
hubServer.on('hubUnregistered', ({ peerId, totalHubs }) => {
|
|
56
|
+
console.log(`🏢 Hub unregistered: ${peerId.substring(0, 8)}... (Remaining hubs: ${totalHubs})`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
hubServer.on('peerAnnounced', ({ peerId, networkName, isHub }) => {
|
|
60
|
+
if (isHub) {
|
|
61
|
+
console.log(`🏢 Hub peer announced: ${peerId.substring(0, 8)}... on network: ${networkName}`);
|
|
62
|
+
} else {
|
|
63
|
+
console.log(`📱 Regular peer announced: ${peerId.substring(0, 8)}... on network: ${networkName}`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await hubServer.start();
|
|
68
|
+
console.log('Hub server started');
|
|
69
|
+
return hubServer;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Example 3: Get hub statistics
|
|
73
|
+
async function displayHubStats(server) {
|
|
74
|
+
const stats = server.getStats();
|
|
75
|
+
const hubStats = server.getHubStats();
|
|
76
|
+
|
|
77
|
+
console.log('\n📊 Server Statistics:');
|
|
78
|
+
console.log(` Is Hub Server: ${stats.isHub}`);
|
|
79
|
+
console.log(` Total Connections: ${stats.connections}`);
|
|
80
|
+
console.log(` Total Peers: ${stats.peers}`);
|
|
81
|
+
console.log(` Total Hubs: ${stats.hubs}`);
|
|
82
|
+
console.log(` Networks: ${stats.networks}`);
|
|
83
|
+
console.log(` Uptime: ${Math.floor(stats.uptime / 1000)}s`);
|
|
84
|
+
|
|
85
|
+
console.log('\n🏢 Hub Statistics:');
|
|
86
|
+
console.log(` Total Hubs: ${hubStats.totalHubs}`);
|
|
87
|
+
console.log(` Connected Hubs: ${hubStats.connectedHubs}`);
|
|
88
|
+
|
|
89
|
+
if (hubStats.hubs.length > 0) {
|
|
90
|
+
console.log(' Hub List:');
|
|
91
|
+
hubStats.hubs.forEach(hub => {
|
|
92
|
+
console.log(` - ${hub.peerId.substring(0, 8)}... (network: ${hub.networkName})`);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Example 4: Client-side hub announcement
|
|
98
|
+
function clientAnnounceAsHub() {
|
|
99
|
+
// When a client connects to the signaling server and wants to identify as a hub:
|
|
100
|
+
const announceMessage = {
|
|
101
|
+
type: 'announce',
|
|
102
|
+
networkName: 'pigeonhub-mesh', // Special namespace for hubs
|
|
103
|
+
data: {
|
|
104
|
+
isHub: true, // Identify as a hub
|
|
105
|
+
// Add any additional hub metadata
|
|
106
|
+
hubVersion: '1.0.0',
|
|
107
|
+
capabilities: ['signaling', 'relay', 'storage']
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Send this message via WebSocket
|
|
112
|
+
// ws.send(JSON.stringify(announceMessage));
|
|
113
|
+
|
|
114
|
+
console.log('Hub announcement message:', announceMessage);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Example 5: Client-side regular peer announcement
|
|
118
|
+
function clientAnnounceAsPeer() {
|
|
119
|
+
// When a regular peer connects:
|
|
120
|
+
const announceMessage = {
|
|
121
|
+
type: 'announce',
|
|
122
|
+
networkName: 'my-app-network', // Regular application network
|
|
123
|
+
data: {
|
|
124
|
+
isHub: false, // Or omit this field
|
|
125
|
+
// Add any peer metadata
|
|
126
|
+
peerType: 'browser',
|
|
127
|
+
capabilities: ['messaging', 'video']
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// Send this message via WebSocket
|
|
132
|
+
// ws.send(JSON.stringify(announceMessage));
|
|
133
|
+
|
|
134
|
+
console.log('Peer announcement message:', announceMessage);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Example 6: Discover other hubs
|
|
138
|
+
function discoverOtherHubs() {
|
|
139
|
+
// When a hub announces on 'pigeonhub-mesh', it will receive notifications about other hubs
|
|
140
|
+
// Listen for 'peer-discovered' messages where isHub === true
|
|
141
|
+
|
|
142
|
+
console.log(`
|
|
143
|
+
When a hub connects to the signaling server and announces on 'pigeonhub-mesh':
|
|
144
|
+
1. It will receive a list of all other hubs already on that network
|
|
145
|
+
2. All other hubs will be notified about this new hub
|
|
146
|
+
3. The server tracks all hubs separately from regular peers
|
|
147
|
+
4. Hubs can then establish WebRTC connections to each other
|
|
148
|
+
`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Main execution
|
|
152
|
+
async function main() {
|
|
153
|
+
console.log('=== PeerPigeon Hub Example ===\n');
|
|
154
|
+
|
|
155
|
+
// Create servers
|
|
156
|
+
console.log('Creating servers...\n');
|
|
157
|
+
const regularServer = await createRegularServer();
|
|
158
|
+
const hubServer = await createHubServer();
|
|
159
|
+
|
|
160
|
+
// Display stats after a moment
|
|
161
|
+
setTimeout(async () => {
|
|
162
|
+
console.log('\n--- Regular Server Stats ---');
|
|
163
|
+
await displayHubStats(regularServer);
|
|
164
|
+
|
|
165
|
+
console.log('\n--- Hub Server Stats ---');
|
|
166
|
+
await displayHubStats(hubServer);
|
|
167
|
+
|
|
168
|
+
console.log('\n--- Client Examples ---');
|
|
169
|
+
clientAnnounceAsHub();
|
|
170
|
+
clientAnnounceAsPeer();
|
|
171
|
+
discoverOtherHubs();
|
|
172
|
+
|
|
173
|
+
// Cleanup
|
|
174
|
+
console.log('\n\nShutting down servers...');
|
|
175
|
+
await regularServer.stop();
|
|
176
|
+
await hubServer.stop();
|
|
177
|
+
console.log('Done!');
|
|
178
|
+
}, 2000);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Run if this is the main module
|
|
182
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
183
|
+
main().catch(console.error);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { createRegularServer, createHubServer, displayHubStats };
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Multi-Hub Cross-Peer Connection Test
|
|
5
|
+
*
|
|
6
|
+
* This example demonstrates:
|
|
7
|
+
* 1. Starting multiple hubs (Hub A on 3000, Hub B on 3001)
|
|
8
|
+
* 2. Hub B connects to Hub A as bootstrap
|
|
9
|
+
* 3. Peers connect to different hubs
|
|
10
|
+
* 4. Peers discover each other across hubs
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* node examples/multi-hub-test.js
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { PeerPigeonServer } from '../server/index.js';
|
|
17
|
+
import { PeerPigeonMesh } from '../index.js';
|
|
18
|
+
|
|
19
|
+
// Setup WebRTC for Node.js
|
|
20
|
+
async function setupWebRTC() {
|
|
21
|
+
try {
|
|
22
|
+
const [WebSocket, wrtc] = await Promise.all([
|
|
23
|
+
import('ws'),
|
|
24
|
+
import('@koush/wrtc')
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
global.RTCPeerConnection = wrtc.default.RTCPeerConnection;
|
|
28
|
+
global.RTCSessionDescription = wrtc.default.RTCSessionDescription;
|
|
29
|
+
global.RTCIceCandidate = wrtc.default.RTCIceCandidate;
|
|
30
|
+
global.WebSocket = WebSocket.default;
|
|
31
|
+
|
|
32
|
+
return true;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error('❌ Failed to setup WebRTC:', error);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function main() {
|
|
40
|
+
console.log('🚀 Multi-Hub Cross-Peer Connection Test\n');
|
|
41
|
+
|
|
42
|
+
// Initialize WebRTC
|
|
43
|
+
if (!await setupWebRTC()) {
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Start Hub A (Bootstrap Hub on port 3000)
|
|
48
|
+
console.log('📡 Starting Hub A (Bootstrap) on port 3000...');
|
|
49
|
+
const hubA = new PeerPigeonServer({
|
|
50
|
+
port: 3000,
|
|
51
|
+
host: 'localhost',
|
|
52
|
+
isHub: true,
|
|
53
|
+
autoConnect: false // This is the bootstrap hub, no need to connect
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await hubA.start();
|
|
57
|
+
console.log('✅ Hub A running\n');
|
|
58
|
+
|
|
59
|
+
// Wait a moment
|
|
60
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
61
|
+
|
|
62
|
+
// Start Hub B (connects to Hub A on port 3000)
|
|
63
|
+
console.log('📡 Starting Hub B on port 3001 (bootstrapping to Hub A)...');
|
|
64
|
+
const hubB = new PeerPigeonServer({
|
|
65
|
+
port: 3001,
|
|
66
|
+
host: 'localhost',
|
|
67
|
+
isHub: true,
|
|
68
|
+
bootstrapHubs: ['ws://localhost:3000'], // Connect to Hub A
|
|
69
|
+
autoConnect: true
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
await hubB.start();
|
|
73
|
+
console.log('✅ Hub B running\n');
|
|
74
|
+
|
|
75
|
+
// Wait for hub connection to establish
|
|
76
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
77
|
+
|
|
78
|
+
// Create Peer 1 connected to Hub A
|
|
79
|
+
console.log('👤 Creating Peer 1 (connected to Hub A on port 3000)...');
|
|
80
|
+
const peer1 = new PeerPigeonMesh({
|
|
81
|
+
networkName: 'test-network',
|
|
82
|
+
ignoreEnvironmentErrors: true
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
let peer1Discovered = false;
|
|
86
|
+
let peer2Discovered = false;
|
|
87
|
+
|
|
88
|
+
peer1.on('peer-discovered', ({ peerId }) => {
|
|
89
|
+
console.log(`✅ Peer 1 DISCOVERED EVENT: ${peerId.substring(0, 8)}...`);
|
|
90
|
+
peer2Discovered = true;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
peer1.on('peerDiscovered', ({ peerId }) => {
|
|
94
|
+
console.log(`✅ Peer 1 peerDiscovered EVENT: ${peerId.substring(0, 8)}...`);
|
|
95
|
+
peer2Discovered = true;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
peer1.on('peer-connected', ({ peerId }) => {
|
|
99
|
+
console.log(`🤝 Peer 1 CONNECTED to peer: ${peerId.substring(0, 8)}...`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
peer1.on('peerConnected', ({ peerId }) => {
|
|
103
|
+
console.log(`🤝 Peer 1 peerConnected to peer: ${peerId.substring(0, 8)}...`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await peer1.init();
|
|
107
|
+
await peer1.connect('ws://localhost:3000');
|
|
108
|
+
console.log(`✅ Peer 1 connected (ID: ${peer1.peerId.substring(0, 8)}...)\n`);
|
|
109
|
+
|
|
110
|
+
// Wait a moment
|
|
111
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
112
|
+
|
|
113
|
+
// Create Peer 2 connected to Hub B
|
|
114
|
+
console.log('👤 Creating Peer 2 (connected to Hub B on port 3001)...');
|
|
115
|
+
const peer2 = new PeerPigeonMesh({
|
|
116
|
+
networkName: 'test-network',
|
|
117
|
+
ignoreEnvironmentErrors: true
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
peer2.on('peer-discovered', ({ peerId }) => {
|
|
121
|
+
console.log(`✅ Peer 2 DISCOVERED EVENT: ${peerId.substring(0, 8)}...`);
|
|
122
|
+
peer1Discovered = true;
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
peer2.on('peerDiscovered', ({ peerId }) => {
|
|
126
|
+
console.log(`✅ Peer 2 peerDiscovered EVENT: ${peerId.substring(0, 8)}...`);
|
|
127
|
+
peer1Discovered = true;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
peer2.on('peer-connected', ({ peerId }) => {
|
|
131
|
+
console.log(`🤝 Peer 2 CONNECTED to peer: ${peerId.substring(0, 8)}...`);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
peer2.on('peerConnected', ({ peerId }) => {
|
|
135
|
+
console.log(`🤝 Peer 2 peerConnected to peer: ${peerId.substring(0, 8)}...`);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
await peer2.init();
|
|
139
|
+
await peer2.connect('ws://localhost:3001');
|
|
140
|
+
console.log(`✅ Peer 2 connected (ID: ${peer2.peerId.substring(0, 8)}...)\n`);
|
|
141
|
+
|
|
142
|
+
// Wait for peer discovery
|
|
143
|
+
console.log('⏳ Waiting for cross-hub peer discovery...');
|
|
144
|
+
|
|
145
|
+
// Wait up to 10 seconds for discovery
|
|
146
|
+
const startTime = Date.now();
|
|
147
|
+
const timeout = 10000;
|
|
148
|
+
|
|
149
|
+
while (!peer1Discovered || !peer2Discovered) {
|
|
150
|
+
if (Date.now() - startTime > timeout) {
|
|
151
|
+
console.error('❌ Timeout waiting for peer discovery!');
|
|
152
|
+
console.error(` Peer 1 discovered Peer 2: ${peer1Discovered}`);
|
|
153
|
+
console.error(` Peer 2 discovered Peer 1: ${peer2Discovered}`);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (peer1Discovered && peer2Discovered) {
|
|
160
|
+
console.log('\n✅ SUCCESS! Peers on different hubs discovered each other!');
|
|
161
|
+
} else {
|
|
162
|
+
console.log('\n❌ FAILED! Peers did not discover each other.');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Show final status
|
|
166
|
+
console.log('\n📊 Final Status:');
|
|
167
|
+
console.log(` Hub A: ${hubA.connections.size} connections`);
|
|
168
|
+
console.log(` Hub B: ${hubB.connections.size} connections`);
|
|
169
|
+
console.log(` Peer 1 discovered peers: ${peer1.discoveredPeers.size}`);
|
|
170
|
+
console.log(` Peer 2 discovered peers: ${peer2.discoveredPeers.size}`);
|
|
171
|
+
|
|
172
|
+
// Cleanup
|
|
173
|
+
console.log('\n🧹 Cleaning up...');
|
|
174
|
+
await peer1.disconnect();
|
|
175
|
+
await peer2.disconnect();
|
|
176
|
+
await hubB.stop();
|
|
177
|
+
await hubA.stop();
|
|
178
|
+
|
|
179
|
+
console.log('✅ Test complete!');
|
|
180
|
+
process.exit(peer1Discovered && peer2Discovered ? 0 : 1);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
main().catch(error => {
|
|
184
|
+
console.error('❌ Test failed:', error);
|
|
185
|
+
process.exit(1);
|
|
186
|
+
});
|
package/examples/node/README.md
CHANGED
|
@@ -101,8 +101,8 @@ Each example initializes a PeerPigeon mesh with the required features:
|
|
|
101
101
|
|
|
102
102
|
```javascript
|
|
103
103
|
const mesh = new PeerPigeonMesh({
|
|
104
|
-
enableWebDHT: true, // Distributed hash table
|
|
105
|
-
enableCrypto: true, // Encryption/decryption
|
|
104
|
+
enableWebDHT: true, // Distributed hash table (default: true)
|
|
105
|
+
enableCrypto: true, // Encryption/decryption (default: true)
|
|
106
106
|
enableDistributedStorage: true // Optional storage layer
|
|
107
107
|
});
|
|
108
108
|
```
|
|
@@ -387,20 +387,154 @@ class StorageTest {
|
|
|
387
387
|
}
|
|
388
388
|
|
|
389
389
|
async testStorageToggle() {
|
|
390
|
-
await this.runTest('Storage
|
|
391
|
-
|
|
392
|
-
|
|
390
|
+
await this.runTest('Storage Toggle', async () => {
|
|
391
|
+
const key = 'test-toggle';
|
|
392
|
+
const value = { test: true };
|
|
393
|
+
|
|
394
|
+
// Store when enabled
|
|
395
|
+
await this.storage.store(key, value);
|
|
396
|
+
let retrieved = await this.storage.retrieve(key);
|
|
397
|
+
if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
|
|
398
|
+
throw new Error('Failed to store when enabled');
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Disable storage
|
|
402
|
+
this.storage.disable();
|
|
393
403
|
if (this.storage.isEnabled()) {
|
|
394
404
|
throw new Error('Storage should be disabled');
|
|
395
405
|
}
|
|
396
406
|
|
|
397
|
-
//
|
|
398
|
-
await this.storage.
|
|
407
|
+
// Operations should fail or return null when disabled
|
|
408
|
+
const disabledResult = await this.storage.store('disabled-key', { test: true });
|
|
409
|
+
if (disabledResult !== false) {
|
|
410
|
+
throw new Error('Store should fail when disabled');
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Re-enable storage
|
|
414
|
+
this.storage.enable();
|
|
399
415
|
if (!this.storage.isEnabled()) {
|
|
400
416
|
throw new Error('Storage should be enabled');
|
|
401
417
|
}
|
|
402
418
|
|
|
403
|
-
|
|
419
|
+
// Should work again
|
|
420
|
+
retrieved = await this.storage.retrieve(key);
|
|
421
|
+
if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
|
|
422
|
+
throw new Error('Failed to retrieve after re-enabling');
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
console.log(' Storage toggle functionality verified');
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async testLexicalInterface() {
|
|
430
|
+
await this.runTest('Lexical Interface - Basic Operations', async () => {
|
|
431
|
+
const lex = this.storage.lexical();
|
|
432
|
+
|
|
433
|
+
// Test basic put/get
|
|
434
|
+
const user = lex.get('users').get('alice');
|
|
435
|
+
await user.put({ name: 'Alice', age: 30, city: 'New York' });
|
|
436
|
+
|
|
437
|
+
const name = await user.get('name').val();
|
|
438
|
+
if (name !== 'Alice') {
|
|
439
|
+
throw new Error(`Expected 'Alice', got '${name}'`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const age = await user.get('age').val();
|
|
443
|
+
if (age !== 30) {
|
|
444
|
+
throw new Error(`Expected 30, got ${age}`);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Test object reconstruction
|
|
448
|
+
const fullUser = await user.val();
|
|
449
|
+
if (!fullUser || fullUser.name !== 'Alice' || fullUser.age !== 30 || fullUser.city !== 'New York') {
|
|
450
|
+
throw new Error('Object reconstruction failed');
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
console.log(' Basic lexical operations verified');
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
await this.runTest('Lexical Interface - Set Operations', async () => {
|
|
457
|
+
const lex = this.storage.lexical();
|
|
458
|
+
const friends = lex.get('users').get('bob').get('friends');
|
|
459
|
+
|
|
460
|
+
// Test set operations
|
|
461
|
+
await friends.set({
|
|
462
|
+
alice: { name: 'Alice', status: 'online' },
|
|
463
|
+
charlie: { name: 'Charlie', status: 'offline' }
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
// Verify set data was stored
|
|
467
|
+
const setData = await this.storage.retrieve('users:bob:friends:_set');
|
|
468
|
+
if (!setData || !setData.alice || !setData.charlie) {
|
|
469
|
+
throw new Error('Set data not stored correctly');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
console.log(' Set operations verified');
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
await this.runTest('Lexical Interface - Property Access', async () => {
|
|
476
|
+
const lex = this.storage.lexical();
|
|
477
|
+
|
|
478
|
+
// Test proxy-based property access
|
|
479
|
+
const settings = lex.users.alice.settings;
|
|
480
|
+
await settings.put({ theme: 'dark', language: 'en' });
|
|
481
|
+
|
|
482
|
+
const theme = await settings.theme.val();
|
|
483
|
+
if (theme !== 'dark') {
|
|
484
|
+
throw new Error(`Expected 'dark', got '${theme}'`);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
console.log(' Property access verified');
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
await this.runTest('Lexical Interface - Update and Delete', async () => {
|
|
491
|
+
const lex = this.storage.lexical();
|
|
492
|
+
const profile = lex.get('profiles').get('user1');
|
|
493
|
+
|
|
494
|
+
// Test update
|
|
495
|
+
await profile.put({ name: 'John', age: 25 });
|
|
496
|
+
await profile.update({ age: 26, city: 'Boston' });
|
|
497
|
+
|
|
498
|
+
const updatedProfile = await profile.val();
|
|
499
|
+
if (updatedProfile.age !== 26 || updatedProfile.city !== 'Boston' || updatedProfile.name !== 'John') {
|
|
500
|
+
throw new Error('Update operation failed');
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Test delete
|
|
504
|
+
await profile.delete();
|
|
505
|
+
const deletedProfile = await profile.val();
|
|
506
|
+
if (deletedProfile !== null) {
|
|
507
|
+
throw new Error('Delete operation failed');
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
console.log(' Update and delete operations verified');
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
await this.runTest('Lexical Interface - Utility Methods', async () => {
|
|
514
|
+
const lex = this.storage.lexical();
|
|
515
|
+
const testObj = lex.get('test').get('object');
|
|
516
|
+
|
|
517
|
+
await testObj.put({ prop1: 'value1', prop2: 'value2', prop3: 'value3' });
|
|
518
|
+
|
|
519
|
+
// Test exists
|
|
520
|
+
const exists = await testObj.exists();
|
|
521
|
+
if (!exists) {
|
|
522
|
+
throw new Error('exists() should return true');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Test keys
|
|
526
|
+
const keys = await testObj.keys();
|
|
527
|
+
if (!keys.includes('prop1') || !keys.includes('prop2') || !keys.includes('prop3')) {
|
|
528
|
+
throw new Error('keys() did not return expected keys');
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Test getPath
|
|
532
|
+
const path = testObj.getPath();
|
|
533
|
+
if (path !== 'test:object') {
|
|
534
|
+
throw new Error(`Expected path 'test:object', got '${path}'`);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
console.log(' Utility methods verified');
|
|
404
538
|
});
|
|
405
539
|
}
|
|
406
540
|
|
|
@@ -427,6 +561,7 @@ class StorageTest {
|
|
|
427
561
|
await this.testBackupAndRestore();
|
|
428
562
|
await this.testStatistics();
|
|
429
563
|
await this.testStorageToggle();
|
|
564
|
+
await this.testLexicalInterface();
|
|
430
565
|
await this.testCleanup();
|
|
431
566
|
|
|
432
567
|
this.printSummary();
|