peerpigeon 0.0.7 ā 0.1.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 +93 -1
- package/examples/browser/app.js +3 -2
- package/examples/browser/index.html +179 -16
- package/examples/browser/styles.css +279 -47
- package/examples/browser/ui.js +560 -14
- package/examples/debug-configuration.js +171 -0
- package/examples/node/README.md +110 -0
- package/examples/node/basic-storage-example.js +90 -0
- package/examples/node/package.json +17 -0
- package/examples/node/storage-spaces-test.js +329 -0
- package/examples/node/storage-test.js +520 -0
- package/index.js +13 -9
- package/package.json +14 -7
- package/server/index.js +12 -12
- package/src/CleanupManager.js +20 -18
- package/src/ConnectionManager.js +407 -102
- package/src/CryptoManager.js +28 -26
- package/src/DebugLogger.js +211 -0
- package/src/DistributedStorageManager.js +1701 -0
- package/src/EnvironmentDetector.js +3 -1
- package/src/EventEmitter.js +1 -0
- package/src/EvictionManager.js +26 -24
- package/src/GossipManager.js +69 -33
- package/src/MediaManager.js +12 -10
- package/src/MeshOptimizer.js +30 -28
- package/src/PeerConnection.js +621 -159
- package/src/PeerDiscovery.js +19 -17
- package/src/PeerPigeonMesh.js +281 -32
- package/src/SignalingClient.js +59 -44
- package/src/SignalingHandler.js +280 -51
- package/src/StorageManager.js +7 -5
- package/src/TimerUtils.js +1 -0
- package/src/WebDHT.js +125 -123
- package/websocket-server/server.js +31 -103
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PeerPigeon Debug Configuration Example
|
|
3
|
+
*
|
|
4
|
+
* This example shows how to configure the debug logging system
|
|
5
|
+
* to control console output from PeerPigeon modules.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { PeerPigeonMesh, DebugLogger } from './index.js';
|
|
9
|
+
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// Debug Configuration Examples
|
|
12
|
+
// ============================================================================
|
|
13
|
+
|
|
14
|
+
// 1. Enable all debugging (shows all console output)
|
|
15
|
+
// DebugLogger.enableAll();
|
|
16
|
+
|
|
17
|
+
// 2. Enable specific modules only
|
|
18
|
+
DebugLogger.enableModules([
|
|
19
|
+
'GossipManager', // Message propagation debugging
|
|
20
|
+
'ConnectionManager', // Connection management debugging
|
|
21
|
+
'PeerConnection' // WebRTC connection debugging
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
// 3. Enable individual modules
|
|
25
|
+
// DebugLogger.enable('PeerPigeonMesh');
|
|
26
|
+
// DebugLogger.enable('SignalingHandler');
|
|
27
|
+
// DebugLogger.enable('WebDHT');
|
|
28
|
+
|
|
29
|
+
// 4. Disable all debugging (default state - no console output)
|
|
30
|
+
// DebugLogger.disableAll();
|
|
31
|
+
|
|
32
|
+
// 5. Disable specific modules
|
|
33
|
+
// DebugLogger.disable('DistributedStorageManager');
|
|
34
|
+
// DebugLogger.disable('CryptoManager');
|
|
35
|
+
|
|
36
|
+
// 6. Configure using options object
|
|
37
|
+
DebugLogger.configure({
|
|
38
|
+
// First disable all, then enable specific ones
|
|
39
|
+
disableAll: true,
|
|
40
|
+
enable: [
|
|
41
|
+
'PeerPigeonMesh',
|
|
42
|
+
'GossipManager',
|
|
43
|
+
'ConnectionManager'
|
|
44
|
+
]
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// ============================================================================
|
|
48
|
+
// Create and Use PeerPigeon with Debug Configuration
|
|
49
|
+
// ============================================================================
|
|
50
|
+
|
|
51
|
+
const mesh = new PeerPigeonMesh({
|
|
52
|
+
signalingUrl: 'ws://localhost:3000',
|
|
53
|
+
minPeers: 2,
|
|
54
|
+
maxPeers: 10,
|
|
55
|
+
autoConnect: true
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ============================================================================
|
|
59
|
+
// Runtime Debug Control Examples
|
|
60
|
+
// ============================================================================
|
|
61
|
+
|
|
62
|
+
// You can change debug settings at runtime
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
console.log('Current debug state:', DebugLogger.getState());
|
|
65
|
+
|
|
66
|
+
// Enable more debugging after 5 seconds
|
|
67
|
+
DebugLogger.enable('WebDHT');
|
|
68
|
+
DebugLogger.enable('DistributedStorageManager');
|
|
69
|
+
|
|
70
|
+
console.log('Available modules:', DebugLogger.getModules());
|
|
71
|
+
}, 5000);
|
|
72
|
+
|
|
73
|
+
// ============================================================================
|
|
74
|
+
// Event Handlers (these will show debug output based on configuration)
|
|
75
|
+
// ============================================================================
|
|
76
|
+
|
|
77
|
+
mesh.on('ready', (peerId) => {
|
|
78
|
+
console.log(`š Mesh ready! Peer ID: ${peerId}`);
|
|
79
|
+
|
|
80
|
+
// Test different functionality that will show debug output
|
|
81
|
+
mesh.broadcastMessage('Hello from debug example!');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
mesh.on('peerConnected', (peerId) => {
|
|
85
|
+
console.log(`š Peer connected: ${peerId}`);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
mesh.on('messageReceived', (message) => {
|
|
89
|
+
console.log('šØ Message received:', message);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// Start the mesh
|
|
93
|
+
mesh.connect();
|
|
94
|
+
|
|
95
|
+
// ============================================================================
|
|
96
|
+
// Example Debug Output Control in Production
|
|
97
|
+
// ============================================================================
|
|
98
|
+
|
|
99
|
+
// For production, you might want to conditionally enable debugging
|
|
100
|
+
const isDevelopment = process.env.NODE_ENV === 'development';
|
|
101
|
+
const debugFromQuery = new URLSearchParams(window?.location?.search || '').get('debug');
|
|
102
|
+
|
|
103
|
+
if (isDevelopment || debugFromQuery) {
|
|
104
|
+
// Enable debugging in development or when ?debug=true
|
|
105
|
+
if (debugFromQuery) {
|
|
106
|
+
// Parse debug modules from query: ?debug=GossipManager,ConnectionManager
|
|
107
|
+
const modules = debugFromQuery.split(',').filter(m => m && m !== 'true');
|
|
108
|
+
if (modules.length > 0) {
|
|
109
|
+
DebugLogger.enableModules(modules);
|
|
110
|
+
} else {
|
|
111
|
+
DebugLogger.enableAll();
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
DebugLogger.enableAll();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
console.log('š Debug mode enabled:', DebugLogger.getState());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ============================================================================
|
|
121
|
+
// Available Modules for Debugging
|
|
122
|
+
// ============================================================================
|
|
123
|
+
|
|
124
|
+
/*
|
|
125
|
+
Available debug modules:
|
|
126
|
+
|
|
127
|
+
Core Components:
|
|
128
|
+
- PeerPigeonMesh - Main mesh instance and lifecycle
|
|
129
|
+
- ConnectionManager - Peer connection management
|
|
130
|
+
- PeerConnection - Individual WebRTC connections
|
|
131
|
+
- SignalingHandler - Signaling message processing
|
|
132
|
+
- SignalingClient - WebSocket signaling client
|
|
133
|
+
|
|
134
|
+
Networking & Discovery:
|
|
135
|
+
- GossipManager - Message propagation and gossip protocol
|
|
136
|
+
- PeerDiscovery - Peer discovery and announcement
|
|
137
|
+
- MeshOptimizer - Network topology optimization
|
|
138
|
+
- EvictionManager - Peer eviction strategies
|
|
139
|
+
|
|
140
|
+
Data & Storage:
|
|
141
|
+
- WebDHT - Distributed hash table operations
|
|
142
|
+
- DistributedStorageManager - P2P storage layer
|
|
143
|
+
- StorageManager - Local storage management
|
|
144
|
+
|
|
145
|
+
Security & Media:
|
|
146
|
+
- CryptoManager - End-to-end encryption
|
|
147
|
+
- MediaManager - Audio/video stream management
|
|
148
|
+
|
|
149
|
+
Utilities:
|
|
150
|
+
- CleanupManager - Resource cleanup and garbage collection
|
|
151
|
+
- EnvironmentDetector - Runtime environment detection
|
|
152
|
+
- EventEmitter - Event system
|
|
153
|
+
- TimerUtils - Timer utilities
|
|
154
|
+
|
|
155
|
+
Usage patterns:
|
|
156
|
+
|
|
157
|
+
// Debug everything
|
|
158
|
+
DebugLogger.enableAll();
|
|
159
|
+
|
|
160
|
+
// Debug networking only
|
|
161
|
+
DebugLogger.enableModules(['GossipManager', 'ConnectionManager', 'PeerConnection']);
|
|
162
|
+
|
|
163
|
+
// Debug storage only
|
|
164
|
+
DebugLogger.enableModules(['WebDHT', 'DistributedStorageManager', 'StorageManager']);
|
|
165
|
+
|
|
166
|
+
// Debug security only
|
|
167
|
+
DebugLogger.enableModules(['CryptoManager']);
|
|
168
|
+
|
|
169
|
+
// Production - no debugging (default)
|
|
170
|
+
DebugLogger.disableAll();
|
|
171
|
+
*/
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Node.js Examples for PeerPigeon
|
|
2
|
+
|
|
3
|
+
This directory contains Node.js examples demonstrating various PeerPigeon features.
|
|
4
|
+
|
|
5
|
+
## Examples
|
|
6
|
+
|
|
7
|
+
### Basic Storage Example (`basic-storage-example.js`)
|
|
8
|
+
|
|
9
|
+
A simple demonstration of core storage operations perfect for getting started:
|
|
10
|
+
|
|
11
|
+
- Store, retrieve, update, and delete data
|
|
12
|
+
- List all stored keys
|
|
13
|
+
- View storage statistics
|
|
14
|
+
- Clean shutdown handling
|
|
15
|
+
|
|
16
|
+
#### Running the Basic Example
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
cd examples/node
|
|
20
|
+
npm run basic-example
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Storage Test (`storage-test.js`)
|
|
24
|
+
|
|
25
|
+
A comprehensive test suite that demonstrates all distributed storage operations:
|
|
26
|
+
|
|
27
|
+
- **Basic Operations**: Store, retrieve, update, delete
|
|
28
|
+
- **Encryption & Access Control**: Private encrypted data, public data, peer access lists
|
|
29
|
+
- **Advanced Features**: Immutable data, CRDT support, TTL expiration
|
|
30
|
+
- **Bulk Operations**: Bulk store, retrieve, and delete
|
|
31
|
+
- **Search**: Search by key patterns, value content, and metadata
|
|
32
|
+
- **Management**: Key listing, statistics, backup/restore
|
|
33
|
+
- **Configuration**: Enable/disable storage, cleanup operations
|
|
34
|
+
|
|
35
|
+
#### Running the Storage Test
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
cd examples/node
|
|
39
|
+
npm run storage-test
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or directly:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
node storage-test.js
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Expected Output
|
|
49
|
+
|
|
50
|
+
The test will run through all storage operations and provide detailed output:
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
š Initializing PeerPigeon Mesh with Storage...
|
|
54
|
+
ā
Mesh initialized with peer ID: abcd1234...
|
|
55
|
+
š¦ Storage enabled: true
|
|
56
|
+
|
|
57
|
+
š§Ŗ Running test: Basic Store and Retrieve
|
|
58
|
+
Stored and retrieved: {"message":"Hello, World!","timestamp":1234567890}
|
|
59
|
+
ā
Test passed: Basic Store and Retrieve
|
|
60
|
+
|
|
61
|
+
š§Ŗ Running test: Update Operation
|
|
62
|
+
Updated: {"counter":1,"name":"original"} ā {"counter":2,"name":"updated"}
|
|
63
|
+
ā
Test passed: Update Operation
|
|
64
|
+
|
|
65
|
+
... (continues with all tests)
|
|
66
|
+
|
|
67
|
+
š TEST SUMMARY
|
|
68
|
+
================================================
|
|
69
|
+
Total Tests: 18
|
|
70
|
+
ā
Passed: 18
|
|
71
|
+
ā Failed: 0
|
|
72
|
+
Success Rate: 100.0%
|
|
73
|
+
|
|
74
|
+
š All tests passed! The Distributed Storage system is working perfectly.
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Basic Example (`basic-example.js`)
|
|
78
|
+
|
|
79
|
+
A simple example showing basic PeerPigeon mesh functionality (to be created).
|
|
80
|
+
|
|
81
|
+
## Requirements
|
|
82
|
+
|
|
83
|
+
- Node.js 16.0.0 or higher
|
|
84
|
+
- ES Modules support (using `"type": "module"` in package.json)
|
|
85
|
+
|
|
86
|
+
## Features Demonstrated
|
|
87
|
+
|
|
88
|
+
The examples show how to use PeerPigeon's distributed storage system with:
|
|
89
|
+
|
|
90
|
+
1. **WebDHT Integration**: Distributed hash table for data persistence
|
|
91
|
+
2. **Encryption**: Automatic encryption for private data using the crypto manager
|
|
92
|
+
3. **Access Control**: Fine-grained permissions for data access
|
|
93
|
+
4. **CRDT Support**: Conflict-free replicated data types for collaborative editing
|
|
94
|
+
5. **Search Capabilities**: Flexible search across keys, values, and metadata
|
|
95
|
+
6. **Bulk Operations**: Efficient batch processing of multiple items
|
|
96
|
+
7. **Backup/Restore**: Data portability and recovery features
|
|
97
|
+
|
|
98
|
+
## Architecture
|
|
99
|
+
|
|
100
|
+
Each example initializes a PeerPigeon mesh with the required features:
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
const mesh = new PeerPigeonMesh({
|
|
104
|
+
enableWebDHT: true, // Distributed hash table
|
|
105
|
+
enableCrypto: true, // Encryption/decryption
|
|
106
|
+
enableDistributedStorage: true // Optional storage layer
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The distributed storage manager provides a high-level API over the WebDHT with additional features like encryption, access control, and search capabilities.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Basic Distributed Storage Example for PeerPigeon
|
|
4
|
+
*
|
|
5
|
+
* This example demonstrates simple usage of the distributed storage layer.
|
|
6
|
+
* For comprehensive testing, see storage-test.js
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PeerPigeonMesh } from '../../src/PeerPigeonMesh.js';
|
|
10
|
+
|
|
11
|
+
async function basicStorageExample() {
|
|
12
|
+
console.log('š¦ PeerPigeon Basic Storage Example');
|
|
13
|
+
console.log('=====================================\n');
|
|
14
|
+
|
|
15
|
+
// Initialize the mesh network with storage enabled
|
|
16
|
+
const mesh = new PeerPigeonMesh({
|
|
17
|
+
enableStorage: true,
|
|
18
|
+
enableWebDHT: true,
|
|
19
|
+
enableCrypto: true,
|
|
20
|
+
nodeId: 'basic-example-node'
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// Wait a moment for initialization
|
|
25
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
26
|
+
|
|
27
|
+
console.log('1. Storing some data...');
|
|
28
|
+
await mesh.storageManager.store('user:123', { name: 'Alice', age: 30 });
|
|
29
|
+
await mesh.storageManager.store('config:theme', 'dark');
|
|
30
|
+
await mesh.storageManager.store('counter', 42);
|
|
31
|
+
console.log(' ā
Data stored successfully\n');
|
|
32
|
+
|
|
33
|
+
console.log('2. Retrieving data...');
|
|
34
|
+
const user = await mesh.storageManager.retrieve('user:123');
|
|
35
|
+
const theme = await mesh.storageManager.retrieve('config:theme');
|
|
36
|
+
const counter = await mesh.storageManager.retrieve('counter');
|
|
37
|
+
|
|
38
|
+
console.log(` User: ${JSON.stringify(user)}`);
|
|
39
|
+
console.log(` Theme: ${theme}`);
|
|
40
|
+
console.log(` Counter: ${counter}\n`);
|
|
41
|
+
|
|
42
|
+
console.log('3. Updating data...');
|
|
43
|
+
await mesh.storageManager.update('counter', 100);
|
|
44
|
+
const updatedCounter = await mesh.storageManager.retrieve('counter');
|
|
45
|
+
console.log(` Updated counter: ${updatedCounter}\n`);
|
|
46
|
+
|
|
47
|
+
console.log('4. Listing all keys...');
|
|
48
|
+
const keys = await mesh.storageManager.listKeys();
|
|
49
|
+
console.log(` Found keys: ${keys.join(', ')}\n`);
|
|
50
|
+
|
|
51
|
+
console.log('5. Getting storage statistics...');
|
|
52
|
+
const stats = await mesh.storageManager.getStats();
|
|
53
|
+
console.log(` Total items: ${stats.itemCount}`);
|
|
54
|
+
console.log(` Storage size: ${stats.totalSize} bytes\n`);
|
|
55
|
+
|
|
56
|
+
console.log('6. Cleaning up...');
|
|
57
|
+
await mesh.storageManager.delete('user:123');
|
|
58
|
+
await mesh.storageManager.delete('config:theme');
|
|
59
|
+
await mesh.storageManager.delete('counter');
|
|
60
|
+
console.log(' ā
Cleanup complete\n');
|
|
61
|
+
|
|
62
|
+
console.log('š Basic storage example completed successfully!');
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error('ā Error in basic storage example:', error.message);
|
|
65
|
+
console.error(error.stack);
|
|
66
|
+
} finally {
|
|
67
|
+
// Clean shutdown
|
|
68
|
+
if (mesh.cleanup) {
|
|
69
|
+
await mesh.cleanup();
|
|
70
|
+
}
|
|
71
|
+
process.exit(0);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Handle process termination gracefully
|
|
76
|
+
process.on('SIGINT', () => {
|
|
77
|
+
console.log('\nš Shutting down gracefully...');
|
|
78
|
+
process.exit(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
82
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
83
|
+
process.exit(1);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Run the example
|
|
87
|
+
basicStorageExample().catch(error => {
|
|
88
|
+
console.error('Fatal error:', error);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "peerpigeon-node-examples",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Node.js examples for PeerPigeon distributed mesh networking",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"storage-test": "node storage-test.js",
|
|
8
|
+
"basic-example": "node basic-storage-example.js",
|
|
9
|
+
"test": "node storage-test.js"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=16.0.0"
|
|
14
|
+
},
|
|
15
|
+
"author": "PeerPigeon Contributors",
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|