@dignetwork/dig-sdk 0.0.1-alpha.34 → 0.0.1-alpha.35
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.
|
@@ -132,10 +132,10 @@ class DigNetwork {
|
|
|
132
132
|
break;
|
|
133
133
|
const peerIp = digPeers[0];
|
|
134
134
|
const digPeer = new DigPeer_1.DigPeer(peerIp, storeId);
|
|
135
|
-
const storeResponse = await digPeer.
|
|
135
|
+
const storeResponse = await digPeer.propagationServer.headStore({
|
|
136
136
|
hasRootHash: rootHash,
|
|
137
137
|
});
|
|
138
|
-
console.log(peerIp, storeResponse
|
|
138
|
+
console.log(peerIp, storeResponse.headers);
|
|
139
139
|
if (storeResponse.success &&
|
|
140
140
|
storeResponse.headers?.["x-has-rootHash"] === "true") {
|
|
141
141
|
if (!key)
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import http from 'http';
|
|
1
2
|
import { Readable } from "stream";
|
|
2
3
|
export declare class PropagationServer {
|
|
3
4
|
private ipAddress;
|
|
@@ -19,7 +20,12 @@ export declare class PropagationServer {
|
|
|
19
20
|
getStatus(): Promise<{
|
|
20
21
|
synced: boolean;
|
|
21
22
|
}>;
|
|
22
|
-
headStore(
|
|
23
|
+
headStore(options?: {
|
|
24
|
+
hasRootHash: string;
|
|
25
|
+
}): Promise<{
|
|
26
|
+
success: boolean;
|
|
27
|
+
headers?: http.IncomingHttpHeaders;
|
|
28
|
+
}>;
|
|
23
29
|
isStoreSynced(): Promise<boolean>;
|
|
24
30
|
getUploadDetails(): Promise<{
|
|
25
31
|
nonce: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAEA,OAAO,IAAI,MAA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AAC;IAMjC,gBAAgB,IAAI,OAAO,CAAC;QACvC,KAAK,EAAE,MAAM,CAAC;QACd,gBAAgB,EAAE,MAAM,CAAC;QACzB,eAAe,EAAE,MAAM,CAAC;QACxB,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;YAmBY,WAAW;YAyCX,kBAAkB;YAoElB,gBAAgB;YAgEhB,cAAc;YAiCd,KAAK;YAmDL,IAAI;YAmEJ,SAAS;YAuCT,2BAA2B;YAiC3B,gBAAgB;CAyC/B"}
|
|
@@ -61,8 +61,11 @@ class PropagationServer {
|
|
|
61
61
|
return JSON.parse(statusJson);
|
|
62
62
|
}
|
|
63
63
|
// Method to check if a specific store exists (HEAD request)
|
|
64
|
-
async headStore() {
|
|
65
|
-
|
|
64
|
+
async headStore(options) {
|
|
65
|
+
let url = `https://${this.ipAddress}:${PropagationServer.port}/${this.storeId}`;
|
|
66
|
+
if (options?.hasRootHash) {
|
|
67
|
+
url += `?hasRootHash=${options.hasRootHash}`;
|
|
68
|
+
}
|
|
66
69
|
return this.head(url);
|
|
67
70
|
}
|
|
68
71
|
// In PropagationServer.ts:
|
|
@@ -275,31 +278,63 @@ class PropagationServer {
|
|
|
275
278
|
});
|
|
276
279
|
}
|
|
277
280
|
// Helper method to perform HEAD requests
|
|
278
|
-
async head(url) {
|
|
281
|
+
async head(url, maxRedirects = 5) {
|
|
279
282
|
return new Promise((resolve, reject) => {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
283
|
+
try {
|
|
284
|
+
// Parse the input URL
|
|
285
|
+
const urlObj = new url_1.URL(url);
|
|
286
|
+
const requestOptions = {
|
|
287
|
+
hostname: urlObj.hostname,
|
|
288
|
+
port: urlObj.port || (urlObj.protocol === "https:" ? 443 : undefined),
|
|
289
|
+
path: urlObj.pathname + urlObj.search,
|
|
290
|
+
method: "HEAD",
|
|
291
|
+
key: fs_1.default.readFileSync(PropagationServer.keyPath),
|
|
292
|
+
cert: fs_1.default.readFileSync(PropagationServer.certPath),
|
|
293
|
+
rejectUnauthorized: false,
|
|
294
|
+
};
|
|
295
|
+
const request = https_1.default.request(requestOptions, (response) => {
|
|
296
|
+
const { statusCode, headers } = response;
|
|
297
|
+
// If status code is 2xx, return success
|
|
298
|
+
if (statusCode && statusCode >= 200 && statusCode < 300) {
|
|
299
|
+
resolve({ success: true, headers });
|
|
300
|
+
}
|
|
301
|
+
// Handle 3xx redirection
|
|
302
|
+
else if (statusCode && statusCode >= 300 && statusCode < 400 && headers.location) {
|
|
303
|
+
if (maxRedirects > 0) {
|
|
304
|
+
let redirectUrl = headers.location;
|
|
305
|
+
// Check if the redirect URL is relative
|
|
306
|
+
if (!/^https?:\/\//i.test(redirectUrl)) {
|
|
307
|
+
// Resolve the relative URL based on the original URL
|
|
308
|
+
redirectUrl = new url_1.URL(redirectUrl, url).toString();
|
|
309
|
+
console.log(`Resolved relative redirect to: ${redirectUrl}`);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
console.log(`Redirecting to: ${redirectUrl}`);
|
|
313
|
+
}
|
|
314
|
+
// Recursively follow the redirection
|
|
315
|
+
this.head(redirectUrl, maxRedirects - 1)
|
|
316
|
+
.then(resolve)
|
|
317
|
+
.catch(reject);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
reject({ success: false, message: "Too many redirects" });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
// For other status codes, consider it a failure
|
|
325
|
+
resolve({ success: false });
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
request.on("error", (error) => {
|
|
329
|
+
console.error(`Request error for ${url}:`, error);
|
|
330
|
+
reject({ success: false });
|
|
331
|
+
});
|
|
332
|
+
request.end();
|
|
333
|
+
}
|
|
334
|
+
catch (err) {
|
|
335
|
+
console.error(`Invalid URL: ${url}`, err);
|
|
336
|
+
reject({ success: false, message: "Invalid URL" });
|
|
337
|
+
}
|
|
303
338
|
});
|
|
304
339
|
}
|
|
305
340
|
// Helper method to fetch JSON data from a URL
|