Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function writeFile(path, force) {
const dest = join(publicDir, path);
const cacheId = `public/${path}`;
const dataStream = wrapDataStream(route.get(path), {bail});
const cacheStream = new CacheStream();
const hashStream = new HashStream();
// Get data => Cache data => Calculate hash
return pipeStream(dataStream, cacheStream, hashStream).then(() => {
const cache = Cache.findById(cacheId);
const hash = hashStream.read().toString('hex');
// Skip generating if hash is unchanged
if (!force && cache && cache.hash === hash) {
return;
}
// Save new hash to cache
return Cache.save({
_id: cacheId,
hash
}).then(() => // Write cache data to public folder
function compareFile(a, b) {
const streamA = new util.HashStream();
const streamB = new util.HashStream();
return Promise.all([
pipeStream(fs.createReadStream(a), streamA),
pipeStream(fs.createReadStream(b), streamB)
]).then(() => streamA.read().equals(streamB.read()));
}
function compareFile(a, b) {
const streamA = new util.HashStream();
const streamB = new util.HashStream();
return Promise.all([
pipeStream(fs.createReadStream(a), streamA),
pipeStream(fs.createReadStream(b), streamB)
]).then(() => streamA.read().equals(streamB.read()));
}
return new Promise((resolve, reject) => {
const src = createReadStream(path);
const hasher = new HashStream();
src.pipe(hasher)
.on('finish', () => {
resolve(hasher.read().toString('hex'));
})
.on('error', reject);
});
}