peerpigeon 0.1.1 → 1.0.2
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 +12450 -0
- package/dist/peerpigeon-browser.js.map +7 -0
- package/docs/API_DOCUMENTATION.md +1976 -0
- package/docs/BOOTSTRAP_HUBS.md +319 -0
- package/docs/CLI_README.md +371 -0
- package/docs/CUSTOM_HUB_NAMESPACES.md +182 -0
- package/docs/CUSTOM_NAMESPACE_IMPLEMENTATION.md +187 -0
- package/docs/DOCUMENTATION_REORGANIZATION.md +172 -0
- package/docs/HUB_QUICK_REF.md +108 -0
- package/docs/HUB_SCRIPTS.md +341 -0
- package/docs/HUB_SYSTEM.md +599 -0
- package/docs/MIGRATION_COMPLETE.md +93 -0
- package/docs/NETWORK_NAMESPACES.md +212 -0
- package/docs/PIGEONRTC_MIGRATION.md +280 -0
- package/docs/README.md +108 -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 +481 -630
- 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 +189 -0
- package/examples/multi-hub-test.js +186 -0
- package/examples/node/README.md +2 -2
- package/examples/programmatic-namespace-example.js +108 -0
- package/examples/selective-streaming-demo.html +698 -0
- package/examples/standard-events-demo.js +126 -0
- package/index.js +7 -1
- package/package.json +34 -16
- package/server/index.js +1058 -98
- package/server/start.js +52 -0
- package/src/ConnectionManager.js +239 -225
- package/src/CryptoManager.js +147 -18
- package/src/DebugLogger.js +0 -7
- package/src/DistributedStorageManager.js +224 -205
- package/src/EnvironmentDetector.js +88 -4
- package/src/EventEmitter.js +79 -1
- package/src/EvictionManager.js +10 -9
- package/src/GossipManager.js +163 -11
- package/src/MediaManager.js +12 -2
- package/src/PeerConnection.js +343 -311
- package/src/PeerPigeonMesh.js +576 -135
- package/src/PigeonRTC-browser.js +247 -0
- 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 +58 -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 -3391
- package/websocket-server/README.md +0 -247
- package/websocket-server/server.js +0 -391
|
@@ -0,0 +1,189 @@
|
|
|
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', // Default hub namespace (customizable on server)
|
|
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
|
+
// Note: If the server is configured with a custom hub mesh namespace,
|
|
112
|
+
// use that namespace instead of 'pigeonhub-mesh'
|
|
113
|
+
|
|
114
|
+
// Send this message via WebSocket
|
|
115
|
+
// ws.send(JSON.stringify(announceMessage));
|
|
116
|
+
|
|
117
|
+
console.log('Hub announcement message:', announceMessage);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Example 5: Client-side regular peer announcement
|
|
121
|
+
function clientAnnounceAsPeer() {
|
|
122
|
+
// When a regular peer connects:
|
|
123
|
+
const announceMessage = {
|
|
124
|
+
type: 'announce',
|
|
125
|
+
networkName: 'my-app-network', // Regular application network
|
|
126
|
+
data: {
|
|
127
|
+
isHub: false, // Or omit this field
|
|
128
|
+
// Add any peer metadata
|
|
129
|
+
peerType: 'browser',
|
|
130
|
+
capabilities: ['messaging', 'video']
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// Send this message via WebSocket
|
|
135
|
+
// ws.send(JSON.stringify(announceMessage));
|
|
136
|
+
|
|
137
|
+
console.log('Peer announcement message:', announceMessage);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Example 6: Discover other hubs
|
|
141
|
+
function discoverOtherHubs() {
|
|
142
|
+
// When a hub announces on 'pigeonhub-mesh', it will receive notifications about other hubs
|
|
143
|
+
// Listen for 'peer-discovered' messages where isHub === true
|
|
144
|
+
|
|
145
|
+
console.log(`
|
|
146
|
+
When a hub connects to the signaling server and announces on 'pigeonhub-mesh':
|
|
147
|
+
1. It will receive a list of all other hubs already on that network
|
|
148
|
+
2. All other hubs will be notified about this new hub
|
|
149
|
+
3. The server tracks all hubs separately from regular peers
|
|
150
|
+
4. Hubs can then establish WebRTC connections to each other
|
|
151
|
+
`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Main execution
|
|
155
|
+
async function main() {
|
|
156
|
+
console.log('=== PeerPigeon Hub Example ===\n');
|
|
157
|
+
|
|
158
|
+
// Create servers
|
|
159
|
+
console.log('Creating servers...\n');
|
|
160
|
+
const regularServer = await createRegularServer();
|
|
161
|
+
const hubServer = await createHubServer();
|
|
162
|
+
|
|
163
|
+
// Display stats after a moment
|
|
164
|
+
setTimeout(async () => {
|
|
165
|
+
console.log('\n--- Regular Server Stats ---');
|
|
166
|
+
await displayHubStats(regularServer);
|
|
167
|
+
|
|
168
|
+
console.log('\n--- Hub Server Stats ---');
|
|
169
|
+
await displayHubStats(hubServer);
|
|
170
|
+
|
|
171
|
+
console.log('\n--- Client Examples ---');
|
|
172
|
+
clientAnnounceAsHub();
|
|
173
|
+
clientAnnounceAsPeer();
|
|
174
|
+
discoverOtherHubs();
|
|
175
|
+
|
|
176
|
+
// Cleanup
|
|
177
|
+
console.log('\n\nShutting down servers...');
|
|
178
|
+
await regularServer.stop();
|
|
179
|
+
await hubServer.stop();
|
|
180
|
+
console.log('Done!');
|
|
181
|
+
}, 2000);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Run if this is the main module
|
|
185
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
186
|
+
main().catch(console.error);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
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
|
```
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Example: Programmatic Hub Namespace Configuration
|
|
5
|
+
*
|
|
6
|
+
* This example demonstrates how to programmatically configure
|
|
7
|
+
* custom hub mesh namespaces using the PeerPigeonServer API.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { PeerPigeonServer } from '../server/index.js';
|
|
11
|
+
|
|
12
|
+
console.log('🚀 Programmatic Hub Namespace Example\n');
|
|
13
|
+
|
|
14
|
+
// Example 1: Create hub with default namespace
|
|
15
|
+
console.log('1️⃣ Creating hub with default namespace...');
|
|
16
|
+
const hub1 = new PeerPigeonServer({
|
|
17
|
+
port: 3000,
|
|
18
|
+
host: 'localhost',
|
|
19
|
+
isHub: true
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
console.log(` Default namespace: ${hub1.getHubMeshNamespace()}\n`);
|
|
23
|
+
|
|
24
|
+
// Example 2: Create hub with custom namespace in constructor
|
|
25
|
+
console.log('2️⃣ Creating hub with custom namespace in constructor...');
|
|
26
|
+
const hub2 = new PeerPigeonServer({
|
|
27
|
+
port: 3001,
|
|
28
|
+
host: 'localhost',
|
|
29
|
+
isHub: true,
|
|
30
|
+
hubMeshNamespace: 'production-mesh'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(` Custom namespace: ${hub2.getHubMeshNamespace()}\n`);
|
|
34
|
+
|
|
35
|
+
// Example 3: Set namespace programmatically before starting
|
|
36
|
+
console.log('3️⃣ Setting namespace programmatically...');
|
|
37
|
+
const hub3 = new PeerPigeonServer({
|
|
38
|
+
port: 3002,
|
|
39
|
+
host: 'localhost',
|
|
40
|
+
isHub: true
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
console.log(` Initial namespace: ${hub3.getHubMeshNamespace()}`);
|
|
44
|
+
hub3.setHubMeshNamespace('staging-mesh');
|
|
45
|
+
console.log(` Updated namespace: ${hub3.getHubMeshNamespace()}\n`);
|
|
46
|
+
|
|
47
|
+
// Example 4: Start servers and verify namespace in stats
|
|
48
|
+
console.log('4️⃣ Starting servers and checking stats...\n');
|
|
49
|
+
|
|
50
|
+
async function startAndVerify() {
|
|
51
|
+
try {
|
|
52
|
+
// Start hub1
|
|
53
|
+
await hub1.start();
|
|
54
|
+
const stats1 = hub1.getStats();
|
|
55
|
+
console.log(` Hub 1 (${stats1.port}): namespace = "${stats1.hubMeshNamespace}"`);
|
|
56
|
+
|
|
57
|
+
// Start hub2
|
|
58
|
+
await hub2.start();
|
|
59
|
+
const stats2 = hub2.getStats();
|
|
60
|
+
console.log(` Hub 2 (${stats2.port}): namespace = "${stats2.hubMeshNamespace}"`);
|
|
61
|
+
|
|
62
|
+
// Start hub3
|
|
63
|
+
await hub3.start();
|
|
64
|
+
const stats3 = hub3.getStats();
|
|
65
|
+
console.log(` Hub 3 (${stats3.port}): namespace = "${stats3.hubMeshNamespace}"\n`);
|
|
66
|
+
|
|
67
|
+
console.log('✅ All hubs started successfully!\n');
|
|
68
|
+
console.log('📝 Note: Hubs with different namespaces will NOT discover each other.');
|
|
69
|
+
console.log(' Only hubs with matching namespaces form a mesh network.\n');
|
|
70
|
+
|
|
71
|
+
// Example 5: Try to change namespace while running (will fail)
|
|
72
|
+
console.log('5️⃣ Attempting to change namespace while running...');
|
|
73
|
+
try {
|
|
74
|
+
hub1.setHubMeshNamespace('should-fail');
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.log(` ❌ Expected error: ${error.message}\n`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Example 6: Access namespace through different methods
|
|
80
|
+
console.log('6️⃣ Accessing namespace through different methods:');
|
|
81
|
+
console.log(` getHubMeshNamespace(): ${hub1.getHubMeshNamespace()}`);
|
|
82
|
+
console.log(` getStats().hubMeshNamespace: ${hub1.getStats().hubMeshNamespace}`);
|
|
83
|
+
console.log(` Direct property access: ${hub1.hubMeshNamespace}\n`);
|
|
84
|
+
|
|
85
|
+
// Cleanup
|
|
86
|
+
console.log('🧹 Cleaning up...');
|
|
87
|
+
await hub1.stop();
|
|
88
|
+
await hub2.stop();
|
|
89
|
+
await hub3.stop();
|
|
90
|
+
console.log('✅ All hubs stopped\n');
|
|
91
|
+
|
|
92
|
+
// Example 7: Change namespace after stopping
|
|
93
|
+
console.log('7️⃣ Changing namespace after stopping...');
|
|
94
|
+
console.log(` Before: ${hub1.getHubMeshNamespace()}`);
|
|
95
|
+
hub1.setHubMeshNamespace('after-stop-mesh');
|
|
96
|
+
console.log(` After: ${hub1.getHubMeshNamespace()}\n`);
|
|
97
|
+
|
|
98
|
+
console.log('🎉 Example complete!');
|
|
99
|
+
process.exit(0);
|
|
100
|
+
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('❌ Error:', error);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Run the example
|
|
108
|
+
startAndVerify();
|