mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Replace the 10 MB of dependencies that is hash-files with a homebrew Promise based implementation of hashFile that uses crypto and fs. This somehow also ends up being the same amount of code as the one using hash-files.
23 lines
569 B
JavaScript
23 lines
569 B
JavaScript
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
|
|
/**
|
|
* Returns a promise that resolves to the SHA1 hash of the file given by path
|
|
*/
|
|
exports.hashFile = function(path) {
|
|
return new Promise(function(resolve, reject) {
|
|
let hasher = crypto.createHash('sha1')
|
|
hasher.setEncoding('hex')
|
|
hasher.on('finish', function() {
|
|
resolve(hasher.read());
|
|
});
|
|
|
|
let stream = fs.createReadStream(path);
|
|
stream.on('error', function(error) {
|
|
// the docs doesn't say anything about what's passed on error
|
|
reject(error);
|
|
});
|
|
stream.pipe(hasher);
|
|
});
|
|
}
|