@dignetwork/dig-sdk 0.0.1-alpha.16 → 0.0.1-alpha.161
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/dist/DataIntegrityTree/DataIntegrityTree.d.ts +11 -2
- package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
- package/dist/DataIntegrityTree/DataIntegrityTree.js +94 -13
- package/dist/DigNetwork/ContentServer.d.ts +24 -5
- package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
- package/dist/DigNetwork/ContentServer.js +239 -27
- package/dist/DigNetwork/DigNetwork.d.ts +15 -9
- package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
- package/dist/DigNetwork/DigNetwork.js +129 -189
- package/dist/DigNetwork/DigPeer.d.ts +8 -3
- package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
- package/dist/DigNetwork/DigPeer.js +68 -115
- package/dist/DigNetwork/IncentiveServer.d.ts.map +1 -1
- package/dist/DigNetwork/IncentiveServer.js +6 -5
- package/dist/DigNetwork/PropagationServer.d.ts +88 -33
- package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
- package/dist/DigNetwork/PropagationServer.js +636 -357
- package/dist/blockchain/DataStore.d.ts +25 -7
- package/dist/blockchain/DataStore.d.ts.map +1 -1
- package/dist/blockchain/DataStore.js +115 -159
- package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
- package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
- package/dist/blockchain/FullNodePeer.d.ts +94 -10
- package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
- package/dist/blockchain/FullNodePeer.js +318 -104
- package/dist/blockchain/ServerCoin.d.ts +13 -5
- package/dist/blockchain/ServerCoin.d.ts.map +1 -1
- package/dist/blockchain/ServerCoin.js +90 -28
- package/dist/blockchain/StoreMonitorRegistry.d.ts +85 -0
- package/dist/blockchain/StoreMonitorRegistry.d.ts.map +1 -0
- package/dist/blockchain/StoreMonitorRegistry.js +241 -0
- package/dist/blockchain/Wallet.d.ts +2 -2
- package/dist/blockchain/Wallet.d.ts.map +1 -1
- package/dist/blockchain/Wallet.js +50 -18
- package/dist/blockchain/coins.d.ts +1 -2
- package/dist/blockchain/coins.d.ts.map +1 -1
- package/dist/blockchain/coins.js +1 -55
- package/dist/blockchain/index.d.ts +1 -0
- package/dist/blockchain/index.d.ts.map +1 -1
- package/dist/blockchain/index.js +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/ContentScanner.d.ts +63 -0
- package/dist/utils/ContentScanner.d.ts.map +1 -0
- package/dist/utils/ContentScanner.js +175 -0
- package/dist/utils/Environment.d.ts +21 -0
- package/dist/utils/Environment.d.ts.map +1 -0
- package/dist/utils/Environment.js +93 -0
- package/dist/utils/FileCache.d.ts +1 -1
- package/dist/utils/FileCache.d.ts.map +1 -1
- package/dist/utils/FileCache.js +2 -2
- package/dist/utils/FileTransfer.d.ts +47 -0
- package/dist/utils/FileTransfer.d.ts.map +1 -0
- package/dist/utils/FileTransfer.js +209 -0
- package/dist/utils/NconfManager.js +2 -1
- package/dist/utils/PeerRanker.d.ts +55 -0
- package/dist/utils/PeerRanker.d.ts.map +1 -0
- package/dist/utils/PeerRanker.js +186 -0
- package/dist/utils/StoreArchiveManager.d.ts +45 -0
- package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
- package/dist/utils/StoreArchiveManager.js +153 -0
- package/dist/utils/config.d.ts +4 -3
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/config.js +12 -7
- package/dist/utils/directoryUtils.d.ts.map +1 -1
- package/dist/utils/directoryUtils.js +9 -12
- package/dist/utils/index.d.ts +3 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +3 -1
- package/dist/utils/merkle.d.ts +2 -0
- package/dist/utils/merkle.d.ts.map +1 -0
- package/dist/utils/merkle.js +28 -0
- package/dist/utils/network.d.ts +2 -1
- package/dist/utils/network.d.ts.map +1 -1
- package/dist/utils/network.js +40 -15
- package/dist/utils/promiseUtils.d.ts +19 -0
- package/dist/utils/promiseUtils.d.ts.map +1 -0
- package/dist/utils/promiseUtils.js +43 -0
- package/dist/utils/ssl.js +2 -2
- package/package.json +17 -5
- package/dist/utils/deltaUtils.d.ts +0 -2
- package/dist/utils/deltaUtils.d.ts.map +0 -1
- package/dist/utils/deltaUtils.js +0 -83
|
@@ -4,23 +4,40 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ContentServer = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
8
|
const http_1 = __importDefault(require("http"));
|
|
8
9
|
const url_1 = require("url");
|
|
10
|
+
const ssl_1 = require("../utils/ssl");
|
|
11
|
+
const network_1 = require("../utils/network");
|
|
12
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
13
|
+
const hasRootHashCache = new node_cache_1.default({ stdTTL: 86400 });
|
|
14
|
+
const wellKnownCache = new node_cache_1.default({ stdTTL: 86400 });
|
|
9
15
|
class ContentServer {
|
|
10
16
|
constructor(ipAddress, storeId) {
|
|
11
17
|
this.ipAddress = ipAddress;
|
|
12
18
|
this.storeId = storeId;
|
|
19
|
+
if (!ContentServer.certPath || !ContentServer.keyPath) {
|
|
20
|
+
const { certPath, keyPath } = (0, ssl_1.getOrCreateSSLCerts)();
|
|
21
|
+
ContentServer.certPath = certPath;
|
|
22
|
+
ContentServer.keyPath = keyPath;
|
|
23
|
+
}
|
|
13
24
|
}
|
|
14
25
|
// Method to get the content of a specified key from the peer, with optional challenge query
|
|
15
26
|
async getKey(key, rootHash, challengeHex) {
|
|
16
27
|
// Construct the base URL
|
|
17
|
-
let url = `
|
|
28
|
+
let url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
|
|
18
29
|
// If a challenge is provided, append it as a query parameter
|
|
19
30
|
if (challengeHex) {
|
|
20
31
|
url += `?challenge=${challengeHex}`;
|
|
21
32
|
}
|
|
22
33
|
return this.fetchWithRetries(url);
|
|
23
34
|
}
|
|
35
|
+
// New method to get only the first chunk of the content
|
|
36
|
+
async getKeyChunk(key, rootHash) {
|
|
37
|
+
// Construct the base URL
|
|
38
|
+
let url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
|
|
39
|
+
return this.fetchFirstChunk(url);
|
|
40
|
+
}
|
|
24
41
|
// Method to get the payment address from the peer
|
|
25
42
|
async getPaymentAddress() {
|
|
26
43
|
console.log(`Fetching payment address from peer ${this.ipAddress}...`);
|
|
@@ -30,43 +47,128 @@ class ContentServer {
|
|
|
30
47
|
}
|
|
31
48
|
catch (error) {
|
|
32
49
|
console.error(`Failed to fetch payment address from ${this.ipAddress}: ${error.message}`);
|
|
33
|
-
|
|
50
|
+
return null;
|
|
34
51
|
}
|
|
35
52
|
}
|
|
36
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Fetches and caches the .well-known information for the store's IP address.
|
|
55
|
+
*
|
|
56
|
+
* @returns A promise that resolves to the .well-known JSON data.
|
|
57
|
+
*/
|
|
37
58
|
async getWellKnown() {
|
|
38
|
-
|
|
39
|
-
|
|
59
|
+
// Construct the cache key based on ipAddress
|
|
60
|
+
const cacheKey = `${this.ipAddress}-wellknown`;
|
|
61
|
+
// Check if the result is already cached
|
|
62
|
+
const cachedResult = wellKnownCache.get(cacheKey);
|
|
63
|
+
if (cachedResult !== undefined) {
|
|
64
|
+
return cachedResult;
|
|
65
|
+
}
|
|
66
|
+
// If not cached, proceed to fetch the .well-known information
|
|
67
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/.well-known`;
|
|
68
|
+
try {
|
|
69
|
+
const data = await this.fetchJson(url);
|
|
70
|
+
wellKnownCache.set(cacheKey, data);
|
|
71
|
+
return data;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
console.error(`Error fetching .well-known information for ${this.ipAddress}:`, error.message);
|
|
75
|
+
throw error; // Propagate the error after logging
|
|
76
|
+
}
|
|
40
77
|
}
|
|
41
78
|
// Method to get the list of known stores
|
|
42
79
|
async getKnownStores() {
|
|
43
|
-
const url = `
|
|
80
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/.well-known/stores`;
|
|
44
81
|
return this.fetchJson(url);
|
|
45
82
|
}
|
|
46
83
|
// Method to get the index of all stores
|
|
47
84
|
async getStoresIndex() {
|
|
48
|
-
const url = `
|
|
85
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/`;
|
|
49
86
|
return this.fetchJson(url);
|
|
50
87
|
}
|
|
51
88
|
// Method to get the index of keys in a store
|
|
52
|
-
async getKeysIndex() {
|
|
53
|
-
|
|
54
|
-
|
|
89
|
+
async getKeysIndex(rootHash) {
|
|
90
|
+
try {
|
|
91
|
+
let udi = `chia.${this.storeId}`;
|
|
92
|
+
if (rootHash) {
|
|
93
|
+
udi += `.${rootHash}`;
|
|
94
|
+
}
|
|
95
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}`;
|
|
96
|
+
return this.fetchJson(url);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (rootHash) {
|
|
100
|
+
hasRootHashCache.del(`${this.storeId}-${rootHash}`);
|
|
101
|
+
}
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
55
104
|
}
|
|
56
105
|
// Method to check if a specific key exists (HEAD request)
|
|
57
|
-
async headKey(key) {
|
|
58
|
-
|
|
59
|
-
|
|
106
|
+
async headKey(key, rootHash) {
|
|
107
|
+
try {
|
|
108
|
+
let udi = `chia.${this.storeId}`;
|
|
109
|
+
if (rootHash) {
|
|
110
|
+
udi += `.${rootHash}`;
|
|
111
|
+
}
|
|
112
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
|
|
113
|
+
return this.head(url);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (rootHash) {
|
|
117
|
+
hasRootHashCache.del(`${this.storeId}-${rootHash}`);
|
|
118
|
+
}
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
60
121
|
}
|
|
61
122
|
// Method to check if a specific store exists (HEAD request)
|
|
62
|
-
async headStore() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
123
|
+
async headStore(options) {
|
|
124
|
+
try {
|
|
125
|
+
let url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}`;
|
|
126
|
+
if (options?.hasRootHash) {
|
|
127
|
+
url += `?hasRootHash=${options.hasRootHash}`;
|
|
128
|
+
}
|
|
129
|
+
return this.head(url);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (options?.hasRootHash) {
|
|
133
|
+
hasRootHashCache.del(`${this.storeId}-${options.hasRootHash}`);
|
|
134
|
+
}
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
66
137
|
}
|
|
67
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Checks if the store has the specified rootHash.
|
|
140
|
+
* Utilizes caching to improve performance.
|
|
141
|
+
*
|
|
142
|
+
* @param rootHash - The root hash to check.
|
|
143
|
+
* @returns A promise that resolves to true if the root hash exists, otherwise false.
|
|
144
|
+
*/
|
|
145
|
+
async hasRootHash(rootHash) {
|
|
146
|
+
// Construct the cache key using storeId and rootHash
|
|
147
|
+
const cacheKey = `${this.storeId}-${rootHash}`;
|
|
148
|
+
// Check if the result is already cached
|
|
149
|
+
const cachedResult = hasRootHashCache.get(cacheKey);
|
|
150
|
+
if (cachedResult !== undefined) {
|
|
151
|
+
return cachedResult;
|
|
152
|
+
}
|
|
153
|
+
// If not cached, perform the headStore request
|
|
154
|
+
const { success, headers } = await this.headStore({
|
|
155
|
+
hasRootHash: rootHash,
|
|
156
|
+
});
|
|
157
|
+
// Determine if the store has the root hash
|
|
158
|
+
const hasHash = success && headers?.["x-has-root-hash"] === "true";
|
|
159
|
+
// Only cache the result if the store has the root hash
|
|
160
|
+
if (hasHash) {
|
|
161
|
+
hasRootHashCache.set(cacheKey, true);
|
|
162
|
+
}
|
|
163
|
+
return hasHash;
|
|
164
|
+
}
|
|
165
|
+
streamKey(key, rootHash) {
|
|
166
|
+
let udi = `chia.${this.storeId}`;
|
|
167
|
+
if (rootHash) {
|
|
168
|
+
udi += `.${rootHash}`;
|
|
169
|
+
}
|
|
68
170
|
return new Promise((resolve, reject) => {
|
|
69
|
-
const url = `
|
|
171
|
+
const url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/${udi}/${key}`;
|
|
70
172
|
const urlObj = new url_1.URL(url);
|
|
71
173
|
const requestOptions = {
|
|
72
174
|
hostname: urlObj.hostname,
|
|
@@ -89,11 +191,12 @@ class ContentServer {
|
|
|
89
191
|
}
|
|
90
192
|
}
|
|
91
193
|
else {
|
|
194
|
+
hasRootHashCache.del(`${this.storeId}-${rootHash}`);
|
|
92
195
|
reject(new Error(`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`));
|
|
93
196
|
}
|
|
94
197
|
});
|
|
95
198
|
request.on("error", (error) => {
|
|
96
|
-
console.error(`Request error for ${url}:`, error);
|
|
199
|
+
console.error(`GET Request error for ${url}:`, error);
|
|
97
200
|
reject(error);
|
|
98
201
|
});
|
|
99
202
|
request.end();
|
|
@@ -111,6 +214,9 @@ class ContentServer {
|
|
|
111
214
|
(urlObj.protocol === "http:" ? 80 : ContentServer.port),
|
|
112
215
|
path: urlObj.pathname + urlObj.search,
|
|
113
216
|
method: "HEAD",
|
|
217
|
+
key: fs_1.default.readFileSync(ContentServer.keyPath),
|
|
218
|
+
cert: fs_1.default.readFileSync(ContentServer.certPath),
|
|
219
|
+
rejectUnauthorized: false,
|
|
114
220
|
};
|
|
115
221
|
const request = http_1.default.request(requestOptions, (response) => {
|
|
116
222
|
const { statusCode, headers } = response;
|
|
@@ -149,7 +255,7 @@ class ContentServer {
|
|
|
149
255
|
}
|
|
150
256
|
});
|
|
151
257
|
request.on("error", (error) => {
|
|
152
|
-
console.error(`
|
|
258
|
+
console.error(`HEAD ${url}:`, error.message);
|
|
153
259
|
reject({ success: false });
|
|
154
260
|
});
|
|
155
261
|
request.end();
|
|
@@ -168,7 +274,7 @@ class ContentServer {
|
|
|
168
274
|
// Helper method to fetch content with retries and redirection handling
|
|
169
275
|
async fetchWithRetries(url) {
|
|
170
276
|
let attempt = 0;
|
|
171
|
-
const maxRetries =
|
|
277
|
+
const maxRetries = 1;
|
|
172
278
|
const initialDelay = 2000; // 2 seconds
|
|
173
279
|
const maxDelay = 10000; // 10 seconds
|
|
174
280
|
const delayMultiplier = 1.5;
|
|
@@ -192,23 +298,48 @@ class ContentServer {
|
|
|
192
298
|
}
|
|
193
299
|
throw new Error(`Failed to retrieve data from ${url} after ${maxRetries} attempts.`);
|
|
194
300
|
}
|
|
195
|
-
// Core method to fetch content from a URL
|
|
301
|
+
// Core method to fetch content from a URL with a 5-second inactivity timeout
|
|
196
302
|
async fetch(url, maxRedirects = 5) {
|
|
197
303
|
return new Promise((resolve, reject) => {
|
|
198
304
|
const urlObj = new url_1.URL(url);
|
|
305
|
+
const timeoutDuration = 5000; // 5 seconds
|
|
306
|
+
let timeout = null; // Initialize timeout
|
|
199
307
|
const requestOptions = {
|
|
200
308
|
hostname: urlObj.hostname,
|
|
201
309
|
port: urlObj.port || ContentServer.port,
|
|
202
310
|
path: urlObj.pathname + urlObj.search, // Include query params
|
|
203
311
|
method: "GET",
|
|
312
|
+
key: fs_1.default.readFileSync(ContentServer.keyPath),
|
|
313
|
+
cert: fs_1.default.readFileSync(ContentServer.certPath),
|
|
314
|
+
rejectUnauthorized: false,
|
|
204
315
|
};
|
|
205
316
|
const request = http_1.default.request(requestOptions, (response) => {
|
|
206
317
|
let data = "";
|
|
318
|
+
// Set timeout for inactivity
|
|
319
|
+
timeout = setTimeout(() => {
|
|
320
|
+
console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
|
|
321
|
+
request.destroy(); // Use destroy instead of abort
|
|
322
|
+
reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
|
|
323
|
+
}, timeoutDuration);
|
|
324
|
+
const resetTimeout = () => {
|
|
325
|
+
if (timeout) {
|
|
326
|
+
clearTimeout(timeout);
|
|
327
|
+
}
|
|
328
|
+
timeout = setTimeout(() => {
|
|
329
|
+
console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
|
|
330
|
+
request.destroy(); // Use destroy instead of abort
|
|
331
|
+
reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
|
|
332
|
+
}, timeoutDuration);
|
|
333
|
+
};
|
|
207
334
|
if (response.statusCode === 200) {
|
|
208
335
|
response.on("data", (chunk) => {
|
|
209
336
|
data += chunk;
|
|
337
|
+
resetTimeout(); // Reset the timeout every time data is received
|
|
210
338
|
});
|
|
211
339
|
response.on("end", () => {
|
|
340
|
+
if (timeout) {
|
|
341
|
+
clearTimeout(timeout);
|
|
342
|
+
}
|
|
212
343
|
resolve(data);
|
|
213
344
|
});
|
|
214
345
|
}
|
|
@@ -217,8 +348,9 @@ class ContentServer {
|
|
|
217
348
|
// Handle redirects
|
|
218
349
|
if (maxRedirects > 0) {
|
|
219
350
|
const redirectUrl = new url_1.URL(response.headers.location, url); // Resolve relative URLs based on the original URL
|
|
220
|
-
|
|
221
|
-
|
|
351
|
+
if (timeout) {
|
|
352
|
+
clearTimeout(timeout);
|
|
353
|
+
}
|
|
222
354
|
this.fetch(redirectUrl.toString(), maxRedirects - 1)
|
|
223
355
|
.then(resolve)
|
|
224
356
|
.catch(reject);
|
|
@@ -228,11 +360,91 @@ class ContentServer {
|
|
|
228
360
|
}
|
|
229
361
|
}
|
|
230
362
|
else {
|
|
363
|
+
if (timeout) {
|
|
364
|
+
clearTimeout(timeout);
|
|
365
|
+
}
|
|
231
366
|
reject(new Error(`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`));
|
|
232
367
|
}
|
|
233
368
|
});
|
|
234
369
|
request.on("error", (error) => {
|
|
235
|
-
|
|
370
|
+
if (timeout) {
|
|
371
|
+
clearTimeout(timeout);
|
|
372
|
+
}
|
|
373
|
+
console.error(`GET ${url}:`, error.message);
|
|
374
|
+
reject(error);
|
|
375
|
+
});
|
|
376
|
+
request.end();
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
// New core method to fetch only the first chunk without retries
|
|
380
|
+
async fetchFirstChunk(url) {
|
|
381
|
+
return new Promise((resolve, reject) => {
|
|
382
|
+
const urlObj = new url_1.URL(url);
|
|
383
|
+
const timeoutDuration = 5000; // 5 seconds
|
|
384
|
+
let timeout = null;
|
|
385
|
+
const requestOptions = {
|
|
386
|
+
hostname: urlObj.hostname,
|
|
387
|
+
port: urlObj.port || ContentServer.port,
|
|
388
|
+
path: urlObj.pathname + urlObj.search, // Include query params
|
|
389
|
+
method: "GET",
|
|
390
|
+
key: fs_1.default.readFileSync(ContentServer.keyPath),
|
|
391
|
+
cert: fs_1.default.readFileSync(ContentServer.certPath),
|
|
392
|
+
rejectUnauthorized: false,
|
|
393
|
+
};
|
|
394
|
+
const request = http_1.default.request(requestOptions, (response) => {
|
|
395
|
+
// Set timeout for inactivity
|
|
396
|
+
timeout = setTimeout(() => {
|
|
397
|
+
console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
|
|
398
|
+
request.destroy(); // Use destroy instead of abort
|
|
399
|
+
reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
|
|
400
|
+
}, timeoutDuration);
|
|
401
|
+
const resetTimeout = () => {
|
|
402
|
+
if (timeout) {
|
|
403
|
+
clearTimeout(timeout);
|
|
404
|
+
}
|
|
405
|
+
timeout = setTimeout(() => {
|
|
406
|
+
console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
|
|
407
|
+
request.destroy(); // Use destroy instead of abort
|
|
408
|
+
reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
|
|
409
|
+
}, timeoutDuration);
|
|
410
|
+
};
|
|
411
|
+
if (response.statusCode === 200) {
|
|
412
|
+
response.once("data", (chunk) => {
|
|
413
|
+
if (timeout) {
|
|
414
|
+
clearTimeout(timeout);
|
|
415
|
+
}
|
|
416
|
+
response.destroy(); // Close the connection after receiving the first chunk
|
|
417
|
+
resolve(chunk);
|
|
418
|
+
});
|
|
419
|
+
response.on("end", () => {
|
|
420
|
+
if (timeout) {
|
|
421
|
+
clearTimeout(timeout);
|
|
422
|
+
}
|
|
423
|
+
// In case the response ends before any data is received
|
|
424
|
+
reject(new Error("No data received"));
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
else if ((response.statusCode === 301 || response.statusCode === 302) &&
|
|
428
|
+
response.headers.location) {
|
|
429
|
+
// Handle redirects
|
|
430
|
+
const redirectUrl = new url_1.URL(response.headers.location, url).toString(); // Resolve relative URLs
|
|
431
|
+
if (timeout) {
|
|
432
|
+
clearTimeout(timeout);
|
|
433
|
+
}
|
|
434
|
+
this.fetchFirstChunk(redirectUrl).then(resolve).catch(reject);
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
if (timeout) {
|
|
438
|
+
clearTimeout(timeout);
|
|
439
|
+
}
|
|
440
|
+
reject(new Error(`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`));
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
request.on("error", (error) => {
|
|
444
|
+
if (timeout) {
|
|
445
|
+
clearTimeout(timeout);
|
|
446
|
+
}
|
|
447
|
+
console.error(`GET ${url}:`, error.message);
|
|
236
448
|
reject(error);
|
|
237
449
|
});
|
|
238
450
|
request.end();
|
|
@@ -240,4 +452,4 @@ class ContentServer {
|
|
|
240
452
|
}
|
|
241
453
|
}
|
|
242
454
|
exports.ContentServer = ContentServer;
|
|
243
|
-
ContentServer.port =
|
|
455
|
+
ContentServer.port = 4161;
|
|
@@ -4,17 +4,23 @@ export declare class DigNetwork {
|
|
|
4
4
|
private serverCoin;
|
|
5
5
|
private storeDir;
|
|
6
6
|
private peerBlacklist;
|
|
7
|
+
private static networkSyncMap;
|
|
7
8
|
constructor(storeId: string);
|
|
8
|
-
private uploadPreflight;
|
|
9
|
-
uploadStoreHead(digPeer: DigPeer): Promise<void>;
|
|
10
|
-
uploadStore(digPeer: DigPeer): Promise<void>;
|
|
11
9
|
static subscribeToStore(storeId: string): Promise<void>;
|
|
10
|
+
static getAllNetworkDataStoreIds(): string[];
|
|
11
|
+
static getUdiContent(udi: string): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Find a peer that has the store key and root hash, using ranked peers first and searching in groups of 5.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} storeId - The ID of the store.
|
|
16
|
+
* @param {string} rootHash - The root hash of the store.
|
|
17
|
+
* @param {string} [key] - Optional key to check for in the store.
|
|
18
|
+
* @returns {Promise<DigPeer | null>} - A valid peer or null if none found.
|
|
19
|
+
*/
|
|
20
|
+
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string): Promise<DigPeer | null>;
|
|
12
21
|
static unsubscribeFromStore(storeId: string): void;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
private downloadManifestFile;
|
|
17
|
-
private downloadFileFromPeers;
|
|
18
|
-
private runProgressBar;
|
|
22
|
+
static pingNetworkOfUpdate(storeId: string, rootHash: string): Promise<void>;
|
|
23
|
+
syncStoreFromPeers(prioritizedPeer?: DigPeer, maxRootsToProcess?: number): Promise<void>;
|
|
24
|
+
fetchAvailablePeers(): Promise<DigPeer[]>;
|
|
19
25
|
}
|
|
20
26
|
//# sourceMappingURL=DigNetwork.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAKpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;IAChD,OAAO,CAAC,MAAM,CAAC,cAAc,CAAmC;gBAEpD,OAAO,EAAE,MAAM;WAOP,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQtD,yBAAyB,IAAI,MAAM,EAAE;WAI/B,aAAa,CAAC,GAAG,EAAE,MAAM;IAI7C;;;;;;;OAOG;WACiB,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;WAsFZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;WAOrC,mBAAmB,CACrC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAcH,kBAAkB,CAC7B,eAAe,CAAC,EAAE,OAAO,EACzB,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,IAAI,CAAC;IAqFH,mBAAmB,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;CAUvD"}
|