Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save batrudinych/b62e4b0e5b73e6fa54003ddce0a25a5d to your computer and use it in GitHub Desktop.
Save batrudinych/b62e4b0e5b73e6fa54003ddce0a25a5d to your computer and use it in GitHub Desktop.
[s3-redeploy]-parallel-hashes-computation
const crypto = require('crypto');
const fs = require('fs');
// Limit of files to be processed simultaneously
const concurrency = 3;
// Promisified hash computation with streams
function computeSingleFileHash(fileName) {
const hash = crypto.createHash('md5');
const fileStream = fs.createReadStream(fileName);
// Error handling is intentionally omitted in this snippet
return new Promise(resolve => {
fileStream
.pipe(hash)
.on('finish', () => {
hash.end();
resolve(hash.read());
});
});
}
// Compute file hashes in parallel, applying concurrency limit
// Returns a promise
function computeFileHashes(fileNames) {
// The same function is used in parallel files uploading snippet
return parallel(
fileNames,
computeSingleFileHash,
concurrency
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment