@xhmikosr/downloader 12.0.0 → 13.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.
Files changed (2) hide show
  1. package/index.js +17 -27
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import {promises as fs} from 'node:fs';
1
+ import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import process from 'node:process';
4
4
  import contentDisposition from 'content-disposition';
@@ -13,10 +13,8 @@ import got from 'got';
13
13
  import mergeOptions from 'merge-options';
14
14
  import {pEvent} from 'p-event';
15
15
 
16
- const filenameFromPath = res => path.basename(new URL(res.requestUrl).pathname);
17
-
18
- const getExtFromMime = res => {
19
- const header = res.headers['content-type'];
16
+ const getExtFromMime = response => {
17
+ const header = response.headers['content-type'];
20
18
 
21
19
  if (!header) {
22
20
  return null;
@@ -24,15 +22,11 @@ const getExtFromMime = res => {
24
22
 
25
23
  const exts = extName.mime(header);
26
24
 
27
- if (exts.length !== 1) {
28
- return null;
29
- }
30
-
31
- return exts[0].ext;
25
+ return exts.length === 1 ? exts[0].ext : null;
32
26
  };
33
27
 
34
- const getFilename = async (res, data) => {
35
- const header = res.headers['content-disposition'];
28
+ const getFilename = async (response, data) => {
29
+ const header = response.headers['content-disposition'];
36
30
 
37
31
  if (header) {
38
32
  const parsed = contentDisposition.parse(header);
@@ -42,11 +36,11 @@ const getFilename = async (res, data) => {
42
36
  }
43
37
  }
44
38
 
45
- let filename = filenameFromPath(res);
39
+ let filename = path.basename(new URL(response.requestUrl).pathname);
46
40
 
47
41
  if (!path.extname(filename)) {
48
42
  const fileType = await fileTypeFromBuffer(data);
49
- const ext = fileType?.ext || getExtFromMime(res);
43
+ const ext = fileType?.ext || getExtFromMime(response);
50
44
 
51
45
  if (ext) {
52
46
  filename = `${filename}.${ext}`;
@@ -77,26 +71,22 @@ const download = (uri, output, options) => {
77
71
  const stream = got.stream(uri, options.got);
78
72
 
79
73
  const promise = pEvent(stream, 'response')
80
- .then(res => {
74
+ .then(response => {
81
75
  const encoding = options.got.responseType === 'buffer' ? 'buffer' : options.got.encoding;
82
- return Promise.all([getStream(stream, {encoding}), res]);
76
+ return Promise.all([getStream(stream, {encoding}), response]);
83
77
  })
84
- .then(async ([data, res]) => {
78
+ .then(async ([data, response]) => {
79
+ const hasArchiveData = options.extract && await archiveType(data);
80
+
85
81
  if (!output) {
86
- return options.extract && await archiveType(data)
87
- ? decompress(data, options.decompress)
88
- : data;
82
+ return hasArchiveData ? decompress(data, options.decompress) : data;
89
83
  }
90
84
 
91
- const filename = options.filename || filenamify(await getFilename(res, data));
85
+ const filename = options.filename || filenamify(await getFilename(response, data));
92
86
  const outputFilepath = path.join(output, filename);
93
87
 
94
- if (options.extract && await archiveType(data)) {
95
- return decompress(
96
- data,
97
- path.dirname(outputFilepath),
98
- options.decompress,
99
- );
88
+ if (hasArchiveData) {
89
+ return decompress(data, path.dirname(outputFilepath), options.decompress);
100
90
  }
101
91
 
102
92
  return fs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xhmikosr/downloader",
3
- "version": "12.0.0",
3
+ "version": "13.0.0",
4
4
  "description": "Download and extract files",
5
5
  "license": "MIT",
6
6
  "repository": "XhmikosR/download",
@@ -40,7 +40,7 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@xhmikosr/archive-type": "^6.0.1",
43
- "@xhmikosr/decompress": "^8.0.0",
43
+ "@xhmikosr/decompress": "^9.0.0",
44
44
  "content-disposition": "^0.5.4",
45
45
  "ext-name": "^5.0.0",
46
46
  "file-type": "^18.5.0",
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "devDependencies": {
54
54
  "ava": "^4.3.3",
55
- "c8": "^7.14.0",
55
+ "c8": "^8.0.0",
56
56
  "is-zip": "^1.0.0",
57
57
  "nock": "^13.3.1",
58
58
  "path-exists": "^5.0.0",