@nwutils/getter 0.1.5 → 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/package.json +1 -2
- 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.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Get NW.js and related binaries for Linux, MacOS and Windows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"NW.js",
|
|
@@ -44,7 +44,6 @@
|
|
|
44
44
|
"npm": "11.6.2"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"axios": "^1.13.2",
|
|
48
47
|
"semver": "^7.7.3",
|
|
49
48
|
"tar": "^7.5.2",
|
|
50
49
|
"yauzl-promise": "^4.0.0"
|
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
|
}
|