Created
September 19, 2021 17:09
-
-
Save fsjuhl/a1dc10229b0ff85d9cefff1283b11854 to your computer and use it in GitHub Desktop.
Hashes all the files in the adjacent metadata folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs") | |
const crypto = require("crypto") | |
const processConcurrently = async (inputs, fetcher, concurrentRequests = 5, maxRetries = 3) => { | |
let index = 0 | |
const output = Array(inputs.length) | |
const worker = (ticket = false, retry = 0) => { | |
if (!ticket) ticket = index++ | |
if (ticket >= inputs.length) return | |
const input = inputs[ticket] | |
return fetcher(input, ticket) | |
.then(result => output[ticket] = result) | |
.then(() => worker()) | |
.catch(() => retry <= maxRetries && worker(ticket, retry++)) | |
} | |
await Promise.all(Array(concurrentRequests).fill().map(() => worker())) | |
return output | |
} | |
const hashFile = path => new Promise((resolve, reject) => { | |
const hash = crypto.createHash("sha256") | |
const input = fs.createReadStream(path) | |
input.on("error", reject) | |
input.on("data", chunk => hash.update(chunk)) | |
input.on("close", () => resolve(hash.digest("hex"))) | |
}) | |
processConcurrently( | |
fs.readdirSync(`${__dirname}/metadata`).map(file => `${__dirname}/metadata/${file}`), | |
hashFile | |
).then(hashes => { | |
// console.log(hashes) | |
const hash = crypto.createHash("sha256") | |
hashes.forEach(_hash => hash.update(_hash)) | |
console.log("Provenance hash:", hash.digest("hex")) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment