| 1 |
const path = require('path'); |
| 2 |
const { readFile } = require('fs/promises'); |
| 3 |
const crypto = require('crypto'); |
| 4 |
|
| 5 |
module.exports = function addHashFilter(absolutePath, callback) { |
| 6 |
const filePath = path.resolve(__dirname, `../../dist/${absolutePath}`); |
| 7 |
readFile(filePath, { encoding: 'utf-8' }) |
| 8 |
.then(content => { |
| 9 |
const hash = crypto.createHash('sha512'); |
| 10 |
hash.update(content, 'utf-8'); |
| 11 |
return hash.digest('hex'); |
| 12 |
}) |
| 13 |
.then(hash => { |
| 14 |
callback(null, `${absolutePath}?hash=${hash.slice(0, 10)}`); |
| 15 |
}) |
| 16 |
.catch(() => callback(null, absolutePath)); |
| 17 |
}; |