|
| 1 | +import * as crypto from 'node:crypto' |
| 2 | +import * as fs from 'node:fs' |
| 3 | + |
| 4 | +/** |
| 5 | + * Computes the combined checksum of the given files |
| 6 | + */ |
| 7 | +export const checkSumOfFiles = async (files: string[]): Promise<string> => { |
| 8 | + const fileChecksums = await checkSum(files) |
| 9 | + const checksum = checkSumOfStrings( |
| 10 | + [...Object.entries(fileChecksums)].map(([, hash]) => hash), |
| 11 | + ) |
| 12 | + return checksum |
| 13 | +} |
| 14 | + |
| 15 | +const checkSumOfStrings = (strings: string[]): string => { |
| 16 | + const hash = crypto.createHash('sha1') |
| 17 | + hash.update(strings.join('')) |
| 18 | + return hash.digest('hex') |
| 19 | +} |
| 20 | + |
| 21 | +const hashCache: { [key: string]: string } = {} |
| 22 | +const hashFile = async (file: string) => { |
| 23 | + if (hashCache[file] === undefined) { |
| 24 | + hashCache[file] = await new Promise((resolve) => { |
| 25 | + const hash = crypto.createHash('sha1') |
| 26 | + hash.setEncoding('hex') |
| 27 | + const fileStream = fs.createReadStream(file) |
| 28 | + fileStream.pipe(hash, { end: false }) |
| 29 | + fileStream.on('end', () => { |
| 30 | + hash.end() |
| 31 | + const h = hash.read().toString() |
| 32 | + resolve(h) |
| 33 | + }) |
| 34 | + }) |
| 35 | + } |
| 36 | + return hashCache[file] as string |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Computes the checksum for the given files |
| 41 | + */ |
| 42 | +const checkSum = async ( |
| 43 | + files: string[], |
| 44 | +): Promise<{ [key: string]: string }> => { |
| 45 | + const hashes: { [key: string]: string } = {} |
| 46 | + await files.reduce( |
| 47 | + async (p, file) => |
| 48 | + p.then(async () => { |
| 49 | + hashes[file] = await hashFile(file) |
| 50 | + }), |
| 51 | + Promise.resolve() as Promise<any>, |
| 52 | + ) |
| 53 | + return hashes |
| 54 | +} |
0 commit comments