@xhmikosr/downloader 16.1.0 → 16.1.2
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 +37 -28
- package/package.json +6 -8
package/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import process from 'node:process';
|
|
|
5
5
|
import contentDisposition 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';
|
|
@@ -64,7 +63,19 @@ const filterEvents = async (name, listener) => {
|
|
|
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.2",
|
|
4
4
|
"description": "Download and extract files",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -43,11 +43,10 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@xhmikosr/archive-type": "^8.0.1",
|
|
46
|
-
"@xhmikosr/decompress": "^11.1.
|
|
47
|
-
"content-disposition": "^1.0
|
|
48
|
-
"defaults": "^2.0.2",
|
|
46
|
+
"@xhmikosr/decompress": "^11.1.1",
|
|
47
|
+
"content-disposition": "^1.1.0",
|
|
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"
|
|
@@ -56,9 +55,8 @@
|
|
|
56
55
|
"@xhmikosr/decompress-unzip": "^8.1.0",
|
|
57
56
|
"ava": "^7.0.0",
|
|
58
57
|
"c8": "^11.0.0",
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"xo": "^1.2.3"
|
|
58
|
+
"nock": "^14.0.12",
|
|
59
|
+
"xo": "^2.0.2"
|
|
62
60
|
},
|
|
63
61
|
"xo": {
|
|
64
62
|
"rules": {
|