@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.
Files changed (2) hide show
  1. package/bin/install.js +28 -12
  2. 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 file = fs.createWriteStream(destFile);
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
- console.error(`Failed to download ${url}: HTTP ${response.statusCode}`);
63
- process.exit(1);
69
+ response.resume();
70
+ fail(new Error(`HTTP ${response.statusCode}`));
71
+ return;
64
72
  }
65
- response.pipe(file);
66
- file.on('finish', () => {
67
- file.close();
68
- fs.chmodSync(destFile, 0o755);
69
- console.log(`Binary saved to ${destFile}`);
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', (err) => {
72
- console.error(err);
73
- process.exit(1);
74
- });
90
+ }).on('error', fail);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghyper9023/u2secure",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Make your Linux system more secure through interactive CLI | 通过交互式的 CLI 让你的 Linux 系统更安全",
5
5
  "bin": {
6
6
  "u2secure": "bin/u2secure-wrapper.js"