mirror of
https://github.com/jakejarvis/careful-downloader.git
synced 2025-04-26 14:38:27 -04:00
"simplify" the checksum file regex pattern
This commit is contained in:
parent
5d3410f47f
commit
1f7f290c90
34
index.js
34
index.js
@ -8,9 +8,13 @@ import debug from "./lib/debug.js";
|
|||||||
import download from "./lib/download.js";
|
import download from "./lib/download.js";
|
||||||
import { checksumViaFile, checksumViaString } from "./lib/checksum.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)}`);
|
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 checksumMethod;
|
||||||
let checksumKey;
|
let checksumKey;
|
||||||
if (options.checksumUrl) {
|
if (options.checksumUrl) {
|
||||||
@ -22,9 +26,9 @@ export default async (downloadUrl, options = {}) => {
|
|||||||
checksumMethod = "string";
|
checksumMethod = "string";
|
||||||
checksumKey = options.checksumHash;
|
checksumKey = options.checksumHash;
|
||||||
} else {
|
} 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
|
// normalize options and set defaults
|
||||||
options = {
|
options = {
|
||||||
@ -61,22 +65,26 @@ export default async (downloadUrl, options = {}) => {
|
|||||||
download(checksumKey, path.join(tempDir, checksumFilename)),
|
download(checksumKey, path.join(tempDir, checksumFilename)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// validate the checksum
|
// finally do the calculations
|
||||||
// eslint-disable-next-line max-len
|
validated = await checksumViaFile(
|
||||||
if (await checksumViaFile(path.join(tempDir, options.filename), path.join(tempDir, checksumFilename), options.algorithm, options.encoding)) {
|
path.join(tempDir, options.filename),
|
||||||
validated = true;
|
path.join(tempDir, checksumFilename),
|
||||||
}
|
options.algorithm,
|
||||||
|
options.encoding,
|
||||||
|
);
|
||||||
} else if (checksumMethod === "string") {
|
} else if (checksumMethod === "string") {
|
||||||
debug("Using a provided hash to validate...");
|
debug("Using a provided hash to validate...");
|
||||||
|
|
||||||
// get the desired file
|
// get the desired file
|
||||||
await download(downloadUrl, path.join(tempDir, options.filename));
|
await download(downloadUrl, path.join(tempDir, options.filename));
|
||||||
|
|
||||||
// validate the checksum
|
// finally do the calculations
|
||||||
// eslint-disable-next-line max-len
|
validated = await checksumViaString(
|
||||||
if (await checksumViaString(path.join(tempDir, options.filename), checksumKey, options.algorithm, options.encoding)) {
|
path.join(tempDir, options.filename),
|
||||||
validated = true;
|
checksumKey,
|
||||||
}
|
options.algorithm,
|
||||||
|
options.encoding,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop here if the checksum wasn't validated by either method
|
// stop here if the checksum wasn't validated by either method
|
||||||
|
@ -64,7 +64,7 @@ const parseChecksumFile = async (checksumFile) => {
|
|||||||
const data = fs.readFileSync(checksumFile, { encoding: "utf8" });
|
const data = fs.readFileSync(checksumFile, { encoding: "utf8" });
|
||||||
|
|
||||||
// https://regexr.com/67k7i
|
// https://regexr.com/67k7i
|
||||||
const lineRegex = /^([\da-fA-F]+) ([ *])(.+)$/;
|
const lineRegex = /^([\da-fA-F]+)[ \t]+[*]?(.+)$/;
|
||||||
|
|
||||||
// extract each file and its hash into this object
|
// extract each file and its hash into this object
|
||||||
const checksums = {};
|
const checksums = {};
|
||||||
@ -84,10 +84,8 @@ const parseChecksumFile = async (checksumFile) => {
|
|||||||
} else {
|
} else {
|
||||||
parsedLine.shift();
|
parsedLine.shift();
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
|
||||||
const [hash, binary, file] = parsedLine;
|
|
||||||
|
|
||||||
// save the current file & hash in the checksums object
|
// save the current file & hash in the checksums object
|
||||||
|
const [hash, file] = parsedLine;
|
||||||
checksums[file] = hash;
|
checksums[file] = hash;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user