@hive-p2p/server 1.0.77 → 1.0.79
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/ice-offer-manager.mjs +10 -4
- package/core/node.mjs +11 -1
- package/package.json +1 -1
- package/services/converter.mjs +36 -6
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { CLOCK } from '../services/clock.mjs';
|
|
2
2
|
import { NODE, TRANSPORTS, LOG_CSS } from './config.mjs';
|
|
3
3
|
import { xxHash32 } from '../libs/xxhash32.mjs';
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
// WE SHOULD LOAD WRTC ONLY WHEN NEEDED ! => Now done in node.mjs
|
|
6
|
+
/*async function getWrtc() {
|
|
5
7
|
if (typeof globalThis.RTCPeerConnection !== 'undefined') return undefined;
|
|
6
8
|
return (await import('wrtc')).default;
|
|
7
9
|
}
|
|
8
|
-
const wrtc = await getWrtc()
|
|
10
|
+
const wrtc = await getWrtc();*/
|
|
9
11
|
|
|
10
12
|
/** - 'OfferObj' Definition
|
|
11
13
|
* @typedef {Object} OfferObj
|
|
@@ -20,6 +22,7 @@ const wrtc = await getWrtc();
|
|
|
20
22
|
|
|
21
23
|
export class OfferManager { // Manages the creation of SDP offers and handling of answers
|
|
22
24
|
id;
|
|
25
|
+
wrtc;
|
|
23
26
|
verbose;
|
|
24
27
|
stunUrls;
|
|
25
28
|
|
|
@@ -36,6 +39,9 @@ export class OfferManager { // Manages the creation of SDP offers and handling o
|
|
|
36
39
|
offersToCreate = TRANSPORTS.MAX_SDP_OFFERS;
|
|
37
40
|
/** @type {Record<string, OfferObj>} key: offerHash **/ offers = {};
|
|
38
41
|
|
|
42
|
+
/** WRTC assignment, this is made to avoid import of wrtc while importing hive-p2P lib */
|
|
43
|
+
assignTransportWrtc(w) { this.wrtc = w; }
|
|
44
|
+
/** WRTC Should be assigned before calling tick() */
|
|
39
45
|
tick() { // called in peerStore to avoid multiple intervals
|
|
40
46
|
const now = CLOCK.time;
|
|
41
47
|
// CLEAR EXPIRED CREATOR OFFER INSTANCES
|
|
@@ -90,7 +96,7 @@ export class OfferManager { // Manages the creation of SDP offers and handling o
|
|
|
90
96
|
};
|
|
91
97
|
#createOffererInstance(expiration) {
|
|
92
98
|
const iceCompleteTimeout = TRANSPORTS.ICE_COMPLETE_TIMEOUT || 1_000;
|
|
93
|
-
const instance = new TRANSPORTS.PEER({ initiator: true, trickle: false, iceCompleteTimeout, wrtc, config: { iceServers: this.stunUrls } });
|
|
99
|
+
const instance = new TRANSPORTS.PEER({ initiator: true, trickle: false, iceCompleteTimeout, wrtc: this.wrtc, config: { iceServers: this.stunUrls } });
|
|
94
100
|
instance.on('error', error => this.#onError(error));
|
|
95
101
|
instance.on('signal', data => { // trickle: false => only one signal event with the full offer
|
|
96
102
|
const { candidate, type } = data; // with trickle, we need to adapt the approach.
|
|
@@ -183,7 +189,7 @@ export class OfferManager { // Manages the creation of SDP offers and handling o
|
|
|
183
189
|
|
|
184
190
|
// type === 'offer' => CREATE ANSWERER INSTANCE
|
|
185
191
|
const iceCompleteTimeout = TRANSPORTS.ICE_COMPLETE_TIMEOUT || 1_000;
|
|
186
|
-
const instance = new TRANSPORTS.PEER({ initiator: false, trickle: false, iceCompleteTimeout, wrtc, config: { iceServers: this.stunUrls } });
|
|
192
|
+
const instance = new TRANSPORTS.PEER({ initiator: false, trickle: false, iceCompleteTimeout, wrtc: this.wrtc, config: { iceServers: this.stunUrls } });
|
|
187
193
|
instance.on('error', (error) => this.#onError(error));
|
|
188
194
|
instance.on('signal', (data) => this.onSignalAnswer(remoteId, data, offerHash));
|
|
189
195
|
instance.on('connect', () => this.onConnect(remoteId, instance));
|
package/core/node.mjs
CHANGED
|
@@ -144,13 +144,23 @@ export class Node {
|
|
|
144
144
|
get publicUrl() { return this.services?.publicUrl; }
|
|
145
145
|
get time() { return CLOCK.time; }
|
|
146
146
|
|
|
147
|
+
async #getWrtc() {
|
|
148
|
+
if (typeof RTCPeerConnection !== 'undefined') return undefined;
|
|
149
|
+
return (await import('wrtc')).default;
|
|
150
|
+
}
|
|
147
151
|
async start() {
|
|
152
|
+
const w = await this.#getWrtc();
|
|
153
|
+
this.offerManager.assignTransportWrtc(w);
|
|
154
|
+
|
|
148
155
|
await CLOCK.sync(this.verbose);
|
|
149
156
|
this.started = true;
|
|
150
157
|
if (SIMULATION.AVOID_INTERVALS) return true; // SIMULATOR CASE
|
|
151
158
|
|
|
152
159
|
this.arbiterInterval = setInterval(() => this.arbiter.tick(), 1000);
|
|
153
|
-
this.peerStoreInterval = setInterval(() => {
|
|
160
|
+
this.peerStoreInterval = setInterval(() => {
|
|
161
|
+
this.peerStore.cleanupExpired();
|
|
162
|
+
this.peerStore.offerManager.tick();
|
|
163
|
+
}, 2500);
|
|
154
164
|
if (this.publicUrl) return true;
|
|
155
165
|
|
|
156
166
|
this.enhancerInterval = setInterval(() => this.topologist.tick(), DISCOVERY.LOOP_DELAY);
|
package/package.json
CHANGED
package/services/converter.mjs
CHANGED
|
@@ -8,16 +8,32 @@ export class Converter {
|
|
|
8
8
|
textDecoder = new TextDecoder();
|
|
9
9
|
buffer2 = new ArrayBuffer(2); view2 = new DataView(this.buffer2);
|
|
10
10
|
buffer4 = new ArrayBuffer(4); view4 = new DataView(this.buffer4);
|
|
11
|
+
buffer6 = new ArrayBuffer(6); view6 = new DataView(this.buffer6);
|
|
11
12
|
buffer8 = new ArrayBuffer(8); view8 = new DataView(this.buffer8);
|
|
12
13
|
hexMap = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15 };
|
|
13
14
|
|
|
14
15
|
// ... TO BYTES
|
|
15
16
|
/** Number should be between 0 and 65535 @param {number} num - Integer to convert to 2 bytes Uint8Array */
|
|
16
|
-
numberTo2Bytes(num) {
|
|
17
|
+
numberTo2Bytes(num) {
|
|
18
|
+
this.view2.setUint16(0, num, true);
|
|
19
|
+
return new Uint8Array(this.buffer2);
|
|
20
|
+
}
|
|
17
21
|
/** Number should be between 0 and 4294967295 @param {number} num - Integer to convert to 4 bytes Uint8Array */
|
|
18
|
-
numberTo4Bytes(num) {
|
|
22
|
+
numberTo4Bytes(num) {
|
|
23
|
+
this.view4.setUint32(0, num, true);
|
|
24
|
+
return new Uint8Array(this.buffer4);
|
|
25
|
+
}
|
|
26
|
+
/** Number should be between 0 and 281474976710655 (2^48-1) @param {number} num - Integer to convert to 6 bytes Uint8Array */
|
|
27
|
+
numberTo6Bytes(num) {
|
|
28
|
+
this.view6.setUint32(0, num % 0x100000000, true); // lower 4 bytes
|
|
29
|
+
this.view6.setUint16(4, Math.floor(num / 0x100000000), true); // upper 2 bytes
|
|
30
|
+
return new Uint8Array(this.buffer6);
|
|
31
|
+
}
|
|
19
32
|
/** Number should be between 0 and 18446744073709551615 @param {number} num - Integer to convert to 8 bytes Uint8Array */
|
|
20
|
-
numberTo8Bytes(num) {
|
|
33
|
+
numberTo8Bytes(num) {
|
|
34
|
+
this.view8.setBigUint64(0, BigInt(num), true);
|
|
35
|
+
return new Uint8Array(this.buffer8);
|
|
36
|
+
}
|
|
21
37
|
stringToBytes(str = 'toto') { return this.textEncoder.encode(str); }
|
|
22
38
|
/** @param {string} hex - Hex string to convert to Uint8Array */
|
|
23
39
|
hexToBytes(hex) {
|
|
@@ -38,11 +54,25 @@ export class Converter {
|
|
|
38
54
|
|
|
39
55
|
// BYTES TO ...
|
|
40
56
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
|
|
41
|
-
bytes2ToNumber(uint8Array) {
|
|
57
|
+
bytes2ToNumber(uint8Array) {
|
|
58
|
+
for (let i = 0; i < 2; i++) this.view2.setUint8(i, uint8Array[i]);
|
|
59
|
+
return this.view2.getUint16(0, true);
|
|
60
|
+
}
|
|
42
61
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
|
|
43
|
-
bytes4ToNumber(uint8Array) {
|
|
62
|
+
bytes4ToNumber(uint8Array) {
|
|
63
|
+
for (let i = 0; i < 4; i++) this.view4.setUint8(i, uint8Array[i]);
|
|
64
|
+
return this.view4.getUint32(0, true);
|
|
65
|
+
}
|
|
66
|
+
/** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
|
|
67
|
+
bytes6ToNumber(uint8Array) {
|
|
68
|
+
for (let i = 0; i < 6; i++) this.view6.setUint8(i, uint8Array[i]);
|
|
69
|
+
return this.view6.getUint32(0, true) + this.view6.getUint16(4, true) * 0x100000000;
|
|
70
|
+
}
|
|
44
71
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
|
|
45
|
-
bytes8ToNumber(uint8Array) {
|
|
72
|
+
bytes8ToNumber(uint8Array) {
|
|
73
|
+
for (let i = 0; i < 8; i++) this.view8.setUint8(i, uint8Array[i]);
|
|
74
|
+
return Number(this.view8.getBigUint64(0, true));
|
|
75
|
+
}
|
|
46
76
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
|
|
47
77
|
bytesToString(uint8Array) { return this.textDecoder.decode(uint8Array); }
|
|
48
78
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
|