osrm-backend/features/support/hash.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-03-04 15:11:05 -05:00
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var d3 = require('d3-queue');
module.exports = function () {
this.hashOfFiles = (paths, cb) => {
paths = Array.isArray(paths) ? paths : [paths];
2016-05-28 15:09:21 -04:00
var shasum = crypto.createHash('sha1'), hashedFiles = false;
2016-03-04 15:11:05 -05:00
var q = d3.queue(1);
var addFile = (path, cb) => {
fs.readFile(path, (err, data) => {
2016-05-28 15:09:21 -04:00
if (err && err.code === 'ENOENT') cb(); // ignore non-existing files
else if (err) cb(err);
else {
shasum.update(data);
hashedFiles = true;
cb();
}
2016-03-04 15:11:05 -05:00
});
};
paths.forEach(path => { q.defer(addFile, path); });
q.awaitAll(err => {
if (err) throw new Error('*** Error reading files:', err);
2016-05-28 15:09:21 -04:00
if (!hashedFiles) throw new Error('*** No files found: [' + paths.join(', ') + ']');
2016-03-04 15:11:05 -05:00
cb(shasum.digest('hex'));
});
};
this.hashProfile = (cb) => {
this.hashOfFiles(path.resolve(this.PROFILES_PATH, this.profile + '.lua'), cb);
};
this.hashString = (str) => {
return crypto.createHash('sha1').update(str).digest('hex');
};
return this;
};