All files / src/lib/helpers checksum.ts

30% Statements 9/30
100% Branches 0/0
0% Functions 0/11
28.57% Lines 6/21

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 302x 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");
};