@nwutils/getter 0.1.5 → 0.2.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/package.json +11 -5
- package/src/main.js +21 -6
- package/src/nw.js +2 -0
- package/src/request.js +71 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nwutils/getter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Get NW.js and related binaries for Linux, MacOS and Windows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"NW.js",
|
|
@@ -37,16 +37,22 @@
|
|
|
37
37
|
"url": "git+https://github.com/nwutils/getter.git"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
+
"lint": "eslint .",
|
|
40
41
|
"test": "node --test --experimental-test-coverage --test-coverage-exclude=\"tests/**/*\" \"tests/specs/*.test.js\""
|
|
41
42
|
},
|
|
42
43
|
"volta": {
|
|
43
|
-
"node": "
|
|
44
|
-
"npm": "11.
|
|
44
|
+
"node": "25.1.0",
|
|
45
|
+
"npm": "11.7.0"
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"axios": "^1.13.2",
|
|
48
48
|
"semver": "^7.7.3",
|
|
49
|
-
"tar": "^7.5.
|
|
49
|
+
"tar": "^7.5.3",
|
|
50
50
|
"yauzl-promise": "^4.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@eslint/js": "^9.39.2",
|
|
54
|
+
"eslint": "^9.39.2",
|
|
55
|
+
"eslint-plugin-jsdoc": "^62.1.0",
|
|
56
|
+
"globals": "^17.0.0"
|
|
51
57
|
}
|
|
52
58
|
}
|
package/src/main.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import process from "node:process";
|
|
4
3
|
import url from "node:url";
|
|
5
4
|
|
|
6
5
|
import semver from "semver";
|
|
@@ -17,9 +16,9 @@ import verify from "./verify.js";
|
|
|
17
16
|
* @property {string | "latest" | "stable" | "lts"} version Runtime version
|
|
18
17
|
* @property {"normal" | "sdk"} flavor Build flavor
|
|
19
18
|
* @property {"linux" | "osx" | "win"} platform Target platform
|
|
20
|
-
* @property {"ia32" | "x64" | "arm64"} arch Target
|
|
21
|
-
* @property {"https://dl.nwjs.io"} downloadUrl Download server
|
|
22
|
-
* @property {"https://nwjs.io/versions.json"} manifestUrl Manifest
|
|
19
|
+
* @property {"ia32" | "x64" | "arm64"} arch Target architecture
|
|
20
|
+
* @property {"https://dl.nwjs.io"} downloadUrl Download server, accepts http and https
|
|
21
|
+
* @property {"https://nwjs.io/versions.json"} manifestUrl Manifest URI, accepts file, http and https
|
|
23
22
|
* @property {string} cacheDir Cache directory
|
|
24
23
|
* @property {boolean} cache If false, remove cache and redownload.
|
|
25
24
|
* @property {boolean} ffmpeg If true, ffmpeg is not downloaded.
|
|
@@ -42,10 +41,18 @@ async function get(options) {
|
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
const manifestFilePath = path.resolve(options.cacheDir, "manifest.json");
|
|
45
|
-
|
|
44
|
+
if (options.manifestUrl.startsWith('file://')) {
|
|
45
|
+
const filePath = url.fileURLToPath(options.manifestUrl);
|
|
46
|
+
fs.writeFileSync(manifestFilePath, fs.readFileSync(filePath, 'utf-8'));
|
|
47
|
+
} else {
|
|
48
|
+
await request(options.manifestUrl, manifestFilePath);
|
|
49
|
+
}
|
|
46
50
|
const manifestData = JSON.parse(await fs.promises.readFile(manifestFilePath, "utf-8"));
|
|
47
51
|
|
|
48
|
-
if (options.version === "latest"
|
|
52
|
+
if (options.version === "latest"
|
|
53
|
+
|| options.version === "stable"
|
|
54
|
+
|| options.version === "lts"
|
|
55
|
+
) {
|
|
49
56
|
options.version = manifestData[options.version].slice(1);
|
|
50
57
|
} else if (semver.valid(semver.coerce(options.version))) {
|
|
51
58
|
options.version = semver.coerce(options.version).version;
|
|
@@ -73,6 +80,14 @@ async function get(options) {
|
|
|
73
80
|
throw new Error('Expected "options.manifestUrl" to be a string. Received: ' + options.manifestUrl);
|
|
74
81
|
}
|
|
75
82
|
|
|
83
|
+
if (typeof options.manifestUrl == "string"
|
|
84
|
+
&& options.manifestUrl.startsWith('file://') === false
|
|
85
|
+
&& options.manifestUrl.startsWith('http://') === false
|
|
86
|
+
&& options.manifestUrl.startsWith('https://') === false
|
|
87
|
+
) {
|
|
88
|
+
throw new Error('Expected "options.manifestUrl" to be a string starting with "file://", "http://", or "https://". Received: ' + options.manifestUrl);
|
|
89
|
+
}
|
|
90
|
+
|
|
76
91
|
if (typeof options.cacheDir !== "string") {
|
|
77
92
|
throw new Error('Expected "options.cacheDir" to be a string. Received: ' + options.cacheDir);
|
|
78
93
|
}
|
package/src/nw.js
CHANGED
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
|
|
4
|
-
|
|
5
|
-
import
|
|
3
|
+
import http from 'node:http';
|
|
4
|
+
import https from 'node:https';
|
|
5
|
+
import { URL } from 'node:url';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Download from `url` and save at `filePath`.
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
11
|
-
* @param {string} url - Download server
|
|
12
|
-
* @param {string} filePath - file path of downloaded content
|
|
9
|
+
* @param {string} url
|
|
10
|
+
* @param {string} filePath
|
|
13
11
|
* @returns {Promise<void>}
|
|
14
12
|
*/
|
|
15
|
-
export default
|
|
13
|
+
export default function request(url, filePath) {
|
|
14
|
+
const parsedUrl = new URL(url);
|
|
15
|
+
const client = parsedUrl.protocol === 'https:' ? https : http;
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const writeStream = fs.createWriteStream(filePath);
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
process.exit();
|
|
26
|
-
});
|
|
20
|
+
/* Handle writeStream errors immediately */
|
|
21
|
+
writeStream.on('error', (err) => {
|
|
22
|
+
cleanup();
|
|
23
|
+
reject(err);
|
|
24
|
+
});
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
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
|
}
|