serverless/lib/plugins/aws/package/lib/getHashForFilePath.js
ifitzsimmons 6e486b3eb1
refactor: Use async in lib/plugins/aws/package (#8870)
Covered the following paths:
* Files put directly into `lib/plugins/aws/package/compile`
* Files put directly into `lib/plugins/aws/package`
* `lib/plugins/aws/package/lib`

Made changes to tests where necessary.
2021-03-01 11:15:34 +01:00

34 lines
848 B
JavaScript

'use strict';
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;