@xhmikosr/downloader 16.1.1 → 16.1.3
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 +42 -33
- package/package.json +7 -8
- package/readme.md +12 -11
package/index.js
CHANGED
|
@@ -2,10 +2,9 @@ import events from 'node:events';
|
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import process from 'node:process';
|
|
5
|
-
import
|
|
5
|
+
import {parse} from 'content-disposition';
|
|
6
6
|
import archiveType from '@xhmikosr/archive-type';
|
|
7
7
|
import decompress from '@xhmikosr/decompress';
|
|
8
|
-
import defaults from 'defaults';
|
|
9
8
|
import extName from 'ext-name';
|
|
10
9
|
import {fileTypeFromBuffer} from 'file-type';
|
|
11
10
|
import filenamify from 'filenamify';
|
|
@@ -26,7 +25,7 @@ const getExtFromMime = response => {
|
|
|
26
25
|
return null;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
const exts = extName.mime(header);
|
|
28
|
+
const exts = extName.mime(header.split(';')[0].trim());
|
|
30
29
|
|
|
31
30
|
return exts.length === 1 ? exts[0].ext : null;
|
|
32
31
|
};
|
|
@@ -35,7 +34,7 @@ const getFilename = async (response, data) => {
|
|
|
35
34
|
const header = response.headers['content-disposition'];
|
|
36
35
|
|
|
37
36
|
if (header) {
|
|
38
|
-
const parsed =
|
|
37
|
+
const parsed = parse(header);
|
|
39
38
|
|
|
40
39
|
if (parsed.parameters?.filename) {
|
|
41
40
|
return parsed.parameters.filename;
|
|
@@ -56,15 +55,27 @@ const getFilename = async (response, data) => {
|
|
|
56
55
|
return filename;
|
|
57
56
|
};
|
|
58
57
|
|
|
59
|
-
const filterEvents = async (
|
|
60
|
-
for await (const [message] of events.on(
|
|
58
|
+
const filterEvents = async (emitter, event) => {
|
|
59
|
+
for await (const [message] of events.on(emitter, event)) {
|
|
61
60
|
if (message) {
|
|
62
61
|
return message;
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
};
|
|
66
65
|
|
|
67
|
-
const
|
|
66
|
+
const mergeDefinedOptions = (defaults, overrides = {}) => {
|
|
67
|
+
const merged = {...defaults};
|
|
68
|
+
|
|
69
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
70
|
+
if (value !== undefined) {
|
|
71
|
+
merged[key] = value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return merged;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const download = (uri, output, options = {}) => {
|
|
68
79
|
if (typeof output === 'object') {
|
|
69
80
|
options = output;
|
|
70
81
|
output = null;
|
|
@@ -72,36 +83,34 @@ const download = (uri, output, options) => {
|
|
|
72
83
|
|
|
73
84
|
options = {
|
|
74
85
|
...options,
|
|
75
|
-
got:
|
|
76
|
-
decompress: options
|
|
86
|
+
got: mergeDefinedOptions(defaultGotOptions, options.got),
|
|
87
|
+
decompress: options.decompress ?? {},
|
|
77
88
|
};
|
|
78
89
|
|
|
79
90
|
const stream = got.stream(uri, options.got);
|
|
80
91
|
|
|
81
|
-
const promise =
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
.then(() => data);
|
|
104
|
-
});
|
|
92
|
+
const promise = (async () => {
|
|
93
|
+
const response = await filterEvents(stream, 'response');
|
|
94
|
+
const streamData = options.got.responseType === 'buffer' ? getStreamAsBuffer(stream) : getStream(stream);
|
|
95
|
+
const data = await streamData;
|
|
96
|
+
|
|
97
|
+
const hasArchiveData = options.extract && await archiveType(data);
|
|
98
|
+
|
|
99
|
+
if (!output) {
|
|
100
|
+
return hasArchiveData ? decompress(data, options.decompress) : data;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const filename = options.filename || filenamify(await getFilename(response, data));
|
|
104
|
+
const outputFilepath = path.join(output, filename);
|
|
105
|
+
|
|
106
|
+
if (hasArchiveData) {
|
|
107
|
+
return decompress(data, path.dirname(outputFilepath), options.decompress);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await fs.mkdir(path.dirname(outputFilepath), {recursive: true});
|
|
111
|
+
await fs.writeFile(outputFilepath, data);
|
|
112
|
+
return data;
|
|
113
|
+
})();
|
|
105
114
|
|
|
106
115
|
// eslint-disable-next-line unicorn/no-thenable
|
|
107
116
|
stream.then = promise.then.bind(promise);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xhmikosr/downloader",
|
|
3
|
-
"version": "16.1.
|
|
3
|
+
"version": "16.1.3",
|
|
4
4
|
"description": "Download and extract files",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -43,21 +43,20 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@xhmikosr/archive-type": "^8.0.1",
|
|
46
|
-
"@xhmikosr/decompress": "^11.1.
|
|
47
|
-
"content-disposition": "^
|
|
48
|
-
"defaults": "^2.0.2",
|
|
46
|
+
"@xhmikosr/decompress": "^11.1.3",
|
|
47
|
+
"content-disposition": "^2.0.1",
|
|
49
48
|
"ext-name": "^5.0.0",
|
|
50
|
-
"file-type": "^21.3.
|
|
49
|
+
"file-type": "^21.3.4",
|
|
51
50
|
"filenamify": "^7.0.1",
|
|
52
51
|
"get-stream": "^9.0.1",
|
|
53
52
|
"got": "^14.6.6"
|
|
54
53
|
},
|
|
55
54
|
"devDependencies": {
|
|
56
|
-
"@xhmikosr/decompress-unzip": "^8.1.
|
|
55
|
+
"@xhmikosr/decompress-unzip": "^8.1.1",
|
|
57
56
|
"ava": "^7.0.0",
|
|
58
57
|
"c8": "^11.0.0",
|
|
59
|
-
"nock": "^14.0.
|
|
60
|
-
"xo": "^
|
|
58
|
+
"nock": "^14.0.15",
|
|
59
|
+
"xo": "^2.0.2"
|
|
61
60
|
},
|
|
62
61
|
"xo": {
|
|
63
62
|
"rules": {
|
package/readme.md
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xhmikosr/downloader [](https://www.npmjs.com/package/@xhmikosr/downloader) [](https://github.com/XhmikosR/download/actions/workflows/ci.yml?query=branch%3Amaster)
|
|
2
2
|
|
|
3
3
|
> Download and extract files
|
|
4
4
|
|
|
5
5
|
*See [download-cli](https://github.com/kevva/download-cli) for the command-line version.*
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
## Install
|
|
9
8
|
|
|
10
9
|
```sh
|
|
11
10
|
npm install @xhmikosr/downloader
|
|
12
11
|
```
|
|
13
12
|
|
|
14
|
-
|
|
15
13
|
## Usage
|
|
16
14
|
|
|
17
15
|
```js
|
|
@@ -23,25 +21,28 @@ import download from '@xhmikosr/downloader';
|
|
|
23
21
|
|
|
24
22
|
fs.writeFileSync('dist/foo.jpg', await download('http://unicorn.com/foo.jpg'));
|
|
25
23
|
|
|
26
|
-
download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
|
|
24
|
+
download('http://unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
|
|
27
25
|
|
|
28
26
|
await Promise.all([
|
|
29
|
-
'unicorn.com/foo.jpg',
|
|
30
|
-
'cats.com/dancing.gif'
|
|
27
|
+
'http://unicorn.com/foo.jpg',
|
|
28
|
+
'http://cats.com/dancing.gif'
|
|
31
29
|
].map(url => download(url, 'dist')));
|
|
32
30
|
})();
|
|
33
31
|
```
|
|
34
32
|
|
|
35
33
|
### Proxies
|
|
36
34
|
|
|
37
|
-
To work with proxies, read the [`got documentation`](https://github.com/sindresorhus/got#
|
|
35
|
+
To work with proxies, read the [`got documentation`](https://github.com/sindresorhus/got/blob/main/documentation/tips.md#proxying).
|
|
36
|
+
|
|
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
40
|
|
|
40
41
|
## API
|
|
41
42
|
|
|
42
43
|
### download(url, destination?, options?)
|
|
43
44
|
|
|
44
|
-
Returns both a `Promise<Buffer>` and a [Duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with [additional events](https://github.com/sindresorhus/got#
|
|
45
|
+
Returns both a `Promise<Buffer>` and a [Duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with [additional events](https://github.com/sindresorhus/got/blob/main/documentation/3-streams.md#events).
|
|
45
46
|
|
|
46
47
|
#### url
|
|
47
48
|
|
|
@@ -53,7 +54,7 @@ URL to download.
|
|
|
53
54
|
|
|
54
55
|
Type: `string`
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
Directory to save the file to.
|
|
57
58
|
|
|
58
59
|
#### options
|
|
59
60
|
|
|
@@ -67,14 +68,14 @@ Same options as [`got`](https://github.com/sindresorhus/got#options).
|
|
|
67
68
|
|
|
68
69
|
Same options as [`decompress`](https://github.com/XhmikosR/decompress#options).
|
|
69
70
|
|
|
70
|
-
##### extract
|
|
71
|
+
##### options.extract
|
|
71
72
|
|
|
72
73
|
* Type: `boolean`
|
|
73
74
|
* Default: `false`
|
|
74
75
|
|
|
75
76
|
If set to `true`, try extracting the file using [`decompress`](https://github.com/XhmikosR/decompress).
|
|
76
77
|
|
|
77
|
-
##### filename
|
|
78
|
+
##### options.filename
|
|
78
79
|
|
|
79
80
|
Type: `string`
|
|
80
81
|
|