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/src/WebDHT.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { EventEmitter } from './EventEmitter.js';
2
2
  import { safeSetInterval, safeClearInterval, safeSetTimeout, safeClearTimeout } from './TimerUtils.js';
3
+ import DebugLogger from './DebugLogger.js';
3
4
 
4
5
  /**
5
6
  * WebDHT - A Kademlia-like Distributed Hash Table for WebRTC mesh networks
@@ -13,6 +14,7 @@ import { safeSetInterval, safeClearInterval, safeSetTimeout, safeClearTimeout }
13
14
  export class WebDHT extends EventEmitter {
14
15
  constructor(mesh) {
15
16
  super();
17
+ this.debug = DebugLogger.create('WebDHT');
16
18
  this.mesh = mesh;
17
19
  this.peerId = mesh.peerId;
18
20
 
@@ -46,81 +48,81 @@ export class WebDHT extends EventEmitter {
46
48
  // Start periodic maintenance
47
49
  this.startMaintenance();
48
50
 
49
- console.log(`WebDHT initialized for peer ${this.peerId.substring(0, 8)}...`);
51
+ this.debug.log(`WebDHT initialized for peer ${this.peerId.substring(0, 8)}...`);
50
52
  }
51
53
 
52
54
  /**
53
55
  * Setup message handlers for DHT operations
54
56
  */
55
57
  setupMessageHandlers() {
56
- console.log(`🔥 DHT Setting up message handlers for peer ${this.peerId.substring(0, 8)}`);
58
+ this.debug.log(`🔥 DHT Setting up message handlers for peer ${this.peerId.substring(0, 8)}`);
57
59
 
58
60
  // DHT messages now come through ConnectionManager's handleIncomingMessage
59
61
  // which routes 'dht' type messages to this.mesh.webDHT.handleMessage
60
62
  // So we don't need to listen to gossip manager events anymore
61
63
 
62
- console.log(`🔥 DHT Message handlers setup complete for peer ${this.peerId.substring(0, 8)}`);
64
+ this.debug.log(`🔥 DHT Message handlers setup complete for peer ${this.peerId.substring(0, 8)}`);
63
65
  }
64
66
 
65
67
  /**
66
68
  * Handle incoming DHT messages - called by ConnectionManager
67
69
  */
68
70
  async handleMessage(message, fromPeerId) {
69
- console.log(`🔥 DHT Message received from ${fromPeerId.substring(0, 8)}:`, message);
71
+ this.debug.log(`🔥 DHT Message received from ${fromPeerId.substring(0, 8)}:`, message);
70
72
 
71
73
  // Extract the DHT operation type and data
72
74
  const type = message.messageType || message.type;
73
75
  const data = message.data;
74
76
 
75
77
  if (!type || !data) {
76
- console.error(`🔥 DHT Invalid message structure from ${fromPeerId.substring(0, 8)}:`, message);
78
+ this.debug.error(`🔥 DHT Invalid message structure from ${fromPeerId.substring(0, 8)}:`, message);
77
79
  return;
78
80
  }
79
81
 
80
- console.log(`🔥 DHT Processing ${type} from ${fromPeerId.substring(0, 8)}, data:`, data);
82
+ this.debug.log(`🔥 DHT Processing ${type} from ${fromPeerId.substring(0, 8)}, data:`, data);
81
83
 
82
84
  try {
83
85
  switch (type) {
84
86
  case 'store':
85
- console.log(`🔥 DHT Handling store request from ${fromPeerId.substring(0, 8)}`);
87
+ this.debug.log(`🔥 DHT Handling store request from ${fromPeerId.substring(0, 8)}`);
86
88
  await this.handleStoreRequest(fromPeerId, data);
87
89
  break;
88
90
  case 'find_value':
89
- console.log(`🔥 DHT Handling find_value request from ${fromPeerId.substring(0, 8)}`);
91
+ this.debug.log(`🔥 DHT Handling find_value request from ${fromPeerId.substring(0, 8)}`);
90
92
  await this.handleFindValueRequest(fromPeerId, data);
91
93
  break;
92
94
  case 'find_node':
93
- console.log(`🔥 DHT Handling find_node request from ${fromPeerId.substring(0, 8)}`);
95
+ this.debug.log(`🔥 DHT Handling find_node request from ${fromPeerId.substring(0, 8)}`);
94
96
  await this.handleFindNodeRequest(fromPeerId, data);
95
97
  break;
96
98
  case 'subscribe':
97
- console.log(`🔥 DHT Handling subscribe request from ${fromPeerId.substring(0, 8)}`);
99
+ this.debug.log(`🔥 DHT Handling subscribe request from ${fromPeerId.substring(0, 8)}`);
98
100
  await this.handleSubscribeRequest(fromPeerId, data);
99
101
  break;
100
102
  case 'unsubscribe':
101
- console.log(`🔥 DHT Handling unsubscribe request from ${fromPeerId.substring(0, 8)}`);
103
+ this.debug.log(`🔥 DHT Handling unsubscribe request from ${fromPeerId.substring(0, 8)}`);
102
104
  await this.handleUnsubscribeRequest(fromPeerId, data);
103
105
  break;
104
106
  case 'value_changed':
105
- console.log(`🔥 DHT Handling value_changed notification from ${fromPeerId.substring(0, 8)}`);
107
+ this.debug.log(`🔥 DHT Handling value_changed notification from ${fromPeerId.substring(0, 8)}`);
106
108
  await this.handleValueChangedNotification(fromPeerId, data);
107
109
  break;
108
110
  case 'update_value':
109
- console.log(`🔥 DHT Handling update_value request from ${fromPeerId.substring(0, 8)}`);
111
+ this.debug.log(`🔥 DHT Handling update_value request from ${fromPeerId.substring(0, 8)}`);
110
112
  await this.handleUpdateValueRequest(fromPeerId, data);
111
113
  break;
112
114
  case 'store_response':
113
115
  case 'find_value_response':
114
116
  case 'find_node_response':
115
117
  case 'update_value_response':
116
- console.log(`🔥 DHT Handling response (${type}) from ${fromPeerId.substring(0, 8)}`);
118
+ this.debug.log(`🔥 DHT Handling response (${type}) from ${fromPeerId.substring(0, 8)}`);
117
119
  this.handleResponse(fromPeerId, data);
118
120
  break;
119
121
  default:
120
- console.warn(`Unknown DHT message type: ${type}`);
122
+ this.debug.warn(`Unknown DHT message type: ${type}`);
121
123
  }
122
124
  } catch (error) {
123
- console.error(`🔥 DHT Error handling message type ${type} from ${fromPeerId.substring(0, 8)}:`, error);
125
+ this.debug.error(`🔥 DHT Error handling message type ${type} from ${fromPeerId.substring(0, 8)}:`, error);
124
126
  }
125
127
  }
126
128
 
@@ -131,7 +133,7 @@ export class WebDHT extends EventEmitter {
131
133
  const keyId = await this.generateKeyId(key);
132
134
  const ttl = options.ttl || this.config.ttl;
133
135
 
134
- console.log(`DHT PUT: ${key} -> ${keyId.substring(0, 8)}...`);
136
+ this.debug.log(`DHT PUT: ${key} -> ${keyId.substring(0, 8)}...`);
135
137
 
136
138
  const storeData = {
137
139
  key,
@@ -147,7 +149,7 @@ export class WebDHT extends EventEmitter {
147
149
 
148
150
  // CRITICAL FIX: Always store locally first (the originator always keeps a copy)
149
151
  this.storeLocally(keyId, storeData);
150
- console.log(`DHT PUT: Stored locally: ${key}`);
152
+ this.debug.log(`DHT PUT: Stored locally: ${key}`);
151
153
 
152
154
  // If this is a new key, activate any pending subscriptions
153
155
  if (isNewKey) {
@@ -155,7 +157,7 @@ export class WebDHT extends EventEmitter {
155
157
 
156
158
  // Notify locally activated subscribers about the new key
157
159
  if (activatedSubscribers.length > 0) {
158
- console.log(`DHT PUT: Notifying ${activatedSubscribers.length} locally pending subscribers about new key ${key}`);
160
+ this.debug.log(`DHT PUT: Notifying ${activatedSubscribers.length} locally pending subscribers about new key ${key}`);
159
161
 
160
162
  const notificationPromises = activatedSubscribers.map(async (subscriberId) => {
161
163
  if (subscriberId !== this.peerId) {
@@ -168,7 +170,7 @@ export class WebDHT extends EventEmitter {
168
170
  isNewKey: true
169
171
  });
170
172
  } catch (error) {
171
- console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
173
+ this.debug.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
172
174
  return null;
173
175
  }
174
176
  }
@@ -180,7 +182,7 @@ export class WebDHT extends EventEmitter {
180
182
 
181
183
  // AUTO-SUBSCRIBE: When putting a value, automatically subscribe to future changes
182
184
  this.mySubscriptions.add(keyId);
183
- console.log(`DHT PUT: Auto-subscribed to ${key} for future updates`);
185
+ this.debug.log(`DHT PUT: Auto-subscribed to ${key} for future updates`);
184
186
 
185
187
  // Find the closest peers to store this key for replication
186
188
  const closestPeers = await this.findClosestPeers(keyId, this.config.k); // Get more peers initially
@@ -198,13 +200,13 @@ export class WebDHT extends EventEmitter {
198
200
  const subscribe = options.skipSubscribe ? false : (options.subscribe !== false);
199
201
  const forceRefresh = options.forceRefresh || false;
200
202
 
201
- console.log(`DHT GET: ${key} -> ${keyId.substring(0, 8)}... (auto-subscribing: ${subscribe}, forceRefresh: ${forceRefresh})`);
203
+ this.debug.log(`DHT GET: ${key} -> ${keyId.substring(0, 8)}... (auto-subscribing: ${subscribe}, forceRefresh: ${forceRefresh})`);
202
204
 
203
205
  // Check local storage FIRST (unless force refresh is requested)
204
206
  if (!forceRefresh && this.localStorage.has(keyId)) {
205
207
  const data = this.localStorage.get(keyId);
206
208
  if (!this.isExpired(data)) {
207
- console.log(`DHT GET: Found locally: ${key} (using cached data)`);
209
+ this.debug.log(`DHT GET: Found locally: ${key} (using cached data)`);
208
210
  // Add subscription even for local data (unless skipSubscribe is true)
209
211
  if (subscribe) {
210
212
  this.mySubscriptions.add(keyId);
@@ -213,14 +215,14 @@ export class WebDHT extends EventEmitter {
213
215
  } else {
214
216
  // Remove expired data
215
217
  this.localStorage.delete(keyId);
216
- console.log(`DHT GET: Removed expired local data for: ${key}`);
218
+ this.debug.log(`DHT GET: Removed expired local data for: ${key}`);
217
219
  }
218
220
  }
219
221
 
220
222
  if (forceRefresh) {
221
- console.log('DHT GET: Force refresh requested - bypassing local cache, fetching from original storing peers');
223
+ this.debug.log('DHT GET: Force refresh requested - bypassing local cache, fetching from original storing peers');
222
224
  } else {
223
- console.log(`DHT GET: Not found locally, routing to network for key: ${key}`);
225
+ this.debug.log(`DHT GET: Not found locally, routing to network for key: ${key}`);
224
226
  }
225
227
 
226
228
  // Implement iterative Kademlia lookup with automatic subscription
@@ -234,7 +236,7 @@ export class WebDHT extends EventEmitter {
234
236
  async subscribe(key) {
235
237
  const keyId = await this.generateKeyId(key);
236
238
 
237
- console.log(`DHT EXPLICIT SUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
239
+ this.debug.log(`DHT EXPLICIT SUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
238
240
 
239
241
  // Add to our subscriptions immediately
240
242
  this.mySubscriptions.add(keyId);
@@ -252,7 +254,7 @@ export class WebDHT extends EventEmitter {
252
254
  subscriber: this.peerId
253
255
  });
254
256
  } catch (error) {
255
- console.warn(`DHT SUBSCRIBE: Failed to subscribe to peer ${peerId.substring(0, 8)}:`, error.message);
257
+ this.debug.warn(`DHT SUBSCRIBE: Failed to subscribe to peer ${peerId.substring(0, 8)}:`, error.message);
256
258
  return null;
257
259
  }
258
260
  }
@@ -270,10 +272,10 @@ export class WebDHT extends EventEmitter {
270
272
  try {
271
273
  currentValue = await this.get(key, { skipSubscribe: true }); // Skip auto-subscribe since we're already subscribed
272
274
  } catch (error) {
273
- console.log(`DHT SUBSCRIBE: Key ${key} doesn't exist yet, but subscription is active for when it's created`);
275
+ this.debug.log(`DHT SUBSCRIBE: Key ${key} doesn't exist yet, but subscription is active for when it's created`);
274
276
  }
275
277
 
276
- console.log(`DHT SUBSCRIBE: Subscribed to ${key}, current value:`, currentValue);
278
+ this.debug.log(`DHT SUBSCRIBE: Subscribed to ${key}, current value:`, currentValue);
277
279
  return currentValue;
278
280
  }
279
281
 
@@ -319,7 +321,7 @@ export class WebDHT extends EventEmitter {
319
321
  }
320
322
  }
321
323
 
322
- console.log(`DHT UNSUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
324
+ this.debug.log(`DHT UNSUBSCRIBE: ${key} -> ${keyId.substring(0, 8)}...`);
323
325
  }
324
326
 
325
327
  /**
@@ -328,11 +330,11 @@ export class WebDHT extends EventEmitter {
328
330
  async update(key, newValue, options = {}) {
329
331
  const keyId = await this.generateKeyId(key);
330
332
 
331
- console.log(`DHT UPDATE: Updating ${key} with new value across all replicas and subscribers`);
333
+ this.debug.log(`DHT UPDATE: Updating ${key} with new value across all replicas and subscribers`);
332
334
 
333
335
  // AUTO-SUBSCRIBE: When updating a value, automatically subscribe to future changes
334
336
  this.mySubscriptions.add(keyId);
335
- console.log(`DHT UPDATE: Auto-subscribed to ${key} for future updates`);
337
+ this.debug.log(`DHT UPDATE: Auto-subscribed to ${key} for future updates`);
336
338
 
337
339
  // Find all peers that should store this key (closest peers)
338
340
  const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
@@ -347,31 +349,31 @@ export class WebDHT extends EventEmitter {
347
349
  publisher: this.peerId
348
350
  };
349
351
 
350
- console.log(`DHT UPDATE: Target replica peers for ${key}:`, closestPeers.map(p => p.substring(0, 8)));
352
+ this.debug.log(`DHT UPDATE: Target replica peers for ${key}:`, closestPeers.map(p => p.substring(0, 8)));
351
353
 
352
354
  // CRITICAL FIX: Always update locally first if we're among the closest
353
355
  let localUpdateSuccess = false;
354
356
  if (this.isAmongClosest(keyId, closestPeers)) {
355
357
  this.storeLocally(keyId, updateData);
356
358
  localUpdateSuccess = true;
357
- console.log(`DHT UPDATE: Updated ${key} locally (we are replica peer)`);
359
+ this.debug.log(`DHT UPDATE: Updated ${key} locally (we are replica peer)`);
358
360
  }
359
361
 
360
362
  // CRITICAL FIX: Send update to ALL replica peers and wait for ALL to succeed
361
363
  const updatePromises = closestPeers.map(async (peerId) => {
362
364
  if (peerId !== this.peerId) {
363
365
  try {
364
- console.log(`DHT UPDATE: Sending update to replica peer ${peerId.substring(0, 8)}`);
366
+ this.debug.log(`DHT UPDATE: Sending update to replica peer ${peerId.substring(0, 8)}`);
365
367
  const response = await this.sendDHTMessage(peerId, 'update_value', updateData);
366
368
  if (response && response.success) {
367
- console.log(`DHT UPDATE: Successfully updated replica peer ${peerId.substring(0, 8)}`);
369
+ this.debug.log(`DHT UPDATE: Successfully updated replica peer ${peerId.substring(0, 8)}`);
368
370
  return { peerId, success: true };
369
371
  } else {
370
- console.warn(`DHT UPDATE: Replica peer ${peerId.substring(0, 8)} rejected update:`, response);
372
+ this.debug.warn(`DHT UPDATE: Replica peer ${peerId.substring(0, 8)} rejected update:`, response);
371
373
  return { peerId, success: false, response };
372
374
  }
373
375
  } catch (error) {
374
- console.warn(`DHT UPDATE: Failed to update replica peer ${peerId.substring(0, 8)}:`, error.message);
376
+ this.debug.warn(`DHT UPDATE: Failed to update replica peer ${peerId.substring(0, 8)}:`, error.message);
375
377
  return { peerId, success: false, error: error.message };
376
378
  }
377
379
  }
@@ -386,16 +388,16 @@ export class WebDHT extends EventEmitter {
386
388
  const successfulUpdates = updateResults.filter(r => r.success).length;
387
389
  const failedUpdates = updateResults.filter(r => !r.success);
388
390
 
389
- console.log(`DHT UPDATE: Replica peer update results for ${key}:`);
390
- console.log(` - Successful: ${successfulUpdates}`);
391
- console.log(` - Failed: ${failedUpdates.length}`);
392
- console.log(` - Local: ${localUpdateSuccess ? 'success' : 'not applicable'}`);
391
+ this.debug.log(`DHT UPDATE: Replica peer update results for ${key}:`);
392
+ this.debug.log(` - Successful: ${successfulUpdates}`);
393
+ this.debug.log(` - Failed: ${failedUpdates.length}`);
394
+ this.debug.log(` - Local: ${localUpdateSuccess ? 'success' : 'not applicable'}`);
393
395
 
394
396
  // ENHANCED: Log failed updates for debugging
395
397
  if (failedUpdates.length > 0) {
396
- console.warn('DHT UPDATE: Failed to update these replica peers:',
398
+ this.debug.warn('DHT UPDATE: Failed to update these replica peers:',
397
399
  failedUpdates.map(f => ({ peer: f.peerId.substring(0, 8), reason: f.error || f.response })));
398
- console.warn('DHT UPDATE: This may lead to consistency issues. Consider implementing retry logic.');
400
+ this.debug.warn('DHT UPDATE: This may lead to consistency issues. Consider implementing retry logic.');
399
401
  }
400
402
 
401
403
  // CRITICAL: Broadcast value change to ALL peers (including non-replicas)
@@ -409,10 +411,10 @@ export class WebDHT extends EventEmitter {
409
411
  const wasSuccessful = totalSuccessful >= totalReplicas;
410
412
 
411
413
  if (!wasSuccessful) {
412
- console.warn(`DHT UPDATE: ${key} update FAILED - only ${totalSuccessful}/${totalReplicas} replicas updated successfully`);
413
- console.warn('DHT UPDATE: This may cause consistency issues where GET operations return stale data');
414
+ this.debug.warn(`DHT UPDATE: ${key} update FAILED - only ${totalSuccessful}/${totalReplicas} replicas updated successfully`);
415
+ this.debug.warn('DHT UPDATE: This may cause consistency issues where GET operations return stale data');
414
416
  } else {
415
- console.log(`DHT UPDATE: ${key} update SUCCESSFUL - all ${totalSuccessful}/${totalReplicas} replicas updated`);
417
+ this.debug.log(`DHT UPDATE: ${key} update SUCCESSFUL - all ${totalSuccessful}/${totalReplicas} replicas updated`);
416
418
  }
417
419
 
418
420
  return wasSuccessful;
@@ -426,7 +428,7 @@ export class WebDHT extends EventEmitter {
426
428
  // Get ALL connected peers, not just subscribers
427
429
  const allPeers = this.mesh.connectionManager.getConnectedPeers().map(p => p.peerId);
428
430
 
429
- console.log(`DHT UPDATE: Broadcasting value change for ${key} to ${allPeers.length} peers`);
431
+ this.debug.log(`DHT UPDATE: Broadcasting value change for ${key} to ${allPeers.length} peers`);
430
432
 
431
433
  const notificationPromises = allPeers.map(async (peerId) => {
432
434
  try {
@@ -438,7 +440,7 @@ export class WebDHT extends EventEmitter {
438
440
  timestamp
439
441
  });
440
442
  } catch (error) {
441
- console.warn(`DHT BROADCAST: Failed to notify peer ${peerId.substring(0, 8)}:`, error.message);
443
+ this.debug.warn(`DHT BROADCAST: Failed to notify peer ${peerId.substring(0, 8)}:`, error.message);
442
444
  return null;
443
445
  }
444
446
  });
@@ -446,7 +448,7 @@ export class WebDHT extends EventEmitter {
446
448
  const results = await Promise.allSettled(notificationPromises);
447
449
  const successfulNotifications = results.filter(r => r.status === 'fulfilled' && r.value).length;
448
450
 
449
- console.log(`DHT UPDATE: Broadcasted ${key} change to ${successfulNotifications}/${allPeers.length} peers`);
451
+ this.debug.log(`DHT UPDATE: Broadcasted ${key} change to ${successfulNotifications}/${allPeers.length} peers`);
450
452
 
451
453
  // Also emit local event if this peer has subscriptions
452
454
  if (this.mySubscriptions.has(keyId)) {
@@ -460,7 +462,7 @@ export class WebDHT extends EventEmitter {
460
462
  async handleStoreRequest(fromPeerId, data) {
461
463
  const { keyId, key, value, timestamp, ttl, publisher, messageId } = data;
462
464
 
463
- console.log(`🔥 DHT STORE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
465
+ this.debug.log(`🔥 DHT STORE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
464
466
 
465
467
  // Check if we should store this key (are we close enough?)
466
468
  const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
@@ -477,7 +479,7 @@ export class WebDHT extends EventEmitter {
477
479
 
478
480
  // Notify newly activated subscribers about the new key
479
481
  if (activatedSubscribers.length > 0) {
480
- console.log(`DHT STORE: Notifying ${activatedSubscribers.length} subscribers about new key ${key}`);
482
+ this.debug.log(`DHT STORE: Notifying ${activatedSubscribers.length} subscribers about new key ${key}`);
481
483
 
482
484
  const notificationPromises = activatedSubscribers.map(async (subscriberId) => {
483
485
  if (subscriberId !== this.peerId && subscriberId !== fromPeerId) {
@@ -490,7 +492,7 @@ export class WebDHT extends EventEmitter {
490
492
  isNewKey: true
491
493
  });
492
494
  } catch (error) {
493
- console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
495
+ this.debug.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)} about new key:`, error.message);
494
496
  return null;
495
497
  }
496
498
  }
@@ -500,7 +502,7 @@ export class WebDHT extends EventEmitter {
500
502
  }
501
503
  }
502
504
 
503
- console.log(`🔥 DHT STORE: Accepting and storing ${key}, sending success response`);
505
+ this.debug.log(`🔥 DHT STORE: Accepting and storing ${key}, sending success response`);
504
506
 
505
507
  // Send response using the same direct messaging structure
506
508
  this.sendDirectToPeer(fromPeerId, {
@@ -514,9 +516,9 @@ export class WebDHT extends EventEmitter {
514
516
  }
515
517
  });
516
518
 
517
- console.log(`DHT STORE: accepted ${key} from ${fromPeerId.substring(0, 8)}...`);
519
+ this.debug.log(`DHT STORE: accepted ${key} from ${fromPeerId.substring(0, 8)}...`);
518
520
  } else {
519
- console.log(`🔥 DHT STORE: Rejecting ${key} (not among closest), sending failure response`);
521
+ this.debug.log(`🔥 DHT STORE: Rejecting ${key} (not among closest), sending failure response`);
520
522
 
521
523
  this.sendDirectToPeer(fromPeerId, {
522
524
  type: 'dht',
@@ -538,14 +540,14 @@ export class WebDHT extends EventEmitter {
538
540
  async handleFindValueRequest(fromPeerId, data) {
539
541
  const { keyId, key, subscribe, requester, messageId } = data;
540
542
 
541
- console.log(`🔥 DHT FIND_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
542
- console.log('🔥 DHT FIND_VALUE REQUEST: Full data received:', data);
543
+ this.debug.log(`🔥 DHT FIND_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}, messageId: ${messageId}`);
544
+ this.debug.log('🔥 DHT FIND_VALUE REQUEST: Full data received:', data);
543
545
 
544
546
  if (this.localStorage.has(keyId)) {
545
547
  const storedData = this.localStorage.get(keyId);
546
548
 
547
549
  if (!this.isExpired(storedData)) {
548
- console.log(`🔥 DHT FIND_VALUE: Found ${key} locally, sending success response with messageId: ${messageId}`);
550
+ this.debug.log(`🔥 DHT FIND_VALUE: Found ${key} locally, sending success response with messageId: ${messageId}`);
549
551
 
550
552
  // Add subscription if requested
551
553
  if (subscribe) {
@@ -564,7 +566,7 @@ export class WebDHT extends EventEmitter {
564
566
  publisher: storedData.publisher
565
567
  };
566
568
 
567
- console.log(`🔥 DHT FIND_VALUE: Sending response data with messageId ${messageId}:`, responseData);
569
+ this.debug.log(`🔥 DHT FIND_VALUE: Sending response data with messageId ${messageId}:`, responseData);
568
570
 
569
571
  this.sendDirectToPeer(fromPeerId, {
570
572
  type: 'dht',
@@ -572,15 +574,15 @@ export class WebDHT extends EventEmitter {
572
574
  data: responseData
573
575
  });
574
576
 
575
- console.log(`DHT FIND_VALUE: found ${key} for ${fromPeerId.substring(0, 8)}...`);
577
+ this.debug.log(`DHT FIND_VALUE: found ${key} for ${fromPeerId.substring(0, 8)}...`);
576
578
  return;
577
579
  } else {
578
580
  this.localStorage.delete(keyId);
579
- console.log(`🔥 DHT FIND_VALUE: Found ${key} but expired, removed from storage`);
581
+ this.debug.log(`🔥 DHT FIND_VALUE: Found ${key} but expired, removed from storage`);
580
582
  }
581
583
  }
582
584
 
583
- console.log(`🔥 DHT FIND_VALUE: ${key} not found locally, sending closest peers response with messageId: ${messageId}`);
585
+ this.debug.log(`🔥 DHT FIND_VALUE: ${key} not found locally, sending closest peers response with messageId: ${messageId}`);
584
586
 
585
587
  // If not found, return closest peers
586
588
  const closestPeers = await this.findClosestPeers(keyId, this.config.k);
@@ -592,7 +594,7 @@ export class WebDHT extends EventEmitter {
592
594
  closestPeers
593
595
  };
594
596
 
595
- console.log(`🔥 DHT FIND_VALUE: Sending not-found response data with messageId ${messageId}:`, responseData);
597
+ this.debug.log(`🔥 DHT FIND_VALUE: Sending not-found response data with messageId ${messageId}:`, responseData);
596
598
 
597
599
  this.sendDirectToPeer(fromPeerId, {
598
600
  type: 'dht',
@@ -607,16 +609,16 @@ export class WebDHT extends EventEmitter {
607
609
  async handleSubscribeRequest(fromPeerId, data) {
608
610
  const { keyId, key, subscriber } = data;
609
611
 
610
- console.log(`DHT SUBSCRIBE REQUEST: ${key} from ${fromPeerId.substring(0, 8)} for subscriber ${subscriber.substring(0, 8)}`);
612
+ this.debug.log(`DHT SUBSCRIBE REQUEST: ${key} from ${fromPeerId.substring(0, 8)} for subscriber ${subscriber.substring(0, 8)}`);
611
613
 
612
614
  if (this.localStorage.has(keyId)) {
613
615
  // Key exists - add to active subscriptions
614
616
  this.addSubscription(keyId, subscriber);
615
- console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} notifications (key exists)`);
617
+ this.debug.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} notifications (key exists)`);
616
618
  } else {
617
619
  // Key doesn't exist yet - add to pending subscriptions
618
620
  this.addPendingSubscription(keyId, subscriber);
619
- console.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} pending notifications (key doesn't exist yet)`);
621
+ this.debug.log(`DHT SUBSCRIBE: added ${subscriber.substring(0, 8)}... to ${key} pending notifications (key doesn't exist yet)`);
620
622
  }
621
623
  }
622
624
 
@@ -632,7 +634,7 @@ export class WebDHT extends EventEmitter {
632
634
  if (this.subscriptions.get(keyId).size === 0) {
633
635
  this.subscriptions.delete(keyId);
634
636
  }
635
- console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} active notifications`);
637
+ this.debug.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} active notifications`);
636
638
  }
637
639
 
638
640
  // Remove from pending subscriptions
@@ -641,7 +643,7 @@ export class WebDHT extends EventEmitter {
641
643
  if (this.pendingSubscriptions.get(keyId).size === 0) {
642
644
  this.pendingSubscriptions.delete(keyId);
643
645
  }
644
- console.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} pending notifications`);
646
+ this.debug.log(`DHT UNSUBSCRIBE: removed ${subscriber.substring(0, 8)}... from ${key} pending notifications`);
645
647
  }
646
648
  }
647
649
 
@@ -651,7 +653,7 @@ export class WebDHT extends EventEmitter {
651
653
  async handleValueChangedNotification(fromPeerId, data) {
652
654
  const { key, keyId, newValue, timestamp, isNewKey } = data;
653
655
 
654
- console.log(`🔥 DHT VALUE_CHANGED notification: ${key} = ${newValue} from ${fromPeerId.substring(0, 8)}${isNewKey ? ' (NEW KEY)' : ''}`);
656
+ this.debug.log(`🔥 DHT VALUE_CHANGED notification: ${key} = ${newValue} from ${fromPeerId.substring(0, 8)}${isNewKey ? ' (NEW KEY)' : ''}`);
655
657
 
656
658
  // CRITICAL FIX: Always update local cache if we have it, regardless of subscriptions
657
659
  // This prevents stale cached values from being returned by get() operations
@@ -661,14 +663,14 @@ export class WebDHT extends EventEmitter {
661
663
  if (!storedData.timestamp || timestamp >= storedData.timestamp) {
662
664
  storedData.value = newValue;
663
665
  storedData.timestamp = timestamp;
664
- console.log(`🔥 DHT VALUE_CHANGED: Updated local cache for ${key} (timestamp: ${timestamp})`);
666
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Updated local cache for ${key} (timestamp: ${timestamp})`);
665
667
  } else {
666
- console.log(`🔥 DHT VALUE_CHANGED: Ignoring older notification for ${key} (current: ${storedData.timestamp}, notification: ${timestamp})`);
668
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Ignoring older notification for ${key} (current: ${storedData.timestamp}, notification: ${timestamp})`);
667
669
  }
668
670
  } else if (isNewKey) {
669
671
  // For new keys, store the data locally even if we don't normally store this key
670
672
  // This helps with caching and consistency
671
- console.log(`🔥 DHT VALUE_CHANGED: Caching new key ${key} locally`);
673
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Caching new key ${key} locally`);
672
674
  this.storeLocally(keyId, {
673
675
  key,
674
676
  value: newValue,
@@ -683,18 +685,18 @@ export class WebDHT extends EventEmitter {
683
685
  const hasLocalSubscribers = this.subscriptions.has(keyId) && this.subscriptions.get(keyId).size > 0;
684
686
 
685
687
  if (hasSubscribers || hasLocalSubscribers) {
686
- console.log(`🔥 DHT VALUE_CHANGED: Processing notification for ${key} (mySubscriptions: ${hasSubscribers}, localSubscribers: ${hasLocalSubscribers})`);
688
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Processing notification for ${key} (mySubscriptions: ${hasSubscribers}, localSubscribers: ${hasLocalSubscribers})`);
687
689
 
688
690
  // Emit change event if this peer is subscribed
689
691
  if (hasSubscribers) {
690
692
  this.emit('valueChanged', { key, keyId, newValue, timestamp, isNewKey });
691
- console.log(`🔥 DHT VALUE_CHANGED: Emitted local event for ${key}${isNewKey ? ' (NEW KEY)' : ''}`);
693
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Emitted local event for ${key}${isNewKey ? ' (NEW KEY)' : ''}`);
692
694
  }
693
695
 
694
696
  // Forward to local subscribers (other peers that subscribed through this peer)
695
697
  if (hasLocalSubscribers) {
696
698
  const localSubscribers = Array.from(this.subscriptions.get(keyId));
697
- console.log(`🔥 DHT VALUE_CHANGED: Forwarding to ${localSubscribers.length} local subscribers for ${key}`);
699
+ this.debug.log(`🔥 DHT VALUE_CHANGED: Forwarding to ${localSubscribers.length} local subscribers for ${key}`);
698
700
 
699
701
  const forwardPromises = localSubscribers.map(async (subscriberId) => {
700
702
  if (subscriberId !== this.peerId && subscriberId !== fromPeerId) {
@@ -707,7 +709,7 @@ export class WebDHT extends EventEmitter {
707
709
  isNewKey
708
710
  });
709
711
  } catch (error) {
710
- console.warn(`Failed to forward notification to ${subscriberId.substring(0, 8)}:`, error.message);
712
+ this.debug.warn(`Failed to forward notification to ${subscriberId.substring(0, 8)}:`, error.message);
711
713
  return null;
712
714
  }
713
715
  }
@@ -716,7 +718,7 @@ export class WebDHT extends EventEmitter {
716
718
  await Promise.allSettled(forwardPromises);
717
719
  }
718
720
  } else {
719
- console.log(`🔥 DHT VALUE_CHANGED: No subscriptions for ${key}, but updated cache anyway`);
721
+ this.debug.log(`🔥 DHT VALUE_CHANGED: No subscriptions for ${key}, but updated cache anyway`);
720
722
  }
721
723
  }
722
724
 
@@ -726,7 +728,7 @@ export class WebDHT extends EventEmitter {
726
728
  async handleUpdateValueRequest(fromPeerId, data) {
727
729
  const { keyId, key, value, timestamp, ttl, publisher, messageId } = data;
728
730
 
729
- console.log(`🔥 DHT UPDATE_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}`);
731
+ this.debug.log(`🔥 DHT UPDATE_VALUE REQUEST: ${key} from ${fromPeerId.substring(0, 8)}`);
730
732
 
731
733
  // Check if we should store this key (are we among the closest?)
732
734
  const closestPeers = await this.findClosestPeers(keyId, this.config.replicationFactor);
@@ -734,12 +736,12 @@ export class WebDHT extends EventEmitter {
734
736
  if (this.isAmongClosest(keyId, closestPeers)) {
735
737
  // Update our local copy
736
738
  this.storeLocally(keyId, { key, value, timestamp, ttl, publisher });
737
- console.log(`DHT UPDATE_VALUE: Updated ${key} locally`);
739
+ this.debug.log(`DHT UPDATE_VALUE: Updated ${key} locally`);
738
740
 
739
741
  // Notify any local subscribers
740
742
  if (this.subscriptions.has(keyId)) {
741
743
  const subscribers = Array.from(this.subscriptions.get(keyId));
742
- console.log(`DHT UPDATE_VALUE: Notifying ${subscribers.length} local subscribers for ${key}`);
744
+ this.debug.log(`DHT UPDATE_VALUE: Notifying ${subscribers.length} local subscribers for ${key}`);
743
745
 
744
746
  const notificationPromises = subscribers.map(async (subscriberId) => {
745
747
  if (subscriberId !== this.peerId) {
@@ -751,7 +753,7 @@ export class WebDHT extends EventEmitter {
751
753
  timestamp
752
754
  });
753
755
  } catch (error) {
754
- console.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)}:`, error.message);
756
+ this.debug.warn(`Failed to notify subscriber ${subscriberId.substring(0, 8)}:`, error.message);
755
757
  return null;
756
758
  }
757
759
  }
@@ -772,7 +774,7 @@ export class WebDHT extends EventEmitter {
772
774
  }
773
775
  });
774
776
  } else {
775
- console.log(`DHT UPDATE_VALUE: Not responsible for ${key}, rejecting update`);
777
+ this.debug.log(`DHT UPDATE_VALUE: Not responsible for ${key}, rejecting update`);
776
778
 
777
779
  // Send failure response
778
780
  this.sendDirectToPeer(fromPeerId, {
@@ -795,18 +797,18 @@ export class WebDHT extends EventEmitter {
795
797
  return new Promise((resolve, reject) => {
796
798
  const messageId = this.generateMessageId();
797
799
 
798
- console.log(`🔥 DHT Sending ${type} to ${targetPeerId.substring(0, 8)} with messageId ${messageId}`);
799
- console.log('🔥 DHT Request data being sent:', { ...data, messageId });
800
+ this.debug.log(`🔥 DHT Sending ${type} to ${targetPeerId.substring(0, 8)} with messageId ${messageId}`);
801
+ this.debug.log('🔥 DHT Request data being sent:', { ...data, messageId });
800
802
 
801
803
  // Set up response handler
802
804
  const timeout = safeSetTimeout(() => {
803
- console.log(`🔥 DHT Timeout for messageId ${messageId} to peer ${targetPeerId.substring(0, 8)}`);
805
+ this.debug.log(`🔥 DHT Timeout for messageId ${messageId} to peer ${targetPeerId.substring(0, 8)}`);
804
806
  this.removeResponseHandler(messageId);
805
807
  reject(new Error('DHT message timeout'));
806
808
  }, 5000);
807
809
 
808
810
  this.setResponseHandler(messageId, (response) => {
809
- console.log(`🔥 DHT Response received for ${type} messageId ${messageId}:`, response);
811
+ this.debug.log(`🔥 DHT Response received for ${type} messageId ${messageId}:`, response);
810
812
  safeClearTimeout(timeout);
811
813
  resolve(response);
812
814
  });
@@ -818,12 +820,12 @@ export class WebDHT extends EventEmitter {
818
820
  messageType: type // The actual DHT operation type
819
821
  };
820
822
 
821
- console.log(`🔥 DHT Full message being sent to ${targetPeerId.substring(0, 8)}:`, messageToSend);
823
+ this.debug.log(`🔥 DHT Full message being sent to ${targetPeerId.substring(0, 8)}:`, messageToSend);
822
824
 
823
825
  const success = this.sendDirectToPeer(targetPeerId, messageToSend);
824
826
 
825
827
  if (!success) {
826
- console.log(`🔥 DHT Failed to send to ${targetPeerId.substring(0, 8)}, cleaning up messageId ${messageId}`);
828
+ this.debug.log(`🔥 DHT Failed to send to ${targetPeerId.substring(0, 8)}, cleaning up messageId ${messageId}`);
827
829
  safeClearTimeout(timeout);
828
830
  this.removeResponseHandler(messageId);
829
831
  reject(new Error('Failed to send DHT message - no direct connection'));
@@ -836,16 +838,16 @@ export class WebDHT extends EventEmitter {
836
838
  * This implements proper Kademlia routing for peers that aren't directly connected
837
839
  */
838
840
  sendDirectToPeer(targetPeerId, message) {
839
- console.log(`DHT: Attempting to send message to ${targetPeerId.substring(0, 8)}:`, message.type);
841
+ this.debug.log(`DHT: Attempting to send message to ${targetPeerId.substring(0, 8)}:`, message.type);
840
842
 
841
843
  // First try direct connection if available
842
844
  const success = this.mesh.connectionManager.sendDirectMessage(targetPeerId, message);
843
845
 
844
846
  if (success) {
845
- console.log(`DHT: Successfully sent direct message to ${targetPeerId.substring(0, 8)}`);
847
+ this.debug.log(`DHT: Successfully sent direct message to ${targetPeerId.substring(0, 8)}`);
846
848
  return true;
847
849
  } else {
848
- console.log(`DHT: No direct connection to ${targetPeerId.substring(0, 8)}, using Kademlia routing`);
850
+ this.debug.log(`DHT: No direct connection to ${targetPeerId.substring(0, 8)}, using Kademlia routing`);
849
851
 
850
852
  // Use gossip/routing mechanism to reach the target peer
851
853
  // Create a special DHT routing message that will be forwarded through the mesh
@@ -860,7 +862,7 @@ export class WebDHT extends EventEmitter {
860
862
  path: [this.peerId] // Track routing path
861
863
  };
862
864
 
863
- console.log(`DHT: Routing message to ${targetPeerId.substring(0, 8)} via gossip network`);
865
+ this.debug.log(`DHT: Routing message to ${targetPeerId.substring(0, 8)} via gossip network`);
864
866
  this.mesh.gossipManager.propagateMessage(routingMessage);
865
867
  return true;
866
868
  }
@@ -872,7 +874,7 @@ export class WebDHT extends EventEmitter {
872
874
  async findClosestPeers(keyId, count) {
873
875
  const allPeers = [this.peerId, ...this.mesh.connectionManager.getConnectedPeers().map(p => p.peerId)];
874
876
 
875
- console.log(`DHT findClosestPeers: Looking for ${count} closest peers to key ${keyId.substring(0, 8)}... from ${allPeers.length} total peers`);
877
+ this.debug.log(`DHT findClosestPeers: Looking for ${count} closest peers to key ${keyId.substring(0, 8)}... from ${allPeers.length} total peers`);
876
878
 
877
879
  // Sort by XOR distance to the key
878
880
  allPeers.sort((a, b) => {
@@ -883,7 +885,7 @@ export class WebDHT extends EventEmitter {
883
885
 
884
886
  const result = allPeers.slice(0, count);
885
887
 
886
- console.log('DHT findClosestPeers result:', result.map(peerId => ({
888
+ this.debug.log('DHT findClosestPeers result:', result.map(peerId => ({
887
889
  peerId: peerId.substring(0, 8) + '...',
888
890
  distance: this.xorDistance(keyId, peerId).substring(0, 8) + '...',
889
891
  isSelf: peerId === this.peerId
@@ -971,7 +973,7 @@ export class WebDHT extends EventEmitter {
971
973
  this.subscriptions.get(keyId).add(subscriberId);
972
974
  });
973
975
 
974
- console.log(`DHT: Activated ${pendingSubscribers.size} pending subscriptions for keyId ${keyId.substring(0, 8)}`);
976
+ this.debug.log(`DHT: Activated ${pendingSubscribers.size} pending subscriptions for keyId ${keyId.substring(0, 8)}`);
975
977
 
976
978
  // Clear pending subscriptions for this key
977
979
  this.pendingSubscriptions.delete(keyId);
@@ -1017,22 +1019,22 @@ export class WebDHT extends EventEmitter {
1017
1019
  }
1018
1020
 
1019
1021
  handleResponse(fromPeerId, data) {
1020
- console.log(`🔥 DHT Response received from ${fromPeerId.substring(0, 8)}: messageId=${data.messageId}, type=${data.type || 'unknown'}`);
1021
- console.log('🔥 DHT Response full data:', data);
1022
- console.log('🔥 DHT Response available handlers:', this.responseHandlers ? Array.from(this.responseHandlers.keys()) : 'none');
1022
+ this.debug.log(`🔥 DHT Response received from ${fromPeerId.substring(0, 8)}: messageId=${data.messageId}, type=${data.type || 'unknown'}`);
1023
+ this.debug.log('🔥 DHT Response full data:', data);
1024
+ this.debug.log('🔥 DHT Response available handlers:', this.responseHandlers ? Array.from(this.responseHandlers.keys()) : 'none');
1023
1025
 
1024
1026
  if (this.responseHandlers && data.messageId) {
1025
1027
  const handler = this.responseHandlers.get(data.messageId);
1026
1028
  if (handler) {
1027
- console.log(`🔥 DHT Response: Found handler for messageId ${data.messageId}, calling handler`);
1029
+ this.debug.log(`🔥 DHT Response: Found handler for messageId ${data.messageId}, calling handler`);
1028
1030
  this.removeResponseHandler(data.messageId);
1029
1031
  handler(data);
1030
1032
  } else {
1031
- console.warn(`🔥 DHT Response: No handler found for messageId ${data.messageId}`);
1032
- console.log('🔥 DHT Response: Available handlers:', Array.from(this.responseHandlers.keys()));
1033
+ this.debug.warn(`🔥 DHT Response: No handler found for messageId ${data.messageId}`);
1034
+ this.debug.log('🔥 DHT Response: Available handlers:', Array.from(this.responseHandlers.keys()));
1033
1035
  }
1034
1036
  } else {
1035
- console.warn('🔥 DHT Response: Missing responseHandlers or messageId', {
1037
+ this.debug.warn('🔥 DHT Response: Missing responseHandlers or messageId', {
1036
1038
  hasHandlers: !!this.responseHandlers,
1037
1039
  messageId: data.messageId
1038
1040
  });
@@ -1059,14 +1061,14 @@ export class WebDHT extends EventEmitter {
1059
1061
  if (this.isExpired(data)) {
1060
1062
  this.localStorage.delete(keyId);
1061
1063
  this.subscriptions.delete(keyId);
1062
- console.log(`DHT MAINTENANCE: removed expired key ${keyId.substring(0, 8)}...`);
1064
+ this.debug.log(`DHT MAINTENANCE: removed expired key ${keyId.substring(0, 8)}...`);
1063
1065
  }
1064
1066
  }
1065
1067
 
1066
1068
  // TODO: Republish keys if needed
1067
1069
  // TODO: Update routing table
1068
1070
 
1069
- console.log(`DHT MAINTENANCE: ${this.localStorage.size} keys stored, ${this.subscriptions.size} subscriptions active`);
1071
+ this.debug.log(`DHT MAINTENANCE: ${this.localStorage.size} keys stored, ${this.subscriptions.size} subscriptions active`);
1070
1072
  }
1071
1073
 
1072
1074
  /**
@@ -1100,7 +1102,7 @@ export class WebDHT extends EventEmitter {
1100
1102
  this.responseHandlers.clear();
1101
1103
  }
1102
1104
 
1103
- console.log('WebDHT cleaned up');
1105
+ this.debug.log('WebDHT cleaned up');
1104
1106
  }
1105
1107
 
1106
1108
  /**
@@ -1114,7 +1116,7 @@ export class WebDHT extends EventEmitter {
1114
1116
  // Remove self from the list
1115
1117
  const availablePeers = closestPeers.filter(peerId => peerId !== this.peerId);
1116
1118
 
1117
- console.log(`DHT Iterative GET: Found ${availablePeers.length} potential peers for key ${key}`);
1119
+ this.debug.log(`DHT Iterative GET: Found ${availablePeers.length} potential peers for key ${key}`);
1118
1120
 
1119
1121
  // Try querying peers in rounds, increasing the number each round
1120
1122
  const maxRounds = 3;
@@ -1130,11 +1132,11 @@ export class WebDHT extends EventEmitter {
1130
1132
  .slice(0, peersPerRound);
1131
1133
 
1132
1134
  if (peersToQuery.length === 0) {
1133
- console.log(`DHT Iterative GET Round ${currentRound}: No more peers to query`);
1135
+ this.debug.log(`DHT Iterative GET Round ${currentRound}: No more peers to query`);
1134
1136
  break;
1135
1137
  }
1136
1138
 
1137
- console.log(`DHT Iterative GET Round ${currentRound}: Querying ${peersToQuery.length} peers`);
1139
+ this.debug.log(`DHT Iterative GET Round ${currentRound}: Querying ${peersToQuery.length} peers`);
1138
1140
 
1139
1141
  // Mark these peers as queried
1140
1142
  peersToQuery.forEach(peerId => queriedPeers.add(peerId));
@@ -1149,7 +1151,7 @@ export class WebDHT extends EventEmitter {
1149
1151
  requester: this.peerId
1150
1152
  });
1151
1153
  } catch (error) {
1152
- console.warn(`DHT GET: Failed to query peer ${peerId.substring(0, 8)}:`, error.message);
1154
+ this.debug.warn(`DHT GET: Failed to query peer ${peerId.substring(0, 8)}:`, error.message);
1153
1155
  return null;
1154
1156
  }
1155
1157
  });
@@ -1168,7 +1170,7 @@ export class WebDHT extends EventEmitter {
1168
1170
  data,
1169
1171
  timestamp: data.timestamp || 0
1170
1172
  });
1171
- console.log(`DHT Iterative GET: Found value on peer ${peersToQuery[i].substring(0, 8)} with timestamp ${data.timestamp}, value: ${JSON.stringify(data.value).substring(0, 50)}...`);
1173
+ this.debug.log(`DHT Iterative GET: Found value on peer ${peersToQuery[i].substring(0, 8)} with timestamp ${data.timestamp}, value: ${JSON.stringify(data.value).substring(0, 50)}...`);
1172
1174
  }
1173
1175
  }
1174
1176
  }
@@ -1179,16 +1181,16 @@ export class WebDHT extends EventEmitter {
1179
1181
  validResponses.sort((a, b) => b.timestamp - a.timestamp);
1180
1182
  const mostRecent = validResponses[0];
1181
1183
 
1182
- console.log(`DHT Iterative GET: Selected most recent value from peer ${mostRecent.peerId.substring(0, 8)} (timestamp: ${mostRecent.timestamp}) in round ${currentRound}`);
1184
+ this.debug.log(`DHT Iterative GET: Selected most recent value from peer ${mostRecent.peerId.substring(0, 8)} (timestamp: ${mostRecent.timestamp}) in round ${currentRound}`);
1183
1185
 
1184
1186
  if (validResponses.length > 1) {
1185
- console.log(`DHT Iterative GET: Found ${validResponses.length} versions, timestamps: ${validResponses.map(r => r.timestamp).join(', ')}`);
1187
+ this.debug.log(`DHT Iterative GET: Found ${validResponses.length} versions, timestamps: ${validResponses.map(r => r.timestamp).join(', ')}`);
1186
1188
  // Log inconsistency warning if timestamps are very different
1187
1189
  const timeDiffs = validResponses.map(r => Math.abs(mostRecent.timestamp - r.timestamp));
1188
1190
  const maxTimeDiff = Math.max(...timeDiffs);
1189
1191
  if (maxTimeDiff > 10000) { // More than 10 seconds difference
1190
- console.warn(`DHT CONSISTENCY WARNING: Large timestamp differences found (max: ${maxTimeDiff}ms). This suggests replica peers have inconsistent data!`);
1191
- console.warn('DHT CONSISTENCY: Detailed responses:', validResponses.map(r => ({
1192
+ this.debug.warn(`DHT CONSISTENCY WARNING: Large timestamp differences found (max: ${maxTimeDiff}ms). This suggests replica peers have inconsistent data!`);
1193
+ this.debug.warn('DHT CONSISTENCY: Detailed responses:', validResponses.map(r => ({
1192
1194
  peer: r.peerId.substring(0, 8),
1193
1195
  timestamp: r.timestamp,
1194
1196
  value: JSON.stringify(r.data.value).substring(0, 30) + '...'
@@ -1202,7 +1204,7 @@ export class WebDHT extends EventEmitter {
1202
1204
  // ALWAYS add subscription when we successfully get a value
1203
1205
  if (subscribe) {
1204
1206
  this.mySubscriptions.add(keyId);
1205
- console.log(`DHT GET: Auto-subscribed to ${key}`);
1207
+ this.debug.log(`DHT GET: Auto-subscribed to ${key}`);
1206
1208
  }
1207
1209
 
1208
1210
  return mostRecent.data.value;
@@ -1212,7 +1214,7 @@ export class WebDHT extends EventEmitter {
1212
1214
  peersPerRound = Math.min(peersPerRound * 2, Math.max(1, Math.floor(availablePeers.length / maxRounds)));
1213
1215
  }
1214
1216
 
1215
- console.log(`DHT Iterative GET failed: key ${key} not found after querying ${queriedPeers.size - 1} peers`);
1217
+ this.debug.log(`DHT Iterative GET failed: key ${key} not found after querying ${queriedPeers.size - 1} peers`);
1216
1218
  return null;
1217
1219
  }
1218
1220
 
@@ -1223,7 +1225,7 @@ export class WebDHT extends EventEmitter {
1223
1225
  const targetReplicas = this.config.replicationFactor;
1224
1226
  const availablePeers = closestPeers.filter(peerId => peerId !== this.peerId);
1225
1227
 
1226
- console.log(`DHT Iterative PUT: Attempting to store ${storeData.key} on ${targetReplicas} of ${availablePeers.length} available peers`);
1228
+ this.debug.log(`DHT Iterative PUT: Attempting to store ${storeData.key} on ${targetReplicas} of ${availablePeers.length} available peers`);
1227
1229
 
1228
1230
  let successful = 0;
1229
1231
  let attempted = 0;
@@ -1234,14 +1236,14 @@ export class WebDHT extends EventEmitter {
1234
1236
  const batch = availablePeers.slice(i, i + batchSize);
1235
1237
  attempted += batch.length;
1236
1238
 
1237
- console.log(`DHT PUT Batch ${Math.floor(i / batchSize) + 1}: Storing on ${batch.length} peers`);
1239
+ this.debug.log(`DHT PUT Batch ${Math.floor(i / batchSize) + 1}: Storing on ${batch.length} peers`);
1238
1240
 
1239
1241
  const storePromises = batch.map(async (peerId) => {
1240
1242
  try {
1241
1243
  const result = await this.sendDHTMessage(peerId, 'store', storeData);
1242
1244
  return { peerId, success: true, result };
1243
1245
  } catch (error) {
1244
- console.warn(`DHT PUT: Failed to store on peer ${peerId.substring(0, 8)}:`, error.message);
1246
+ this.debug.warn(`DHT PUT: Failed to store on peer ${peerId.substring(0, 8)}:`, error.message);
1245
1247
  return { peerId, success: false, error };
1246
1248
  }
1247
1249
  });
@@ -1255,7 +1257,7 @@ export class WebDHT extends EventEmitter {
1255
1257
 
1256
1258
  successful += batchSuccessful;
1257
1259
 
1258
- console.log(`DHT PUT Batch completed: ${batchSuccessful}/${batch.length} successful (total: ${successful}/${targetReplicas})`);
1260
+ this.debug.log(`DHT PUT Batch completed: ${batchSuccessful}/${batch.length} successful (total: ${successful}/${targetReplicas})`);
1259
1261
 
1260
1262
  // If we've reached our target, we can stop
1261
1263
  if (successful >= targetReplicas) {
@@ -1268,7 +1270,7 @@ export class WebDHT extends EventEmitter {
1268
1270
  }
1269
1271
  }
1270
1272
 
1271
- console.log(`DHT PUT completed: ${successful}/${targetReplicas} target replicas stored (${attempted} peers attempted)`);
1273
+ this.debug.log(`DHT PUT completed: ${successful}/${targetReplicas} target replicas stored (${attempted} peers attempted)`);
1272
1274
  return successful > 0;
1273
1275
  }
1274
1276
  }