@hive-p2p/server 1.0.23 → 1.0.24

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.
@@ -59,27 +59,23 @@ export class NodeServices {
59
59
  ws.on('message', (data) => { // When peer proves his id, we can handle data normally
60
60
  if (remoteId) for (const cb of this.peerStore.callbacks.data) cb(remoteId, data);
61
61
  else { // FIRST MESSAGE SHOULD BE HANDSHAKE WITH ID
62
- try {
63
- const d = new Uint8Array(data); if (d[0] > 127) return; // not unicast, ignore
64
- const message = this.cryptoCodex.readUnicastMessage(d);
65
- if (!message) return; // invalid unicast message, ignore
66
-
67
- const { route, type, neighborsList } = message;
68
- if (type !== 'handshake' || route.length !== 2) return;
69
-
70
- const { signatureStart, pubkey, signature } = message;
71
- const signedData = d.subarray(0, signatureStart);
72
- if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
73
-
74
- remoteId = route[0];
75
- this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
76
- this.peerStore.connecting[remoteId]?.out?.close(); // close outgoing connection if any
77
- if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
78
- this.peerStore.connecting[remoteId].in = new PeerConnection(remoteId, ws, 'in', true);
79
- for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'in');
80
- } catch (error) {
81
- console.error(`Error handling WebSocket message on Node #${this.id}:`, error);
82
- }
62
+ const d = new Uint8Array(data); if (d[0] > 127) return; // not unicast, ignore
63
+ const message = this.cryptoCodex.readUnicastMessage(d);
64
+ if (!message) return; // invalid unicast message, ignore
65
+
66
+ const { route, type, neighborsList } = message;
67
+ if (type !== 'handshake' || route.length !== 2) return;
68
+
69
+ const { signatureStart, pubkey, signature } = message;
70
+ const signedData = d.subarray(0, signatureStart);
71
+ if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
72
+
73
+ remoteId = route[0];
74
+ this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
75
+ this.peerStore.connecting[remoteId]?.out?.close(); // close outgoing connection if any
76
+ if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
77
+ this.peerStore.connecting[remoteId].in = new PeerConnection(remoteId, ws, 'in', true);
78
+ for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'in');
83
79
  }
84
80
  });
85
81
  ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
package/core/node.mjs CHANGED
@@ -16,7 +16,8 @@ import { NodeServices } from './node-services.mjs';
16
16
  * @param {string} [options.domain] If provided, the node will operate as a public node and start necessary services (e.g., WebSocket server)
17
17
  * @param {number} [options.port] If provided, the node will listen on this port (default: SERVICE.PORT)
18
18
  * @param {number} [options.verbose] Verbosity level for logging (default: NODE.DEFAULT_VERBOSE) */
19
- export async function createPublicNode(options) {
19
+ export async function createPublicNode(options) {
20
+ await CLOCK.sync(this.verbose);
20
21
  const verbose = options.verbose !== undefined ? options.verbose : NODE.DEFAULT_VERBOSE;
21
22
  const domain = options.domain || undefined;
22
23
  const codex = options.cryptoCodex || new CryptoCodex(undefined, verbose);
@@ -157,27 +157,23 @@ export class Topologist {
157
157
  ws.onmessage = (data) => {
158
158
  if (remoteId) for (const cb of this.peerStore.callbacks.data) cb(remoteId, data.data);
159
159
  else { // FIRST MESSAGE SHOULD BE HANDSHAKE WITH ID
160
- try {
161
- const d = new Uint8Array(data.data); if (d[0] > 127) return; // not unicast, ignore
162
- const message = this.cryptoCodex.readUnicastMessage(d);
163
- if (!message) return; // invalid unicast message, ignore
164
-
165
- const { route, type, neighborsList } = message;
166
- if (type !== 'handshake' || route.length !== 2) return;
167
-
168
- const { signatureStart, pubkey, signature } = message;
169
- const signedData = d.subarray(0, signatureStart);
170
- if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
171
-
172
- remoteId = route[0];
173
- this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
174
- this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
175
- if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
176
- this.peerStore.connecting[remoteId].out = new PeerConnection(remoteId, ws, 'out', true);
177
- for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'out');
178
- } catch (error) {
179
- console.error(`Error handling WebSocket message on Node #${this.id}:`, error);
180
- }
160
+ const d = new Uint8Array(data.data); if (d[0] > 127) return; // not unicast, ignore
161
+ const message = this.cryptoCodex.readUnicastMessage(d);
162
+ if (!message) return; // invalid unicast message, ignore
163
+
164
+ const { route, type, neighborsList } = message;
165
+ if (type !== 'handshake' || route.length !== 2) return;
166
+
167
+ const { signatureStart, pubkey, signature } = message;
168
+ const signedData = d.subarray(0, signatureStart);
169
+ if (!this.cryptoCodex.verifySignature(pubkey, signature, signedData)) return;
170
+
171
+ remoteId = route[0];
172
+ this.peerStore.digestPeerNeighbors(remoteId, neighborsList); // Update known store
173
+ this.peerStore.connecting[remoteId]?.in?.close(); // close incoming connection if any
174
+ if (!this.peerStore.connecting[remoteId]) this.peerStore.connecting[remoteId] = {};
175
+ this.peerStore.connecting[remoteId].out = new PeerConnection(remoteId, ws, 'out', true);
176
+ for (const cb of this.peerStore.callbacks.connect) cb(remoteId, 'out');
181
177
  }
182
178
  };
183
179
  ws.send(this.cryptoCodex.createUnicastMessage('handshake', null, [this.id, this.id], this.peerStore.neighborsList));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.23",
3
+ "version": "1.0.24",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },