@dignetwork/dig-sdk 0.0.1-alpha.144 → 0.0.1-alpha.145
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.
|
@@ -10,24 +10,20 @@ export interface PeerMetrics {
|
|
|
10
10
|
* Configuration options for the PeerRanker.
|
|
11
11
|
*/
|
|
12
12
|
interface PeerRankerOptions {
|
|
13
|
-
clientCertPath: string;
|
|
14
|
-
clientKeyPath: string;
|
|
15
|
-
bandwidthTestPath: string;
|
|
16
13
|
pingPath?: string;
|
|
17
14
|
timeout?: number;
|
|
18
|
-
|
|
15
|
+
uploadTestSize?: number;
|
|
19
16
|
}
|
|
20
17
|
/**
|
|
21
|
-
* Utility class to rank peers based on latency and bandwidth using HTTPS with mTLS.
|
|
18
|
+
* Utility class to rank peers based on latency and upload bandwidth using HTTPS with mTLS.
|
|
22
19
|
*/
|
|
23
20
|
export declare class PeerRanker {
|
|
24
21
|
private ipAddresses;
|
|
25
22
|
private static certPath;
|
|
26
23
|
private static keyPath;
|
|
27
|
-
private bandwidthTestPath;
|
|
28
24
|
private pingPath;
|
|
29
25
|
private timeout;
|
|
30
|
-
private
|
|
26
|
+
private uploadTestSize;
|
|
31
27
|
private sortedPeers;
|
|
32
28
|
private currentIndex;
|
|
33
29
|
/**
|
|
@@ -44,13 +40,13 @@ export declare class PeerRanker {
|
|
|
44
40
|
*/
|
|
45
41
|
private measureLatency;
|
|
46
42
|
/**
|
|
47
|
-
* Measures the bandwidth of a given IP address by
|
|
43
|
+
* Measures the upload bandwidth of a given IP address by sending random data.
|
|
48
44
|
* @param ip - The IP address of the peer.
|
|
49
|
-
* @returns Promise resolving to the bandwidth in bytes per second.
|
|
45
|
+
* @returns Promise resolving to the upload bandwidth in bytes per second.
|
|
50
46
|
*/
|
|
51
47
|
private measureBandwidth;
|
|
52
48
|
/**
|
|
53
|
-
* Ranks the peers based on measured latency and bandwidth.
|
|
49
|
+
* Ranks the peers based on measured latency and upload bandwidth.
|
|
54
50
|
* @returns Promise resolving to an array of PeerMetrics sorted by latency and bandwidth.
|
|
55
51
|
*/
|
|
56
52
|
rankPeers(): Promise<PeerMetrics[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PeerRanker.d.ts","sourceRoot":"","sources":["../../src/utils/PeerRanker.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,UAAU,iBAAiB;IACzB,
|
|
1
|
+
{"version":3,"file":"PeerRanker.d.ts","sourceRoot":"","sources":["../../src/utils/PeerRanker.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,UAAU,iBAAiB;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,WAAW,CAAW;IAC9B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAS;IAG/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAa;IAEjC;;;;OAIG;gBACS,WAAW,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,iBAAiB;IAW7D;;;;;OAKG;YACW,cAAc;IA8C5B;;;;OAIG;YACW,gBAAgB;IAwC9B;;;OAGG;IACU,SAAS,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IA4BhD;;;;OAIG;IACI,eAAe,IAAI,WAAW,GAAG,IAAI;IAW5C;;OAEG;IACI,aAAa,IAAI,IAAI;CAG7B"}
|
package/dist/utils/PeerRanker.js
CHANGED
|
@@ -10,7 +10,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
10
10
|
const https_1 = __importDefault(require("https"));
|
|
11
11
|
const ssl_1 = require("./ssl");
|
|
12
12
|
/**
|
|
13
|
-
* Utility class to rank peers based on latency and bandwidth using HTTPS with mTLS.
|
|
13
|
+
* Utility class to rank peers based on latency and upload bandwidth using HTTPS with mTLS.
|
|
14
14
|
*/
|
|
15
15
|
class PeerRanker {
|
|
16
16
|
/**
|
|
@@ -23,10 +23,9 @@ class PeerRanker {
|
|
|
23
23
|
this.sortedPeers = [];
|
|
24
24
|
this.currentIndex = 0;
|
|
25
25
|
this.ipAddresses = ipAddresses;
|
|
26
|
-
this.bandwidthTestPath = options.bandwidthTestPath;
|
|
27
26
|
this.pingPath = options.pingPath || '/'; // Default to root path if not provided
|
|
28
27
|
this.timeout = options.timeout || 5000; // Default timeout: 5 seconds
|
|
29
|
-
this.
|
|
28
|
+
this.uploadTestSize = options.uploadTestSize || 1024 * 1024; // Default: 1MB
|
|
30
29
|
const { certPath, keyPath } = (0, ssl_1.getOrCreateSSLCerts)();
|
|
31
30
|
PeerRanker.certPath = certPath;
|
|
32
31
|
PeerRanker.keyPath = keyPath;
|
|
@@ -82,40 +81,38 @@ class PeerRanker {
|
|
|
82
81
|
}
|
|
83
82
|
}
|
|
84
83
|
/**
|
|
85
|
-
* Measures the bandwidth of a given IP address by
|
|
84
|
+
* Measures the upload bandwidth of a given IP address by sending random data.
|
|
86
85
|
* @param ip - The IP address of the peer.
|
|
87
|
-
* @returns Promise resolving to the bandwidth in bytes per second.
|
|
86
|
+
* @returns Promise resolving to the upload bandwidth in bytes per second.
|
|
88
87
|
*/
|
|
89
88
|
async measureBandwidth(ip) {
|
|
90
|
-
const url = `https://${ip}
|
|
89
|
+
const url = `https://${ip}/upload`; // Assume /upload as the endpoint for upload testing
|
|
90
|
+
// Generate random data
|
|
91
|
+
const randomData = Buffer.alloc(this.uploadTestSize, 'a'); // 1MB of 'a's
|
|
91
92
|
const config = {
|
|
92
93
|
url: url,
|
|
93
|
-
method: '
|
|
94
|
-
|
|
94
|
+
method: 'POST',
|
|
95
|
+
data: randomData,
|
|
96
|
+
headers: {
|
|
97
|
+
'Content-Type': 'application/octet-stream',
|
|
98
|
+
'Content-Length': this.uploadTestSize,
|
|
99
|
+
},
|
|
95
100
|
httpsAgent: new https_1.default.Agent({
|
|
96
101
|
cert: fs_1.default.readFileSync(PeerRanker.certPath),
|
|
97
102
|
key: fs_1.default.readFileSync(PeerRanker.keyPath),
|
|
98
103
|
rejectUnauthorized: false, // Set to true in production
|
|
99
104
|
}),
|
|
100
105
|
timeout: this.timeout,
|
|
106
|
+
maxContentLength: Infinity,
|
|
107
|
+
maxBodyLength: Infinity,
|
|
101
108
|
};
|
|
102
109
|
return new Promise((resolve) => {
|
|
103
110
|
const startTime = Date.now();
|
|
104
|
-
let bytesReceived = 0;
|
|
105
111
|
(0, axios_1.default)(config)
|
|
106
|
-
.then((
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
response.data.on('end', () => {
|
|
111
|
-
const timeElapsed = (Date.now() - startTime) / 1000; // seconds
|
|
112
|
-
const bandwidth = bytesReceived / timeElapsed; // bytes per second
|
|
113
|
-
resolve(bandwidth);
|
|
114
|
-
});
|
|
115
|
-
response.data.on('error', (err) => {
|
|
116
|
-
console.error(`Bandwidth measurement error for IP ${ip}:`, err.message);
|
|
117
|
-
resolve(0); // Indicate failure in measuring bandwidth
|
|
118
|
-
});
|
|
112
|
+
.then(() => {
|
|
113
|
+
const timeElapsed = (Date.now() - startTime) / 1000; // seconds
|
|
114
|
+
const bandwidth = this.uploadTestSize / timeElapsed; // bytes per second
|
|
115
|
+
resolve(bandwidth);
|
|
119
116
|
})
|
|
120
117
|
.catch((error) => {
|
|
121
118
|
console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
|
|
@@ -124,7 +121,7 @@ class PeerRanker {
|
|
|
124
121
|
});
|
|
125
122
|
}
|
|
126
123
|
/**
|
|
127
|
-
* Ranks the peers based on measured latency and bandwidth.
|
|
124
|
+
* Ranks the peers based on measured latency and upload bandwidth.
|
|
128
125
|
* @returns Promise resolving to an array of PeerMetrics sorted by latency and bandwidth.
|
|
129
126
|
*/
|
|
130
127
|
async rankPeers() {
|