@leofcoin/chain 1.10.21 → 1.10.22
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 +21 -0
- package/exports/browser/beacon-wire.js +1 -1
- package/exports/browser/{browser-CCfR9KOM-BGMMnVsR.js → browser-CCfR9KOM-UO9mzy-j.js} +2 -2
- package/exports/browser/chain.js +55 -125
- package/exports/browser/client-DbZBfpG6-CYDyUGUC.js +5943 -0
- package/exports/browser/{connection-monitor-D-krNiRH.js → connection-monitor-DMGAhcC0.js} +111 -2
- package/exports/browser/{index-dTQVgkpf-DOL2rIjp.js → index-dTQVgkpf-C6SJ0ZYW.js} +2 -2
- package/exports/browser/{index-DCbLvMKL.js → index-dxD17Fom.js} +231 -227
- package/exports/browser/{messages-DCePyqKC-BSvJxDWZ.js → messages-DZYCxoZI-D6kIiLKF.js} +2 -2
- package/exports/browser/{node-browser-Ha4Lvntj.js → node-browser-BMfelxXV.js} +23 -7
- package/exports/browser/node-browser.js +2 -2
- package/exports/chain.js +54 -124
- package/exports/connection-monitor-h2L8vsS7.js +435 -0
- package/exports/connection-monitor.js +2 -324
- package/exports/node.js +3 -1
- package/package.json +5 -5
- package/exports/browser/client-DKKzPSlb-C_1ug6ux.js +0 -2401
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { Codec } from '@leofcoin/codec-format-interface';
|
|
2
|
+
|
|
3
|
+
const limit = 1800;
|
|
4
|
+
const transactionLimit = 2500;
|
|
5
|
+
const requestTimeout = 3e4;
|
|
6
|
+
const syncTimeout = 3e4;
|
|
7
|
+
class Protocol {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.resolveTimeout = 1e4;
|
|
10
|
+
if (config?.resolveTimeout) this.resolveTimeout = config.resolveTimeout;
|
|
11
|
+
}
|
|
12
|
+
get limit() {
|
|
13
|
+
return limit;
|
|
14
|
+
}
|
|
15
|
+
get transactionLimit() {
|
|
16
|
+
return transactionLimit;
|
|
17
|
+
}
|
|
18
|
+
get requestTimeout() {
|
|
19
|
+
return requestTimeout;
|
|
20
|
+
}
|
|
21
|
+
get syncTimeout() {
|
|
22
|
+
return syncTimeout;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const INITIAL_RECONNECT_DELAY = 5e3;
|
|
27
|
+
const MAX_RECONNECT_DELAY = 3e4;
|
|
28
|
+
const guardProviderDiscovery = (peernet) => {
|
|
29
|
+
peernet.walk = async (hash) => {
|
|
30
|
+
if (!hash) throw new Error("hash expected for provider discovery");
|
|
31
|
+
const request = new peernet.protos["peernet-dht"]({ hash });
|
|
32
|
+
const message = await peernet.prepareMessage(request);
|
|
33
|
+
const connections = peernet.connections;
|
|
34
|
+
const requests = Object.entries(connections).filter(
|
|
35
|
+
([id, peer]) => id !== peernet.id && peer.connected
|
|
36
|
+
).map(async ([id, peer]) => {
|
|
37
|
+
let timeout;
|
|
38
|
+
try {
|
|
39
|
+
const result = await Promise.race([
|
|
40
|
+
peer.request(message.encoded),
|
|
41
|
+
new Promise((_, reject) => {
|
|
42
|
+
timeout = setTimeout(() => reject(new Error("provider discovery timed out")), requestTimeout);
|
|
43
|
+
})
|
|
44
|
+
]);
|
|
45
|
+
const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
|
|
46
|
+
if (new Codec(bytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
|
|
47
|
+
const response = new peernet.protos["peernet-dht-response"](bytes).decoded;
|
|
48
|
+
if (response.hash !== hash) throw new Error("provider response hash mismatch");
|
|
49
|
+
if (!response.has) throw new Error("peer does not provide content");
|
|
50
|
+
peernet.dht.addProvider({ id, address: peer.remoteAddress }, hash);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error.message !== "peer does not provide content") {
|
|
53
|
+
console.warn(`Provider discovery failed for ${id}:`, error?.message ?? error);
|
|
54
|
+
}
|
|
55
|
+
throw error;
|
|
56
|
+
} finally {
|
|
57
|
+
clearTimeout(timeout);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (requests.length === 0) return;
|
|
61
|
+
try {
|
|
62
|
+
await Promise.any(requests);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (!(error instanceof AggregateError)) throw error;
|
|
65
|
+
console.warn(`No connected provider found for ${hash}`);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const requestData = peernet.requestData.bind(peernet);
|
|
69
|
+
peernet.requestData = async (hash, store, options) => {
|
|
70
|
+
const storeName = typeof store === "string" ? store : store?.name;
|
|
71
|
+
if (!["block", "transaction", "transactionPool"].includes(storeName)) {
|
|
72
|
+
return requestData(hash, store, options);
|
|
73
|
+
}
|
|
74
|
+
const probe = await peernet.prepareMessage(new peernet.protos["peernet-dht"]({ hash, store: storeName }));
|
|
75
|
+
const request = await peernet.prepareMessage(new peernet.protos["peernet-data"]({ hash, store: storeName }));
|
|
76
|
+
const connections = peernet.connections;
|
|
77
|
+
const requests = Object.entries(connections).filter(
|
|
78
|
+
([id, peer]) => id !== peernet.id && peer.connected
|
|
79
|
+
).map(async ([id, peer]) => {
|
|
80
|
+
let timeout;
|
|
81
|
+
try {
|
|
82
|
+
const fetch = async () => {
|
|
83
|
+
const availability = await peer.request(probe.encoded);
|
|
84
|
+
const probeBytes = availability instanceof Uint8Array ? availability : new Uint8Array(Object.values(availability));
|
|
85
|
+
if (new Codec(probeBytes).name !== "peernet-dht-response") throw new Error("invalid provider response codec");
|
|
86
|
+
const provider = new peernet.protos["peernet-dht-response"](probeBytes).decoded;
|
|
87
|
+
if (provider.hash !== hash || !provider.has) throw new Error("peer does not provide content in requested store");
|
|
88
|
+
const result = await peer.request(request.encoded);
|
|
89
|
+
const bytes = result instanceof Uint8Array ? result : new Uint8Array(Object.values(result));
|
|
90
|
+
if (new Codec(bytes).name !== "peernet-data-response") throw new Error("invalid data response codec");
|
|
91
|
+
const response = new peernet.protos["peernet-data-response"](bytes).decoded;
|
|
92
|
+
if (response.hash !== hash || !(response.data instanceof Uint8Array) || response.data.length === 0) {
|
|
93
|
+
throw new Error("invalid content response");
|
|
94
|
+
}
|
|
95
|
+
return response.data;
|
|
96
|
+
};
|
|
97
|
+
return await Promise.race([
|
|
98
|
+
fetch(),
|
|
99
|
+
new Promise((_, reject) => {
|
|
100
|
+
timeout = setTimeout(() => reject(new Error(`content request timed out for ${id}`)), requestTimeout);
|
|
101
|
+
})
|
|
102
|
+
]);
|
|
103
|
+
} finally {
|
|
104
|
+
clearTimeout(timeout);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
return await Promise.any(requests);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
throw new Error(`Content unavailable in ${storeName}: ${hash}`, { cause: error });
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
const isSendablePeer = (peer) => {
|
|
115
|
+
if (!peer || typeof peer !== "object") return false;
|
|
116
|
+
const candidate = peer;
|
|
117
|
+
if (typeof candidate.send !== "function") return false;
|
|
118
|
+
if (candidate.connected === true) return true;
|
|
119
|
+
return typeof candidate.once === "function" && typeof candidate.removeListener === "function";
|
|
120
|
+
};
|
|
121
|
+
const guardPeerMessages = (peernet) => {
|
|
122
|
+
const sendMessage = peernet.sendMessage.bind(peernet);
|
|
123
|
+
peernet.sendMessage = (peer, ...args) => {
|
|
124
|
+
if (isSendablePeer(peer)) return sendMessage(peer, ...args);
|
|
125
|
+
for (const [peerId, connection] of Object.entries(peernet.connections || {})) {
|
|
126
|
+
if (connection === peer) delete peernet.connections[peerId];
|
|
127
|
+
}
|
|
128
|
+
return Promise.resolve(void 0);
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
class ConnectionMonitor {
|
|
132
|
+
#isMonitoring = false;
|
|
133
|
+
#checkInterval = null;
|
|
134
|
+
#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
135
|
+
#reconnectTimer = null;
|
|
136
|
+
#healthCheckInterval = 6e4;
|
|
137
|
+
#version;
|
|
138
|
+
#lastHealthCheckAt = 0;
|
|
139
|
+
#reconnecting = false;
|
|
140
|
+
// event handlers to remove later
|
|
141
|
+
#onOnline = null;
|
|
142
|
+
#onVisibilityChange = null;
|
|
143
|
+
#onSigcont = null;
|
|
144
|
+
#onPeerConnected = null;
|
|
145
|
+
get isMonitoring() {
|
|
146
|
+
return this.#isMonitoring;
|
|
147
|
+
}
|
|
148
|
+
get connectedPeers() {
|
|
149
|
+
return Object.values(globalThis.peernet?.connections || {}).filter((peer) => peer.connected);
|
|
150
|
+
}
|
|
151
|
+
get compatiblePeers() {
|
|
152
|
+
return this.connectedPeers.filter((peer) => {
|
|
153
|
+
if (!peer.version || !this.#version) return false;
|
|
154
|
+
const [peerMajor, peerMinor] = peer.version.split(".");
|
|
155
|
+
const [localMajor, localMinor] = this.#version.split(".");
|
|
156
|
+
return peerMajor === localMajor && peerMinor === localMinor;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
get disconnectedPeers() {
|
|
160
|
+
return Object.values(globalThis.peernet?.connections || {}).filter((peer) => !peer.connected);
|
|
161
|
+
}
|
|
162
|
+
#pruneInvalidConnections() {
|
|
163
|
+
const connections = globalThis.peernet?.connections;
|
|
164
|
+
if (!connections) return;
|
|
165
|
+
for (const [peerId, peer] of Object.entries(connections)) {
|
|
166
|
+
if (!isSendablePeer(peer)) {
|
|
167
|
+
delete connections[peerId];
|
|
168
|
+
console.warn(`Removed invalid peer connection: ${peerId}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
start(version) {
|
|
173
|
+
this.#version = version;
|
|
174
|
+
console.log(`\u{1F517} Connection Monitor initialized for version: ${this.#version}`);
|
|
175
|
+
if (this.#isMonitoring) return;
|
|
176
|
+
this.#isMonitoring = true;
|
|
177
|
+
console.log("\u{1F504} Starting connection monitor...");
|
|
178
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
179
|
+
this.#onOnline = () => {
|
|
180
|
+
console.log("\u{1F310} Network online \u2014 attempting restore");
|
|
181
|
+
void this.#restoreNetwork();
|
|
182
|
+
};
|
|
183
|
+
window.addEventListener("online", this.#onOnline);
|
|
184
|
+
this.#onVisibilityChange = () => {
|
|
185
|
+
if (document.visibilityState === "visible") {
|
|
186
|
+
console.log("\u{1F4A1} Visibility regained");
|
|
187
|
+
if (this.connectedPeers.length === 0) {
|
|
188
|
+
console.log("\u{1F4A1} Visibility regained \u2014 attempting restore");
|
|
189
|
+
void this.#restoreNetwork();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
document.addEventListener("visibilitychange", this.#onVisibilityChange);
|
|
194
|
+
}
|
|
195
|
+
if (typeof process !== "undefined" && typeof process.on === "function") {
|
|
196
|
+
this.#onSigcont = () => {
|
|
197
|
+
console.log("\u{1F514} Process resumed (SIGCONT) \u2014 attempting restore");
|
|
198
|
+
void this.#restoreNetwork();
|
|
199
|
+
};
|
|
200
|
+
try {
|
|
201
|
+
process.on("SIGCONT", this.#onSigcont);
|
|
202
|
+
} catch (e) {
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
this.#lastHealthCheckAt = Date.now();
|
|
206
|
+
this.#checkInterval = setInterval(() => {
|
|
207
|
+
this.#healthCheck();
|
|
208
|
+
}, this.#healthCheckInterval);
|
|
209
|
+
this.#onPeerConnected = () => {
|
|
210
|
+
if (this.#reconnectTimer) {
|
|
211
|
+
clearTimeout(this.#reconnectTimer);
|
|
212
|
+
this.#reconnectTimer = null;
|
|
213
|
+
}
|
|
214
|
+
this.#reconnecting = false;
|
|
215
|
+
this.#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
216
|
+
};
|
|
217
|
+
globalThis.pubsub?.subscribe("peer:connected", this.#onPeerConnected);
|
|
218
|
+
}
|
|
219
|
+
stop() {
|
|
220
|
+
if (!this.#isMonitoring) return;
|
|
221
|
+
this.#isMonitoring = false;
|
|
222
|
+
if (this.#checkInterval) {
|
|
223
|
+
clearInterval(this.#checkInterval);
|
|
224
|
+
this.#checkInterval = null;
|
|
225
|
+
}
|
|
226
|
+
if (typeof window !== "undefined") {
|
|
227
|
+
if (this.#onOnline) {
|
|
228
|
+
window.removeEventListener("online", this.#onOnline);
|
|
229
|
+
this.#onOnline = null;
|
|
230
|
+
}
|
|
231
|
+
if (this.#onVisibilityChange) {
|
|
232
|
+
document.removeEventListener("visibilitychange", this.#onVisibilityChange);
|
|
233
|
+
this.#onVisibilityChange = null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (typeof process !== "undefined" && typeof process.removeListener === "function" && this.#onSigcont) {
|
|
237
|
+
try {
|
|
238
|
+
process.removeListener("SIGCONT", this.#onSigcont);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
}
|
|
241
|
+
this.#onSigcont = null;
|
|
242
|
+
}
|
|
243
|
+
if (this.#onPeerConnected) {
|
|
244
|
+
globalThis.pubsub?.unsubscribe("peer:connected", this.#onPeerConnected);
|
|
245
|
+
this.#onPeerConnected = null;
|
|
246
|
+
}
|
|
247
|
+
if (this.#reconnectTimer) {
|
|
248
|
+
clearTimeout(this.#reconnectTimer);
|
|
249
|
+
this.#reconnectTimer = null;
|
|
250
|
+
}
|
|
251
|
+
console.log("\u23F9\uFE0F Connection monitor stopped");
|
|
252
|
+
}
|
|
253
|
+
async #healthCheck() {
|
|
254
|
+
this.#pruneInvalidConnections();
|
|
255
|
+
const now = Date.now();
|
|
256
|
+
const expectedNext = this.#lastHealthCheckAt + this.#healthCheckInterval;
|
|
257
|
+
const drift = now - expectedNext;
|
|
258
|
+
this.#lastHealthCheckAt = now;
|
|
259
|
+
if (drift > 5e3) {
|
|
260
|
+
console.log(`\u23F0 Detected timer drift of ${drift}ms (likely system sleep) \u2014 attempting restore`);
|
|
261
|
+
void this.#restoreNetwork();
|
|
262
|
+
}
|
|
263
|
+
const connectedPeers = this.connectedPeers;
|
|
264
|
+
const compatiblePeers = this.compatiblePeers;
|
|
265
|
+
const disconnectedPeers = this.disconnectedPeers;
|
|
266
|
+
console.log(
|
|
267
|
+
`\u{1F50D} Health check: ${connectedPeers.length} connected, ${compatiblePeers.length} compatible, ${disconnectedPeers.length} negotiating`
|
|
268
|
+
);
|
|
269
|
+
if (connectedPeers.length === 0 && disconnectedPeers.length === 0 && !this.#reconnectTimer) {
|
|
270
|
+
console.warn("\u26A0\uFE0F No peer connections detected \u2014 attempting reconnection");
|
|
271
|
+
await this.#attemptReconnection();
|
|
272
|
+
} else if (compatiblePeers.length === 0 && connectedPeers.length > 0 && !this.#reconnectTimer) {
|
|
273
|
+
console.warn("\u26A0\uFE0F No compatible peers found \u2014 attempting reconnection");
|
|
274
|
+
await this.#attemptReconnection();
|
|
275
|
+
}
|
|
276
|
+
globalThis.pubsub?.publish("connection-status", {
|
|
277
|
+
connected: connectedPeers.length,
|
|
278
|
+
compatible: compatiblePeers.length,
|
|
279
|
+
healthy: compatiblePeers.length > 0
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
// lightweight TCP probe to detect internet connectivity in Node.js
|
|
283
|
+
async #isOnLine(timeout = 1500) {
|
|
284
|
+
if (navigator?.onLine !== void 0) {
|
|
285
|
+
return navigator.onLine;
|
|
286
|
+
}
|
|
287
|
+
return new Promise(async (resolve) => {
|
|
288
|
+
try {
|
|
289
|
+
const net = await import('net');
|
|
290
|
+
const socket = new net.Socket();
|
|
291
|
+
let finished = false;
|
|
292
|
+
const finish = (val) => {
|
|
293
|
+
if (finished) return;
|
|
294
|
+
finished = true;
|
|
295
|
+
try {
|
|
296
|
+
socket.destroy();
|
|
297
|
+
} catch (e) {
|
|
298
|
+
}
|
|
299
|
+
resolve(val);
|
|
300
|
+
};
|
|
301
|
+
socket.setTimeout(timeout);
|
|
302
|
+
socket.once("connect", () => finish(true));
|
|
303
|
+
socket.once("error", () => finish(false));
|
|
304
|
+
socket.once("timeout", () => finish(false));
|
|
305
|
+
socket.connect(53, "1.1.1.1");
|
|
306
|
+
} catch (e) {
|
|
307
|
+
resolve(false);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
// Called on visibility/online/resume events
|
|
312
|
+
async #restoreNetwork() {
|
|
313
|
+
if (this.#reconnecting) {
|
|
314
|
+
console.log("\u{1F501} Reconnection already in progress, skipping");
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
this.#reconnecting = true;
|
|
318
|
+
if (this.connectedPeers.length > 0) {
|
|
319
|
+
console.log("\u2705 Already connected to peers, skipping restoration");
|
|
320
|
+
this.#reconnecting = false;
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
console.log("\u{1F501} Restoring network");
|
|
324
|
+
try {
|
|
325
|
+
const online = await this.#isOnLine(1500);
|
|
326
|
+
if (!online) {
|
|
327
|
+
console.warn("\u26A0\uFE0F No internet detected, skipping restore");
|
|
328
|
+
this.#reconnecting = false;
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
} catch (e) {
|
|
332
|
+
console.warn("\u26A0\uFE0F Online probe failed, proceeding with restore", e?.message || e);
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
console.log("\u{1F504} Attempting network restoration...");
|
|
336
|
+
if (globalThis.peernet?.client?.reinit) {
|
|
337
|
+
console.log(" \u2192 Trying client.reinit()");
|
|
338
|
+
try {
|
|
339
|
+
await globalThis.peernet.client.reinit();
|
|
340
|
+
console.log(" \u2705 client.reinit() succeeded");
|
|
341
|
+
} catch (e) {
|
|
342
|
+
console.warn(" \u26A0\uFE0F client.reinit() failed:", e?.message || e);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (globalThis.peernet?.start) {
|
|
346
|
+
console.log(" \u2192 Trying peernet.start()");
|
|
347
|
+
try {
|
|
348
|
+
await globalThis.peernet.start();
|
|
349
|
+
console.log(" \u2705 peernet.start() succeeded");
|
|
350
|
+
} catch (e) {
|
|
351
|
+
console.warn(" \u26A0\uFE0F peernet.start() failed:", e?.message || e);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
const networkName = globalThis.peernet?.network;
|
|
356
|
+
if (networkName && typeof networkName === "string") {
|
|
357
|
+
const { default: networks } = await import('@leofcoin/networks');
|
|
358
|
+
const [mainKey, subKey] = networkName.split(":");
|
|
359
|
+
const networkConfig = networks?.[mainKey]?.[subKey];
|
|
360
|
+
if (networkConfig?.stars && Array.isArray(networkConfig.stars)) {
|
|
361
|
+
console.log(" \u2192 Attempting to dial star servers:", networkConfig.stars.join(", "));
|
|
362
|
+
for (const star of networkConfig.stars) {
|
|
363
|
+
try {
|
|
364
|
+
if (globalThis.peernet?.client && "dial" in globalThis.peernet.client) {
|
|
365
|
+
await globalThis.peernet.client.dial(star);
|
|
366
|
+
console.log(` \u2705 Connected to star server: ${star}`);
|
|
367
|
+
}
|
|
368
|
+
} catch (e) {
|
|
369
|
+
console.warn(` \u26A0\uFE0F Failed to dial ${star}:`, e?.message || e);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
} catch (e) {
|
|
375
|
+
console.warn(" \u26A0\uFE0F Could not load or dial star servers:", e?.message || e);
|
|
376
|
+
}
|
|
377
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
378
|
+
this.#pruneInvalidConnections();
|
|
379
|
+
const connectedAfter = this.connectedPeers.length;
|
|
380
|
+
console.log(`\u{1F504} Restoration complete. Connected peers: ${connectedAfter}`);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
console.error("\u274C Network restoration failed:", error?.message || error);
|
|
383
|
+
} finally {
|
|
384
|
+
this.#reconnecting = false;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async waitForPeers(timeoutMs = 3e4) {
|
|
388
|
+
return new Promise((resolve) => {
|
|
389
|
+
const startTime = Date.now();
|
|
390
|
+
const checkPeers = () => {
|
|
391
|
+
if (this.compatiblePeers.length > 0) {
|
|
392
|
+
resolve(true);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (Date.now() - startTime >= timeoutMs) {
|
|
396
|
+
resolve(false);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
setTimeout(checkPeers, 1e3);
|
|
400
|
+
};
|
|
401
|
+
checkPeers();
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
async #attemptReconnection() {
|
|
405
|
+
try {
|
|
406
|
+
await this.#restoreNetwork();
|
|
407
|
+
const hasConnections = this.connectedPeers.length > 0 || this.disconnectedPeers.length > 0;
|
|
408
|
+
if (hasConnections) {
|
|
409
|
+
console.log("\u2705 Reconnection successful, resetting backoff delay");
|
|
410
|
+
this.#reconnectDelay = INITIAL_RECONNECT_DELAY;
|
|
411
|
+
} else {
|
|
412
|
+
console.warn("\u26A0\uFE0F Reconnection attempt completed but no peers connected");
|
|
413
|
+
this.#scheduleReconnect();
|
|
414
|
+
}
|
|
415
|
+
} catch (error) {
|
|
416
|
+
console.error("\u274C Reconnection failed:", error?.message || error);
|
|
417
|
+
this.#scheduleReconnect();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
#scheduleReconnect() {
|
|
421
|
+
if (!this.#isMonitoring || this.#reconnectTimer) return;
|
|
422
|
+
if (this.#reconnectDelay >= MAX_RECONNECT_DELAY) {
|
|
423
|
+
console.warn("\u26A0\uFE0F No peers available; automatic retries paused until the next health check or network event");
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
this.#reconnectDelay = Math.min(this.#reconnectDelay * 1.5, MAX_RECONNECT_DELAY);
|
|
427
|
+
console.warn(`\u26A0\uFE0F Increasing reconnection delay to ${this.#reconnectDelay} ms`);
|
|
428
|
+
this.#reconnectTimer = setTimeout(() => {
|
|
429
|
+
this.#reconnectTimer = null;
|
|
430
|
+
void this.#attemptReconnection();
|
|
431
|
+
}, this.#reconnectDelay);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export { ConnectionMonitor as C, Protocol as P, guardProviderDiscovery as a, guardPeerMessages as g, isSendablePeer as i };
|