@ghyper9023/u2secure 0.4.1 → 0.5.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/bin/install.js +28 -12
- package/package.json +1 -1
package/bin/install.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const os = require('os');
|
|
6
|
+
const { pipeline } = require('stream');
|
|
6
7
|
const { https } = require('follow-redirects');
|
|
7
8
|
|
|
8
9
|
const pkgJson = require('../package.json');
|
|
@@ -56,19 +57,34 @@ console.log(`Downloading from ${url} ...`);
|
|
|
56
57
|
|
|
57
58
|
if (!fs.existsSync(destDir)) fs.mkdirSync(destDir, { recursive: true });
|
|
58
59
|
|
|
59
|
-
const
|
|
60
|
+
const staging = fs.mkdtempSync(path.join(destDir, '.download-'));
|
|
61
|
+
const temporary = path.join(staging, 'binary');
|
|
62
|
+
function fail(error) {
|
|
63
|
+
console.error(`Download failed: ${error.message}`);
|
|
64
|
+
process.exitCode = 1;
|
|
65
|
+
fs.rmSync(staging, { recursive: true, force: true });
|
|
66
|
+
}
|
|
60
67
|
https.get(url, (response) => {
|
|
61
68
|
if (response.statusCode !== 200) {
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
response.resume();
|
|
70
|
+
fail(new Error(`HTTP ${response.statusCode}`));
|
|
71
|
+
return;
|
|
64
72
|
}
|
|
65
|
-
response.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
73
|
+
pipeline(response, fs.createWriteStream(temporary, { flags: 'wx' }), (error) => {
|
|
74
|
+
if (error) return fail(error);
|
|
75
|
+
try {
|
|
76
|
+
const expected = response.headers['content-length'];
|
|
77
|
+
const bytes = fs.statSync(temporary).size;
|
|
78
|
+
if (!response.complete || bytes === 0 ||
|
|
79
|
+
(expected !== undefined && Number(expected) !== bytes)) {
|
|
80
|
+
throw new Error('Incomplete binary response');
|
|
81
|
+
}
|
|
82
|
+
fs.chmodSync(temporary, 0o755);
|
|
83
|
+
fs.renameSync(temporary, destFile);
|
|
84
|
+
fs.rmdirSync(staging);
|
|
85
|
+
console.log(`Binary saved to ${destFile}`);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
fail(error);
|
|
88
|
+
}
|
|
70
89
|
});
|
|
71
|
-
}).on('error',
|
|
72
|
-
console.error(err);
|
|
73
|
-
process.exit(1);
|
|
74
|
-
});
|
|
90
|
+
}).on('error', fail);
|