mirror of
https://github.com/jakejarvis/careful-downloader.git
synced 2025-04-28 06:50:31 -04:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
4e59dcbc0a | |||
0b51c8bbcb | |||
![]() |
caf7571aa4 | ||
f92b50e9ee | |||
51e8e0161f | |||
b181818b02 | |||
ae885be192 | |||
9454810eab | |||
efbca27fd0 |
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
@ -16,19 +16,21 @@ jobs:
|
||||
- macos-latest
|
||||
- windows-latest
|
||||
node:
|
||||
- 20
|
||||
- 18
|
||||
- 16
|
||||
- 14
|
||||
- 12
|
||||
fail-fast: false
|
||||
runs-on: ${{ matrix.os }}
|
||||
name: Node ${{ matrix.node }} on ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node }}
|
||||
- run: yarn install --frozen-lockfile
|
||||
- run: yarn audit
|
||||
continue-on-error: true
|
||||
- run: yarn test
|
||||
env:
|
||||
DEBUG: careful-downloader
|
||||
|
6
.github/workflows/release.yml
vendored
6
.github/workflows/release.yml
vendored
@ -10,10 +10,10 @@ jobs:
|
||||
name: Publish to NPM
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 14
|
||||
node-version: 18
|
||||
registry-url: https://registry.npmjs.org/
|
||||
- env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ node_modules/
|
||||
# potentially leftover test artifacts
|
||||
downloads/
|
||||
test/temp/
|
||||
*.log
|
||||
|
10
index.js
10
index.js
@ -1,6 +1,6 @@
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
import tempy from "tempy";
|
||||
import { temporaryDirectory } from "tempy";
|
||||
import decompress from "decompress";
|
||||
import isPathInside from "is-path-inside";
|
||||
|
||||
@ -47,7 +47,7 @@ export default async (downloadUrl, options) => {
|
||||
}
|
||||
|
||||
// initialize temporary directory
|
||||
const tempDir = tempy.directory();
|
||||
const tempDir = temporaryDirectory();
|
||||
debug(`Temp dir generated: '${tempDir}'`);
|
||||
|
||||
try {
|
||||
@ -93,14 +93,14 @@ export default async (downloadUrl, options) => {
|
||||
}
|
||||
|
||||
// optionally clear the target directory of existing files
|
||||
if (options.cleanDestDir && fs.existsSync(options.destDir)) {
|
||||
if (options.cleanDestDir && await fs.access(options.destDir)) {
|
||||
debug(`Deleting contents of '${options.destDir}'`);
|
||||
await fs.remove(options.destDir);
|
||||
await fs.emptyDir(options.destDir);
|
||||
}
|
||||
|
||||
// ensure the target directory exists
|
||||
debug(`Ensuring target '${options.destDir}' exists`);
|
||||
await fs.mkdirp(options.destDir);
|
||||
await fs.ensureDir(options.destDir);
|
||||
|
||||
if (options.extract) {
|
||||
// decompress download and move resulting files to final destination
|
||||
|
@ -50,7 +50,7 @@ export const checksumViaString = async (desiredFile, correctHash, algorithm, enc
|
||||
|
||||
// Takes a path to a file and returns its hash.
|
||||
const generateHashFromFile = async (file, algorithm, encoding) => {
|
||||
const fileBuffer = fs.readFileSync(file);
|
||||
const fileBuffer = await fs.readFile(file);
|
||||
const hashSum = crypto.createHash(algorithm);
|
||||
hashSum.update(fileBuffer);
|
||||
|
||||
@ -61,7 +61,7 @@ const generateHashFromFile = async (file, algorithm, encoding) => {
|
||||
// https://github.com/malept/sumchecker/blob/28aed640a02787490d033fda56eaee30e24e5a71/src/index.ts#L97
|
||||
const parseChecksumFile = async (checksumFile) => {
|
||||
// read the text file holding one or more checksums
|
||||
const data = fs.readFileSync(checksumFile, { encoding: "utf8" });
|
||||
const data = await fs.readFile(checksumFile, { encoding: "utf8" });
|
||||
|
||||
// https://regexr.com/67k7i
|
||||
const lineRegex = /^([\da-fA-F]+)[ \t]+[*]?(.+)$/;
|
||||
|
22
package.json
22
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "careful-downloader",
|
||||
"version": "2.0.0",
|
||||
"version": "3.0.0",
|
||||
"description": "🕵️♀️ Downloads a file and its checksums, validates the hash, and optionally extracts it if safe.",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/jakejarvis/careful-downloader",
|
||||
@ -25,24 +25,24 @@
|
||||
"test": "eslint . && mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "^4.3.2",
|
||||
"debug": "^4.3.4",
|
||||
"decompress": "^4.2.1",
|
||||
"fs-extra": "^10.0.0",
|
||||
"got": "^11.8.2",
|
||||
"fs-extra": "^11.1.1",
|
||||
"got": "^12.6.0",
|
||||
"is-path-inside": "^4.0.0",
|
||||
"tempy": "^2.0.0"
|
||||
"tempy": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@jakejarvis/eslint-config": "*",
|
||||
"@types/debug": "^4.1.7",
|
||||
"@types/debug": "^4.1.8",
|
||||
"@types/decompress": "^4.2.4",
|
||||
"@types/fs-extra": "^9.0.13",
|
||||
"chai": "^4.3.4",
|
||||
"eslint": "^8.0.1",
|
||||
"mocha": "^9.1.3"
|
||||
"@types/fs-extra": "^11.0.1",
|
||||
"chai": "^4.3.7",
|
||||
"eslint": "^8.41.0",
|
||||
"mocha": "^10.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
||||
"node": ">=14.14"
|
||||
},
|
||||
"keywords": [
|
||||
"download",
|
||||
|
@ -42,7 +42,7 @@ describe("checksum via downloaded text file", function () {
|
||||
},
|
||||
)).to.throw;
|
||||
|
||||
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.false;
|
||||
expect(fs.existsSync(path.join(__dirname, "temp", "hugo_0.88.0_Windows-64bit.zip"))).to.be.false;
|
||||
|
||||
// clean up
|
||||
fs.removeSync(path.join(__dirname, "temp"));
|
||||
@ -101,7 +101,7 @@ describe("checksum via string", function () {
|
||||
this.timeout(30000); // increase timeout to an excessive 30 seconds for CI
|
||||
|
||||
expect(async () => download(
|
||||
// download mismatching versions to trigger error
|
||||
// validate against a clearly incorrect hash
|
||||
"https://github.com/gohugoio/hugo/releases/download/v0.88.0/hugo_0.88.0_Windows-64bit.zip",
|
||||
{
|
||||
checksumHash: "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234",
|
||||
@ -111,7 +111,7 @@ describe("checksum via string", function () {
|
||||
},
|
||||
)).to.throw;
|
||||
|
||||
expect(fs.existsSync(path.join(__dirname, "temp", "hugo.exe"))).to.be.false;
|
||||
expect(fs.existsSync(path.join(__dirname, "temp", "hugo_0.88.0_Windows-64bit.zip"))).to.be.false;
|
||||
|
||||
// clean up
|
||||
fs.removeSync(path.join(__dirname, "temp"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user