serverless
Version:
Serverless Framework - Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more
34 lines (30 loc) • 848 B
JavaScript
;
const memoize = require('memoizee');
const crypto = require('crypto');
const fs = require('fs');
const getHashForFilePath = memoize(
async (filePath) => {
const fileHash = crypto.createHash('sha256');
fileHash.setEncoding('base64');
return new Promise((resolve, reject) => {
const readStream = fs.createReadStream(filePath);
readStream
.on('data', (chunk) => {
fileHash.write(chunk);
})
.on('close', () => {
fileHash.end();
resolve(fileHash.read());
})
.on('error', (error) => {
reject(
new Error(
`Error: ${error} encountered during hash calculation for provided filePath: ${filePath}`
)
);
});
});
},
{ promise: true }
);
module.exports = getHashForFilePath;