peerpigeon 1.1.0 → 1.1.1
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/dist/peerpigeon-browser.js +113 -15
- package/dist/peerpigeon-browser.js.map +2 -2
- package/docs/CLOUD_DEPLOYMENT.md +132 -0
- package/examples/browser/app.js +52 -4
- package/package.json +2 -1
- package/server/index.js +246 -118
- package/src/ConnectionManager.js +62 -3
- package/src/GossipManager.js +12 -1
- package/src/PeerDiscovery.js +11 -0
- package/src/PeerPigeonMesh.js +59 -1
- package/src/SignalingClient.js +25 -11
|
@@ -4224,15 +4224,16 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
4224
4224
|
};
|
|
4225
4225
|
this.websocket.onclose = (event) => {
|
|
4226
4226
|
clearTimeout(connectTimeout);
|
|
4227
|
+
const wasConnected = this.connected;
|
|
4227
4228
|
this.connected = false;
|
|
4228
4229
|
this.connectionPromise = null;
|
|
4230
|
+
this.isReconnecting = false;
|
|
4229
4231
|
this.debug.log("WebSocket closed:", event.code, event.reason);
|
|
4230
|
-
|
|
4232
|
+
if (wasConnected) {
|
|
4233
|
+
this.emit("disconnected");
|
|
4234
|
+
}
|
|
4231
4235
|
if (!this.deliberateDisconnect) {
|
|
4232
|
-
this.
|
|
4233
|
-
if (!this.isReconnecting) {
|
|
4234
|
-
this.attemptReconnect();
|
|
4235
|
-
}
|
|
4236
|
+
this.attemptReconnect();
|
|
4236
4237
|
} else {
|
|
4237
4238
|
this.deliberateDisconnect = false;
|
|
4238
4239
|
}
|
|
@@ -4274,6 +4275,14 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
4274
4275
|
this.debug.log("Max reconnection attempts reached, using exponential backoff");
|
|
4275
4276
|
const baseExtendedDelay = hasHealthyPeers ? 6e5 : this.maxReconnectDelay * 2;
|
|
4276
4277
|
const extendedDelay = Math.min(baseExtendedDelay, 6e5);
|
|
4278
|
+
this.emit("statusChanged", {
|
|
4279
|
+
type: "reconnecting",
|
|
4280
|
+
message: `Max attempts reached. Retrying in ${Math.round(extendedDelay / 1e3)}s...`,
|
|
4281
|
+
delay: extendedDelay,
|
|
4282
|
+
attempt: this.reconnectAttempts,
|
|
4283
|
+
maxAttempts: this.maxReconnectAttempts,
|
|
4284
|
+
extendedBackoff: true
|
|
4285
|
+
});
|
|
4277
4286
|
this.reconnectTimeout = setTimeout(() => {
|
|
4278
4287
|
this.reconnectAttempts = Math.floor(this.maxReconnectAttempts / 2);
|
|
4279
4288
|
this.attemptReconnect();
|
|
@@ -4286,18 +4295,21 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
4286
4295
|
const delayMultiplier = hasHealthyPeers ? 3 : 1;
|
|
4287
4296
|
const delay = Math.min(baseDelay * delayMultiplier, hasHealthyPeers ? 3e5 : this.maxReconnectDelay);
|
|
4288
4297
|
this.debug.log(`Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts}, healthy peers: ${hasHealthyPeers})`);
|
|
4298
|
+
this.emit("statusChanged", {
|
|
4299
|
+
type: "reconnecting",
|
|
4300
|
+
message: `Reconnecting in ${Math.round(delay / 1e3)}s (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`,
|
|
4301
|
+
delay,
|
|
4302
|
+
attempt: this.reconnectAttempts,
|
|
4303
|
+
maxAttempts: this.maxReconnectAttempts
|
|
4304
|
+
});
|
|
4289
4305
|
this.reconnectTimeout = setTimeout(async () => {
|
|
4290
4306
|
if (!this.connected && this.signalingUrl) {
|
|
4291
4307
|
try {
|
|
4292
4308
|
await this.connect(this.signalingUrl);
|
|
4293
4309
|
this.emit("statusChanged", { type: "info", message: "WebSocket reconnected successfully" });
|
|
4294
4310
|
} catch (error) {
|
|
4295
|
-
this.debug.error("Reconnection failed:", error);
|
|
4296
|
-
this.isReconnecting = false;
|
|
4297
|
-
this.attemptReconnect();
|
|
4311
|
+
this.debug.error("Reconnection attempt failed:", error);
|
|
4298
4312
|
}
|
|
4299
|
-
} else {
|
|
4300
|
-
this.isReconnecting = false;
|
|
4301
4313
|
}
|
|
4302
4314
|
}, delay);
|
|
4303
4315
|
}
|
|
@@ -4393,6 +4405,10 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
4393
4405
|
clearTimeout(this.meshOptimizationTimeout);
|
|
4394
4406
|
this.meshOptimizationTimeout = null;
|
|
4395
4407
|
}
|
|
4408
|
+
}
|
|
4409
|
+
// Full cleanup method for deliberate disconnects
|
|
4410
|
+
cleanup() {
|
|
4411
|
+
this.stop();
|
|
4396
4412
|
this.discoveredPeers.clear();
|
|
4397
4413
|
this.connectionAttempts.clear();
|
|
4398
4414
|
}
|
|
@@ -6089,8 +6105,14 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
6089
6105
|
}
|
|
6090
6106
|
const shouldBeInitiator = this.mesh.peerId > targetPeerId;
|
|
6091
6107
|
if (!shouldBeInitiator) {
|
|
6092
|
-
|
|
6093
|
-
|
|
6108
|
+
const connectedCount = this.getConnectedPeerCount();
|
|
6109
|
+
const floor = this.mesh.connectivityFloor || 0;
|
|
6110
|
+
if (connectedCount < floor) {
|
|
6111
|
+
this.debug.log(`\u{1F504} INITIATOR OVERRIDE: Forcing initiation to ${targetPeerId.substring(0, 8)}... (connected ${connectedCount} < floor ${floor})`);
|
|
6112
|
+
} else {
|
|
6113
|
+
this.debug.log(`\u{1F504} INITIATOR LOGIC: Not becoming initiator for ${targetPeerId.substring(0, 8)}... (our ID smaller, connected ${connectedCount} >= floor ${floor})`);
|
|
6114
|
+
return;
|
|
6115
|
+
}
|
|
6094
6116
|
}
|
|
6095
6117
|
this.debug.log(`\u{1F504} INITIATOR LOGIC: Becoming initiator for ${targetPeerId.substring(0, 8)}... (our ID: ${this.mesh.peerId.substring(0, 8)}... is greater)`);
|
|
6096
6118
|
if (!this.mesh.canAcceptMorePeers()) {
|
|
@@ -6151,10 +6173,44 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
6151
6173
|
this.mesh.emit("statusChanged", { type: "info", message: `Offer sent to ${targetPeerId.substring(0, 8)}...` });
|
|
6152
6174
|
} catch (error) {
|
|
6153
6175
|
this.debug.error("Failed to connect to peer:", error);
|
|
6154
|
-
|
|
6176
|
+
const isConnectionTimeout = error.message?.includes("timeout");
|
|
6177
|
+
if (!isConnectionTimeout) {
|
|
6178
|
+
this.mesh.emit("statusChanged", {
|
|
6179
|
+
type: "error",
|
|
6180
|
+
message: `Failed to connect to ${targetPeerId.substring(0, 8)}...: ${error.message}`
|
|
6181
|
+
});
|
|
6182
|
+
}
|
|
6155
6183
|
this.cleanupFailedConnection(targetPeerId);
|
|
6156
6184
|
}
|
|
6157
6185
|
}
|
|
6186
|
+
/**
|
|
6187
|
+
* Force connection attempt ignoring standard initiator lexicographic rule (used for connectivity floor).
|
|
6188
|
+
*/
|
|
6189
|
+
async connectToPeerOverride(targetPeerId) {
|
|
6190
|
+
if (this.peers.has(targetPeerId)) return;
|
|
6191
|
+
const connectedCount = this.getConnectedPeerCount();
|
|
6192
|
+
const floor = this.mesh.connectivityFloor || 0;
|
|
6193
|
+
if (connectedCount >= floor) return;
|
|
6194
|
+
this.debug.log(`\u26A1 OVERRIDE CONNECT: initiating to ${targetPeerId.substring(0, 8)}... (connected ${connectedCount} < floor ${floor})`);
|
|
6195
|
+
this.mesh.peerDiscovery.clearConnectionAttempt(targetPeerId);
|
|
6196
|
+
const options = {
|
|
6197
|
+
localStream: null,
|
|
6198
|
+
enableAudio: true,
|
|
6199
|
+
enableVideo: true
|
|
6200
|
+
};
|
|
6201
|
+
const peerConnection = new PeerConnection(targetPeerId, true, options);
|
|
6202
|
+
this.setupPeerConnectionHandlers(peerConnection);
|
|
6203
|
+
this.peers.set(targetPeerId, peerConnection);
|
|
6204
|
+
try {
|
|
6205
|
+
await peerConnection.createConnection();
|
|
6206
|
+
const offer = await peerConnection.createOffer();
|
|
6207
|
+
await this.mesh.sendSignalingMessage({ type: "offer", data: offer }, targetPeerId);
|
|
6208
|
+
} catch (e) {
|
|
6209
|
+
this.debug.warn(`Override connection failed to ${targetPeerId.substring(0, 8)}...: ${e.message}`);
|
|
6210
|
+
this.mesh.peerDiscovery.removeDiscoveredPeer(targetPeerId);
|
|
6211
|
+
this.peers.delete(targetPeerId);
|
|
6212
|
+
}
|
|
6213
|
+
}
|
|
6158
6214
|
cleanupFailedConnection(peerId) {
|
|
6159
6215
|
this.debug.log(`Cleaning up failed connection for ${peerId.substring(0, 8)}...`);
|
|
6160
6216
|
let peerRemoved = false;
|
|
@@ -7978,7 +8034,7 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
7978
8034
|
this.seenMessages = /* @__PURE__ */ new Map();
|
|
7979
8035
|
this.messageHistory = /* @__PURE__ */ new Map();
|
|
7980
8036
|
this.processedKeyExchanges = /* @__PURE__ */ new Map();
|
|
7981
|
-
this.maxTTL =
|
|
8037
|
+
this.maxTTL = 40;
|
|
7982
8038
|
this.messageExpiryTime = 5 * 60 * 1e3;
|
|
7983
8039
|
this.cleanupInterval = 60 * 1e3;
|
|
7984
8040
|
this.cleanupTimer = null;
|
|
@@ -8032,6 +8088,11 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
8032
8088
|
}
|
|
8033
8089
|
return messageId;
|
|
8034
8090
|
}
|
|
8091
|
+
/**
|
|
8092
|
+
* Attempt adaptive resend of recent broadcasts to peers that connected
|
|
8093
|
+
* after initial propagation window.
|
|
8094
|
+
*/
|
|
8095
|
+
// (Rollback) attemptAdaptiveResends removed
|
|
8035
8096
|
/**
|
|
8036
8097
|
* Send a direct message to a specific peer using gossip routing (DM)
|
|
8037
8098
|
* @param {string} targetPeerId - The destination peer's ID
|
|
@@ -11304,6 +11365,8 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
11304
11365
|
this.originalNetworkName = this.networkName;
|
|
11305
11366
|
this.maxPeers = options.maxPeers !== void 0 ? options.maxPeers : 3;
|
|
11306
11367
|
this.minPeers = options.minPeers !== void 0 ? options.minPeers : 2;
|
|
11368
|
+
const defaultFloor = Math.min(3, this.maxPeers ?? 3);
|
|
11369
|
+
this.connectivityFloor = options.connectivityFloor !== void 0 ? options.connectivityFloor : defaultFloor;
|
|
11307
11370
|
this.autoConnect = options.autoConnect !== false;
|
|
11308
11371
|
this.autoDiscovery = options.autoDiscovery !== false;
|
|
11309
11372
|
this.evictionStrategy = options.evictionStrategy !== false;
|
|
@@ -11334,6 +11397,7 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
11334
11397
|
this.setupManagerEventHandlers();
|
|
11335
11398
|
this.cleanupManager.setupUnloadHandlers();
|
|
11336
11399
|
this.storageManager.loadSignalingUrlFromStorage();
|
|
11400
|
+
this._connectivityEnforcementTimer = null;
|
|
11337
11401
|
}
|
|
11338
11402
|
setupManagerEventHandlers() {
|
|
11339
11403
|
this.connectionManager.addEventListener("peersUpdated", () => {
|
|
@@ -11571,6 +11635,7 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
11571
11635
|
maxPeers: this.maxPeers
|
|
11572
11636
|
});
|
|
11573
11637
|
this.setupDiscoveryHandlers();
|
|
11638
|
+
this.startConnectivityEnforcement();
|
|
11574
11639
|
if (this.cryptoManager) {
|
|
11575
11640
|
try {
|
|
11576
11641
|
this.debug.log("\u{1F510} Initializing crypto manager with automatic key persistence...");
|
|
@@ -11676,7 +11741,11 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
11676
11741
|
this.signalingClient.disconnect();
|
|
11677
11742
|
}
|
|
11678
11743
|
if (this.peerDiscovery) {
|
|
11679
|
-
this.peerDiscovery.
|
|
11744
|
+
this.peerDiscovery.cleanup();
|
|
11745
|
+
}
|
|
11746
|
+
if (this._connectivityEnforcementTimer) {
|
|
11747
|
+
clearInterval(this._connectivityEnforcementTimer);
|
|
11748
|
+
this._connectivityEnforcementTimer = null;
|
|
11680
11749
|
}
|
|
11681
11750
|
this.connectionManager.disconnectAllPeers();
|
|
11682
11751
|
this.connectionManager.cleanup();
|
|
@@ -11685,6 +11754,35 @@ ${b64.match(/.{1,64}/g).join("\n")}
|
|
|
11685
11754
|
this.gossipManager.cleanup();
|
|
11686
11755
|
this.emit("statusChanged", { type: "disconnected" });
|
|
11687
11756
|
}
|
|
11757
|
+
/**
|
|
11758
|
+
* Periodically attempt extra connections for under-connected peers until reaching connectivityFloor.
|
|
11759
|
+
*/
|
|
11760
|
+
startConnectivityEnforcement() {
|
|
11761
|
+
if (this._connectivityEnforcementTimer) return;
|
|
11762
|
+
const intervalMs = 4e3;
|
|
11763
|
+
this._connectivityEnforcementTimer = setInterval(() => {
|
|
11764
|
+
if (!this.connected || !this.peerDiscovery) return;
|
|
11765
|
+
const connectedCount = this.connectionManager.getConnectedPeerCount();
|
|
11766
|
+
if (connectedCount >= this.connectivityFloor) return;
|
|
11767
|
+
const discovered = this.peerDiscovery.getDiscoveredPeers().map((p) => p.peerId);
|
|
11768
|
+
const notConnected = discovered.filter(
|
|
11769
|
+
(pid) => !this.connectionManager.hasPeer(pid) && !this.peerDiscovery.isAttemptingConnection(pid)
|
|
11770
|
+
);
|
|
11771
|
+
if (notConnected.length === 0) return;
|
|
11772
|
+
const prioritized = notConnected.sort((a, b) => {
|
|
11773
|
+
const distA = this.peerDiscovery.calculateXorDistance(this.peerId, a);
|
|
11774
|
+
const distB = this.peerDiscovery.calculateXorDistance(this.peerId, b);
|
|
11775
|
+
return distA < distB ? -1 : 1;
|
|
11776
|
+
});
|
|
11777
|
+
const needed = this.connectivityFloor - connectedCount;
|
|
11778
|
+
const attemptLimit = Math.min(prioritized.length, Math.max(1, needed));
|
|
11779
|
+
const batch = prioritized.slice(0, attemptLimit);
|
|
11780
|
+
batch.forEach((pid) => {
|
|
11781
|
+
this.debug.log(`\u{1F527} CONNECTIVITY FLOOR (${connectedCount}/${this.connectivityFloor}) attempting extra connection to ${pid.substring(0, 8)}...`);
|
|
11782
|
+
this.connectionManager.connectToPeer(pid);
|
|
11783
|
+
});
|
|
11784
|
+
}, intervalMs);
|
|
11785
|
+
}
|
|
11688
11786
|
// Configuration methods
|
|
11689
11787
|
setMaxPeers(maxPeers) {
|
|
11690
11788
|
this.maxPeers = Math.max(1, Math.min(50, maxPeers));
|