@dignetwork/dig-sdk 0.0.1-alpha.12 → 0.0.1-alpha.13

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,9 +1,3 @@
1
1
  import { DataIntegrityTree } from "../DataIntegrityTree";
2
2
  export declare const addDirectory: (datalayer: DataIntegrityTree, dirPath: string, baseDir?: string) => Promise<void>;
3
- /**
4
- * Calculate the total size of the DIG_FOLDER_PATH
5
- * @param folderPath - The path of the folder to calculate size.
6
- * @returns The total size of the folder in bytes.
7
- */
8
- export declare const calculateFolderSize: (folderPath: string) => bigint;
9
3
  //# sourceMappingURL=directoryUtils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"directoryUtils.d.ts","sourceRoot":"","sources":["../../src/utils/directoryUtils.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AASzD,eAAO,MAAM,YAAY,cACZ,iBAAiB,WACnB,MAAM,YACN,MAAM,KACd,OAAO,CAAC,IAAI,CAyCd,CAAC;AAIF;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,eAAgB,MAAM,KAAG,MAiBxD,CAAC"}
1
+ {"version":3,"file":"directoryUtils.d.ts","sourceRoot":"","sources":["../../src/utils/directoryUtils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AA2BzD,eAAO,MAAM,YAAY,cACZ,iBAAiB,WACnB,MAAM,YACN,MAAM,KACd,OAAO,CAAC,IAAI,CA2Cd,CAAC"}
@@ -26,17 +26,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.calculateFolderSize = exports.addDirectory = void 0;
30
- const fs = __importStar(require("fs"));
29
+ exports.addDirectory = void 0;
31
30
  const path = __importStar(require("path"));
31
+ const fs = __importStar(require("fs"));
32
32
  const ignore_1 = __importDefault(require("ignore"));
33
- // Function to dynamically load p-limit since it's an ES module
34
- async function loadPLimit() {
35
- const { default: pLimit } = await Promise.resolve().then(() => __importStar(require('p-limit')));
36
- return pLimit;
37
- }
33
+ // Custom concurrency handler
34
+ const limitConcurrency = async (concurrencyLimit, tasks) => {
35
+ const results = [];
36
+ const executing = [];
37
+ for (const task of tasks) {
38
+ const p = task();
39
+ results.push(p);
40
+ // Once the limit is reached, wait for one to complete
41
+ if (executing.length >= concurrencyLimit) {
42
+ await Promise.race(executing);
43
+ }
44
+ // Add the new task to the executing array
45
+ executing.push(p);
46
+ // When a task completes, remove it from the executing array
47
+ p.finally(() => executing.splice(executing.indexOf(p), 1));
48
+ }
49
+ // Wait for all remaining tasks to complete
50
+ return Promise.all(results);
51
+ };
38
52
  const addDirectory = async (datalayer, dirPath, baseDir = dirPath) => {
39
- const limit = await loadPLimit(); // Dynamically load p-limit and get the default export
40
53
  const ig = (0, ignore_1.default)();
41
54
  const gitignorePath = path.join(baseDir, ".gitignore");
42
55
  // Load .gitignore rules if the file exists
@@ -45,20 +58,22 @@ const addDirectory = async (datalayer, dirPath, baseDir = dirPath) => {
45
58
  ig.add(gitignoreContent);
46
59
  }
47
60
  const files = fs.readdirSync(dirPath);
48
- await Promise.all(files.map(async (file) => {
61
+ const tasks = [];
62
+ for (const file of files) {
49
63
  const filePath = path.join(dirPath, file);
50
64
  const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
51
65
  // Skip the .dig, .git folders and files or directories ignored by .gitignore
52
66
  if (file === ".dig" || file === ".git" || ig.ignores(relativePath)) {
53
- return;
67
+ continue;
54
68
  }
55
69
  const stat = fs.statSync(filePath);
56
70
  if (stat.isDirectory()) {
57
- await (0, exports.addDirectory)(datalayer, filePath, baseDir);
71
+ // Recursively process the directory
72
+ tasks.push(() => (0, exports.addDirectory)(datalayer, filePath, baseDir));
58
73
  }
59
74
  else {
60
- // Use the dynamically loaded p-limit to limit concurrent file processing
61
- return limit(10)(() => new Promise((resolve, reject) => {
75
+ // Add a task for each file to be processed
76
+ tasks.push(() => new Promise((resolve, reject) => {
62
77
  const stream = fs.createReadStream(filePath);
63
78
  datalayer
64
79
  .upsertKey(stream, Buffer.from(relativePath).toString("hex"))
@@ -66,27 +81,8 @@ const addDirectory = async (datalayer, dirPath, baseDir = dirPath) => {
66
81
  .catch(reject);
67
82
  }));
68
83
  }
69
- }));
70
- };
71
- exports.addDirectory = addDirectory;
72
- /**
73
- * Calculate the total size of the DIG_FOLDER_PATH
74
- * @param folderPath - The path of the folder to calculate size.
75
- * @returns The total size of the folder in bytes.
76
- */
77
- const calculateFolderSize = (folderPath) => {
78
- let totalSize = BigInt(0);
79
- const files = fs.readdirSync(folderPath);
80
- for (const file of files) {
81
- const filePath = path.join(folderPath, file);
82
- const stat = fs.statSync(filePath);
83
- if (stat.isDirectory()) {
84
- totalSize += (0, exports.calculateFolderSize)(filePath);
85
- }
86
- else {
87
- totalSize += BigInt(stat.size);
88
- }
89
84
  }
90
- return totalSize;
85
+ // Run tasks with limited concurrency (set the concurrency limit as needed)
86
+ await limitConcurrency(10, tasks); // Adjust 10 based on your system limits
91
87
  };
92
- exports.calculateFolderSize = calculateFolderSize;
88
+ exports.addDirectory = addDirectory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.12",
3
+ "version": "0.0.1-alpha.13",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",