peerpigeon 0.0.6 → 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.
package/src/WebDHT.js CHANGED
@@ -22,6 +22,9 @@ export class WebDHT extends EventEmitter {
22
22
  // Subscriptions: key -> Set of peer IDs that want notifications
23
23
  this.subscriptions = new Map();
24
24
 
25
+ // Pending subscriptions for keys that don't exist yet: keyId -> Set of peer IDs
26
+ this.pendingSubscriptions = new Map();
27
+
25
28
  // Active subscriptions from this peer to others
26
29
  this.mySubscriptions = new Set();
27
30
 
@@ -139,10 +142,42 @@ export class WebDHT extends EventEmitter {
139
142
  publisher: this.peerId
140
143
  };
141
144
 
145
+ // Check if this is a new key (for pending subscription activation)
146
+ const isNewKey = !this.localStorage.has(keyId);
147
+
142
148
  // CRITICAL FIX: Always store locally first (the originator always keeps a copy)
143
149
  this.storeLocally(keyId, storeData);
144
150
  console.log(`DHT PUT: Stored locally: ${key}`);
145
151
 
152
+ // If this is a new key, activate any pending subscriptions
153
+ if (isNewKey) {
154
+ const activatedSubscribers = this.activatePendingSubscriptions(keyId);
155
+
156
+ // Notify locally activated subscribers about the new key
157
+ if (activatedSubscribers.length > 0) {
158
+ console.log(`DHT PUT: Notifying ${activatedSubscribers.length} locally pending subscribers about new key ${key}`);
159
+
160
+ const notificationPromises = activatedSubscribers.map(async (subscriberId) => {
161
+ if (subscriberId !== this.peerId) {
162
+ try {
163
+ return await this.sendDHTMessage(subscriberId, 'value_changed', {
164
+ key,
165
+ keyId,
166
+ newValue: value,
167
+ timestamp: storeData.timestamp,
168
+ isNewKey: true
169
+ });
170
+ } catch (error) {
171
+ console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
172
+ return null;
173
+ }
174
+ }
175
+ }).filter(Boolean);
176
+
177
+ await Promise.allSettled(notificationPromises);
178
+ }
179
+ }
180
+
146
181
  // AUTO-SUBSCRIBE: When putting a value, automatically subscribe to future changes
147
182
  this.mySubscriptions.add(keyId);
148
183
  console.log(`DHT PUT: Auto-subscribed to ${key} for future updates`);
@@ -159,19 +194,21 @@ export class WebDHT extends EventEmitter {
159
194
  */
160
195
  async get(key, options = {}) {
161
196
  const keyId = await this.generateKeyId(key);
162
- // ALWAYS subscribe when doing a get() - this is the desired behavior
163
- const subscribe = true;
197
+ // Subscribe unless explicitly disabled or skipSubscribe is true
198
+ const subscribe = options.skipSubscribe ? false : (options.subscribe !== false);
164
199
  const forceRefresh = options.forceRefresh || false;
165
200
 
166
- console.log(`DHT GET: ${key} -> ${keyId.substring(0, 8)}... (auto-subscribing, forceRefresh: ${forceRefresh})`);
201
+ console.log(`DHT GET: ${key} -> ${keyId.substring(0, 8)}... (auto-subscribing: ${subscribe}, forceRefresh: ${forceRefresh})`);
167
202
 
168
203
  // Check local storage FIRST (unless force refresh is requested)
169
204
  if (!forceRefresh && this.localStorage.has(keyId)) {
170
205
  const data = this.localStorage.get(keyId);
171
206
  if (!this.isExpired(data)) {
172
207
  console.log(`DHT GET: Found locally: ${key} (using cached data)`);
173
- // Always add subscription even for local data
174
- this.mySubscriptions.add(keyId);
208
+ // Add subscription even for local data (unless skipSubscribe is true)
209
+ if (subscribe) {
210
+ this.mySubscriptions.add(keyId);
211
+ }
175
212
  return data.value;
176
213
  } else {
177
214
  // Remove expired data
@@ -192,7 +229,7 @@ export class WebDHT extends EventEmitter {
192
229
 
193
230
  /**
194
231
  * Subscribe to changes for a key
195
- * NOTE: get() operations automatically subscribe, so this is mainly for explicit subscription without retrieval
232
+ * NOTE: This now supports subscribing to keys that don't exist yet
196
233
  */
197
234
  async subscribe(key) {
198
235
  const keyId = await this.generateKeyId(key);
@@ -202,8 +239,39 @@ export class WebDHT extends EventEmitter {
202
239
  // Add to our subscriptions immediately
203
240
  this.mySubscriptions.add(keyId);
204
241
 
205
- // Try to get the current value (which will also ensure we're subscribed on storage peers)
206
- const currentValue = await this.get(key);
242
+ // Find the peers responsible for storing this key
243
+ const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
244
+
245
+ // Send subscription requests to all potential storage peers
246
+ const subscribePromises = closestPeers.map(async (peerId) => {
247
+ if (peerId !== this.peerId) {
248
+ try {
249
+ return await this.sendDHTMessage(peerId, 'subscribe', {
250
+ keyId,
251
+ key,
252
+ subscriber: this.peerId
253
+ });
254
+ } catch (error) {
255
+ console.warn(`DHT SUBSCRIBE: Failed to subscribe to peer ${peerId.substring(0, 8)}:`, error.message);
256
+ return null;
257
+ }
258
+ }
259
+ }).filter(Boolean);
260
+
261
+ await Promise.allSettled(subscribePromises);
262
+
263
+ // Also add ourselves to pending subscriptions if we're among the closest
264
+ if (this.isAmongClosest(keyId, closestPeers)) {
265
+ this.addPendingSubscription(keyId, this.peerId);
266
+ }
267
+
268
+ // Try to get the current value (if it exists)
269
+ let currentValue = null;
270
+ try {
271
+ currentValue = await this.get(key, { skipSubscribe: true }); // Skip auto-subscribe since we're already subscribed
272
+ } catch (error) {
273
+ console.log(`DHT SUBSCRIBE: Key ${key} doesn't exist yet, but subscription is active for when it's created`);
274
+ }
207
275
 
208
276
  console.log(`DHT SUBSCRIBE: Subscribed to ${key}, current value:`, currentValue);
209
277
  return currentValue;
@@ -243,6 +311,14 @@ export class WebDHT extends EventEmitter {
243
311
  }
244
312
  }
245
313
 
314
+ // Remove local pending subscription if we're storing this key
315
+ if (this.pendingSubscriptions.has(keyId)) {
316
+ this.pendingSubscriptions.get(keyId).delete(this.peerId);
317
+ if (this.pendingSubscriptions.get(keyId).size === 0) {
318
+ this.pendingSubscriptions.delete(keyId);
319
+ }
320
+ }
321
+
246
322
  console.log(`DHT UNSUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
247
323
  }
248
324
 
@@ -390,8 +466,40 @@ export class WebDHT extends EventEmitter {
390
466
  const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
391
467
 
392
468
  if (this.isAmongClosest(keyId, closestPeers)) {
469
+ // Check if this is a new key (for pending subscription activation)
470
+ const isNewKey = !this.localStorage.has(keyId);
471
+
393
472
  this.storeLocally(keyId, { key, value, timestamp, ttl, publisher });
394
473
 
474
+ // If this is a new key, activate any pending subscriptions
475
+ if (isNewKey) {
476
+ const activatedSubscribers = this.activatePendingSubscriptions(keyId);
477
+
478
+ // Notify newly activated subscribers about the new key
479
+ if (activatedSubscribers.length > 0) {
480
+ console.log(`DHT STORE: Notifying ${activatedSubscribers.length} subscribers about new key ${key}`);
481
+
482
+ const notificationPromises = activatedSubscribers.map(async (subscriberId) => {
483
+ if (subscriberId !== this.peerId && subscriberId !== fromPeerId) {
484
+ try {
485
+ return await this.sendDHTMessage(subscriberId, 'value_changed', {
486
+ key,
487
+ keyId,
488
+ newValue: value,
489
+ timestamp,
490
+ isNewKey: true
491
+ });
492
+ } catch (error) {
493
+ console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
494
+ return null;
495
+ }
496
+ }
497
+ }).filter(Boolean);
498
+
499
+ await Promise.allSettled(notificationPromises);
500
+ }
501
+ }
502
+
395
503
  console.log(`🔥 DHT STORE: Accepting and storing ${key}, sending success response`);
396
504
 
397
505
  // Send response using the same direct messaging structure
@@ -499,9 +607,16 @@ export class WebDHT extends EventEmitter {
499
607
  async handleSubscribeRequest(fromPeerId, data) {
500
608
  const { keyId, key, subscriber } = data;
501
609
 
610
+ console.log(`DHT SUBSCRIBE REQUEST: ${key} from ${fromPeerId.substring(0, 8)} for subscriber ${subscriber.substring(0, 8)}`);
611
+
502
612
  if (this.localStorage.has(keyId)) {
613
+ // Key exists - add to active subscriptions
503
614
  this.addSubscription(keyId, subscriber);
504
- console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} notifications`);
615
+ console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} notifications (key exists)`);
616
+ } else {
617
+ // Key doesn't exist yet - add to pending subscriptions
618
+ this.addPendingSubscription(keyId, subscriber);
619
+ console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} pending notifications (key doesn't exist yet)`);
505
620
  }
506
621
  }
507
622
 
@@ -511,12 +626,22 @@ export class WebDHT extends EventEmitter {
511
626
  async handleUnsubscribeRequest(fromPeerId, data) {
512
627
  const { keyId, key, subscriber } = data;
513
628
 
629
+ // Remove from active subscriptions
514
630
  if (this.subscriptions.has(keyId)) {
515
631
  this.subscriptions.get(keyId).delete(subscriber);
516
632
  if (this.subscriptions.get(keyId).size === 0) {
517
633
  this.subscriptions.delete(keyId);
518
634
  }
519
- console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} notifications`);
635
+ console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} active notifications`);
636
+ }
637
+
638
+ // Remove from pending subscriptions
639
+ if (this.pendingSubscriptions.has(keyId)) {
640
+ this.pendingSubscriptions.get(keyId).delete(subscriber);
641
+ if (this.pendingSubscriptions.get(keyId).size === 0) {
642
+ this.pendingSubscriptions.delete(keyId);
643
+ }
644
+ console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} pending notifications`);
520
645
  }
521
646
  }
522
647
 
@@ -524,9 +649,9 @@ export class WebDHT extends EventEmitter {
524
649
  * Handle value changed notification
525
650
  */
526
651
  async handleValueChangedNotification(fromPeerId, data) {
527
- const { key, keyId, newValue, timestamp } = data;
652
+ const { key, keyId, newValue, timestamp, isNewKey } = data;
528
653
 
529
- console.log(`🔥 DHT VALUE_CHANGED notification: ${key} = ${newValue} from ${fromPeerId.substring(0, 8)}`);
654
+ console.log(`🔥 DHT VALUE_CHANGED notification: ${key} = ${newValue} from ${fromPeerId.substring(0, 8)}${isNewKey ? ' (NEW KEY)' : ''}`);
530
655
 
531
656
  // CRITICAL FIX: Always update local cache if we have it, regardless of subscriptions
532
657
  // This prevents stale cached values from being returned by get() operations
@@ -540,6 +665,17 @@ export class WebDHT extends EventEmitter {
540
665
  } else {
541
666
  console.log(`🔥 DHT VALUE_CHANGED: Ignoring older notification for ${key} (current: ${storedData.timestamp}, notification: ${timestamp})`);
542
667
  }
668
+ } else if (isNewKey) {
669
+ // For new keys, store the data locally even if we don't normally store this key
670
+ // This helps with caching and consistency
671
+ console.log(`🔥 DHT VALUE_CHANGED: Caching new key ${key} locally`);
672
+ this.storeLocally(keyId, {
673
+ key,
674
+ value: newValue,
675
+ timestamp,
676
+ ttl: null, // Use default TTL
677
+ publisher: fromPeerId
678
+ }, false); // Not a primary replica
543
679
  }
544
680
 
545
681
  // Check if we have subscribers for this key
@@ -551,8 +687,8 @@ export class WebDHT extends EventEmitter {
551
687
 
552
688
  // Emit change event if this peer is subscribed
553
689
  if (hasSubscribers) {
554
- this.emit('valueChanged', { key, keyId, newValue, timestamp });
555
- console.log(`🔥 DHT VALUE_CHANGED: Emitted local event for ${key}`);
690
+ this.emit('valueChanged', { key, keyId, newValue, timestamp, isNewKey });
691
+ console.log(`🔥 DHT VALUE_CHANGED: Emitted local event for ${key}${isNewKey ? ' (NEW KEY)' : ''}`);
556
692
  }
557
693
 
558
694
  // Forward to local subscribers (other peers that subscribed through this peer)
@@ -567,7 +703,8 @@ export class WebDHT extends EventEmitter {
567
703
  key,
568
704
  keyId,
569
705
  newValue,
570
- timestamp
706
+ timestamp,
707
+ isNewKey
571
708
  });
572
709
  } catch (error) {
573
710
  console.warn(`Failed to forward notification to ${subscriberId.substring(0, 8)}:`, error.message);
@@ -808,6 +945,42 @@ export class WebDHT extends EventEmitter {
808
945
  this.subscriptions.get(keyId).add(subscriberId);
809
946
  }
810
947
 
948
+ /**
949
+ * Add a pending subscription for a key that doesn't exist yet
950
+ */
951
+ addPendingSubscription(keyId, subscriberId) {
952
+ if (!this.pendingSubscriptions.has(keyId)) {
953
+ this.pendingSubscriptions.set(keyId, new Set());
954
+ }
955
+ this.pendingSubscriptions.get(keyId).add(subscriberId);
956
+ }
957
+
958
+ /**
959
+ * Activate pending subscriptions when a key is first created
960
+ */
961
+ activatePendingSubscriptions(keyId) {
962
+ if (this.pendingSubscriptions.has(keyId)) {
963
+ const pendingSubscribers = this.pendingSubscriptions.get(keyId);
964
+
965
+ // Move all pending subscribers to active subscriptions
966
+ if (!this.subscriptions.has(keyId)) {
967
+ this.subscriptions.set(keyId, new Set());
968
+ }
969
+
970
+ pendingSubscribers.forEach(subscriberId => {
971
+ this.subscriptions.get(keyId).add(subscriberId);
972
+ });
973
+
974
+ console.log(`DHT: Activated ${pendingSubscribers.size} pending subscriptions for keyId ${keyId.substring(0, 8)}`);
975
+
976
+ // Clear pending subscriptions for this key
977
+ this.pendingSubscriptions.delete(keyId);
978
+
979
+ return Array.from(pendingSubscribers);
980
+ }
981
+ return [];
982
+ }
983
+
811
984
  /**
812
985
  * Check if this peer is among the closest to a key
813
986
  */
@@ -903,6 +1076,7 @@ export class WebDHT extends EventEmitter {
903
1076
  return {
904
1077
  storedKeys: this.localStorage.size,
905
1078
  activeSubscriptions: this.subscriptions.size,
1079
+ pendingSubscriptions: this.pendingSubscriptions.size,
906
1080
  mySubscriptions: this.mySubscriptions.size,
907
1081
  connectedPeers: this.mesh.connectionManager.getConnectedPeerCount()
908
1082
  };
@@ -919,6 +1093,7 @@ export class WebDHT extends EventEmitter {
919
1093
 
920
1094
  this.localStorage.clear();
921
1095
  this.subscriptions.clear();
1096
+ this.pendingSubscriptions.clear();
922
1097
  this.mySubscriptions.clear();
923
1098
 
924
1099
  if (this.responseHandlers) {