@xhmikosr/downloader 16.2.0 → 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.
Files changed (3) hide show
  1. package/index.js +28 -0
  2. package/package.json +9 -5
  3. package/readme.md +13 -1
package/index.js CHANGED
@@ -1,3 +1,4 @@
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';
@@ -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
 
@@ -94,6 +120,8 @@ const download = (uri, output, options = {}) => {
94
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.2.0",
3
+ "version": "16.3.0",
4
4
  "description": "Download and extract files",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -42,25 +42,29 @@
42
42
  "url"
43
43
  ],
44
44
  "dependencies": {
45
- "@xhmikosr/archive-type": "^8.0.1",
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.1",
50
+ "filenamify": "^7.0.2",
51
51
  "got": "^14.6.6"
52
52
  },
53
53
  "devDependencies": {
54
- "@xhmikosr/decompress-unzip": "^8.1.1",
54
+ "@xhmikosr/decompress-unzip": "^8.2.1",
55
55
  "ava": "^7.0.0",
56
56
  "c8": "^11.0.0",
57
- "nock": "^14.0.15",
57
+ "nock": "^14.0.16",
58
58
  "xo": "^2.0.2"
59
59
  },
60
60
  "xo": {
61
61
  "rules": {
62
62
  "promise/prefer-await-to-then": "off",
63
+ "require-unicode-regexp": "off",
63
64
  "unicorn/prevent-abbreviations": "off"
64
65
  }
66
+ },
67
+ "allowScripts": {
68
+ "unrs-resolver@1.11.1": true
65
69
  }
66
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
+ ```