@hive-p2p/server 1.0.24 → 1.0.26

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/arbiter.mjs CHANGED
@@ -67,7 +67,7 @@ export class Arbiter {
67
67
  adjustTrust(peerId, delta, reason = 'na') { // Internal and API use - return true if peer isn't banished
68
68
  if (peerId === this.id) return; // self
69
69
  if (delta) this.trustBalances[peerId] = Math.min(MAX_TRUST, (this.trustBalances[peerId] || 0) + delta);
70
- if (delta && this.verbose > 2) console.log(`%c(Arbiter: ${this.id}) ${peerId} +${delta}ms (${reason}). Updated: ${this.trustBalances[peerId]}ms.`, LOG_CSS.ARBITER);
70
+ if (delta && this.verbose > 3) console.log(`%c(Arbiter: ${this.id}) ${peerId} +${delta}ms (${reason}). Updated: ${this.trustBalances[peerId]}ms.`, LOG_CSS.ARBITER);
71
71
  if (this.isBanished(peerId) && this.verbose > 1) console.log(`%c(Arbiter: ${this.id}) Peer ${peerId} is now banished.`, LOG_CSS.ARBITER);
72
72
  }
73
73
  isBanished(peerId = 'toto') { return (this.trustBalances[peerId] || 0) < 0; }
package/core/config.mjs CHANGED
@@ -97,6 +97,8 @@ export const IDENTITY = {
97
97
  }
98
98
 
99
99
  export const TRANSPORTS = {
100
+ /** If true, we always add centralized STUN servers (Google) to the STUN URLs list */
101
+ CENTRALIZED_STUN_SERVERS: false,
100
102
  /** Maximum SDP offers to create in advance to be ready for new connections | Default: 2 */
101
103
  MAX_SDP_OFFERS: 2,
102
104
  /** Time to wait for ICE gathering to complete | Default: 1_000 (1 second) */
@@ -104,7 +106,7 @@ export const TRANSPORTS = {
104
106
  /** Time to wait for signal before destroying WTRC connection | Default: 8_000 (8 seconds) */
105
107
  SIGNAL_CREATION_TIMEOUT: 8_000,
106
108
  /** Time to consider an SDP offer as valid | Default: 40_000 (40 seconds) */
107
- SDP_OFFER_EXPIRATION: 40_000,
109
+ SDP_OFFER_EXPIRATION: 40_000,
108
110
 
109
111
  WS_CLIENT: WebSocket,
110
112
  WS_SERVER: isNode ? (await import('ws')).WebSocketServer : null,
@@ -188,7 +188,7 @@ export class CryptoCodex {
188
188
  /** @param {Uint8Array | ArrayBuffer} serialized @return {GossipMessage | null } */
189
189
  readGossipMessage(serialized) {
190
190
  if (this.verbose > 3) console.log(`%creadGossipMessage ${serialized.byteLength} bytes`, LOG_CSS.CRYPTO_CODEX);
191
- if (this.verbose > 3) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
191
+ if (this.verbose > 4) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
192
192
  try { // 1, 1, 1, 8, 4, 32, X, 64, 1
193
193
  const { marker, dataCode, neighLength, timestamp, dataLength, pubkey, associatedId } = this.readBufferHeader(serialized);
194
194
  const topic = GOSSIP.MARKERS_BYTES[marker];
@@ -208,7 +208,7 @@ export class CryptoCodex {
208
208
  /** @param {Uint8Array | ArrayBuffer} serialized @return {DirectMessage | ReroutedDirectMessage | null} */
209
209
  readUnicastMessage(serialized) {
210
210
  if (this.verbose > 3) console.log(`%creadUnicastMessage ${serialized.byteLength} bytes`, LOG_CSS.CRYPTO_CODEX);
211
- if (this.verbose > 3) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
211
+ if (this.verbose > 4) console.log(`%c${serialized}`, LOG_CSS.CRYPTO_CODEX);
212
212
  try { // 1, 1, 1, 8, 4, 32, X, 1, X, 64
213
213
  const { marker, dataCode, neighLength, timestamp, dataLength, pubkey } = this.readBufferHeader(serialized, false);
214
214
  const type = UNICAST.MARKERS_BYTES[marker];
@@ -89,6 +89,7 @@ export class NodeServices {
89
89
  this.stunServer.send(this.#buildSTUNResponse(msg, rinfo), rinfo.port, rinfo.address);
90
90
  });
91
91
  this.stunServer.bind(port, host);
92
+ if (this.verbose > 2) console.log(`%cSTUN server listening on ${host}:${port}`, LOG_CSS.SERVICE);
92
93
  }
93
94
  #isValidSTUNRequest(msg) {
94
95
  if (msg.length < 20) return false;
@@ -117,7 +118,7 @@ export class NodeServices {
117
118
  return response;
118
119
  }
119
120
  /** @param {string[]} bootstraps */
120
- static deriveSTUNServers(bootstraps, includesCentralized = false) {
121
+ static deriveSTUNServers(bootstraps) {
121
122
  /** @type {Array<{urls: string}>} */
122
123
  const stunUrls = [];
123
124
  for (const b of bootstraps) {
@@ -125,13 +126,13 @@ export class NodeServices {
125
126
  const port = parseInt(b.split(':')[2]) + 1;
126
127
  stunUrls.push({ urls: `stun:${domain}:${port}` });
127
128
  }
128
- if (!includesCentralized) return stunUrls;
129
+ if (!TRANSPORTS.CENTRALIZED_STUN_SERVERS) return stunUrls;
129
130
 
130
131
  // CENTRALIZED STUN SERVERS FALLBACK (GOOGLE) - OPTIONAL
131
- this.stunUrls.push({ urls: 'stun:stun.l.google.com:5349' });
132
- this.stunUrls.push({ urls: 'stun:stun.l.google.com:19302' });
133
- this.stunUrls.push({ urls: 'stun:stun1.l.google.com:3478' });
134
- this.stunUrls.push({ urls: 'stun:stun1.l.google.com:5349' });
132
+ stunUrls.push({ urls: 'stun:stun.l.google.com:5349' });
133
+ stunUrls.push({ urls: 'stun:stun.l.google.com:19302' });
134
+ stunUrls.push({ urls: 'stun:stun1.l.google.com:3478' });
135
+ stunUrls.push({ urls: 'stun:stun1.l.google.com:5349' });
135
136
  return stunUrls;
136
137
  }
137
138
  }
@@ -144,9 +144,19 @@ export class Topologist {
144
144
  }
145
145
  /** Get overlap information for multiple peers @param {string[]} peerIds */
146
146
  #getOverlaps(peerIds = []) { return peerIds.map(id => ({ id, ...this.#getOverlap(id) })); }
147
+ #getFullWsUrl(url) {
148
+ // Auto-detect protocol: use wss:// if in browser + HTTPS
149
+ const isBrowser = typeof window !== 'undefined';
150
+ const isSecure = isBrowser && window.location.protocol === 'https:';
151
+ const protocol = isSecure ? 'wss://' : 'ws://';
152
+ const host = isBrowser ? window.location.host : url; // Récupère le host du browser
153
+
154
+ // Build full URL if not already prefixed
155
+ return url.startsWith('ws') ? url : `${protocol}${urlhost}`;
156
+ }
147
157
  #connectToPublicNode(publicUrl = 'localhost:8080') {
148
158
  let remoteId = null;
149
- const ws = new TRANSPORTS.WS_CLIENT(publicUrl); ws.binaryType = 'arraybuffer';
159
+ const ws = new TRANSPORTS.WS_CLIENT(this.#getFullWsUrl(publicUrl)); ws.binaryType = 'arraybuffer';
150
160
  ws.onerror = (error) => console.error(`WebSocket error:`, error.stack);
151
161
  ws.onopen = () => {
152
162
  this.bootstrapsConnectionState.set(publicUrl, true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },