@xhmikosr/downloader 9.0.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 +100 -0
- package/license +9 -0
- package/package.json +70 -0
- package/readme.md +75 -0
package/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {promises as fs} from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import contentDisposition from 'content-disposition';
|
|
5
|
+
import archiveType from 'archive-type';
|
|
6
|
+
import decompress from '@xhmikosr/decompress';
|
|
7
|
+
import filenamify from 'filenamify';
|
|
8
|
+
import getStream from 'get-stream';
|
|
9
|
+
import got from 'got';
|
|
10
|
+
import {pEvent} from 'p-event';
|
|
11
|
+
import fileType from 'file-type';
|
|
12
|
+
import extName from 'ext-name';
|
|
13
|
+
|
|
14
|
+
const filenameFromPath = res => path.basename(new URL(res.requestUrl).pathname);
|
|
15
|
+
|
|
16
|
+
const getExtFromMime = res => {
|
|
17
|
+
const header = res.headers['content-type'];
|
|
18
|
+
|
|
19
|
+
if (!header) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const exts = extName.mime(header);
|
|
24
|
+
|
|
25
|
+
if (exts.length !== 1) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return exts[0].ext;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const getFilename = (res, data) => {
|
|
33
|
+
const header = res.headers['content-disposition'];
|
|
34
|
+
|
|
35
|
+
if (header) {
|
|
36
|
+
const parsed = contentDisposition.parse(header);
|
|
37
|
+
|
|
38
|
+
if (parsed.parameters && parsed.parameters.filename) {
|
|
39
|
+
return parsed.parameters.filename;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let filename = filenameFromPath(res);
|
|
44
|
+
|
|
45
|
+
if (!path.extname(filename)) {
|
|
46
|
+
const ext = (fileType(data) || {}).ext || getExtFromMime(res);
|
|
47
|
+
|
|
48
|
+
if (ext) {
|
|
49
|
+
filename = `${filename}.${ext}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return filename;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const download = (uri, output, options) => {
|
|
57
|
+
if (typeof output === 'object') {
|
|
58
|
+
options = output;
|
|
59
|
+
output = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
options = {
|
|
63
|
+
responseType: 'buffer',
|
|
64
|
+
https: {
|
|
65
|
+
rejectUnauthorized: process.env.npm_config_strict_ssl !== 'false',
|
|
66
|
+
},
|
|
67
|
+
...options,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const stream = got.stream(uri, options);
|
|
71
|
+
|
|
72
|
+
const promise = pEvent(stream, 'response').then(res => {
|
|
73
|
+
const encoding = options.responseType === 'buffer' ? 'buffer' : options.encoding;
|
|
74
|
+
return Promise.all([getStream(stream, {encoding}), res]);
|
|
75
|
+
}).then(result => {
|
|
76
|
+
const [data, res] = result;
|
|
77
|
+
|
|
78
|
+
if (!output) {
|
|
79
|
+
return options.extract && archiveType(data) ? decompress(data, options) : data;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const filename = options.filename || filenamify(getFilename(res, data));
|
|
83
|
+
const outputFilepath = path.join(output, filename);
|
|
84
|
+
|
|
85
|
+
if (options.extract && archiveType(data)) {
|
|
86
|
+
return decompress(data, path.dirname(outputFilepath), options);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return fs.mkdir(path.dirname(outputFilepath), {recursive: true})
|
|
90
|
+
.then(() => fs.writeFile(outputFilepath, data))
|
|
91
|
+
.then(() => data);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
stream.then = promise.then.bind(promise); // eslint-disable-line unicorn/no-thenable
|
|
95
|
+
stream.catch = promise.catch.bind(promise);
|
|
96
|
+
|
|
97
|
+
return stream;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export default download;
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Kevin Mårtensson <kevinmartensson@gmail.com> (github.com/kevva)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xhmikosr/downloader",
|
|
3
|
+
"version": "9.0.0",
|
|
4
|
+
"description": "Download and extract files",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "XhmikosR/download",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"email": "kevinmartensson@gmail.com",
|
|
12
|
+
"name": "Kevin Mårtensson",
|
|
13
|
+
"url": "github.com/kevva"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": "^12.20.0 || ^14.14.0 || >=16.0.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"ava": "ava",
|
|
20
|
+
"xo": "xo",
|
|
21
|
+
"test": "npm run xo && npm run ava",
|
|
22
|
+
"test-ci": "npm run xo && c8 ava"
|
|
23
|
+
},
|
|
24
|
+
"main": "index.js",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./index.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js"
|
|
31
|
+
],
|
|
32
|
+
"keywords": [
|
|
33
|
+
"download",
|
|
34
|
+
"extract",
|
|
35
|
+
"http",
|
|
36
|
+
"request",
|
|
37
|
+
"url"
|
|
38
|
+
],
|
|
39
|
+
"c8": {
|
|
40
|
+
"reporter": [
|
|
41
|
+
"lcovonly",
|
|
42
|
+
"text"
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@xhmikosr/decompress": "^5.0.0",
|
|
47
|
+
"archive-type": "^4.0.0",
|
|
48
|
+
"content-disposition": "^0.5.4",
|
|
49
|
+
"ext-name": "^5.0.0",
|
|
50
|
+
"file-type": "^12.4.2",
|
|
51
|
+
"filenamify": "^5.1.1",
|
|
52
|
+
"get-stream": "^6.0.1",
|
|
53
|
+
"got": "^11.8.5",
|
|
54
|
+
"p-event": "^5.0.1"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"ava": "^4.3.0",
|
|
58
|
+
"c8": "^7.11.3",
|
|
59
|
+
"is-zip": "^1.0.0",
|
|
60
|
+
"nock": "^13.2.6",
|
|
61
|
+
"path-exists": "^5.0.0",
|
|
62
|
+
"random-buffer": "^0.1.0",
|
|
63
|
+
"xo": "^0.49.0"
|
|
64
|
+
},
|
|
65
|
+
"xo": {
|
|
66
|
+
"rules": {
|
|
67
|
+
"unicorn/prevent-abbreviations": "off"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# download [](https://github.com/XhmikosR/download/actions/workflows/ci.yml)
|
|
2
|
+
|
|
3
|
+
> Download and extract files
|
|
4
|
+
|
|
5
|
+
*See [download-cli](https://github.com/kevva/download-cli) for the command-line version.*
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @xhmikosr/downloader
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import download from '@xhmikosr/downloader';
|
|
20
|
+
|
|
21
|
+
(async () => {
|
|
22
|
+
await download('http://unicorn.com/foo.jpg', 'dist');
|
|
23
|
+
|
|
24
|
+
fs.writeFileSync('dist/foo.jpg', await download('http://unicorn.com/foo.jpg'));
|
|
25
|
+
|
|
26
|
+
download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
|
|
27
|
+
|
|
28
|
+
await Promise.all([
|
|
29
|
+
'unicorn.com/foo.jpg',
|
|
30
|
+
'cats.com/dancing.gif'
|
|
31
|
+
].map(url => download(url, 'dist')));
|
|
32
|
+
})();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Proxies
|
|
36
|
+
|
|
37
|
+
To work with proxies, read the [`got documentation`](https://github.com/sindresorhus/got#proxies).
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
### download(url, destination?, options?)
|
|
43
|
+
|
|
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#streams-1).
|
|
45
|
+
|
|
46
|
+
#### url
|
|
47
|
+
|
|
48
|
+
Type: `string`
|
|
49
|
+
|
|
50
|
+
URL to download.
|
|
51
|
+
|
|
52
|
+
#### destination
|
|
53
|
+
|
|
54
|
+
Type: `string`
|
|
55
|
+
|
|
56
|
+
Path to where your file will be written.
|
|
57
|
+
|
|
58
|
+
#### options
|
|
59
|
+
|
|
60
|
+
Type: `Object`
|
|
61
|
+
|
|
62
|
+
Same options as [`got`](https://github.com/sindresorhus/got#options) and [`decompress`](https://github.com/XhmikosR/decompress#options) in addition to the ones below.
|
|
63
|
+
|
|
64
|
+
##### extract
|
|
65
|
+
|
|
66
|
+
* Type: `boolean`
|
|
67
|
+
* Default: `false`
|
|
68
|
+
|
|
69
|
+
If set to `true`, try extracting the file using [`decompress`](https://github.com/XhmikosR/decompress).
|
|
70
|
+
|
|
71
|
+
##### filename
|
|
72
|
+
|
|
73
|
+
Type: `string`
|
|
74
|
+
|
|
75
|
+
Name of the saved file.
|