@hive-p2p/server 1.0.31 → 1.0.33

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.
@@ -121,11 +121,13 @@ export class NodeServices {
121
121
  static deriveSTUNServers(bootstraps) {
122
122
  /** @type {Array<{urls: string}>} */
123
123
  const stunUrls = [];
124
- for (const b of bootstraps) { // {urls: 'stun:42312:NaN'}
124
+ for (const b of bootstraps) {
125
125
  if (!b.includes(':')) continue;
126
- const url = b.replace('/ws', '/signal'); // in case someone put domain/ws
127
- url.replace('/wss', '/signal'); // in case someone put domain/wss
128
- url.replace('ws://', 'stun:');
126
+ if (b.endsWith('/ws') || b.endsWith('/wss')) continue; // ugly hardcode
127
+ // useless ? => proxy can't serve as stun server
128
+ //const url = b.replace('/ws', '/signal'); // in case someone put domain/ws
129
+ //url.replace('/wss', '/signal'); // in case someone put domain/wss
130
+ const url = b.replace('ws://', 'stun:');
129
131
  url.replace('wss://', 'stun:');
130
132
  if (!url.includes('stun:')) continue;
131
133
  stunUrls.push({ urls: url });
package/core/node.mjs CHANGED
@@ -17,12 +17,13 @@ import { NodeServices } from './node-services.mjs';
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
19
  export async function createPublicNode(options) {
20
- await CLOCK.sync(this.verbose);
21
20
  const verbose = options.verbose !== undefined ? options.verbose : NODE.DEFAULT_VERBOSE;
22
21
  const domain = options.domain || undefined;
23
22
  const codex = options.cryptoCodex || new CryptoCodex(undefined, verbose);
23
+ const clockSync = CLOCK.sync(verbose);
24
24
  if (!codex.publicKey) await codex.generate(domain ? true : false);
25
25
 
26
+ await clockSync;
26
27
  const node = new Node(codex, options.bootstraps || [], verbose);
27
28
  if (domain) {
28
29
  node.services = new NodeServices(codex, node.peerStore, undefined, verbose);
@@ -42,8 +43,10 @@ export async function createPublicNode(options) {
42
43
  export async function createNode(options = {}) {
43
44
  const verbose = options.verbose !== undefined ? options.verbose : NODE.DEFAULT_VERBOSE;
44
45
  const codex = options.cryptoCodex || new CryptoCodex(undefined, verbose);
46
+ const clockSync = CLOCK.sync(verbose);
45
47
  if (!codex.publicKey) await codex.generate(false);
46
48
 
49
+ await clockSync;
47
50
  const node = new Node(codex, options.bootstraps || [], verbose);
48
51
  if (options.autoStart !== false) await node.start();
49
52
  return node;
@@ -136,6 +139,7 @@ export class Node {
136
139
  }
137
140
 
138
141
  // PUBLIC API
142
+ /** @returns {string | undefined} */
139
143
  get publicUrl() { return this.services?.publicUrl; }
140
144
 
141
145
  onMessageData(callback) { this.messager.on('message', callback); }
@@ -145,10 +149,13 @@ export class Node {
145
149
  await CLOCK.sync(this.verbose);
146
150
  this.started = true;
147
151
  if (SIMULATION.AVOID_INTERVALS) return true; // SIMULATOR CASE
148
- this.topologist.tryConnectNextBootstrap(); // first shot ASAP
152
+
149
153
  this.arbiterInterval = setInterval(() => this.arbiter.tick(), 1000);
150
- this.enhancerInterval = setInterval(() => this.topologist.tick(), DISCOVERY.LOOP_DELAY);
151
154
  this.peerStoreInterval = setInterval(() => { this.peerStore.cleanupExpired(); this.peerStore.offerManager.tick(); }, 2500);
155
+ if (this.publicUrl) return true;
156
+
157
+ this.enhancerInterval = setInterval(() => this.topologist.tick(), DISCOVERY.LOOP_DELAY);
158
+ this.topologist.tryConnectNextBootstrap(); // first shot ASAP
152
159
  return true;
153
160
  }
154
161
  /** Broadcast a message to all connected peers or to a specified peer
package/index.mjs CHANGED
@@ -2,6 +2,16 @@ import { Node, createNode, createPublicNode } from "./core/node.mjs";
2
2
  import { CryptoCodex } from "./core/crypto-codex.mjs";
3
3
  import CONFIG from "./core/config.mjs";
4
4
 
5
+ /**
6
+ * @typedef {Object} HiveP2PNamespace
7
+ * @property {typeof Node} Node
8
+ * @property {typeof createNode} createNode
9
+ * @property {typeof CryptoCodex} CryptoCodex
10
+ * @property {typeof createPublicNode} createPublicNode
11
+ * @property {typeof CONFIG} CONFIG
12
+ */
13
+
14
+ /** @type {HiveP2PNamespace} */
5
15
  const HiveP2P = { Node, createNode, createPublicNode, CryptoCodex, CONFIG };
6
16
  export { Node, createNode, createPublicNode, CryptoCodex, CONFIG };
7
17
  export default HiveP2P;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },