@nwutils/getter 0.1.0 → 0.1.6

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # @nwutils/getter
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/@nwutils/getter/latest)](https://www.npmjs.com/package/@nwutils/getter/v/latest)
4
+
3
5
  Download NW.js and related binaries for Linux, MacOS and Windows.
4
6
 
5
7
  ## Getting Started
@@ -13,7 +15,17 @@ Download NW.js and related binaries for Linux, MacOS and Windows.
13
15
  import get from "@nwutils/getter";
14
16
 
15
17
  await get({
16
- ...
18
+ version: "latest",
19
+ flavor: "normal",
20
+ platform: "linux",
21
+ arch: "x64",
22
+ downloadUrl: "https://dl.nwjs.io",
23
+ manifestUrl: "https://nwjs.io/versions.json",
24
+ cacheDir: "./cache",
25
+ cache: true,
26
+ ffmpeg: false,
27
+ nativeAddon: false,
28
+ shaSum: true,
17
29
  });
18
30
  ```
19
31
 
@@ -21,20 +33,19 @@ await get({
21
33
 
22
34
  Options
23
35
 
24
- | Name | Type | Default | Description |
25
- | ---- | ------- | --------- | ----------- |
26
- | mode | `"get" \| "run" \| "build"` | `"build"` | Choose between get, run or build mode |
27
- | version | `string \| "latest" \| "stable"` | `"latest"` | Runtime version |
28
- | flavor | `"normal" \| "sdk"` | `"normal"` | Runtime flavor |
29
- | platform | `"linux" \| "osx" \| "win"` | | Host platform |
30
- | arch | `"ia32" \| "x64" \| "arm64"` | | Host architecture |
31
- | downloadUrl | `"https://dl.nwjs.io" \| "https://npm.taobao.org/mirrors/nwjs" \| https://npmmirror.com/mirrors/nwjs \| "https://github.com/corwin-of-amber/nw.js/releases/"` | `"https://dl.nwjs.io"` | Download server. Supports file systems too (for example `file:///home/localghost/nwjs_mirror`) |
32
- | manifestUrl | `"https://nwjs.io/versions.json" \| "https://raw.githubusercontent.com/nwutils/nw-builder/main/src/util/osx.arm.versions.json"` | `"https://nwjs.io/versions.json"` | Versions manifest |
33
- | cacheDir | `string` | `"./cache"` | Directory to cache NW binaries |
34
- | cache | `boolean` | `true`| If true the existing cache is used. Otherwise it removes and redownloads it. |
35
- | ffmpeg | `boolean` | `false`| If true the chromium ffmpeg is replaced by community version with proprietary codecs. |
36
- | logLevel | `"error" \| "warn" \| "info" \| "debug"` | `"info"`| Specify level of logging. |
37
- | shaSum | `boolean` | `true` | Flag to enable/disable shasum checks. |
36
+ | Name | Type | Description |
37
+ | ---- | ------- | ----------- |
38
+ | version | `string \| "latest" \| "stable"` | Runtime version |
39
+ | flavor | `"normal" \| "sdk"` | Runtime flavor |
40
+ | platform | `"linux" \| "osx" \| "win"` | Host platform |
41
+ | arch | `"ia32" \| "x64" \| "arm64"` | Host architecture |
42
+ | downloadUrl | `"https://dl.nwjs.io" \| "https://npm.taobao.org/mirrors/nwjs" \| https://npmmirror.com/mirrors/nwjs \| "https://github.com/corwin-of-amber/nw.js/releases/"` | Download server. Supports file systems too (for example `file:///home/user/nwjs_mirror`) |
43
+ | manifestUrl | `"https://nwjs.io/versions.json" \| "https://raw.githubusercontent.com/nwutils/nw-builder/main/src/util/osx.arm.versions.json"` | Versions manifest |
44
+ | cacheDir | `string` | Directory to cache NW binaries |
45
+ | cache | `boolean` | If true the existing cache is used. Otherwise it removes and redownloads it. |
46
+ | ffmpeg | `boolean` | If true the chromium ffmpeg is replaced by community version with proprietary codecs. |
47
+ | nativeAddon | `boolean` | If true download NW.js Node headers. |
48
+ | shaSum | `boolean` | Flag to enable/disable shasum checks. |
38
49
 
39
50
  ## Contributing
40
51
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nwutils/getter",
3
- "version": "0.1.0",
4
- "description": "Get NW.js desktop applications for MacOS, Windows and Linux.",
3
+ "version": "0.1.6",
4
+ "description": "Get NW.js and related binaries for Linux, MacOS and Windows.",
5
5
  "keywords": [
6
6
  "NW.js",
7
7
  "node-webkit",
@@ -15,7 +15,7 @@
15
15
  "maintainers": [
16
16
  {
17
17
  "name": "Ayushman Chhabra",
18
- "url": "https://ayushmanchhabra.com/"
18
+ "url": "https://ayushmanchhabra.com"
19
19
  }
20
20
  ],
21
21
  "contributors": [
@@ -34,7 +34,7 @@
34
34
  "homepage": "https://github.com/nwutils/getter",
35
35
  "repository": {
36
36
  "type": "git",
37
- "url": "https://github.com/nwutils/getter.git"
37
+ "url": "git+https://github.com/nwutils/getter.git"
38
38
  },
39
39
  "scripts": {
40
40
  "test": "node --test --experimental-test-coverage --test-coverage-exclude=\"tests/**/*\" \"tests/specs/*.test.js\""
@@ -44,7 +44,7 @@
44
44
  "npm": "11.6.2"
45
45
  },
46
46
  "dependencies": {
47
- "axios": "^1.13.2",
47
+ "semver": "^7.7.3",
48
48
  "tar": "^7.5.2",
49
49
  "yauzl-promise": "^4.0.0"
50
50
  }
package/src/decompress.js CHANGED
@@ -8,23 +8,28 @@ import yauzl from 'yauzl-promise';
8
8
 
9
9
  /**
10
10
  * Decompresses a file at `filePath` to `cacheDir` directory.
11
+ * @async
12
+ * @function
11
13
  * @param {string} filePath - file path to compressed binary
12
14
  * @param {string} cacheDir - directory to decompress into
15
+ * @throws {Error}
16
+ * @returns {Promise<void>}
13
17
  */
14
18
  export default async function decompress(filePath, cacheDir) {
15
19
  if (filePath.endsWith('.zip')) {
16
20
  await unzip(filePath, cacheDir);
17
21
  } else {
18
- tar.extract({
22
+ await tar.extract({
19
23
  file: filePath,
20
- C: cacheDir,
21
- sync: true,
24
+ C: cacheDir
22
25
  });
23
26
  }
24
27
  }
25
28
 
26
29
  /**
27
30
  * Get file mode from entry. Reference implementation is [here](https://github.com/fpsqdb/zip-lib/blob/ac447d269218d396e05cd7072d0e9cd82b5ec52c/src/unzip.ts#L380).
31
+ * @async
32
+ * @function
28
33
  * @param {yauzl.Entry} entry - Yauzl entry
29
34
  * @returns {number} - entry's file mode
30
35
  */
@@ -42,6 +47,7 @@ function modeFromEntry(entry) {
42
47
  * @function
43
48
  * @param {string} zippedFile - file path to .zip file
44
49
  * @param {string} cacheDir - directory to unzip in
50
+ * @throws {Error}
45
51
  * @returns {Promise<void>}
46
52
  */
47
53
  async function unzip(zippedFile, cacheDir) {
package/src/ffmpeg.js CHANGED
@@ -4,15 +4,17 @@ import request from './request.js';
4
4
 
5
5
  /**
6
6
  * Download community FFmpeg binary from `https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt`.
7
+ * @async
8
+ * @function
7
9
  * @param {string} downloadUrl - Download server
8
10
  * @param {string} version - Runtime version
9
11
  * @param {string} platform - NW supported platform
10
12
  * @param {string} arch - NW supported architecture
11
13
  * @param {string} cacheDir - Directory to store FFmpeg binary
12
14
  * @throws {Error} - When download fails
13
- * @returns {string} - Path of compressed file which containscommunity FFmpeg binary.
15
+ * @returns {Promise<string>} - Path of compressed file which containscommunity FFmpeg binary.
14
16
  */
15
- export default function ffmpeg(downloadUrl, version, platform, arch, cacheDir) {
17
+ export default async function ffmpeg(downloadUrl, version, platform, arch, cacheDir) {
16
18
 
17
19
  /**
18
20
  * URL to download specific FFmpeg binary from.
@@ -32,11 +34,5 @@ export default function ffmpeg(downloadUrl, version, platform, arch, cacheDir) {
32
34
  `ffmpeg-${version}-${platform}-${arch}.zip`,
33
35
  );
34
36
 
35
- request(url, ffmpegFileAbs, function (error) {
36
- if (error) {
37
- throw error;
38
- } else {
39
- return ffmpegFileAbs;
40
- }
41
- });
37
+ await request(url, ffmpegFileAbs);
42
38
  }
package/src/main.js CHANGED
@@ -1,51 +1,103 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import url from 'node:url';
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+ import url from "node:url";
4
5
 
5
- import decompress from './decompress.js';
6
- import ffmpeg from './ffmpeg.js';
7
- import node from './node.js';
8
- import nw from './nw.js';
9
- import verify from './verify.js';
6
+ import semver from "semver";
7
+
8
+ import decompress from "./decompress.js";
9
+ import ffmpeg from "./ffmpeg.js";
10
+ import node from "./node.js";
11
+ import nw from "./nw.js";
12
+ import request from "./request.js";
13
+ import verify from "./verify.js";
10
14
 
11
15
  /**
12
- * @typedef {object} GetOptions
13
- * @property {string | "latest" | "stable" | "lts"} [version = "latest"] Runtime version
14
- * @property {"normal" | "sdk"} [flavor = "normal"] Build flavor
15
- * @property {"linux" | "osx" | "win"} [platform] Target platform
16
- * @property {"ia32" | "x64" | "arm64"} [arch] Target arch
17
- * @property {string} [downloadUrl = "https://dl.nwjs.io"] Download server
18
- * @property {string} [cacheDir = "./cache"] Cache directory
19
- * @property {boolean} [cache = true] If false, remove cache and redownload.
20
- * @property {boolean} [ffmpeg = false] If true, ffmpeg is not downloaded.
21
- * @property {false | "gyp"} [nativeAddon = false] Rebuild native modules
22
- * @property {string} [logLevel = 'info'] User defined log level.
23
- * @property {boolean} [shaSum = true] If true shasum is enabled, otherwise disabled.
16
+ * @typedef {object} Options
17
+ * @property {string | "latest" | "stable" | "lts"} version Runtime version
18
+ * @property {"normal" | "sdk"} flavor Build flavor
19
+ * @property {"linux" | "osx" | "win"} platform Target platform
20
+ * @property {"ia32" | "x64" | "arm64"} arch Target arch
21
+ * @property {"https://dl.nwjs.io"} downloadUrl Download server
22
+ * @property {"https://nwjs.io/versions.json"} manifestUrl Manifest URL
23
+ * @property {string} cacheDir Cache directory
24
+ * @property {boolean} cache If false, remove cache and redownload.
25
+ * @property {boolean} ffmpeg If true, ffmpeg is not downloaded.
26
+ * @property {boolean} nativeAddon If true, download node headers.
27
+ * @property {boolean} shaSum If true shasum is enabled, otherwise disabled.
24
28
  */
25
29
 
26
30
  /**
27
- * Get binaries.
31
+ * Get NW.js and related binaries for Linux, MacOS and Windows.
28
32
  * @async
29
33
  * @function
30
- * @param {GetOptions} options Get mode options
34
+ * @param {Options} options Get mode options
31
35
  * @returns {Promise<void>}
32
36
  */
33
37
  async function get(options) {
34
38
 
35
- const uri = new url.URL(options.downloadUrl);
39
+ /* If `options.cacheDir` exists, then `true`. Otherwise, it is `false`. */
40
+ if (fs.existsSync(options.cacheDir) === false) {
41
+ await fs.promises.mkdir(options.cacheDir, { recursive: true });
42
+ }
36
43
 
37
- /* Download server is the cached directory. */
38
- if (uri.protocol === 'file:') {
39
- options.cacheDir = path.resolve(decodeURIComponent(options.downloadUrl.slice('file://'.length)));
44
+ const manifestFilePath = path.resolve(options.cacheDir, "manifest.json");
45
+ await request(options.manifestUrl, manifestFilePath);
46
+ const manifestData = JSON.parse(await fs.promises.readFile(manifestFilePath, "utf-8"));
47
+
48
+ if (options.version === "latest" | options.version === "stable" | options.version === "lts") {
49
+ options.version = manifestData[options.version].slice(1);
50
+ } else if (semver.valid(semver.coerce(options.version))) {
51
+ options.version = semver.coerce(options.version).version;
52
+ } else {
53
+ throw new Error('Expected "options.version" to be "latest", "stable", "lts" or a valid semver version. Received: ' + options.version);
40
54
  }
41
55
 
42
- /**
43
- * If `options.cacheDir` exists, then `true`. Otherwise, it is `false`.
44
- * @type {boolean}
45
- */
46
- const cacheDirExists = fs.existsSync(options.cacheDir);
47
- if (cacheDirExists === false) {
48
- await fs.promises.mkdir(options.cacheDir, { recursive: true });
56
+ if (options.flavor !== "normal" && options.flavor !== "sdk") {
57
+ throw new Error('Expected "options.flavor" to be "normal" or "sdk". Received: ' + options.flavor);
58
+ }
59
+
60
+ if (options.platform !== "linux" && options.platform !== "osx" && options.platform !== "win") {
61
+ throw new Error('Expected "options.platform" to be "linux", "osx" or "win". Received: ' + options.platform);
62
+ }
63
+
64
+ if (options.arch !== "ia32" && options.arch !== "x64" && options.arch !== "arm64") {
65
+ throw new Error('Expected "options.arch" to be "ia32", "x64" or "arm64". Received: ' + options.arch);
66
+ }
67
+
68
+ if (typeof options.downloadUrl !== "string") {
69
+ throw new Error('Expected "options.downloadUrl" to be a string. Received: ' + options.downloadUrl);
70
+ }
71
+
72
+ if (typeof options.manifestUrl !== "string") {
73
+ throw new Error('Expected "options.manifestUrl" to be a string. Received: ' + options.manifestUrl);
74
+ }
75
+
76
+ if (typeof options.cacheDir !== "string") {
77
+ throw new Error('Expected "options.cacheDir" to be a string. Received: ' + options.cacheDir);
78
+ }
79
+
80
+ if (typeof options.cache !== "boolean") {
81
+ throw new Error('Expected "options.cache" to be a boolean. Received: ' + options.cache);
82
+ }
83
+
84
+ if (typeof options.ffmpeg !== "boolean") {
85
+ throw new Error('Expected "options.ffmpeg" to be a boolean. Received: ' + options.ffmpeg);
86
+ }
87
+
88
+ if (typeof options.nativeAddon !== "boolean") {
89
+ throw new Error('Expected "options.nativeAddon" to be a boolean. Received: ' + options.nativeAddon);
90
+ }
91
+
92
+ if (typeof options.shaSum !== "boolean") {
93
+ throw new Error('Expected "options.shaSum" to be a boolean. Received: ' + options.shaSum);
94
+ }
95
+
96
+ const uri = new url.URL(options.downloadUrl);
97
+
98
+ /* Download server is the cached directory. */
99
+ if (uri.protocol === "file:") {
100
+ options.cacheDir = path.resolve(decodeURIComponent(options.downloadUrl.slice("file://".length)));
49
101
  }
50
102
 
51
103
  /**
@@ -54,17 +106,17 @@ async function get(options) {
54
106
  */
55
107
  let nwFilePath = path.resolve(
56
108
  options.cacheDir,
57
- `nwjs${options.flavor === 'sdk' ? '-sdk' : ''}-v${options.version}-${options.platform}-${options.arch}.${options.platform === 'linux' ? 'tar.gz' : 'zip'
109
+ `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}.${options.platform === "linux" ? "tar.gz" : "zip"
58
110
  }`,
59
111
  );
60
112
 
61
113
  /**
62
- * File path to directory which contain NW.js and related binaries.
114
+ * File path to directory which contains NW.js and related binaries.
63
115
  * @type {string}
64
116
  */
65
117
  let nwDirPath = path.resolve(
66
118
  options.cacheDir,
67
- `nwjs${options.flavor === 'sdk' ? '-sdk' : ''}-v${options.version}-${options.platform}-${options.arch}`,
119
+ `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`,
68
120
  );
69
121
 
70
122
  // If `options.cache` is false, then remove the compressed binary.
@@ -81,12 +133,8 @@ async function get(options) {
81
133
  // This is important since the community ffmpeg builds have specific licensing constraints.
82
134
  await fs.promises.rm(nwDirPath, { recursive: true, force: true });
83
135
 
84
- /**
85
- * If the compressed binary exists, then `true`. Otherwise, it is `false`.
86
- * @type {boolean}
87
- */
88
- const nwFilePathExists = fs.existsSync(nwFilePath);
89
- if (nwFilePathExists === false) {
136
+ /* If the compressed binary exists, then `true`. Otherwise, it is `false`. */
137
+ if (fs.existsSync(nwFilePath) === false) {
90
138
  nwFilePath = await nw(options.downloadUrl, options.version, options.flavor, options.platform, options.arch, options.cacheDir);
91
139
  }
92
140
 
@@ -97,7 +145,6 @@ async function get(options) {
97
145
  `${options.cacheDir}/shasum/${options.version}.txt`,
98
146
  options.cacheDir,
99
147
  options.ffmpeg,
100
- options.logLevel,
101
148
  options.shaSum,
102
149
  );
103
150
 
@@ -120,15 +167,11 @@ async function get(options) {
120
167
  });
121
168
  }
122
169
 
123
- /**
124
- * If the compressed binary exists, then `true`. Otherwise, it is `false`.
125
- * @type {boolean}
126
- */
127
- const ffmpegFilePathExists = fs.existsSync(ffmpegFilePath);
128
- if (ffmpegFilePathExists === false) {
170
+ /* If the compressed binary exists, then `true`. Otherwise, it is `false`. */
171
+ if (fs.existsSync(ffmpegFilePath) === false) {
129
172
  // Do not update the options.downloadUrl with the ffmpeg URL here. Doing so would lead to error when options.ffmpeg and options.nativeAddon are both enabled.
130
173
  const downloadUrl =
131
- 'https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download';
174
+ "https://github.com/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/download";
132
175
  ffmpegFilePath = await ffmpeg(downloadUrl, options.version, options.platform, options.arch, options.cacheDir);
133
176
  }
134
177
 
@@ -138,14 +181,14 @@ async function get(options) {
138
181
  * Platform dependant file name of FFmpeg binary.
139
182
  * @type {string}
140
183
  */
141
- let ffmpegFileName = '';
142
-
143
- if (options.platform === 'linux') {
144
- ffmpegFileName = 'libffmpeg.so';
145
- } else if (options.platform === 'win') {
146
- ffmpegFileName = 'ffmpeg.dll';
147
- } else if (options.platform === 'osx') {
148
- ffmpegFileName = 'libffmpeg.dylib';
184
+ let ffmpegFileName = "";
185
+
186
+ if (options.platform === "linux") {
187
+ ffmpegFileName = "libffmpeg.so";
188
+ } else if (options.platform === "win") {
189
+ ffmpegFileName = "ffmpeg.dll";
190
+ } else if (options.platform === "osx") {
191
+ ffmpegFileName = "libffmpeg.dylib";
149
192
  }
150
193
 
151
194
  /**
@@ -158,21 +201,21 @@ async function get(options) {
158
201
  * File path of where FFmpeg will be copied to.
159
202
  * @type {string}
160
203
  */
161
- let ffmpegBinaryDest = '';
204
+ let ffmpegBinaryDest = "";
162
205
 
163
- if (options.platform === 'linux') {
164
- ffmpegBinaryDest = path.resolve(nwDirPath, 'lib', ffmpegFileName);
165
- } else if (options.platform === 'win') {
206
+ if (options.platform === "linux") {
207
+ ffmpegBinaryDest = path.resolve(nwDirPath, "lib", ffmpegFileName);
208
+ } else if (options.platform === "win") {
166
209
  ffmpegBinaryDest = path.resolve(nwDirPath, ffmpegFileName);
167
- } else if (options.platform === 'osx') {
210
+ } else if (options.platform === "osx") {
168
211
  ffmpegBinaryDest = path.resolve(
169
212
  nwDirPath,
170
- 'nwjs.app',
171
- 'Contents',
172
- 'Frameworks',
173
- 'nwjs Framework.framework',
174
- 'Versions',
175
- 'Current',
213
+ "nwjs.app",
214
+ "Contents",
215
+ "Frameworks",
216
+ "nwjs Framework.framework",
217
+ "Versions",
218
+ "Current",
176
219
  ffmpegFileName,
177
220
  );
178
221
  }
@@ -181,10 +224,10 @@ async function get(options) {
181
224
 
182
225
  }
183
226
 
184
- if (options.nativeAddon === 'gyp') {
227
+ if (options.nativeAddon === true) {
185
228
 
186
229
  /**
187
- * File path to NW'js Node headers tarball.
230
+ * File path to NW.js Node headers tarball.
188
231
  * @type {string}
189
232
  */
190
233
  let nodeFilePath = path.resolve(
@@ -200,17 +243,12 @@ async function get(options) {
200
243
  });
201
244
  }
202
245
 
203
- /**
204
- * If the compressed binary exists, then `true`. Otherwise, it is `false`.
205
- * @type {boolean}
206
- */
207
- const nodeFilePathExists = fs.existsSync(nodeFilePath);
208
- if (nodeFilePathExists === false) {
246
+ /* If the compressed binary exists, then `true`. Otherwise, it is `false`. */
247
+ if (fs.existsSync(nodeFilePath) === false) {
209
248
  nodeFilePath = await node(options.downloadUrl, options.version, options.cacheDir);
210
249
  }
211
250
 
212
251
  await decompress(nodeFilePath, options.cacheDir);
213
-
214
252
  }
215
253
  }
216
254
 
package/src/node.js CHANGED
@@ -4,13 +4,15 @@ import request from './request.js';
4
4
 
5
5
  /**
6
6
  * Download NW.js's Node.js headers.
7
+ * @async
8
+ * @function
7
9
  * @param {string} downloadUrl - Download server
8
10
  * @param {string} version - Runtime version
9
11
  * @param {string} cacheDir - Directory to store NW binaries
10
12
  * @throws {Error} - When download fails
11
- * @returns {string} - path of compressed file which contains the Node headers.
13
+ * @returns {Promise<string>} - path of compressed file which contains the Node headers.
12
14
  */
13
- export default function nw(downloadUrl, version, cacheDir) {
15
+ export default async function nw(downloadUrl, version, cacheDir) {
14
16
 
15
17
  /**
16
18
  * Name of directory which contains Node headers.
@@ -42,11 +44,5 @@ export default function nw(downloadUrl, version, cacheDir) {
42
44
  nwFile
43
45
  );
44
46
 
45
- request(url, nwFileAbs, function (error) {
46
- if (error) {
47
- throw error;
48
- } else {
49
- return nwFileAbs;
50
- }
51
- });
47
+ await request(url, nwFileAbs);
52
48
  }
package/src/nw.js CHANGED
@@ -5,6 +5,8 @@ import request from './request.js';
5
5
 
6
6
  /**
7
7
  * Download NW.js binary.
8
+ * @async
9
+ * @function
8
10
  * @param {string} downloadUrl - Download server
9
11
  * @param {string} version - Runtime version
10
12
  * @param {string} flavor - Runtime build flavor
@@ -12,9 +14,9 @@ import request from './request.js';
12
14
  * @param {string} arch - NW supported architecture
13
15
  * @param {string} cacheDir - Directory to store NW binaries
14
16
  * @throws {Error} - When download fails
15
- * @returns {string} - Path of compressed file which contains NW.js binaries.
17
+ * @returns {Promise<string>} - Path of compressed file which contains NW.js binaries.
16
18
  */
17
- export default function nw(downloadUrl, version, flavor, platform, arch, cacheDir) {
19
+ export default async function nw(downloadUrl, version, flavor, platform, arch, cacheDir) {
18
20
 
19
21
  /**
20
22
  * Name of directory which contains NW.js binaries.
@@ -53,11 +55,7 @@ export default function nw(downloadUrl, version, flavor, platform, arch, cacheDi
53
55
  nwFile
54
56
  );
55
57
 
56
- request(url, nwFileAbs, function (error) {
57
- if (error) {
58
- throw error;
59
- } else {
60
- return nwFileAbs;
61
- }
62
- });
58
+ await request(url, nwFileAbs);
59
+
60
+ return nwFileAbs;
63
61
  }
package/src/request.js CHANGED
@@ -1,36 +1,84 @@
1
1
  import fs from 'node:fs';
2
2
  import process from 'node:process';
3
- import stream from 'node:stream';
4
-
5
- import axios from 'axios';
3
+ import http from 'node:http';
4
+ import https from 'node:https';
5
+ import { URL } from 'node:url';
6
6
 
7
7
  /**
8
- * Download from `url`.
9
- * @async
10
- * @function
11
- * @param {string} url - Download server
12
- * @param {string} filePath - file path of downloaded content
8
+ * Download from `url` and save at `filePath`.
9
+ * @param {string} url
10
+ * @param {string} filePath
13
11
  * @returns {Promise<void>}
14
12
  */
15
- export default async function request(url, filePath) {
13
+ export default function request(url, filePath) {
14
+ const parsedUrl = new URL(url);
15
+ const client = parsedUrl.protocol === 'https:' ? https : http;
16
16
 
17
- const writeStream = fs.createWriteStream(filePath);
17
+ return new Promise((resolve, reject) => {
18
+ const writeStream = fs.createWriteStream(filePath);
18
19
 
19
- /* Listen for SIGINT (Ctrl+C) */
20
- process.on('SIGINT', function () {
21
- /* Delete file if it exists. This prevents unnecessary `Central Directory not found` errors. */
22
- if (fs.existsSync(filePath)) {
23
- fs.unlinkSync(filePath);
24
- }
25
- process.exit();
26
- });
20
+ /* Handle writeStream errors immediately */
21
+ writeStream.on('error', (err) => {
22
+ cleanup();
23
+ reject(err);
24
+ });
27
25
 
28
- const response = await axios({
29
- method: 'get',
30
- url: url,
31
- responseType: 'stream'
32
- });
26
+ /* Ctrl+C cleanup */
27
+ const onSigInt = () => {
28
+ writeStream.destroy();
29
+ if (fs.existsSync(filePath)) {
30
+ fs.unlinkSync(filePath);
31
+ }
32
+ process.exit();
33
+ };
34
+ process.once('SIGINT', onSigInt);
35
+
36
+ const req = client.get(
37
+ {
38
+ hostname: parsedUrl.hostname,
39
+ port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
40
+ path: parsedUrl.pathname + parsedUrl.search,
41
+ headers: {
42
+ 'User-Agent': 'node:http(s)'
43
+ }
44
+ },
45
+ (res) => {
46
+ /* Redirect handling */
47
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
48
+ cleanup();
49
+
50
+ const redirectedUrl = new URL(res.headers.location, parsedUrl).toString();
51
+ return resolve(request(redirectedUrl, filePath));
52
+ }
33
53
 
34
- await stream.promises.pipeline(response.data, writeStream);
54
+ if (res.statusCode !== 200) {
55
+ cleanup();
56
+ return reject(new Error(`Request failed. Status code: ${res.statusCode}`));
57
+ }
35
58
 
59
+ res.pipe(writeStream);
60
+
61
+ writeStream.on('finish', () => {
62
+ cleanup();
63
+ resolve();
64
+ });
65
+
66
+ res.on('error', (err) => {
67
+ cleanup();
68
+ reject(err);
69
+ });
70
+ }
71
+ );
72
+
73
+ req.on('error', (err) => {
74
+ cleanup();
75
+ reject(err);
76
+ });
77
+
78
+ function cleanup() {
79
+ process.removeListener('SIGINT', onSigInt);
80
+ req.destroy();
81
+ writeStream.destroy();
82
+ }
83
+ });
36
84
  }
package/src/verify.js CHANGED
@@ -6,16 +6,17 @@ import request from './request.js';
6
6
 
7
7
  /**
8
8
  * Verify the SHA256 checksum of downloaded artifacts.
9
+ * @async
10
+ * @function
9
11
  * @param {string} shaUrl - URL to get the shasum text file from.
10
12
  * @param {string} shaOut - File path to shasum text file.
11
13
  * @param {string} cacheDir - File path to cache directory.
12
14
  * @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
13
- * @param {string} logLevel - User defined log level.
14
15
  * @param {boolean} shaSum - Throws error if true, otherwise logs a warning.
15
16
  * @throws {Error}
16
17
  * @returns {Promise<boolean>} - Returns true if the checksums match.
17
18
  */
18
- export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg, logLevel, shaSum) {
19
+ export default async function verify(shaUrl, shaOut, cacheDir, ffmpeg, shaSum) {
19
20
  const shaOutExists = fs.existsSync(shaOut);
20
21
 
21
22
  if (shaOutExists === false) {