@xhmikosr/downloader 16.1.3 → 16.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +30 -2
- package/package.json +9 -6
- package/readme.md +13 -1
package/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
import {createHash} from 'node:crypto';
|
|
1
2
|
import events from 'node:events';
|
|
2
3
|
import fs from 'node:fs/promises';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import process from 'node:process';
|
|
6
|
+
import {buffer, text} from 'node:stream/consumers';
|
|
5
7
|
import {parse} from 'content-disposition';
|
|
6
8
|
import archiveType from '@xhmikosr/archive-type';
|
|
7
9
|
import decompress from '@xhmikosr/decompress';
|
|
8
10
|
import extName from 'ext-name';
|
|
9
11
|
import {fileTypeFromBuffer} from 'file-type';
|
|
10
12
|
import filenamify from 'filenamify';
|
|
11
|
-
import getStream, {getStreamAsBuffer} from 'get-stream';
|
|
12
13
|
import got from 'got';
|
|
13
14
|
|
|
14
15
|
const defaultGotOptions = {
|
|
@@ -63,6 +64,31 @@ const filterEvents = async (emitter, event) => {
|
|
|
63
64
|
}
|
|
64
65
|
};
|
|
65
66
|
|
|
67
|
+
const verifyHash = (data, hash) => {
|
|
68
|
+
if (!hash) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const colon = typeof hash === 'string' ? hash.indexOf(':') : -1;
|
|
73
|
+
const algorithm = colon === -1 ? '' : hash.slice(0, colon);
|
|
74
|
+
const value = colon === -1 ? '' : hash.slice(colon + 1);
|
|
75
|
+
|
|
76
|
+
if (!algorithm || !value) {
|
|
77
|
+
throw new Error('Invalid `hash` option, expected "<algorithm>:<hex>".');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let actual;
|
|
81
|
+
try {
|
|
82
|
+
actual = createHash(algorithm).update(data).digest('hex');
|
|
83
|
+
} catch {
|
|
84
|
+
throw new Error(`Unsupported hash algorithm: ${algorithm}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (actual.toLowerCase() !== value.toLowerCase()) {
|
|
88
|
+
throw new Error(`Hash mismatch, expected ${algorithm} ${value.toLowerCase()} but got ${actual}`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
66
92
|
const mergeDefinedOptions = (defaults, overrides = {}) => {
|
|
67
93
|
const merged = {...defaults};
|
|
68
94
|
|
|
@@ -91,9 +117,11 @@ const download = (uri, output, options = {}) => {
|
|
|
91
117
|
|
|
92
118
|
const promise = (async () => {
|
|
93
119
|
const response = await filterEvents(stream, 'response');
|
|
94
|
-
const streamData = options.got.responseType === 'buffer' ?
|
|
120
|
+
const streamData = options.got.responseType === 'buffer' ? buffer(stream) : text(stream);
|
|
95
121
|
const data = await streamData;
|
|
96
122
|
|
|
123
|
+
verifyHash(data, options.hash);
|
|
124
|
+
|
|
97
125
|
const hasArchiveData = options.extract && await archiveType(data);
|
|
98
126
|
|
|
99
127
|
if (!output) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xhmikosr/downloader",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.3.0",
|
|
4
4
|
"description": "Download and extract files",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -42,26 +42,29 @@
|
|
|
42
42
|
"url"
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@xhmikosr/archive-type": "^8.0
|
|
45
|
+
"@xhmikosr/archive-type": "^8.1.0",
|
|
46
46
|
"@xhmikosr/decompress": "^11.1.3",
|
|
47
47
|
"content-disposition": "^2.0.1",
|
|
48
48
|
"ext-name": "^5.0.0",
|
|
49
49
|
"file-type": "^21.3.4",
|
|
50
|
-
"filenamify": "^7.0.
|
|
51
|
-
"get-stream": "^9.0.1",
|
|
50
|
+
"filenamify": "^7.0.2",
|
|
52
51
|
"got": "^14.6.6"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
55
|
-
"@xhmikosr/decompress-unzip": "^8.
|
|
54
|
+
"@xhmikosr/decompress-unzip": "^8.2.1",
|
|
56
55
|
"ava": "^7.0.0",
|
|
57
56
|
"c8": "^11.0.0",
|
|
58
|
-
"nock": "^14.0.
|
|
57
|
+
"nock": "^14.0.16",
|
|
59
58
|
"xo": "^2.0.2"
|
|
60
59
|
},
|
|
61
60
|
"xo": {
|
|
62
61
|
"rules": {
|
|
63
62
|
"promise/prefer-await-to-then": "off",
|
|
63
|
+
"require-unicode-regexp": "off",
|
|
64
64
|
"unicorn/prevent-abbreviations": "off"
|
|
65
65
|
}
|
|
66
|
+
},
|
|
67
|
+
"allowScripts": {
|
|
68
|
+
"unrs-resolver@1.11.1": true
|
|
66
69
|
}
|
|
67
70
|
}
|
package/readme.md
CHANGED
|
@@ -36,7 +36,7 @@ To work with proxies, read the [`got documentation`](https://github.com/sindreso
|
|
|
36
36
|
|
|
37
37
|
### SSL
|
|
38
38
|
|
|
39
|
-
TLS certificate verification is enabled by default. It honors npm's [`strict-ssl`](https://docs.npmjs.com/cli/v11/using-npm/config#strict-ssl) config, so running `npm config set strict-ssl false` disables it for self-signed certificates or proxy setups. Override per call with [`options.got.https.rejectUnauthorized`]https://github.com/sindresorhus/got/blob/v14.6.6/documentation/5-https.md).
|
|
39
|
+
TLS certificate verification is enabled by default. It honors npm's [`strict-ssl`](https://docs.npmjs.com/cli/v11/using-npm/config#strict-ssl) config, so running `npm config set strict-ssl false` disables it for self-signed certificates or proxy setups. Override per call with [`options.got.https.rejectUnauthorized`](https://github.com/sindresorhus/got/blob/v14.6.6/documentation/5-https.md).
|
|
40
40
|
|
|
41
41
|
## API
|
|
42
42
|
|
|
@@ -80,3 +80,15 @@ If set to `true`, try extracting the file using [`decompress`](https://github.co
|
|
|
80
80
|
Type: `string`
|
|
81
81
|
|
|
82
82
|
Name of the saved file.
|
|
83
|
+
|
|
84
|
+
##### options.hash
|
|
85
|
+
|
|
86
|
+
Type: `string`
|
|
87
|
+
|
|
88
|
+
Expected hash of the downloaded data as `"<algorithm>:<hex>"`, checked before it's extracted or written. A mismatch throws. `algorithm` is any digest [`crypto.createHash`](https://nodejs.org/api/crypto.html#cryptocreatehashalgorithm-options) accepts.
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
await download('http://unicorn.com/foo.tar.gz', 'dist', {
|
|
92
|
+
hash: 'sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
|
|
93
|
+
});
|
|
94
|
+
```
|