@dignetwork/dig-sdk 0.0.1-alpha.110 → 0.0.1-alpha.111

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.
@@ -8,6 +8,7 @@ export declare class ContentServer {
8
8
  private static readonly port;
9
9
  constructor(ipAddress: string, storeId: string);
10
10
  getKey(key: string, rootHash: string, challengeHex?: string): Promise<string>;
11
+ getKeyChunk(key: string, rootHash: string): Promise<Buffer>;
11
12
  getPaymentAddress(): Promise<string | null>;
12
13
  getWellKnown(): Promise<any>;
13
14
  getKnownStores(): Promise<any>;
@@ -29,5 +30,6 @@ export declare class ContentServer {
29
30
  private fetchJson;
30
31
  private fetchWithRetries;
31
32
  private fetch;
33
+ private fetchFirstChunk;
32
34
  }
33
35
  //# sourceMappingURL=ContentServer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAIlC,qBAAa,aAAa;IACxB,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;gBAExB,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAYjC,MAAM,CACjB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,CAAC;IAaL,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAe3C,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,cAAc,IAAI,OAAO,CAAC,GAAG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,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,CAAC;IAaJ,WAAW,CACvB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC;IAOL,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAe3C,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,YAAY,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAY7C,OAAO,CAClB,GAAG,EAAE,MAAM,EACX,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAA;KAAE,CAAC;IAYvD,SAAS,CAAC,OAAO,CAAC,EAAE;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC;QACjE,OAAO,EAAE,OAAO,CAAC;QACjB,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAC;KACpC,CAAC;IAUW,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAUrD,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;YAgDrD,IAAI;YA0EJ,SAAS;YAMT,gBAAgB;YAiChB,KAAK;YA+GL,eAAe;CA2G9B"}
@@ -29,6 +29,12 @@ class ContentServer {
29
29
  }
30
30
  return this.fetchWithRetries(url);
31
31
  }
32
+ // New method to get only the first chunk of the content
33
+ async getKeyChunk(key, rootHash) {
34
+ // Construct the base URL
35
+ let url = `https://${(0, network_1.formatHost)(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
36
+ return this.fetchFirstChunk(url);
37
+ }
32
38
  // Method to get the payment address from the peer
33
39
  async getPaymentAddress() {
34
40
  console.log(`Fetching payment address from peer ${this.ipAddress}...`);
@@ -304,6 +310,82 @@ class ContentServer {
304
310
  request.end();
305
311
  });
306
312
  }
313
+ // New core method to fetch only the first chunk without retries
314
+ async fetchFirstChunk(url) {
315
+ return new Promise((resolve, reject) => {
316
+ const urlObj = new url_1.URL(url);
317
+ const timeoutDuration = 5000; // 5 seconds
318
+ let timeout = null;
319
+ const requestOptions = {
320
+ hostname: urlObj.hostname,
321
+ port: urlObj.port || ContentServer.port,
322
+ path: urlObj.pathname + urlObj.search, // Include query params
323
+ method: "GET",
324
+ key: fs_1.default.readFileSync(ContentServer.keyPath),
325
+ cert: fs_1.default.readFileSync(ContentServer.certPath),
326
+ rejectUnauthorized: false,
327
+ };
328
+ const request = http_1.default.request(requestOptions, (response) => {
329
+ // Set timeout for inactivity
330
+ timeout = setTimeout(() => {
331
+ console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
332
+ request.destroy(); // Use destroy instead of abort
333
+ reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
334
+ }, timeoutDuration);
335
+ const resetTimeout = () => {
336
+ if (timeout) {
337
+ clearTimeout(timeout);
338
+ }
339
+ timeout = setTimeout(() => {
340
+ console.error(`Request timeout: No data received for ${timeoutDuration / 1000} seconds.`);
341
+ request.destroy(); // Use destroy instead of abort
342
+ reject(new Error(`Request timed out after ${timeoutDuration / 1000} seconds of inactivity`));
343
+ }, timeoutDuration);
344
+ };
345
+ if (response.statusCode === 200) {
346
+ response.once("data", (chunk) => {
347
+ if (timeout) {
348
+ clearTimeout(timeout);
349
+ }
350
+ response.destroy(); // Close the connection after receiving the first chunk
351
+ resolve(chunk);
352
+ });
353
+ response.on("end", () => {
354
+ if (timeout) {
355
+ clearTimeout(timeout);
356
+ }
357
+ // In case the response ends before any data is received
358
+ reject(new Error("No data received"));
359
+ });
360
+ }
361
+ else if ((response.statusCode === 301 || response.statusCode === 302) &&
362
+ response.headers.location) {
363
+ // Handle redirects
364
+ const redirectUrl = new url_1.URL(response.headers.location, url).toString(); // Resolve relative URLs
365
+ if (timeout) {
366
+ clearTimeout(timeout);
367
+ }
368
+ this.fetchFirstChunk(redirectUrl)
369
+ .then(resolve)
370
+ .catch(reject);
371
+ }
372
+ else {
373
+ if (timeout) {
374
+ clearTimeout(timeout);
375
+ }
376
+ reject(new Error(`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`));
377
+ }
378
+ });
379
+ request.on("error", (error) => {
380
+ if (timeout) {
381
+ clearTimeout(timeout);
382
+ }
383
+ console.error(`GET ${url}:`, error.message);
384
+ reject(error);
385
+ });
386
+ request.end();
387
+ });
388
+ }
307
389
  }
308
390
  exports.ContentServer = ContentServer;
309
391
  ContentServer.port = 4161;
@@ -47,7 +47,7 @@ const selectUnspentCoins = async (peer, coinAmount, feeBigInt, omitCoins = [], w
47
47
  // Filter expired reserved coins and omit valid reservations
48
48
  const now = Date.now();
49
49
  const omitCoinIds = omitCoins.map((coin) => (0, datalayer_driver_1.getCoinId)(coin).toString("hex"));
50
- const validReservedCoins = cachedReservedCoins.filter((coinId) => {
50
+ const validReservedCoins = cachedReservedCoins.map((coinId) => {
51
51
  const reservation = cache.get(coinId);
52
52
  if (reservation && reservation.expiry > now) {
53
53
  // Valid reservation, add to omit list
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.110",
3
+ "version": "0.0.1-alpha.111",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",