@hive-p2p/browser 1.0.96 → 1.0.98
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/core/node-services.mjs +2 -2
- package/core/topologist.mjs +21 -9
- package/hive-p2p.min.js +3 -3
- package/package.json +1 -1
package/core/node-services.mjs
CHANGED
|
@@ -50,10 +50,10 @@ export class NodeServices {
|
|
|
50
50
|
}
|
|
51
51
|
#startWebSocketServer(domain = 'localhost', port = SERVICE.PORT) {
|
|
52
52
|
this.wsServer = new TRANSPORTS.WS_SERVER({ port, host: domain });
|
|
53
|
-
this.wsServer.on('error', (error) => console.warn(`WebSocket error on Node #${this.id}:`, error));
|
|
53
|
+
if (this.verbose > 1) this.wsServer.on('error', (error) => console.warn(`WebSocket error on Node #${this.id}:`, error));
|
|
54
54
|
this.wsServer.on('connection', (ws) => {
|
|
55
55
|
ws.on('close', () => { if (remoteId) for (const cb of this.peerStore.callbacks.disconnect) cb(remoteId, 'in'); });
|
|
56
|
-
ws.on('error', (error) => console.warn(`WebSocket error on Node #${this.id} with peer ${remoteId}:`, error.stack));
|
|
56
|
+
if (this.verbose > 1) ws.on('error', (error) => console.warn(`WebSocket error on Node #${this.id} with peer ${remoteId}:`, error.stack));
|
|
57
57
|
|
|
58
58
|
let remoteId;
|
|
59
59
|
ws.on('message', (data) => { // When peer proves his id, we can handle data normally
|
package/core/topologist.mjs
CHANGED
|
@@ -49,8 +49,9 @@ export class OfferQueue {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export class Topologist {
|
|
52
|
-
id; cryptoCodex; gossip; messager; peerStore; bootstraps; offersQueue = new OfferQueue();
|
|
53
|
-
/** @type {Map<string, boolean>} */
|
|
52
|
+
verbose; id; cryptoCodex; gossip; messager; peerStore; bootstraps; offersQueue = new OfferQueue();
|
|
53
|
+
/** @type {Map<string, boolean>} */ bootstrapConnexionStates = new Map();
|
|
54
|
+
/** @type {Set<string>} */ connectedBootstrapIds = new Set();
|
|
54
55
|
/** @type {import('./node-services.mjs').NodeServices | undefined} */ services;
|
|
55
56
|
/** @type {number} */ NEIGHBORS_TARGET;
|
|
56
57
|
/** @type {number} */ HALF_TARGET;
|
|
@@ -67,10 +68,9 @@ export class Topologist {
|
|
|
67
68
|
get isPublicNode() { return this.services?.publicUrl ? true : false; }
|
|
68
69
|
|
|
69
70
|
/** @param {string} selfId @param {import('./crypto-codex.mjs').CryptoCodex} cryptoCodex @param {import('./gossip.mjs').Gossip} gossip @param {import('./unicast.mjs').UnicastMessager} messager @param {import('./peer-store.mjs').PeerStore} peerStore @param {string[]} bootstraps */
|
|
70
|
-
constructor(selfId, cryptoCodex, gossip, messager, peerStore, bootstraps) {
|
|
71
|
+
constructor(selfId, cryptoCodex, gossip, messager, peerStore, bootstraps, verbose = 1) {
|
|
71
72
|
this.setNeighborsTarget(DISCOVERY.TARGET_NEIGHBORS_COUNT);
|
|
72
|
-
this.id = selfId; this.cryptoCodex = cryptoCodex; this.gossip = gossip; this.messager = messager; this.peerStore = peerStore;
|
|
73
|
-
for (const url of bootstraps) this.bootstrapsConnectionState.set(url, false);
|
|
73
|
+
this.verbose = verbose; this.id = selfId; this.cryptoCodex = cryptoCodex; this.gossip = gossip; this.messager = messager; this.peerStore = peerStore;
|
|
74
74
|
this.bootstraps = [...bootstraps].sort(() => Math.random() - 0.5); // shuffle
|
|
75
75
|
this.nextBootstrapIndex = Math.random() * this.bootstraps.length | 0;
|
|
76
76
|
}
|
|
@@ -132,7 +132,7 @@ export class Topologist {
|
|
|
132
132
|
else if (connectingCount + nonPublicNeighborsCount > this.TWICE_TARGET) return; // no more bootstrap needed
|
|
133
133
|
|
|
134
134
|
const publicUrl = this.bootstraps[this.nextBootstrapIndex++ % this.bootstraps.length];
|
|
135
|
-
if (this.
|
|
135
|
+
if (this.bootstrapConnexionStates.get(publicUrl)) return; // already connected/connecting
|
|
136
136
|
this.#connectToPublicNode(publicUrl);
|
|
137
137
|
}
|
|
138
138
|
|
|
@@ -169,12 +169,15 @@ export class Topologist {
|
|
|
169
169
|
}
|
|
170
170
|
#connectToPublicNode(publicUrl = 'localhost:8080') {
|
|
171
171
|
let remoteId = null;
|
|
172
|
+
let isAliasDetected = false; // for early close on "ids match" case
|
|
172
173
|
const ws = new TRANSPORTS.WS_CLIENT(this.#getFullWsUrl(publicUrl)); ws.binaryType = 'arraybuffer';
|
|
173
|
-
ws.onerror = (error) => console.warn(`WebSocket error:`, error.stack);
|
|
174
|
+
if (this.verbose > 1) ws.onerror = (error) => console.warn(`WebSocket error:`, error.stack);
|
|
174
175
|
ws.onopen = () => {
|
|
175
|
-
this.
|
|
176
|
+
this.bootstrapConnexionStates.set(publicUrl, true);
|
|
176
177
|
ws.onclose = () => {
|
|
177
|
-
|
|
178
|
+
if (isAliasDetected) return;
|
|
179
|
+
this.bootstrapConnexionStates.set(publicUrl, false);
|
|
180
|
+
this.connectedBootstrapIds.delete(remoteId);
|
|
178
181
|
for (const cb of this.peerStore.callbacks.disconnect) cb(remoteId, 'out');
|
|
179
182
|
}
|
|
180
183
|
ws.onmessage = (data) => {
|
|
@@ -192,6 +195,15 @@ export class Topologist {
|
|
|
192
195
|
if (!this.cryptoCodex.verifySignature(pubkey, signedData, signature)) return;
|
|
193
196
|
|
|
194
197
|
remoteId = route[0];
|
|
198
|
+
|
|
199
|
+
if (this.connectedBootstrapIds.has(remoteId)) { // AVOID ALIASES => REMOVE URL FROM BOOTSTRAP LIST
|
|
200
|
+
isAliasDetected = true;
|
|
201
|
+
this.bootstraps = this.bootstraps.filter(url => url !== publicUrl);
|
|
202
|
+
return ws.close();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// FINALIZE PEERSTORE CONNECTION
|
|
206
|
+
this.connectedBootstrapIds.add(remoteId);
|
|
195
207
|
this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
|
|
196
208
|
this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
|
|
197
209
|
if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
|