careful-downloader
Advanced tools
Comparing version 1.1.0 to 1.2.0
37
index.js
@@ -12,2 +12,3 @@ import path from "path"; | ||
export default async function downloader(downloadUrl, checksumUrl, options) { | ||
// intialize options if none are set | ||
options = options || {}; | ||
@@ -40,20 +41,22 @@ | ||
// validate the checksum of the download | ||
await checkChecksum(options.tempDir, options.filename, "checksums.txt", options.algorithm, options.encoding); | ||
if (await checkChecksum(options.tempDir, options.filename, "checksums.txt", options.algorithm, options.encoding)) { | ||
// optionally clear the target directory of existing files | ||
if (options.cleanDestDir) { | ||
await fs.remove(options.destDir); | ||
} | ||
// optionally clear the target directory of existing files | ||
if (options.cleanDestDir) { | ||
await fs.remove(options.destDir); | ||
} | ||
// ensure the target directory exists | ||
await fs.mkdirp(options.destDir); | ||
// ensure the target directory exists | ||
await fs.mkdirp(options.destDir); | ||
if (options.extract) { | ||
// decompress download and move resulting files to final destination | ||
await decompress(path.join(options.tempDir, options.filename), options.destDir); | ||
return options.destDir; | ||
if (options.extract) { | ||
// decompress download and move resulting files to final destination | ||
await decompress(path.join(options.tempDir, options.filename), options.destDir); | ||
return options.destDir; | ||
} else { | ||
// move verified download to final destination as-is | ||
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename)); | ||
return path.join(options.destDir, options.filename); | ||
} | ||
} else { | ||
// move verified download to final destination as-is | ||
await fs.copy(path.join(options.tempDir, options.filename), path.join(options.destDir, options.filename)); | ||
return path.join(options.destDir, options.filename); | ||
throw new Error(`Invalid checksum for ${options.filename}.`); | ||
} | ||
@@ -72,3 +75,3 @@ } finally { | ||
return await pipeline( | ||
return pipeline( | ||
got.stream(url, { followRedirect: true }), // GitHub releases redirect to unpredictable URLs | ||
@@ -85,3 +88,3 @@ fs.createWriteStream(dest), | ||
return await checker.validate(baseDir, downloadFile); | ||
return checker.validate(baseDir, downloadFile); | ||
} |
{ | ||
"name": "careful-downloader", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "🕵️♀️ Downloads a file and its checksums to a temporary directory, validates the hash, and optionally extracts it if safe.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11038
135