1
mirror of https://github.com/jakejarvis/careful-downloader.git synced 2025-04-26 03:05:23 -04:00

"simplify" the checksum file regex pattern

This commit is contained in:
Jake Jarvis 2021-10-16 06:53:45 -04:00
parent 5d3410f47f
commit 1f7f290c90
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
2 changed files with 23 additions and 17 deletions

View File

@ -8,9 +8,13 @@ import debug from "./lib/debug.js";
import download from "./lib/download.js";
import { checksumViaFile, checksumViaString } from "./lib/checksum.js";
export default async (downloadUrl, options = {}) => {
export default async (downloadUrl, options) => {
if (!options) {
throw new Error("Missing the options object. See README for details.");
}
debug(`User-provided config: ${JSON.stringify(options)}`);
// figure out which method we're using to validate (via a checksum file or straight-up hash)
let checksumMethod;
let checksumKey;
if (options.checksumUrl) {
@ -22,9 +26,9 @@ export default async (downloadUrl, options = {}) => {
checksumMethod = "string";
checksumKey = options.checksumHash;
} else {
throw new Error("must either provide checksumUrl or checksumHash.");
throw new Error("Must provide either checksumUrl or checksumHash.");
}
debug(`Provided a ${checksumMethod} to validate against: ${checksumKey}`);
debug(`Provided a ${checksumMethod} to validate against: '${checksumKey}'`);
// normalize options and set defaults
options = {
@ -61,22 +65,26 @@ export default async (downloadUrl, options = {}) => {
download(checksumKey, path.join(tempDir, checksumFilename)),
]);
// validate the checksum
// eslint-disable-next-line max-len
if (await checksumViaFile(path.join(tempDir, options.filename), path.join(tempDir, checksumFilename), options.algorithm, options.encoding)) {
validated = true;
}
// finally do the calculations
validated = await checksumViaFile(
path.join(tempDir, options.filename),
path.join(tempDir, checksumFilename),
options.algorithm,
options.encoding,
);
} else if (checksumMethod === "string") {
debug("Using a provided hash to validate...");
// get the desired file
await download(downloadUrl, path.join(tempDir, options.filename));
// validate the checksum
// eslint-disable-next-line max-len
if (await checksumViaString(path.join(tempDir, options.filename), checksumKey, options.algorithm, options.encoding)) {
validated = true;
}
// finally do the calculations
validated = await checksumViaString(
path.join(tempDir, options.filename),
checksumKey,
options.algorithm,
options.encoding,
);
}
// stop here if the checksum wasn't validated by either method

View File

@ -64,7 +64,7 @@ const parseChecksumFile = async (checksumFile) => {
const data = fs.readFileSync(checksumFile, { encoding: "utf8" });
// https://regexr.com/67k7i
const lineRegex = /^([\da-fA-F]+) ([ *])(.+)$/;
const lineRegex = /^([\da-fA-F]+)[ \t]+[*]?(.+)$/;
// extract each file and its hash into this object
const checksums = {};
@ -84,10 +84,8 @@ const parseChecksumFile = async (checksumFile) => {
} else {
parsedLine.shift();
// eslint-disable-next-line no-unused-vars
const [hash, binary, file] = parsedLine;
// save the current file & hash in the checksums object
const [hash, file] = parsedLine;
checksums[file] = hash;
}
}