@atlas-coder/atlas-agent 0.1.2 → 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 +1 -1
- package/scripts/postinstall.js +39 -31
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Postinstall: download the platform-specific binary from GitHub Releases.
|
|
3
|
+
// Errors are printed to stderr and exit 1 so npm install surfaces them.
|
|
3
4
|
const fs = require('fs');
|
|
4
5
|
const path = require('path');
|
|
5
6
|
const https = require('https');
|
|
@@ -7,7 +8,7 @@ const { URL } = require('url');
|
|
|
7
8
|
|
|
8
9
|
const PLATFORM_MAP = { win32: 'windows', darwin: 'darwin', linux: 'linux' };
|
|
9
10
|
const platform = PLATFORM_MAP[process.platform] || process.platform;
|
|
10
|
-
const arch = process.arch;
|
|
11
|
+
const arch = process.arch;
|
|
11
12
|
const ext = process.platform === 'win32' ? '.exe' : '';
|
|
12
13
|
const assetName = `atlas-agent-${platform}-${arch}${ext}`;
|
|
13
14
|
|
|
@@ -19,44 +20,51 @@ const binDir = path.join(__dirname, '..', 'bin');
|
|
|
19
20
|
fs.mkdirSync(binDir, { recursive: true });
|
|
20
21
|
const dest = path.join(binDir, assetName);
|
|
21
22
|
|
|
22
|
-
function
|
|
23
|
+
function download(url, redirectsLeft) {
|
|
24
|
+
if (redirectsLeft == null) redirectsLeft = 5;
|
|
23
25
|
return new Promise((resolve, reject) => {
|
|
24
|
-
https.get(url, (res) => {
|
|
26
|
+
const req = https.get(url, { headers: { 'User-Agent': 'atlas-agent-installer' } }, (res) => {
|
|
27
|
+
// Follow redirects
|
|
25
28
|
if ([301, 302, 303, 307, 308].includes(res.statusCode)) {
|
|
26
|
-
if (redirectsLeft <= 0) return reject(new Error('
|
|
27
|
-
|
|
29
|
+
if (redirectsLeft <= 0) return reject(new Error('Too many redirects for ' + url));
|
|
30
|
+
const next = res.headers.location;
|
|
31
|
+
if (!next) return reject(new Error('Redirect with no Location header'));
|
|
32
|
+
res.resume();
|
|
33
|
+
return resolve(download(new URL(next, url).toString(), redirectsLeft - 1));
|
|
28
34
|
}
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
if (res.statusCode !== 200) {
|
|
36
|
+
return reject(new Error('HTTP ' + res.statusCode + ' for ' + url));
|
|
37
|
+
}
|
|
38
|
+
const tmp = dest + '.part';
|
|
39
|
+
const out = fs.createWriteStream(tmp);
|
|
40
|
+
res.pipe(out);
|
|
41
|
+
out.on('finish', () => out.close(() => {
|
|
42
|
+
try { fs.renameSync(tmp, dest); resolve(); }
|
|
43
|
+
catch (e) { reject(e); }
|
|
44
|
+
}));
|
|
45
|
+
out.on('error', reject);
|
|
46
|
+
res.on('error', reject);
|
|
47
|
+
});
|
|
48
|
+
req.on('error', reject);
|
|
49
|
+
req.setTimeout(60000, () => { req.destroy(new Error('Download timeout (60s)')); });
|
|
31
50
|
});
|
|
32
51
|
}
|
|
33
52
|
|
|
34
|
-
async
|
|
53
|
+
(async () => {
|
|
35
54
|
const url = `https://github.com/${REPO}/releases/download/${TAG}/${assetName}`;
|
|
36
|
-
console.log(`Atlas Agent postinstall: downloading ${assetName}
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
55
|
+
console.log(`Atlas Agent postinstall: downloading ${assetName} from ${url}`);
|
|
56
|
+
await download(url);
|
|
57
|
+
if (process.platform !== 'win32') {
|
|
58
|
+
fs.chmodSync(dest, 0o755);
|
|
40
59
|
}
|
|
41
|
-
const tmp = dest + '.part';
|
|
42
|
-
await new Promise((resolve, reject) => {
|
|
43
|
-
const f = fs.createWriteStream(tmp);
|
|
44
|
-
res.pipe(f);
|
|
45
|
-
f.on('finish', () => f.close(resolve));
|
|
46
|
-
f.on('error', reject);
|
|
47
|
-
});
|
|
48
|
-
fs.renameSync(tmp, dest);
|
|
49
|
-
if (process.platform !== 'win32') fs.chmodSync(dest, 0o755);
|
|
50
60
|
const size = (fs.statSync(dest).size / 1024 / 1024).toFixed(1);
|
|
51
61
|
console.log(`Atlas Agent postinstall: installed ${assetName} (${size} MB)`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
console.error(
|
|
56
|
-
console.error('
|
|
57
|
-
console.error('
|
|
58
|
-
console.error(
|
|
59
|
-
|
|
60
|
-
console.error(' - Network/registry blocked. Try again later.');
|
|
61
|
-
// Don't fail install — the launcher will print a clear error if the binary is missing.
|
|
62
|
+
})().catch((err) => {
|
|
63
|
+
console.error('Atlas Agent postinstall FAILED: ' + err.message);
|
|
64
|
+
console.error('URL: https://github.com/' + REPO + '/releases/download/' + TAG + '/' + assetName);
|
|
65
|
+
console.error('You can manually download it from:');
|
|
66
|
+
console.error(' https://github.com/' + REPO + '/releases/tag/' + TAG);
|
|
67
|
+
console.error('and place it at:');
|
|
68
|
+
console.error(' ' + dest);
|
|
69
|
+
process.exit(1);
|
|
62
70
|
});
|