@dignetwork/dig-sdk 0.0.1-alpha.34 → 0.0.1-alpha.36

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.
@@ -1 +1 @@
1
- {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;YAOb,eAAe;IA8ChB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAsDhD,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WA6BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WA6CX,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQ5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAiEF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAoB7B"}
1
+ {"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;YAOb,eAAe;IA8ChB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAsDhD,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WA6BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WAuCX,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQ5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAiEF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YAuErB,cAAc;CAoB7B"}
@@ -132,26 +132,23 @@ 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.contentServer.headStore({
135
+ const storeResponse = await digPeer.propagationServer.headStore({
136
136
  hasRootHash: rootHash,
137
137
  });
138
- console.log(peerIp, storeResponse, storeResponse.headers);
139
- if (storeResponse.success &&
140
- storeResponse.headers?.["x-has-rootHash"] === "true") {
141
- if (!key)
138
+ console.log(peerIp, storeResponse.headers);
139
+ if (storeResponse.headers?.["x-has-roothash"] === "true") {
140
+ console.log(`Found Peer at ${peerIp} for storeId: ${storeId}, root hash ${rootHash}`);
141
+ if (!key) {
142
142
  return peerIp;
143
+ }
143
144
  const keyResponse = await digPeer.contentServer.headKey(key);
144
- if (keyResponse.success &&
145
- keyResponse.headers?.["x-key-exists"] === "true") {
145
+ if (keyResponse.headers?.["x-key-exists"] === "true") {
146
146
  return peerIp;
147
147
  }
148
148
  }
149
149
  peerBlackList.push(peerIp);
150
150
  }
151
- catch (error) {
152
- console.error("Error while sampling the epoch or contacting peer:", error);
153
- break;
154
- }
151
+ catch { }
155
152
  }
156
153
  return null;
157
154
  }
@@ -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(): Promise<boolean>;
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":"AAOA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAGlC,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAS;IAC/B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAQ;IACpC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAK;IACvC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAQ;IAC5C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAS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E,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
- const url = `https://${this.ipAddress}:${PropagationServer.port}/${this.storeId}`;
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
- const urlObj = new url_1.URL(url);
281
- const options = {
282
- hostname: urlObj.hostname,
283
- port: urlObj.port || PropagationServer.port,
284
- path: urlObj.pathname + urlObj.search,
285
- method: "HEAD",
286
- key: fs_1.default.readFileSync(PropagationServer.keyPath),
287
- cert: fs_1.default.readFileSync(PropagationServer.certPath),
288
- rejectUnauthorized: false,
289
- };
290
- const request = https_1.default.request(options, (response) => {
291
- if (response.statusCode === 200) {
292
- resolve(true);
293
- }
294
- else {
295
- resolve(false);
296
- }
297
- });
298
- request.on("error", (error) => {
299
- console.error(`Request error for ${url}:`, error);
300
- reject(false);
301
- });
302
- request.end();
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.34",
3
+ "version": "0.0.1-alpha.36",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",