Created
January 9, 2019 10:31
-
-
Save batrudinych/b62e4b0e5b73e6fa54003ddce0a25a5d to your computer and use it in GitHub Desktop.
[s3-redeploy]-parallel-hashes-computation
This file contains 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 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