@dignetwork/dig-sdk 0.0.1-alpha.145 → 0.0.1-alpha.147

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.
@@ -6,14 +6,6 @@ export interface PeerMetrics {
6
6
  latency: number;
7
7
  bandwidth: number;
8
8
  }
9
- /**
10
- * Configuration options for the PeerRanker.
11
- */
12
- interface PeerRankerOptions {
13
- pingPath?: string;
14
- timeout?: number;
15
- uploadTestSize?: number;
16
- }
17
9
  /**
18
10
  * Utility class to rank peers based on latency and upload bandwidth using HTTPS with mTLS.
19
11
  */
@@ -21,7 +13,6 @@ export declare class PeerRanker {
21
13
  private ipAddresses;
22
14
  private static certPath;
23
15
  private static keyPath;
24
- private pingPath;
25
16
  private timeout;
26
17
  private uploadTestSize;
27
18
  private sortedPeers;
@@ -29,27 +20,28 @@ export declare class PeerRanker {
29
20
  /**
30
21
  * Constructs a PeerRanker instance.
31
22
  * @param ipAddresses - Array of IP addresses to rank.
32
- * @param options - Configuration options including paths to client certificates.
33
23
  */
34
- constructor(ipAddresses: string[], options: PeerRankerOptions);
24
+ constructor(ipAddresses: string[], timeout?: number, uploadTestSize?: number);
35
25
  /**
36
26
  * Measures the latency of a given IP address using an HTTPS request.
37
27
  * Tries HEAD first, then falls back to GET if HEAD is not supported.
38
28
  * @param ip - The IP address of the peer.
39
- * @returns Promise resolving to the latency in milliseconds.
29
+ * @returns Promise resolving to the latency in milliseconds or rejecting if the peer fails.
40
30
  */
41
31
  private measureLatency;
42
32
  /**
43
33
  * Measures the upload bandwidth of a given IP address by sending random data.
44
34
  * @param ip - The IP address of the peer.
45
- * @returns Promise resolving to the upload bandwidth in bytes per second.
35
+ * @returns Promise resolving to the upload bandwidth in bytes per second or rejecting if the peer fails.
46
36
  */
47
37
  private measureBandwidth;
48
38
  /**
49
39
  * Ranks the peers based on measured latency and upload bandwidth.
40
+ * Unresponsive peers are excluded from the final ranking.
41
+ * @param cooldown - Cooldown time in milliseconds between batches.
50
42
  * @returns Promise resolving to an array of PeerMetrics sorted by latency and bandwidth.
51
43
  */
52
- rankPeers(): Promise<PeerMetrics[]>;
44
+ rankPeers(cooldown?: number): Promise<PeerMetrics[]>;
53
45
  /**
54
46
  * Returns the next best peer based on the ranking.
55
47
  * Cycles through the list, resetting to the beginning once the end is reached.
@@ -61,5 +53,4 @@ export declare class PeerRanker {
61
53
  */
62
54
  resetIterator(): void;
63
55
  }
64
- export {};
65
56
  //# sourceMappingURL=PeerRanker.d.ts.map
@@ -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,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"}
1
+ {"version":3,"file":"PeerRanker.d.ts","sourceRoot":"","sources":["../../src/utils/PeerRanker.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;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,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAS;IAG/B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,YAAY,CAAa;IAEjC;;;OAGG;gBACS,WAAW,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,MAAa,EAAE,cAAc,GAAE,MAAoB;IAW/F;;;;;OAKG;YACW,cAAc;IA2C5B;;;;OAIG;YACW,gBAAgB;IAmC9B;;;;;OAKG;IACU,SAAS,CAAC,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAmCtE;;;;OAIG;IACI,eAAe,IAAI,WAAW,GAAG,IAAI;IAW5C;;OAEG;IACI,aAAa,IAAI,IAAI;CAG7B"}
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- // src/PeerRanker.ts
3
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
4
  };
@@ -9,6 +8,7 @@ const axios_1 = __importDefault(require("axios"));
9
8
  const fs_1 = __importDefault(require("fs"));
10
9
  const https_1 = __importDefault(require("https"));
11
10
  const ssl_1 = require("./ssl");
11
+ const promiseUtils_1 = require("./promiseUtils");
12
12
  /**
13
13
  * Utility class to rank peers based on latency and upload bandwidth using HTTPS with mTLS.
14
14
  */
@@ -16,16 +16,15 @@ class PeerRanker {
16
16
  /**
17
17
  * Constructs a PeerRanker instance.
18
18
  * @param ipAddresses - Array of IP addresses to rank.
19
- * @param options - Configuration options including paths to client certificates.
20
19
  */
21
- constructor(ipAddresses, options) {
20
+ constructor(ipAddresses, timeout = 5000, uploadTestSize = 1024 * 1024) {
22
21
  // Internal properties for iteration
23
22
  this.sortedPeers = [];
24
23
  this.currentIndex = 0;
25
24
  this.ipAddresses = ipAddresses;
26
- this.pingPath = options.pingPath || '/'; // Default to root path if not provided
27
- this.timeout = options.timeout || 5000; // Default timeout: 5 seconds
28
- this.uploadTestSize = options.uploadTestSize || 1024 * 1024; // Default: 1MB
25
+ this.timeout = timeout; // Allow customizable timeout
26
+ this.uploadTestSize = uploadTestSize; // Default upload size: 1MB
27
+ // Fetch the SSL certificates used for mTLS.
29
28
  const { certPath, keyPath } = (0, ssl_1.getOrCreateSSLCerts)();
30
29
  PeerRanker.certPath = certPath;
31
30
  PeerRanker.keyPath = keyPath;
@@ -34,39 +33,36 @@ class PeerRanker {
34
33
  * Measures the latency of a given IP address using an HTTPS request.
35
34
  * Tries HEAD first, then falls back to GET if HEAD is not supported.
36
35
  * @param ip - The IP address of the peer.
37
- * @returns Promise resolving to the latency in milliseconds.
36
+ * @returns Promise resolving to the latency in milliseconds or rejecting if the peer fails.
38
37
  */
39
38
  async measureLatency(ip) {
40
- const path = this.pingPath;
41
- const url = `https://${ip}${path}`;
42
- // Configuration for HEAD request
39
+ const url = `https://${ip}:4159/diagnostics/ping`;
43
40
  const configHead = {
44
41
  url: url,
45
42
  method: 'HEAD',
46
43
  httpsAgent: new https_1.default.Agent({
47
44
  cert: fs_1.default.readFileSync(PeerRanker.certPath),
48
45
  key: fs_1.default.readFileSync(PeerRanker.keyPath),
49
- rejectUnauthorized: false, // Set to true in production
46
+ rejectUnauthorized: false,
50
47
  }),
51
48
  timeout: this.timeout,
52
- validateStatus: (status) => status < 500, // Resolve only if status is less than 500
49
+ validateStatus: (status) => status < 500,
53
50
  };
54
51
  const startTime = Date.now();
55
52
  try {
56
53
  const response = await (0, axios_1.default)(configHead);
57
- if (response.status === 405) { // Method Not Allowed
58
- // Fallback to GET with Range header to minimize data transfer
54
+ if (response.status === 405) {
59
55
  const configGet = {
60
56
  url: url,
61
57
  method: 'GET',
62
58
  httpsAgent: new https_1.default.Agent({
63
59
  cert: fs_1.default.readFileSync(PeerRanker.certPath),
64
60
  key: fs_1.default.readFileSync(PeerRanker.keyPath),
65
- rejectUnauthorized: false, // Set to true in production
61
+ rejectUnauthorized: false,
66
62
  }),
67
63
  timeout: this.timeout,
68
64
  headers: {
69
- 'Range': 'bytes=0-0', // Request only the first byte
65
+ 'Range': 'bytes=0-0',
70
66
  },
71
67
  validateStatus: (status) => status < 500,
72
68
  };
@@ -77,18 +73,17 @@ class PeerRanker {
77
73
  }
78
74
  catch (error) {
79
75
  console.error(`Latency measurement failed for IP ${ip}:`, error.message);
80
- return Infinity; // Indicate unreachable or unresponsive peer
76
+ throw new Error(`Latency measurement failed for IP ${ip}`);
81
77
  }
82
78
  }
83
79
  /**
84
80
  * Measures the upload bandwidth of a given IP address by sending random data.
85
81
  * @param ip - The IP address of the peer.
86
- * @returns Promise resolving to the upload bandwidth in bytes per second.
82
+ * @returns Promise resolving to the upload bandwidth in bytes per second or rejecting if the peer fails.
87
83
  */
88
84
  async measureBandwidth(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
85
+ const url = `https://${ip}:4159/diagnostics/bandwidth`;
86
+ const randomData = Buffer.alloc(this.uploadTestSize, 'a');
92
87
  const config = {
93
88
  url: url,
94
89
  method: 'POST',
@@ -100,39 +95,47 @@ class PeerRanker {
100
95
  httpsAgent: new https_1.default.Agent({
101
96
  cert: fs_1.default.readFileSync(PeerRanker.certPath),
102
97
  key: fs_1.default.readFileSync(PeerRanker.keyPath),
103
- rejectUnauthorized: false, // Set to true in production
98
+ rejectUnauthorized: false,
104
99
  }),
105
100
  timeout: this.timeout,
106
101
  maxContentLength: Infinity,
107
102
  maxBodyLength: Infinity,
108
103
  };
109
- return new Promise((resolve) => {
110
- const startTime = Date.now();
111
- (0, axios_1.default)(config)
112
- .then(() => {
113
- const timeElapsed = (Date.now() - startTime) / 1000; // seconds
114
- const bandwidth = this.uploadTestSize / timeElapsed; // bytes per second
115
- resolve(bandwidth);
116
- })
117
- .catch((error) => {
118
- console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
119
- resolve(0); // Indicate failure in measuring bandwidth
120
- });
121
- });
104
+ const startTime = Date.now();
105
+ try {
106
+ await (0, axios_1.default)(config);
107
+ const timeElapsed = (Date.now() - startTime) / 1000;
108
+ const bandwidth = this.uploadTestSize / timeElapsed;
109
+ return bandwidth;
110
+ }
111
+ catch (error) {
112
+ console.error(`Bandwidth measurement failed for IP ${ip}:`, error.message);
113
+ throw new Error(`Bandwidth measurement failed for IP ${ip}`);
114
+ }
122
115
  }
123
116
  /**
124
117
  * Ranks the peers based on measured latency and upload bandwidth.
118
+ * Unresponsive peers are excluded from the final ranking.
119
+ * @param cooldown - Cooldown time in milliseconds between batches.
125
120
  * @returns Promise resolving to an array of PeerMetrics sorted by latency and bandwidth.
126
121
  */
127
- async rankPeers() {
128
- const metricsPromises = this.ipAddresses.map(async (ip) => {
129
- const [latency, bandwidth] = await Promise.all([
130
- this.measureLatency(ip),
131
- this.measureBandwidth(ip),
132
- ]);
133
- return { ip, latency, bandwidth };
134
- });
135
- const peerMetrics = await Promise.all(metricsPromises);
122
+ async rankPeers(cooldown = 500) {
123
+ const limit = 5; // Limit to 5 parallel requests at a time
124
+ const iteratorFn = async (ip) => {
125
+ try {
126
+ const [latency, bandwidth] = await Promise.all([
127
+ this.measureLatency(ip),
128
+ this.measureBandwidth(ip),
129
+ ]);
130
+ return { ip, latency, bandwidth };
131
+ }
132
+ catch (error) {
133
+ // Peer failed, skip it by returning null
134
+ return null;
135
+ }
136
+ };
137
+ // Process all peers with a concurrency limit and cooldown between batches
138
+ const peerMetrics = (await (0, promiseUtils_1.asyncPool)(limit, this.ipAddresses, iteratorFn, cooldown)).filter((metrics) => metrics !== null); // Use a type guard
136
139
  // Sort by lowest latency first, then by highest bandwidth
137
140
  peerMetrics.sort((a, b) => {
138
141
  if (a.latency === b.latency) {
@@ -140,9 +143,7 @@ class PeerRanker {
140
143
  }
141
144
  return a.latency - b.latency; // Lower latency is better
142
145
  });
143
- // Update the internal sorted list
144
146
  this.sortedPeers = peerMetrics;
145
- // Reset the iterator index
146
147
  this.currentIndex = 0;
147
148
  return peerMetrics;
148
149
  }
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * Processes items in sequential batches with a concurrency limit.
3
+ * Adds a cooldown between batches.
3
4
  * @param {number} limit - The maximum number of concurrent executions per batch.
4
5
  * @param {Array<T>} items - The array of items to process.
5
6
  * @param {(item: T) => Promise<R>} iteratorFn - The async function to apply to each item.
7
+ * @param {number} cooldownMs - The cooldown duration between batches in milliseconds.
6
8
  * @returns {Promise<Array<R>>} - A promise that resolves when all items have been processed.
7
9
  */
8
- export declare function asyncPool<T, R>(limit: number, items: T[], iteratorFn: (item: T) => Promise<R>): Promise<R[]>;
10
+ export declare function asyncPool<T, R>(limit: number, items: T[], iteratorFn: (item: T) => Promise<R>, cooldownMs?: number): Promise<R[]>;
9
11
  /**
10
12
  * Helper function to add a timeout to a promise.
11
13
  * @param promise The original promise.
@@ -1 +1 @@
1
- {"version":3,"file":"promiseUtils.d.ts","sourceRoot":"","sources":["../../src/utils/promiseUtils.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAChC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBd;AAED;;;;;;EAMC;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,WAClB,OAAO,CAAC,CAAC,CAAC,MACf,MAAM,kBACM,MAAM,KACrB,OAAO,CAAC,CAAC,CAOX,CAAC"}
1
+ {"version":3,"file":"promiseUtils.d.ts","sourceRoot":"","sources":["../../src/utils/promiseUtils.ts"],"names":[],"mappings":"AACA;;;;;;;;GAQG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,CAAC,EAClC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,EAAE,EACV,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EACnC,UAAU,GAAE,MAAY,GACvB,OAAO,CAAC,CAAC,EAAE,CAAC,CAkBd;AACC;;;;;;EAMC;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,WAClB,OAAO,CAAC,CAAC,CAAC,MACf,MAAM,kBACM,MAAM,KACrB,OAAO,CAAC,CAAC,CAOX,CAAC"}
@@ -4,12 +4,15 @@ exports.withTimeout = void 0;
4
4
  exports.asyncPool = asyncPool;
5
5
  /**
6
6
  * Processes items in sequential batches with a concurrency limit.
7
+ * Adds a cooldown between batches.
7
8
  * @param {number} limit - The maximum number of concurrent executions per batch.
8
9
  * @param {Array<T>} items - The array of items to process.
9
10
  * @param {(item: T) => Promise<R>} iteratorFn - The async function to apply to each item.
11
+ * @param {number} cooldownMs - The cooldown duration between batches in milliseconds.
10
12
  * @returns {Promise<Array<R>>} - A promise that resolves when all items have been processed.
11
13
  */
12
- async function asyncPool(limit, items, iteratorFn) {
14
+ async function asyncPool(limit, items, iteratorFn, cooldownMs = 500 // Default cooldown of 500ms
15
+ ) {
13
16
  const ret = [];
14
17
  for (let i = 0; i < items.length; i += limit) {
15
18
  const batchItems = items.slice(i, i + limit);
@@ -17,8 +20,10 @@ async function asyncPool(limit, items, iteratorFn) {
17
20
  // Wait for the current batch to complete before starting the next one
18
21
  const batchResults = await Promise.all(batchPromises);
19
22
  ret.push(...batchResults);
20
- // Optional: add a cooldown between batches
21
- // await new Promise((resolve) => setTimeout(resolve, 500));
23
+ // Add a cooldown between batches, except after the last batch
24
+ if (i + limit < items.length) {
25
+ await new Promise((resolve) => setTimeout(resolve, cooldownMs));
26
+ }
22
27
  }
23
28
  return ret;
24
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.145",
3
+ "version": "0.0.1-alpha.147",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",