@dignetwork/dig-sdk 0.0.1-alpha.11 → 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,15 +1,3 @@
1
1
  import { DataIntegrityTree } from "../DataIntegrityTree";
2
- /**
3
- * Recursively add all files in a directory to the Merkle tree, skipping the .dig, .git folders, and files in .gitignore.
4
- * @param datalayer - The DataStoreManager instance.
5
- * @param dirPath - The directory path.
6
- * @param baseDir - The base directory for relative paths.
7
- */
8
2
  export declare const addDirectory: (datalayer: DataIntegrityTree, dirPath: string, baseDir?: string) => Promise<void>;
9
- /**
10
- * Calculate the total size of the DIG_FOLDER_PATH
11
- * @param folderPath - The path of the folder to calculate size.
12
- * @returns The total size of the folder in bytes.
13
- */
14
- export declare const calculateFolderSize: (folderPath: string) => bigint;
15
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;AAUzD;;;;;GAKG;AACH,eAAO,MAAM,YAAY,cACZ,iBAAiB,WACnB,MAAM,YACN,MAAM,KACd,OAAO,CAAC,IAAI,CA0Cd,CAAC;AAGF;;;;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,48 +26,54 @@ 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"));
32
- const util_1 = require("util");
31
+ const fs = __importStar(require("fs"));
33
32
  const ignore_1 = __importDefault(require("ignore"));
34
- const p_limit_1 = __importDefault(require("p-limit"));
35
- const limit = (0, p_limit_1.default)(10); // Limit the concurrency to 10 (adjust based on your system's file descriptor limit)
36
- // Promisify fs methods
37
- const readdir = (0, util_1.promisify)(fs.readdir);
38
- const stat = (0, util_1.promisify)(fs.stat);
39
- const readFile = (0, util_1.promisify)(fs.readFile);
40
- /**
41
- * Recursively add all files in a directory to the Merkle tree, skipping the .dig, .git folders, and files in .gitignore.
42
- * @param datalayer - The DataStoreManager instance.
43
- * @param dirPath - The directory path.
44
- * @param baseDir - The base directory for relative paths.
45
- */
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
+ };
46
52
  const addDirectory = async (datalayer, dirPath, baseDir = dirPath) => {
47
53
  const ig = (0, ignore_1.default)();
48
54
  const gitignorePath = path.join(baseDir, ".gitignore");
49
55
  // Load .gitignore rules if the file exists
50
56
  if (fs.existsSync(gitignorePath)) {
51
- const gitignoreContent = await readFile(gitignorePath, "utf-8");
57
+ const gitignoreContent = fs.readFileSync(gitignorePath, "utf-8");
52
58
  ig.add(gitignoreContent);
53
59
  }
54
- const files = await readdir(dirPath);
55
- // Process each file or directory
56
- await Promise.all(files.map(async (file) => {
60
+ const files = fs.readdirSync(dirPath);
61
+ const tasks = [];
62
+ for (const file of files) {
57
63
  const filePath = path.join(dirPath, file);
58
64
  const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
59
65
  // Skip the .dig, .git folders and files or directories ignored by .gitignore
60
66
  if (file === ".dig" || file === ".git" || ig.ignores(relativePath)) {
61
- return;
67
+ continue;
62
68
  }
63
- const fileStat = await stat(filePath);
64
- if (fileStat.isDirectory()) {
69
+ const stat = fs.statSync(filePath);
70
+ if (stat.isDirectory()) {
65
71
  // Recursively process the directory
66
- return (0, exports.addDirectory)(datalayer, filePath, baseDir);
72
+ tasks.push(() => (0, exports.addDirectory)(datalayer, filePath, baseDir));
67
73
  }
68
74
  else {
69
- // Process the file with limited concurrency
70
- return limit(() => new Promise((resolve, reject) => {
75
+ // Add a task for each file to be processed
76
+ tasks.push(() => new Promise((resolve, reject) => {
71
77
  const stream = fs.createReadStream(filePath);
72
78
  datalayer
73
79
  .upsertKey(stream, Buffer.from(relativePath).toString("hex"))
@@ -75,27 +81,8 @@ const addDirectory = async (datalayer, dirPath, baseDir = dirPath) => {
75
81
  .catch(reject);
76
82
  }));
77
83
  }
78
- }));
79
- };
80
- exports.addDirectory = addDirectory;
81
- /**
82
- * Calculate the total size of the DIG_FOLDER_PATH
83
- * @param folderPath - The path of the folder to calculate size.
84
- * @returns The total size of the folder in bytes.
85
- */
86
- const calculateFolderSize = (folderPath) => {
87
- let totalSize = BigInt(0);
88
- const files = fs.readdirSync(folderPath);
89
- for (const file of files) {
90
- const filePath = path.join(folderPath, file);
91
- const stat = fs.statSync(filePath);
92
- if (stat.isDirectory()) {
93
- totalSize += (0, exports.calculateFolderSize)(filePath);
94
- }
95
- else {
96
- totalSize += BigInt(stat.size);
97
- }
98
84
  }
99
- 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
100
87
  };
101
- 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.11",
3
+ "version": "0.0.1-alpha.13",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",