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.
package/README.md CHANGED
@@ -20,8 +20,9 @@ PeerPigeon is a production-ready library for building decentralized applications
20
20
  - **🗄️ Distributed Storage** - Encrypted, CRDT-enabled storage across the mesh
21
21
  - **🎥 Selective Streaming** - Efficient audio/video streaming with bandwidth management
22
22
  - **📦 Binary Messages** - Native support for efficient binary data transfer (Uint8Array/ArrayBuffer)
23
- - **� Stream API** - Transfer large files with ReadableStream/WritableStream (memory-efficient, backpressure handling)
24
- - **�🏢 Hub System** - Connect multiple signaling servers for global peer discovery
23
+ - **🌊 Stream API** - Transfer large files with ReadableStream/WritableStream (memory-efficient, backpressure handling)
24
+ - **🏢 Hub System** - Connect multiple signaling servers for global peer discovery
25
+ - **🔗 Hub P2P Mesh** - Hubs form WebRTC mesh with XOR routing, auto-migrate from WebSocket to P2P
25
26
  - **🔐 End-to-End Encryption** - Built-in crypto for secure communication
26
27
  - **💰 Cost Optimized** - Smart routing reduces server costs by ~95%
27
28
  - **📱 Multi-Platform** - Browser, Node.js, NativeScript support
@@ -197,9 +198,16 @@ const hub2 = new PeerPigeonServer({
197
198
  });
198
199
  await hub2.start();
199
200
 
201
+ // Hubs automatically form P2P mesh with XOR routing
202
+ // WebSocket connections are closed once P2P mesh is established
203
+ // Clients connect via WebSocket, hub-to-hub uses P2P for efficiency
204
+
200
205
  // Peers on hub2 can discover peers on hub1!
201
206
  ```
202
207
 
208
+ **New in v1.1**: Hubs now form a true P2P mesh network using WebRTC with XOR-based routing. WebSocket connections between hubs are automatically closed once the P2P mesh is established, significantly reducing server load. See [docs/HUB_P2P_MESH.md](docs/HUB_P2P_MESH.md) for details.
209
+ ```
210
+
203
211
  ## 📖 Documentation
204
212
 
205
213
  ### Core Guides
@@ -4404,12 +4404,26 @@ ${b64.match(/.{1,64}/g).join("\n")}
4404
4404
  this.discoveredPeers.set(peerId, Date.now());
4405
4405
  this.emit("peerDiscovered", { peerId });
4406
4406
  this.debug.log(`Discovered peer ${peerId.substring(0, 8)}...`);
4407
- if (this.autoDiscovery && this.shouldInitiateConnection(peerId) && !this.connectionAttempts.has(peerId)) {
4407
+ const shouldInitiate = this.shouldInitiateConnection(peerId);
4408
+ const alreadyAttempting = this.connectionAttempts.has(peerId);
4409
+ this.debug.log(`\u{1F50D} Connection decision for ${peerId.substring(0, 8)}...: autoDiscovery=${this.autoDiscovery}, shouldInitiate=${shouldInitiate}, alreadyAttempting=${alreadyAttempting}`);
4410
+ if (this.autoDiscovery && shouldInitiate && !alreadyAttempting) {
4408
4411
  this.debug.log(`Considering connection to ${peerId.substring(0, 8)}...`);
4409
4412
  const canAccept = this.canAcceptMorePeers();
4413
+ this.debug.log(`Can accept more peers: ${canAccept}`);
4410
4414
  if (canAccept) {
4411
- this.debug.log(`Connecting to ${peerId.substring(0, 8)}...`);
4415
+ this.debug.log(`\u{1F680} Connecting to ${peerId.substring(0, 8)}...`);
4412
4416
  this.emit("connectToPeer", { peerId });
4417
+ } else {
4418
+ this.debug.log(`\u274C Cannot accept more peers (at capacity)`);
4419
+ }
4420
+ } else {
4421
+ if (!this.autoDiscovery) {
4422
+ this.debug.log(`\u274C Auto-discovery disabled, not connecting to ${peerId.substring(0, 8)}...`);
4423
+ } else if (!shouldInitiate) {
4424
+ this.debug.log(`\u23F8\uFE0F Not initiating to ${peerId.substring(0, 8)}... (they should initiate to us)`);
4425
+ } else if (alreadyAttempting) {
4426
+ this.debug.log(`\u23F8\uFE0F Already attempting connection to ${peerId.substring(0, 8)}...`);
4413
4427
  }
4414
4428
  }
4415
4429
  this.scheduleMeshOptimization();
@@ -6332,15 +6346,16 @@ ${b64.match(/.{1,64}/g).join("\n")}
6332
6346
  }
6333
6347
  canAcceptMorePeers() {
6334
6348
  const connectedCount = this.getConnectedPeerCount();
6335
- if (connectedCount < this.mesh.maxPeers) {
6336
- return true;
6337
- }
6338
- const stalePeerCount = this.getStalePeerCount();
6339
6349
  const totalPeerCount = this.peers.size;
6340
- if (stalePeerCount > 0 && totalPeerCount >= this.mesh.maxPeers) {
6341
- this.debug.log(`At capacity (${connectedCount}/${this.mesh.maxPeers} connected, ${totalPeerCount} total) but have ${stalePeerCount} stale peers that can be evicted`);
6342
- return true;
6350
+ if (connectedCount >= this.mesh.maxPeers) {
6351
+ this.debug.log(`At or over capacity: ${connectedCount}/${this.mesh.maxPeers} connected, rejecting new connections`);
6352
+ return false;
6353
+ }
6354
+ if (totalPeerCount >= this.mesh.maxPeers) {
6355
+ this.debug.log(`Total peer count at capacity: ${totalPeerCount}/${this.mesh.maxPeers}, rejecting new connections`);
6356
+ return false;
6343
6357
  }
6358
+ return true;
6344
6359
  this.debug.log(`Cannot accept more peers: ${connectedCount}/${this.mesh.maxPeers} connected, ${totalPeerCount} total peers in Map`);
6345
6360
  return false;
6346
6361
  }
@@ -6605,6 +6620,11 @@ ${b64.match(/.{1,64}/g).join("\n")}
6605
6620
  this.debug.warn("Received invalid message from", fromPeerId?.substring(0, 8));
6606
6621
  return;
6607
6622
  }
6623
+ this.mesh.emit("messageReceived", {
6624
+ from: fromPeerId,
6625
+ data: message,
6626
+ timestamp: Date.now()
6627
+ });
6608
6628
  if (message.type === "binary" && message.data instanceof Uint8Array) {
6609
6629
  this.debug.log(`\u{1F4E6} Received binary message (${message.size} bytes) from ${fromPeerId.substring(0, 8)}...`);
6610
6630
  this.mesh.emit("binaryMessageReceived", {
@@ -11494,9 +11514,10 @@ ${b64.match(/.{1,64}/g).join("\n")}
11494
11514
  }
11495
11515
  if (environmentDetector.isBrowser) {
11496
11516
  try {
11517
+ const disableLocalhostMedia = typeof window !== "undefined" && window.DISABLE_LOCALHOST_MEDIA_REQUEST;
11497
11518
  const isLocalhost = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname === "";
11498
- if (isLocalhost && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
11499
- this.debug.log("\u{1F3A4} Requesting media permissions for localhost WebRTC...");
11519
+ if (!disableLocalhostMedia && isLocalhost && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
11520
+ this.debug.log("\u{1F3A4} Requesting media permissions for localhost WebRTC (not disabled)...");
11500
11521
  const stream = await navigator.mediaDevices.getUserMedia({
11501
11522
  audio: true,
11502
11523
  video: false
@@ -11508,6 +11529,8 @@ ${b64.match(/.{1,64}/g).join("\n")}
11508
11529
  stream.getTracks().forEach((track) => track.stop());
11509
11530
  this.debug.log("\u2705 Media permissions granted - localhost connections will work");
11510
11531
  }
11532
+ } else if (disableLocalhostMedia) {
11533
+ this.debug.log("\u{1F6AB} Skipping localhost media permission request (DISABLE_LOCALHOST_MEDIA_REQUEST flag set)");
11511
11534
  }
11512
11535
  } catch (error) {
11513
11536
  this.debug.warn("Could not request media permissions:", error.message);