@dignetwork/dig-sdk 0.0.1-alpha.106 → 0.0.1-alpha.107

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.
@@ -47,7 +47,7 @@ export declare class PropagationServer {
47
47
  * Upload a file to the server by sending a PUT request.
48
48
  * Logs progress using a local cli-progress bar.
49
49
  */
50
- uploadFile(label: string, dataPath: string): Promise<void>;
50
+ uploadFile(label: string, dataPath: string, uncompress?: boolean): Promise<void>;
51
51
  /**
52
52
  * Commit the upload session by sending a POST request to the server.
53
53
  */
@@ -1 +1 @@
1
- {"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,MAAM,OAAO,CAAC;AAW1B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAkBrC,qBAAa,iBAAiB;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAE7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAQ;IACpC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAQ;gBAErC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAY9C;;OAEG;IACG,gBAAgB;IAOtB;;OAEG;IACH,gBAAgB;IAQhB;;OAEG;IACH,oBAAoB,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM;IAyB3D;;OAEG;IACG,gBAAgB,CACpB,QAAQ,CAAC,EAAE,MAAM,GAChB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E,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IA4B7C;;;OAGG;IACG,YAAY,CAChB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM;IA0GjB;;OAEG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA2FpB"}
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -13,7 +36,8 @@ const https_1 = __importDefault(require("https"));
13
36
  const os_1 = __importDefault(require("os"));
14
37
  const path_1 = __importDefault(require("path"));
15
38
  const progress_stream_1 = __importDefault(require("progress-stream"));
16
- const asyncPool_1 = require("../utils/asyncPool");
39
+ const zlib = __importStar(require("zlib"));
40
+ const promiseUtils_1 = require("../utils/promiseUtils");
17
41
  const nanospinner_1 = require("nanospinner");
18
42
  const hashUtils_1 = require("../utils/hashUtils");
19
43
  const ssl_1 = require("../utils/ssl");
@@ -183,7 +207,7 @@ class PropagationServer {
183
207
  * Upload a file to the server by sending a PUT request.
184
208
  * Logs progress using a local cli-progress bar.
185
209
  */
186
- async uploadFile(label, dataPath) {
210
+ async uploadFile(label, dataPath, uncompress = false) {
187
211
  const filePath = path_1.default.join(config_1.STORE_PATH, this.storeId, dataPath);
188
212
  const { nonce, fileExists } = await this.getFileNonce(dataPath);
189
213
  if (fileExists) {
@@ -223,8 +247,20 @@ class PropagationServer {
223
247
  });
224
248
  // Add inactivity timeout to the progress stream
225
249
  const passThroughStream = this.addInactivityTimeout(new stream_1.PassThrough(), PropagationServer.inactivityTimeout);
226
- // Pipe the read stream through the progress stream into the PassThrough stream
227
- fileReadStream.pipe(progressStream).pipe(passThroughStream);
250
+ // Decide whether to uncompress the file during upload
251
+ if (uncompress) {
252
+ // Create a gunzip (uncompression) stream
253
+ const gunzip = zlib.createGunzip();
254
+ // Pipe the streams: fileReadStream -> gunzip -> progressStream -> passThroughStream
255
+ fileReadStream
256
+ .pipe(gunzip)
257
+ .pipe(progressStream)
258
+ .pipe(passThroughStream);
259
+ }
260
+ else {
261
+ // Pipe the streams: fileReadStream -> progressStream -> passThroughStream
262
+ fileReadStream.pipe(progressStream).pipe(passThroughStream);
263
+ }
228
264
  // Use form-data to construct the request body
229
265
  const formData = new form_data_1.default();
230
266
  formData.append("file", passThroughStream);
@@ -311,7 +347,7 @@ class PropagationServer {
311
347
  // Start the upload session
312
348
  await propagationServer.startUploadSession(rootHash);
313
349
  const dataStore = blockchain_1.DataStore.from(storeId);
314
- const files = await dataStore.getFileSetForRootHash(rootHash);
350
+ const files = await dataStore.getFileSetForRootHash(rootHash, true);
315
351
  // Prepare upload tasks
316
352
  const uploadTasks = files.map((file) => ({
317
353
  label: file.name,
@@ -319,8 +355,8 @@ class PropagationServer {
319
355
  }));
320
356
  // Limit the number of concurrent uploads
321
357
  const concurrencyLimit = 10; // Adjust this number as needed
322
- await (0, asyncPool_1.asyncPool)(concurrencyLimit, uploadTasks, async (task) => {
323
- await propagationServer.uploadFile(task.label, task.dataPath);
358
+ await (0, promiseUtils_1.asyncPool)(concurrencyLimit, uploadTasks, async (task) => {
359
+ await propagationServer.uploadFile(task.label, task.dataPath, true);
324
360
  });
325
361
  // Commit the session after all files have been uploaded
326
362
  await propagationServer.commitUploadSession();
@@ -520,7 +556,7 @@ class PropagationServer {
520
556
  const tempDir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "downloadStore-"));
521
557
  try {
522
558
  // Download files to the temporary directory
523
- await (0, asyncPool_1.asyncPool)(concurrencyLimit, downloadTasks, async (task) => {
559
+ await (0, promiseUtils_1.asyncPool)(concurrencyLimit, downloadTasks, async (task) => {
524
560
  await propagationServer.downloadFile(task.label, task.dataPath, rootHash, tempDir);
525
561
  });
526
562
  // Save the rootHash.dat file to the temporary directory
@@ -59,7 +59,7 @@ export declare class DataStore {
59
59
  * @param {string} rootHash - The root hash to fetch the file set.
60
60
  * @returns {Promise<{ fileName: string, file: string }[]>} - An array of unique file objects.
61
61
  */
62
- getFileSetForRootHash(rootHash: string): Promise<{
62
+ getFileSetForRootHash(rootHash: string, skipIntegirtyCheck?: boolean): Promise<{
63
63
  name: string;
64
64
  path: string;
65
65
  }[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"DataStore.d.ts","sourceRoot":"","sources":["../../src/blockchain/DataStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,SAAS,IAAI,eAAe,EAE5B,iBAAiB,EAIlB,MAAM,8BAA8B,CAAC;AAWtC,OAAO,EAAE,eAAe,EAAW,MAAM,UAAU,CAAC;AAGpD,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACzB,MA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,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CA6C7C"}
@@ -20,6 +20,7 @@ const FileCache_1 = require("../utils/FileCache");
20
20
  const DataStoreSerializer_1 = require("./DataStoreSerializer");
21
21
  const node_cache_1 = __importDefault(require("node-cache"));
22
22
  const config_2 = require("../utils/config");
23
+ const StoreInfoCacheUpdater_1 = require("./StoreInfoCacheUpdater");
23
24
  // Initialize the cache with a TTL of 180 seconds (3 minutes)
24
25
  const rootHistoryCache = new node_cache_1.default({ stdTTL: 180 });
25
26
  const stat = (0, util_1.promisify)(fs_1.default.stat);
@@ -202,47 +203,26 @@ class DataStore {
202
203
  }
203
204
  async fetchCoinInfo() {
204
205
  try {
206
+ // Kick off the updater instance in the backgounf if not already running
207
+ StoreInfoCacheUpdater_1.StoreInfoCacheUpdater.initInstance();
205
208
  // Initialize the cache for the current storeId's coin info
206
209
  const storeCoinCache = new FileCache_1.FileCache(`stores`);
207
210
  // Try to get cached store info
208
211
  const cachedInfo = storeCoinCache.get(this.storeId);
209
212
  if (cachedInfo) {
210
- try {
211
- const { latestStore: serializedStore, latestHeight: previousHeight, latestHash: previousHash, } = cachedInfo;
212
- // Deserialize the stored data using DataStoreSerializer
213
- const { latestStore: previousInfo } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
214
- latestStore: serializedStore,
215
- latestHeight: previousHeight.toString(),
216
- latestHash: previousHash,
217
- });
218
- // Sync with peer if necessary
219
- const peer = await FullNodePeer_1.FullNodePeer.connect();
220
- const { latestStore, latestHeight } = await peer.syncStore(previousInfo, previousHeight, Buffer.from(previousHash, "hex"), false);
221
- const latestHash = await peer.getHeaderHash(latestHeight);
222
- // Serialize the store data for caching
223
- const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, latestHeight, latestHash).serialize();
224
- // Cache updated store info
225
- storeCoinCache.set(this.storeId, {
226
- latestStore: serializedLatestStore,
227
- latestHeight,
228
- latestHash: latestHash.toString("hex"),
229
- });
230
- return { latestStore, latestHeight, latestHash };
231
- }
232
- catch {
233
- // Return cached info if sync fails
234
- const { latestStore, latestHeight, latestHash } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
235
- latestStore: cachedInfo.latestStore,
236
- latestHeight: cachedInfo.latestHeight.toString(),
237
- latestHash: cachedInfo.latestHash,
238
- });
239
- return {
240
- latestStore,
241
- latestHeight,
242
- latestHash: latestHash,
243
- };
244
- }
213
+ // If we have cached info, return it directly
214
+ const { latestStore, latestHeight } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
215
+ latestStore: cachedInfo.latestStore,
216
+ latestHeight: cachedInfo.latestHeight.toString(),
217
+ latestHash: cachedInfo.latestHash,
218
+ });
219
+ return {
220
+ latestStore,
221
+ latestHeight: cachedInfo.latestHeight,
222
+ latestHash: Buffer.from(cachedInfo.latestHash, "hex"),
223
+ };
245
224
  }
225
+ // If no cached info, proceed to fetch and cache it
246
226
  // Use getCreationHeight to retrieve height and hash information
247
227
  const { createdAtHeight, createdAtHash } = await this.getCreationHeight();
248
228
  // Sync store from peer
@@ -384,7 +364,7 @@ class DataStore {
384
364
  * @param {string} rootHash - The root hash to fetch the file set.
385
365
  * @returns {Promise<{ fileName: string, file: string }[]>} - An array of unique file objects.
386
366
  */
387
- async getFileSetForRootHash(rootHash) {
367
+ async getFileSetForRootHash(rootHash, skipIntegirtyCheck = false) {
388
368
  const datFilePath = path_1.default.join(config_1.STORE_PATH, this.storeId, `${rootHash}.dat`);
389
369
  const datFileContent = JSON.parse(fs_1.default.readFileSync(datFilePath, "utf-8"));
390
370
  // Use a Set to ensure uniqueness
@@ -393,8 +373,12 @@ class DataStore {
393
373
  for (const [fileKey, fileData] of Object.entries(datFileContent.files)) {
394
374
  const filePath = (0, hashUtils_1.getFilePathFromSha256)(datFileContent.files[fileKey].sha256, "data");
395
375
  // Perform the integrity check
396
- const integrityCheck = await DataIntegrityTree_1.DataIntegrityTree.validateKeyIntegrityWithForeignTree(fileKey, datFileContent.files[fileKey].sha256, datFileContent, rootHash, path_1.default.join(config_1.STORE_PATH, this.storeId, "data"));
397
- if (integrityCheck) {
376
+ let integrityCheck;
377
+ if (!skipIntegirtyCheck) {
378
+ integrityCheck =
379
+ await DataIntegrityTree_1.DataIntegrityTree.validateKeyIntegrityWithForeignTree(fileKey, datFileContent.files[fileKey].sha256, datFileContent, rootHash, path_1.default.join(config_1.STORE_PATH, this.storeId, "data"));
380
+ }
381
+ if (integrityCheck || skipIntegirtyCheck) {
398
382
  // Add the file to the Set
399
383
  filesInvolved.add({
400
384
  name: Buffer.from(fileKey, "hex").toString("utf-8"),
@@ -0,0 +1,10 @@
1
+ export declare class StoreInfoCacheUpdater {
2
+ private static instance;
3
+ private storeCoinCache;
4
+ private updateInterval;
5
+ private constructor();
6
+ static initInstance(): StoreInfoCacheUpdater;
7
+ private startCacheUpdater;
8
+ private updateCache;
9
+ }
10
+ //# sourceMappingURL=StoreInfoCacheUpdater.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StoreInfoCacheUpdater.d.ts","sourceRoot":"","sources":["../../src/blockchain/StoreInfoCacheUpdater.ts"],"names":[],"mappings":"AAMA,qBAAa,qBAAqB;IAChC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAwB;IAC/C,OAAO,CAAC,cAAc,CAInB;IACH,OAAO,CAAC,cAAc,CAAS;IAE/B,OAAO;WAMO,YAAY,IAAI,qBAAqB;IAOnD,OAAO,CAAC,iBAAiB;YAIX,WAAW;CAsE1B"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StoreInfoCacheUpdater = void 0;
4
+ const timers_1 = require("timers");
5
+ const FullNodePeer_1 = require("./FullNodePeer");
6
+ const utils_1 = require("../utils");
7
+ const DataStoreSerializer_1 = require("./DataStoreSerializer");
8
+ const utils_2 = require("../utils");
9
+ class StoreInfoCacheUpdater {
10
+ constructor(updateIntervalInMinutes = 5) {
11
+ this.storeCoinCache = new utils_1.FileCache(`stores`);
12
+ this.updateInterval = updateIntervalInMinutes * 60 * 1000; // Convert minutes to milliseconds
13
+ this.startCacheUpdater();
14
+ }
15
+ static initInstance() {
16
+ if (!StoreInfoCacheUpdater.instance) {
17
+ StoreInfoCacheUpdater.instance = new StoreInfoCacheUpdater();
18
+ }
19
+ return StoreInfoCacheUpdater.instance;
20
+ }
21
+ startCacheUpdater() {
22
+ (0, timers_1.setInterval)(() => this.updateCache(), this.updateInterval);
23
+ }
24
+ async updateCache() {
25
+ try {
26
+ const storeIds = this.storeCoinCache.getCachedKeys();
27
+ for (const storeId of storeIds) {
28
+ try {
29
+ const cachedInfo = this.storeCoinCache.get(storeId);
30
+ if (!cachedInfo) {
31
+ continue;
32
+ }
33
+ // Deserialize the cached store info
34
+ const { latestStore: serializedStore, latestHeight, latestHash } = cachedInfo;
35
+ const { latestStore: previousInfo } = DataStoreSerializer_1.DataStoreSerializer.deserialize({
36
+ latestStore: serializedStore,
37
+ latestHeight: latestHeight.toString(),
38
+ latestHash: latestHash,
39
+ });
40
+ // Wrap the connection with a timeout
41
+ const peer = await (0, utils_2.withTimeout)(FullNodePeer_1.FullNodePeer.connect(), 60000, "Timeout connecting to FullNodePeer");
42
+ // Wrap the syncStore call with a timeout
43
+ const { latestStore, latestHeight: newHeight } = await (0, utils_2.withTimeout)(peer.syncStore(previousInfo, latestHeight, Buffer.from(latestHash, "hex"), false), 60000, `Timeout syncing store for storeId ${storeId}`);
44
+ // Wrap the getHeaderHash call with a timeout
45
+ const latestHashBuffer = await (0, utils_2.withTimeout)(peer.getHeaderHash(newHeight), 60000, `Timeout getting header hash for height ${newHeight}`);
46
+ // Serialize the updated store data for caching
47
+ const serializedLatestStore = new DataStoreSerializer_1.DataStoreSerializer(latestStore, newHeight, latestHashBuffer).serialize();
48
+ // Recache the updated store info
49
+ this.storeCoinCache.set(storeId, {
50
+ latestStore: serializedLatestStore,
51
+ latestHeight: newHeight,
52
+ latestHash: latestHashBuffer.toString("hex"),
53
+ });
54
+ }
55
+ catch (error) {
56
+ console.error(`Failed to update cache for storeId ${storeId}:`, error);
57
+ // Optionally handle specific errors or continue with the next storeId
58
+ }
59
+ }
60
+ }
61
+ catch (error) {
62
+ console.error("Failed to update store cache:", error);
63
+ }
64
+ }
65
+ }
66
+ exports.StoreInfoCacheUpdater = StoreInfoCacheUpdater;
@@ -11,5 +11,5 @@ export * from './spinnerUtils';
11
11
  export * from './ssl';
12
12
  export * from './validationUtils';
13
13
  export * from './Environment';
14
- export * from './asyncPool';
14
+ export * from './promiseUtils';
15
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC"}
@@ -27,4 +27,4 @@ __exportStar(require("./spinnerUtils"), exports);
27
27
  __exportStar(require("./ssl"), exports);
28
28
  __exportStar(require("./validationUtils"), exports);
29
29
  __exportStar(require("./Environment"), exports);
30
- __exportStar(require("./asyncPool"), exports);
30
+ __exportStar(require("./promiseUtils"), exports);
@@ -6,4 +6,12 @@
6
6
  * @returns {Promise<Array<R>>} - A promise that resolves when all items have been processed.
7
7
  */
8
8
  export declare function asyncPool<T, R>(limit: number, items: T[], iteratorFn: (item: T) => Promise<R>): Promise<R[]>;
9
- //# sourceMappingURL=asyncPool.d.ts.map
9
+ /**
10
+ * Helper function to add a timeout to a promise.
11
+ * @param promise The original promise.
12
+ * @param ms Timeout in milliseconds.
13
+ * @param timeoutMessage The error message when the timeout is reached.
14
+ * @returns Promise that resolves before the timeout or rejects with an error.
15
+ */
16
+ export declare const withTimeout: <T>(promise: Promise<T>, ms: number, timeoutMessage: string) => Promise<T>;
17
+ //# sourceMappingURL=promiseUtils.d.ts.map
@@ -0,0 +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,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimeout = void 0;
3
4
  exports.asyncPool = asyncPool;
4
5
  /**
5
6
  * Processes items in sequential batches with a concurrency limit.
@@ -21,3 +22,17 @@ async function asyncPool(limit, items, iteratorFn) {
21
22
  }
22
23
  return ret;
23
24
  }
25
+ /**
26
+ * Helper function to add a timeout to a promise.
27
+ * @param promise The original promise.
28
+ * @param ms Timeout in milliseconds.
29
+ * @param timeoutMessage The error message when the timeout is reached.
30
+ * @returns Promise that resolves before the timeout or rejects with an error.
31
+ */
32
+ const withTimeout = (promise, ms, timeoutMessage) => {
33
+ return Promise.race([
34
+ promise,
35
+ new Promise((_, reject) => setTimeout(() => reject(new Error(timeoutMessage)), ms)),
36
+ ]);
37
+ };
38
+ exports.withTimeout = withTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.106",
3
+ "version": "0.0.1-alpha.107",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -1 +0,0 @@
1
- {"version":3,"file":"asyncPool.d.ts","sourceRoot":"","sources":["../../src/utils/asyncPool.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"}