peerpigeon 0.0.7 โ†’ 0.0.8

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.
@@ -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
+ }