Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.0.1 alpha.11 #11

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.11](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.10...v0.0.1-alpha.11) (2024-09-10)


### Features

* add plimit to directory add ([468cff2](https://github.com/DIG-Network/dig-chia-sdk/commit/468cff28397c993b22af7388a8472b1d2068aebd))

### [0.0.1-alpha.10](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.9...v0.0.1-alpha.10) (2024-09-10)


Expand Down
52 changes: 39 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.10",
"version": "0.0.1-alpha.11",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down Expand Up @@ -41,6 +41,7 @@
"merkletreejs": "^0.4.0",
"nanospinner": "^1.1.0",
"nconf": "^0.12.1",
"p-limit": "^6.1.0",
"superagent": "^10.0.0"
},
"devDependencies": {
Expand Down
62 changes: 40 additions & 22 deletions src/utils/directoryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import * as fs from "fs";
import * as path from "path";
import { promisify } from "util";
import ignore from "ignore";
import pLimit from "p-limit";
import { DataIntegrityTree } from "../DataIntegrityTree";


const limit = pLimit(10); // Limit the concurrency to 10 (adjust based on your system's file descriptor limit)

// Promisify fs methods
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const readFile = promisify(fs.readFile);

/**
* Recursively add all files in a directory to the Merkle tree, skipping the .dig, .git folders, and files in .gitignore.
* @param datalayer - The DataStoreManager instance.
Expand All @@ -19,37 +29,45 @@ export const addDirectory = async (

// Load .gitignore rules if the file exists
if (fs.existsSync(gitignorePath)) {
const gitignoreContent = fs.readFileSync(gitignorePath, "utf-8");
const gitignoreContent = await readFile(gitignorePath, "utf-8");
ig.add(gitignoreContent);
}

const files = fs.readdirSync(dirPath);
const files = await readdir(dirPath);

for (const file of files) {
const filePath = path.join(dirPath, file);
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
// Process each file or directory
await Promise.all(
files.map(async (file) => {
const filePath = path.join(dirPath, file);
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");

// Skip the .dig, .git folders and files or directories ignored by .gitignore
if (file === ".dig" || file === ".git" || ig.ignores(relativePath)) {
continue;
}
// Skip the .dig, .git folders and files or directories ignored by .gitignore
if (file === ".dig" || file === ".git" || ig.ignores(relativePath)) {
return;
}

const stat = fs.statSync(filePath);
const fileStat = await stat(filePath);

if (stat.isDirectory()) {
await addDirectory(datalayer, filePath, baseDir);
} else {
await new Promise<void>((resolve, reject) => {
const stream = fs.createReadStream(filePath);
datalayer
.upsertKey(stream, Buffer.from(relativePath).toString("hex"))
.then(resolve)
.catch(reject);
});
}
}
if (fileStat.isDirectory()) {
// Recursively process the directory
return addDirectory(datalayer, filePath, baseDir);
} else {
// Process the file with limited concurrency
return limit(() =>
new Promise<void>((resolve, reject) => {
const stream = fs.createReadStream(filePath);
datalayer
.upsertKey(stream, Buffer.from(relativePath).toString("hex"))
.then(resolve)
.catch(reject);
})
);
}
})
);
};


/**
* Calculate the total size of the DIG_FOLDER_PATH
* @param folderPath - The path of the folder to calculate size.
Expand Down
Loading