peerpigeon 1.0.9 β†’ 1.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.
@@ -428,25 +428,24 @@ export class ConnectionManager extends EventEmitter {
428
428
  }
429
429
 
430
430
  canAcceptMorePeers() {
431
- // Count connected peers first (these are guaranteed slots)
431
+ // Count connected peers for capacity check
432
432
  const connectedCount = this.getConnectedPeerCount();
433
-
434
- // If we have room for connected peers, always accept
435
- if (connectedCount < this.mesh.maxPeers) {
436
- return true;
437
- }
438
-
439
- // If at max connected peers, check if we have stale peers we can evict
440
- const stalePeerCount = this.getStalePeerCount();
441
433
  const totalPeerCount = this.peers.size;
442
-
443
- // Accept if we have stale peers that can be cleaned up
444
- if (stalePeerCount > 0 && totalPeerCount >= this.mesh.maxPeers) {
445
- this.debug.log(`At capacity (${connectedCount}/${this.mesh.maxPeers} connected, ${totalPeerCount} total) but have ${stalePeerCount} stale peers that can be evicted`);
446
- return true;
434
+
435
+ // STRICT: Don't accept if we're at or over maxPeers
436
+ // Remove the "+1 overhead" logic that was causing overshoot
437
+ if (connectedCount >= this.mesh.maxPeers) {
438
+ this.debug.log(`At or over capacity: ${connectedCount}/${this.mesh.maxPeers} connected, rejecting new connections`);
439
+ return false;
447
440
  }
448
-
449
- // Reject if we're at capacity with all viable peers
441
+
442
+ // Also check total peer count (including connecting) to prevent race conditions
443
+ if (totalPeerCount >= this.mesh.maxPeers) {
444
+ this.debug.log(`Total peer count at capacity: ${totalPeerCount}/${this.mesh.maxPeers}, rejecting new connections`);
445
+ return false;
446
+ }
447
+
448
+ return true;
450
449
  this.debug.log(`Cannot accept more peers: ${connectedCount}/${this.mesh.maxPeers} connected, ${totalPeerCount} total peers in Map`);
451
450
  return false;
452
451
  }
@@ -760,6 +759,13 @@ export class ConnectionManager extends EventEmitter {
760
759
  return;
761
760
  }
762
761
 
762
+ // Emit generic messageReceived event for all messages (used by hub server)
763
+ this.mesh.emit('messageReceived', {
764
+ from: fromPeerId,
765
+ data: message,
766
+ timestamp: Date.now()
767
+ });
768
+
763
769
  // Handle binary messages first
764
770
  if (message.type === 'binary' && message.data instanceof Uint8Array) {
765
771
  this.debug.log(`πŸ“¦ Received binary message (${message.size} bytes) from ${fromPeerId.substring(0, 8)}...`);
@@ -51,13 +51,29 @@ export class PeerDiscovery extends EventEmitter {
51
51
  this.debug.log(`Discovered peer ${peerId.substring(0, 8)}...`);
52
52
 
53
53
  // Only auto-connect if we should initiate to this peer and we're not already trying
54
- if (this.autoDiscovery && this.shouldInitiateConnection(peerId) && !this.connectionAttempts.has(peerId)) {
54
+ const shouldInitiate = this.shouldInitiateConnection(peerId);
55
+ const alreadyAttempting = this.connectionAttempts.has(peerId);
56
+
57
+ this.debug.log(`πŸ” Connection decision for ${peerId.substring(0, 8)}...: autoDiscovery=${this.autoDiscovery}, shouldInitiate=${shouldInitiate}, alreadyAttempting=${alreadyAttempting}`);
58
+
59
+ if (this.autoDiscovery && shouldInitiate && !alreadyAttempting) {
55
60
  this.debug.log(`Considering connection to ${peerId.substring(0, 8)}...`);
56
61
 
57
62
  const canAccept = this.canAcceptMorePeers();
63
+ this.debug.log(`Can accept more peers: ${canAccept}`);
58
64
  if (canAccept) {
59
- this.debug.log(`Connecting to ${peerId.substring(0, 8)}...`);
65
+ this.debug.log(`πŸš€ Connecting to ${peerId.substring(0, 8)}...`);
60
66
  this.emit('connectToPeer', { peerId });
67
+ } else {
68
+ this.debug.log(`❌ Cannot accept more peers (at capacity)`);
69
+ }
70
+ } else {
71
+ if (!this.autoDiscovery) {
72
+ this.debug.log(`❌ Auto-discovery disabled, not connecting to ${peerId.substring(0, 8)}...`);
73
+ } else if (!shouldInitiate) {
74
+ this.debug.log(`⏸️ Not initiating to ${peerId.substring(0, 8)}... (they should initiate to us)`);
75
+ } else if (alreadyAttempting) {
76
+ this.debug.log(`⏸️ Already attempting connection to ${peerId.substring(0, 8)}...`);
61
77
  }
62
78
  }
63
79
 
@@ -334,17 +334,18 @@ export class PeerPigeonMesh extends EventEmitter {
334
334
  this.debug.warn('PigeonRTC initialization failed:', error.message);
335
335
  }
336
336
 
337
- // Request media permissions for localhost WebRTC to work
338
- // This is required for Chrome to gather ICE candidates on localhost
337
+ // Request media permissions for localhost WebRTC to work unless explicitly disabled
338
+ // Added DISABLE_LOCALHOST_MEDIA_REQUEST override for automated/headless test environments
339
339
  if (environmentDetector.isBrowser) {
340
340
  try {
341
+ const disableLocalhostMedia = typeof window !== 'undefined' && window.DISABLE_LOCALHOST_MEDIA_REQUEST;
341
342
  const isLocalhost = window.location.hostname === 'localhost' ||
342
343
  window.location.hostname === '127.0.0.1' ||
343
344
  window.location.hostname === '';
344
-
345
- if (isLocalhost && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
346
- this.debug.log('🎀 Requesting media permissions for localhost WebRTC...');
347
-
345
+
346
+ if (!disableLocalhostMedia && isLocalhost && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
347
+ this.debug.log('🎀 Requesting media permissions for localhost WebRTC (not disabled)...');
348
+
348
349
  // Request minimal audio permission (no video to reduce intrusiveness)
349
350
  const stream = await navigator.mediaDevices.getUserMedia({
350
351
  audio: true,
@@ -353,12 +354,14 @@ export class PeerPigeonMesh extends EventEmitter {
353
354
  this.debug.warn('Media permission denied - connections may fail on localhost:', err.message);
354
355
  return null;
355
356
  });
356
-
357
+
357
358
  // Immediately stop all tracks - we only needed the permission
358
359
  if (stream) {
359
360
  stream.getTracks().forEach(track => track.stop());
360
361
  this.debug.log('βœ… Media permissions granted - localhost connections will work');
361
362
  }
363
+ } else if (disableLocalhostMedia) {
364
+ this.debug.log('🚫 Skipping localhost media permission request (DISABLE_LOCALHOST_MEDIA_REQUEST flag set)');
362
365
  }
363
366
  } catch (error) {
364
367
  this.debug.warn('Could not request media permissions:', error.message);