All files / src/lib/helpers checksum.ts

29.73% Statements 11/37
100% Branches 0/0
0% Functions 0/12
26.92% Lines 7/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 402x 2x 2x   2x                   2x                   2x           2x                  
import { createReadStream } from "fs-extra";
import crypto from "crypto";
import SparkMD5 from "spark-md5";
 
export const checksumFile = (path: string): Promise<string> => {
  return new Promise((resolve, reject) => {
    const hash = crypto.createHash("sha1");
    const stream = createReadStream(path);
    stream.on("error", (err) => reject(err));
    stream.on("data", (chunk) => hash.update(chunk));
    stream.on("end", () => resolve(hash.digest("hex")));
  });
};
 
export const checksumMD5File = (path: string): Promise<string> => {
  return new Promise((resolve, reject) => {
    const spark = new SparkMD5.ArrayBuffer();
    const stream = createReadStream(path);
    stream.on("error", (err) => reject(err));
    stream.on("data", (chunk: ArrayBuffer) => spark.append(chunk));
    stream.on("end", () => resolve(spark.end()));
  });
};
 
export const checksumString = (string: string): string => {
  const hash = crypto.createHash("sha1");
  hash.update(string);
  return hash.digest("hex");
};
 
export const mergeChecksums = (checksums: string[]): string => {
  const hash = crypto.createHash("sha1");
 
  for (let i = 0; i < checksums.length; i++) {
    hash.update(checksums[i]);
  }
 
  return hash.digest("hex");
};