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.
Files changed (72) hide show
  1. package/README.md +141 -1365
  2. package/cli.js +1382 -0
  3. package/dist/peerpigeon-browser.js +12290 -0
  4. package/dist/peerpigeon-browser.js.map +7 -0
  5. package/docs/API_DOCUMENTATION.md +1945 -0
  6. package/docs/BOOTSTRAP_HUBS.md +319 -0
  7. package/docs/CLI_README.md +371 -0
  8. package/docs/DOCUMENTATION_REORGANIZATION.md +172 -0
  9. package/docs/HUB_QUICK_REF.md +107 -0
  10. package/docs/HUB_SCRIPTS.md +331 -0
  11. package/docs/HUB_SYSTEM.md +552 -0
  12. package/docs/MIGRATION_COMPLETE.md +93 -0
  13. package/docs/NETWORK_NAMESPACES.md +212 -0
  14. package/docs/README.md +107 -0
  15. package/docs/SELECTIVE_STREAMING_GUIDE.md +427 -0
  16. package/docs/TEST_CLEANUP_COMPLETE.md +80 -0
  17. package/examples/bootstrap-hub-example.js +292 -0
  18. package/examples/browser/README.md +278 -0
  19. package/examples/browser/app.js +2426 -49
  20. package/examples/browser/index.html +482 -543
  21. package/examples/browser/minimal/README.md +55 -0
  22. package/examples/browser/minimal/index.html +75 -0
  23. package/examples/browser/minimal/minimal.js +234 -0
  24. package/examples/browser/styles.css +687 -1425
  25. package/examples/browser/test-bundle.html +35 -0
  26. package/examples/browser-2/index.html +30 -0
  27. package/examples/browser-2/package.json +21 -0
  28. package/examples/browser-2/src/App-simple.vue +21 -0
  29. package/examples/browser-2/src/App.vue +420 -0
  30. package/examples/browser-2/src/components/CryptoView.vue +1661 -0
  31. package/examples/browser-2/src/components/DHTView.vue +1281 -0
  32. package/examples/browser-2/src/components/DebugView.vue +1773 -0
  33. package/examples/browser-2/src/components/MediaView.vue +1048 -0
  34. package/examples/browser-2/src/components/MessagingView.vue +775 -0
  35. package/examples/browser-2/src/components/NetworkView.vue +930 -0
  36. package/examples/browser-2/src/components/StorageView.vue +1376 -0
  37. package/examples/browser-2/src/main.js +5 -0
  38. package/examples/browser-2/src/stores/peerpigeon.js +895 -0
  39. package/examples/browser-2/vite.config.js +6 -0
  40. package/examples/hub-example.js +186 -0
  41. package/examples/multi-hub-test.js +186 -0
  42. package/examples/node/README.md +2 -2
  43. package/examples/node/storage-test.js +141 -6
  44. package/examples/selective-streaming-demo.html +698 -0
  45. package/examples/standard-events-demo.js +126 -0
  46. package/package.json +33 -16
  47. package/server/index.js +1032 -98
  48. package/server/start.js +49 -0
  49. package/src/ConnectionManager.js +239 -225
  50. package/src/CryptoManager.js +147 -18
  51. package/src/DebugLogger.js +0 -7
  52. package/src/DistributedStorageManager.js +245 -205
  53. package/src/EnvironmentDetector.js +109 -2
  54. package/src/EventEmitter.js +79 -1
  55. package/src/EvictionManager.js +10 -9
  56. package/src/GossipManager.js +163 -11
  57. package/src/LexicalStorageInterface.js +332 -0
  58. package/src/PeerConnection.js +333 -310
  59. package/src/PeerPigeonMesh.js +577 -135
  60. package/src/SignalingClient.js +14 -243
  61. package/src/SignalingHandler.js +31 -10
  62. package/src/StorageManager.js +0 -39
  63. package/src/WebDHT-Simple.js +389 -0
  64. package/src/WebDHT.js +363 -1165
  65. package/src/browser-entry.js +40 -0
  66. package/examples/browser/assets/images/favicon.png +0 -0
  67. package/examples/browser/assets/images/pigeon.png +0 -0
  68. package/examples/browser/crypto-test.html +0 -427
  69. package/examples/browser/ui-old.js +0 -1926
  70. package/examples/browser/ui.js +0 -3010
  71. package/websocket-server/README.md +0 -247
  72. package/websocket-server/server.js +0 -391
@@ -0,0 +1,332 @@
1
+ import { EventEmitter } from './EventEmitter.js';
2
+ import DebugLogger from './DebugLogger.js';
3
+
4
+ /**
5
+ * Lexical Storage Interface - GUN-like chaining API for DistributedStorageManager
6
+ *
7
+ * Usage:
8
+ * const user = storage.get('users').get('alice');
9
+ * await user.put({name: 'Alice', age: 30});
10
+ * const name = await user.get('name').val();
11
+ *
12
+ * user.get('friends').set({bob: true, charlie: true});
13
+ * user.get('friends').map().on((friend, key) => console.log(key, friend));
14
+ */
15
+ export class LexicalStorageInterface extends EventEmitter {
16
+ constructor(distributedStorage, path = []) {
17
+ super();
18
+ this.debug = DebugLogger.create('LexicalStorageInterface');
19
+ this.storage = distributedStorage;
20
+ this.path = path.slice(); // Create a copy of the path
21
+ this.subscriptions = new Map();
22
+ this._isMap = false;
23
+ }
24
+
25
+ /**
26
+ * Navigate to a key (creates a new interface instance)
27
+ * @param {string} key - The key to navigate to
28
+ * @returns {LexicalStorageInterface} New interface for the key
29
+ */
30
+ get(key) {
31
+ const newInterface = new LexicalStorageInterface(this.storage, [...this.path, key]);
32
+ return newInterface;
33
+ }
34
+
35
+ /**
36
+ * Store data at the current path
37
+ * @param {any} value - Value to store
38
+ * @param {Object} options - Storage options
39
+ * @returns {Promise<LexicalStorageInterface>} This interface for chaining
40
+ */
41
+ async put(value, options = {}) {
42
+ const fullKey = this.path.join(':');
43
+
44
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
45
+ // For objects, store each property as a separate key
46
+ const promises = Object.entries(value).map(([prop, val]) => {
47
+ const propKey = `${fullKey}:${prop}`;
48
+ return this.storage.store(propKey, val, options);
49
+ });
50
+ await Promise.all(promises);
51
+
52
+ // Also store the object structure for reference
53
+ await this.storage.store(fullKey, { _keys: Object.keys(value), _type: 'object' }, options);
54
+ } else {
55
+ // For primitives, store directly
56
+ await this.storage.store(fullKey, value, options);
57
+ }
58
+
59
+ // Emit change event
60
+ this.emit('put', { key: fullKey, value });
61
+
62
+ return this;
63
+ }
64
+
65
+ /**
66
+ * Retrieve the current value
67
+ * @param {Object} options - Retrieval options
68
+ * @returns {Promise<any>} The stored value
69
+ */
70
+ async val(options = {}) {
71
+ const fullKey = this.path.join(':');
72
+ const value = await this.storage.retrieve(fullKey, options);
73
+
74
+ // If it's an object structure, reconstruct it
75
+ if (value && typeof value === 'object' && value._keys && value._type === 'object') {
76
+ const reconstructed = {};
77
+ const promises = value._keys.map(async (key) => {
78
+ const keyValue = await this.storage.retrieve(`${fullKey}:${key}`, options);
79
+ reconstructed[key] = keyValue;
80
+ });
81
+ await Promise.all(promises);
82
+ return reconstructed;
83
+ }
84
+
85
+ return value;
86
+ }
87
+
88
+ /**
89
+ * Set multiple key-value pairs (like GUN's set)
90
+ * @param {Object} obj - Object with key-value pairs to set
91
+ * @param {Object} options - Storage options
92
+ * @returns {Promise<LexicalStorageInterface>} This interface for chaining
93
+ */
94
+ async set(obj, options = {}) {
95
+ const fullKey = this.path.join(':');
96
+
97
+ // Store each entry with a unique ID
98
+ const promises = Object.entries(obj).map(([key, value]) => {
99
+ const setKey = `${fullKey}:${key}`;
100
+ return this.storage.store(setKey, value, options);
101
+ });
102
+
103
+ await Promise.all(promises);
104
+
105
+ // Update the set index
106
+ const existingSet = await this.storage.retrieve(`${fullKey}:_set`) || {};
107
+ const updatedSet = { ...existingSet, ...obj };
108
+ await this.storage.store(`${fullKey}:_set`, updatedSet, options);
109
+
110
+ // Emit set event
111
+ this.emit('set', { key: fullKey, values: obj });
112
+
113
+ return this;
114
+ }
115
+
116
+ /**
117
+ * Map over a set (like GUN's map)
118
+ * @returns {LexicalStorageInterface} New interface for mapping
119
+ */
120
+ map() {
121
+ const mapInterface = new LexicalStorageInterface(this.storage, this.path);
122
+ mapInterface._isMap = true;
123
+ return mapInterface;
124
+ }
125
+
126
+ /**
127
+ * Subscribe to changes (like GUN's on)
128
+ * @param {Function} callback - Callback for changes
129
+ * @returns {Function} Unsubscribe function
130
+ */
131
+ on(callback) {
132
+ const fullKey = this.path.join(':');
133
+
134
+ if (this._isMap) {
135
+ // For maps, listen to set changes
136
+ const setKey = `${fullKey}:_set`;
137
+
138
+ const handleChange = async () => {
139
+ const setData = await this.storage.retrieve(setKey);
140
+ if (setData) {
141
+ Object.entries(setData).forEach(([key, value]) => {
142
+ callback(value, key);
143
+ });
144
+ }
145
+ };
146
+
147
+ // Subscribe to the set key using storage's subscription mechanism
148
+ if (this.storage.subscribe) {
149
+ this.storage.subscribe(setKey).then(() => {
150
+ this.storage.on('dataUpdated', (event) => {
151
+ if (event.key === setKey) {
152
+ handleChange();
153
+ }
154
+ });
155
+ });
156
+ }
157
+
158
+ // Initial call
159
+ handleChange();
160
+
161
+ return () => {
162
+ if (this.storage.unsubscribe) {
163
+ this.storage.unsubscribe(setKey);
164
+ }
165
+ };
166
+ } else {
167
+ // For regular keys, listen to direct changes
168
+ if (this.storage.subscribe) {
169
+ this.storage.subscribe(fullKey).then((currentValue) => {
170
+ callback(currentValue, fullKey);
171
+ });
172
+
173
+ this.storage.on('dataUpdated', (event) => {
174
+ if (event.key === fullKey) {
175
+ this.storage.retrieve(fullKey).then(value => {
176
+ callback(value, fullKey);
177
+ });
178
+ }
179
+ });
180
+ }
181
+
182
+ return () => {
183
+ if (this.storage.unsubscribe) {
184
+ this.storage.unsubscribe(fullKey);
185
+ }
186
+ };
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Once - listen for a single change
192
+ * @param {Function} callback - Callback for the change
193
+ * @returns {Promise} Promise that resolves with the value
194
+ */
195
+ async once(callback) {
196
+ return new Promise((resolve) => {
197
+ const unsubscribe = this.on((value, key) => {
198
+ if (callback) callback(value, key);
199
+ unsubscribe();
200
+ resolve(value);
201
+ });
202
+ });
203
+ }
204
+
205
+ /**
206
+ * Delete the current path
207
+ * @returns {Promise<boolean>} Success status
208
+ */
209
+ async delete() {
210
+ const fullKey = this.path.join(':');
211
+
212
+ // If it's an object, delete all its properties first
213
+ const value = await this.storage.retrieve(fullKey);
214
+ if (value && typeof value === 'object' && value._keys && value._type === 'object') {
215
+ const deletePromises = value._keys.map(key => {
216
+ const propKey = `${fullKey}:${key}`;
217
+ return this.storage.delete(propKey);
218
+ });
219
+ await Promise.all(deletePromises);
220
+ }
221
+
222
+ const result = await this.storage.delete(fullKey);
223
+
224
+ // Emit delete event
225
+ this.emit('delete', { key: fullKey });
226
+
227
+ return result;
228
+ }
229
+
230
+ /**
231
+ * Update with merge semantics
232
+ * @param {any} value - Value to merge
233
+ * @param {Object} options - Update options
234
+ * @returns {Promise<LexicalStorageInterface>} This interface for chaining
235
+ */
236
+ async update(value, options = {}) {
237
+ const fullKey = this.path.join(':');
238
+ const currentValue = await this.storage.retrieve(fullKey);
239
+
240
+ let mergedValue;
241
+ if (typeof value === 'object' && value !== null && !Array.isArray(value) &&
242
+ typeof currentValue === 'object' && currentValue !== null && !Array.isArray(currentValue)) {
243
+ // Handle object reconstruction first
244
+ if (currentValue._keys && currentValue._type === 'object') {
245
+ const reconstructed = {};
246
+ const promises = currentValue._keys.map(async (key) => {
247
+ const keyValue = await this.storage.retrieve(`${fullKey}:${key}`, options);
248
+ reconstructed[key] = keyValue;
249
+ });
250
+ await Promise.all(promises);
251
+ mergedValue = { ...reconstructed, ...value };
252
+ } else {
253
+ mergedValue = { ...currentValue, ...value };
254
+ }
255
+ } else {
256
+ mergedValue = value;
257
+ }
258
+
259
+ // Use put instead of update to ensure proper object decomposition
260
+ await this.put(mergedValue, options);
261
+
262
+ // Emit update event
263
+ this.emit('update', { key: fullKey, value: mergedValue });
264
+
265
+ return this;
266
+ }
267
+
268
+ /**
269
+ * Create a proxied interface that allows property access
270
+ * @returns {Proxy} Proxied interface
271
+ */
272
+ proxy() {
273
+ return new Proxy(this, {
274
+ get(target, prop) {
275
+ // If it's a method, return it bound
276
+ if (typeof target[prop] === 'function') {
277
+ return target[prop].bind(target);
278
+ }
279
+
280
+ // If it's a property access, create a new get() call
281
+ if (typeof prop === 'string' && !prop.startsWith('_') && prop !== 'constructor') {
282
+ return target.get(prop).proxy();
283
+ }
284
+
285
+ return target[prop];
286
+ }
287
+ });
288
+ }
289
+
290
+ /**
291
+ * Get the current path as a string
292
+ * @returns {string} The path
293
+ */
294
+ getPath() {
295
+ return this.path.join(':');
296
+ }
297
+
298
+ /**
299
+ * Check if a value exists at the current path
300
+ * @returns {Promise<boolean>} Whether the value exists
301
+ */
302
+ async exists() {
303
+ const fullKey = this.path.join(':');
304
+ const value = await this.storage.retrieve(fullKey);
305
+ return value !== null && value !== undefined;
306
+ }
307
+
308
+ /**
309
+ * Get all keys under the current path (for object-like structures)
310
+ * @returns {Promise<string[]>} Array of keys
311
+ */
312
+ async keys() {
313
+ const fullKey = this.path.join(':');
314
+ const value = await this.storage.retrieve(fullKey);
315
+
316
+ if (value && typeof value === 'object' && value._keys && value._type === 'object') {
317
+ return value._keys;
318
+ }
319
+
320
+ return [];
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Create a lexical interface for the distributed storage
326
+ * @param {DistributedStorageManager} distributedStorage
327
+ * @returns {Proxy} Proxied lexical interface
328
+ */
329
+ export function createLexicalInterface(distributedStorage) {
330
+ const lexicalInterface = new LexicalStorageInterface(distributedStorage);
331
+ return lexicalInterface.proxy();
332
+ }