|
1 | | -'use strict'; |
| 1 | +// File content hashing utilities for cache invalidation and content verification |
| 2 | +import fs from 'fs'; |
| 3 | +import crypto from 'crypto'; |
| 4 | +import d3 from 'd3-queue'; |
2 | 5 |
|
3 | | -const fs = require('fs'); |
4 | | -const crypto = require('crypto'); |
5 | | -const d3 = require('d3-queue'); |
6 | | - |
7 | | -module.exports = { |
8 | | - hashOfFiles: (paths, cb) => { |
9 | | - let queue = d3.queue(); |
10 | | - for (let i = 0; i < paths.length; ++i) { |
11 | | - queue.defer(fs.readFile, paths[i]); |
12 | | - } |
13 | | - queue.awaitAll((err, results) => { |
14 | | - if (err) return cb(err); |
15 | | - let checksum = crypto.createHash('md5'); |
16 | | - for (let i = 0; i < results.length; ++i) { |
17 | | - checksum.update(results[i]); |
18 | | - } |
19 | | - cb(null, checksum.digest('hex')); |
20 | | - }); |
21 | | - }, |
22 | | - |
23 | | - hashOfFile: (path, additional_content, cb) => { |
24 | | - fs.readFile(path, (err, result) => { |
25 | | - if (err) return cb(err); |
26 | | - let checksum = crypto.createHash('md5'); |
27 | | - checksum.update(result + (additional_content || "") ); |
28 | | - cb(null, checksum.digest('hex')); |
29 | | - }); |
| 6 | +// Computes MD5 hash of multiple files concatenated together |
| 7 | +const hashOfFiles = (paths, cb) => { |
| 8 | + let queue = d3.queue(); |
| 9 | + for (let i = 0; i < paths.length; ++i) { |
| 10 | + queue.defer(fs.readFile, paths[i]); |
| 11 | + } |
| 12 | + queue.awaitAll((err, results) => { |
| 13 | + if (err) return cb(err); |
| 14 | + let checksum = crypto.createHash('md5'); |
| 15 | + for (let i = 0; i < results.length; ++i) { |
| 16 | + checksum.update(results[i]); |
30 | 17 | } |
| 18 | + cb(null, checksum.digest('hex')); |
| 19 | + }); |
31 | 20 | }; |
| 21 | + |
| 22 | +// Computes MD5 hash of single file with optional additional content |
| 23 | +const hashOfFile = (path, additional_content, cb) => { |
| 24 | + fs.readFile(path, (err, result) => { |
| 25 | + if (err) return cb(err); |
| 26 | + let checksum = crypto.createHash('md5'); |
| 27 | + checksum.update(result + (additional_content || '')); |
| 28 | + cb(null, checksum.digest('hex')); |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +export { hashOfFiles, hashOfFile }; |
0 commit comments