Skip to content

Commit

Permalink
Read file by chunks to avoid error of maximum size `ERR_FS_FILE_TOO_L…
Browse files Browse the repository at this point in the history
…ARGE` (#450)

* Read file by chunks to avoid error of maximum size `ERR_FS_FILE_TOO_LARGE`

* use node 20 in gha
  • Loading branch information
pablomendezroyo authored Oct 16, 2024
1 parent a16b3b9 commit 8073906
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false # don't cancel all jobs if some of them failed
matrix:
node: ["18", "20"]
node: ["20"]
steps:
- uses: actions/checkout@v4
- name: Setup node
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Node.js 18.x
- name: Set up Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.org/

- name: Install dependencies
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test-ga-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install
- run: yarn build

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test-ga-bump-upstream-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install
- run: yarn build

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test-ga-bump-upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install
- run: yarn build

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test-ga-end-to-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
runs-on: packages
steps:
- uses: actions/checkout@v4
- name: Set up Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install
- run: yarn build

Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"description": "dappnodesdk is a tool to make the creation of new dappnode packages as simple as possible. It helps to initialize and publish in ethereum blockchain",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"dappnodesdk": "dist/dappnodesdk.js"
},
"bin": "dist/dappnodesdk.js",
"scripts": {
"tag-and-publish": "npm version patch && git push --follow-tags",
"test": "mocha \"./{,!(node_modules)/**}/*.test.ts\"",
Expand Down Expand Up @@ -78,7 +76,7 @@
"@types/lodash-es": "^4.17.7",
"@types/mime-types": "^2.1.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.15.11",
"@types/node": "^22.7.5",
"@types/prettier": "^2.1.5",
"@types/request": "^2.48.5",
"@types/rimraf": "^3.0.0",
Expand All @@ -91,9 +89,9 @@
"eslint": "^7.3.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.3"
"typescript": "^5.6.3"
},
"engines": {
"node": ">=18.0.0"
"node": ">=20.0.0"
}
}
34 changes: 22 additions & 12 deletions src/utils/getFileHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ import fs from "fs";
import crypto from "crypto";

/**
* Hashes a file's buffer
* Hashes a file by reading it in chunks
*
* @param path
* @return file's sha3 hash: 0x36d2fe6d4582e8cc1e5ea4c6c05e44bc94b88f4567edca12ba5fd5745796edef
* @return file's sha256 hash
*/
export function getFileHash(path: string): Promise<string | null> {
return fs.promises
.readFile(path)
.then(data => {
const hash = crypto.createHash("sha256");
hash.update(data);
return hash.digest("hex");
})
.catch(e => {
if (e.code === "ENOENT") return null;
else throw e;
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256");
const stream = fs.createReadStream(path);

stream.on("data", (chunk: Buffer) => {
hash.update(chunk); // Should now recognize Buffer correctly
});

stream.on("end", () => {
const fileHash = hash.digest("hex");
resolve(fileHash);
});

stream.on("error", (err: NodeJS.ErrnoException) => {
if (err.code === "ENOENT") {
resolve(null); // file not found
} else {
reject(err); // other errors
}
});
});
}
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@
resolved "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz"
integrity sha512-/fvYntiO1GeICvqbQ3doGDIP97vWmvFt83GKguJ6prmQM2iXZfFcq6YE8KteFyRtX2/h5Hf91BYvPodJKFYv5Q==

"@types/node@*", "@types/node@^18.11.18", "@types/node@^18.15.11":
"@types/node@*", "@types/node@^18.11.18":
version "18.15.11"
resolved "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz"
integrity sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==
Expand All @@ -680,6 +680,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.16.14.tgz#ab67bb907f1146afc6fedb9ce60ae8a99c989631"
integrity sha512-+ImzUB3mw2c5ISJUq0punjDilUQ5GnUim0ZRvchHIWJmOC0G+p0kzhXBqj6cDjK0QdPFwzrHWgrJp3RPvCG5qg==

"@types/node@^22.7.5":
version "22.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.5.tgz#cfde981727a7ab3611a481510b473ae54442b92b"
integrity sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==
dependencies:
undici-types "~6.19.2"

"@types/prettier@^2.1.5":
version "2.1.5"
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-2.1.5.tgz"
Expand Down Expand Up @@ -3708,10 +3715,10 @@ type-fest@^0.8.1:
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

typescript@^4.7.3:
version "4.7.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.7.3.tgz"
integrity sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA==
typescript@^5.6.3:
version "5.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.3.tgz#5f3449e31c9d94febb17de03cc081dd56d81db5b"
integrity sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==

uint8arraylist@^2.1.2, uint8arraylist@^2.4.3:
version "2.4.3"
Expand All @@ -3734,6 +3741,11 @@ uint8arrays@^4.0.3:
dependencies:
multiformats "^11.0.0"

undici-types@~6.19.2:
version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

undici@^5.12.0:
version "5.22.1"
resolved "https://registry.yarnpkg.com/undici/-/undici-5.22.1.tgz#877d512effef2ac8be65e695f3586922e1a57d7b"
Expand Down

0 comments on commit 8073906

Please sign in to comment.