@bloret-crew/blora-agent 0.1.0-beta.0 → 0.1.0-beta.2
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 +93 -20
- package/package.json +1 -1
package/bin/install.js
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const { createWriteStream, existsSync, mkdirSync, renameSync } = require("node:fs");
|
|
4
|
-
const {
|
|
3
|
+
const { createWriteStream, existsSync, mkdirSync, renameSync, rmSync } = require("node:fs");
|
|
4
|
+
const { tmpdir } = require("node:os");
|
|
5
5
|
const path = require("node:path");
|
|
6
|
+
const { execFile } = require("node:child_process");
|
|
7
|
+
const { promisify } = require("node:util");
|
|
6
8
|
const { pipeline } = require("node:stream/promises");
|
|
7
9
|
const { request } = require("node:https");
|
|
8
10
|
|
|
11
|
+
const execFileAsync = promisify(execFile);
|
|
12
|
+
|
|
9
13
|
const version = require("../package.json").version;
|
|
10
14
|
const repo = "BloretCrew/Blora-Agent";
|
|
11
15
|
const installDir = path.join(__dirname);
|
|
12
16
|
const executable = process.platform === "win32" ? "blora.exe" : "blora";
|
|
13
17
|
const destination = path.join(installDir, executable);
|
|
18
|
+
const DOWNLOAD_TIMEOUT_MS = 120_000;
|
|
19
|
+
const MAX_ATTEMPTS = 4;
|
|
14
20
|
|
|
15
21
|
function platformAsset() {
|
|
16
22
|
const arch = process.arch === "x64" ? "x86_64" : process.arch;
|
|
@@ -19,35 +25,96 @@ function platformAsset() {
|
|
|
19
25
|
return `blora-${version}-${platform}-${arch}.${ext}`;
|
|
20
26
|
}
|
|
21
27
|
|
|
28
|
+
function wait(ms) {
|
|
29
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
30
|
+
}
|
|
31
|
+
|
|
22
32
|
function download(url, destinationPath, redirects = 0) {
|
|
23
|
-
if (redirects >
|
|
33
|
+
if (redirects > 8) return Promise.reject(new Error("too many redirects"));
|
|
24
34
|
return new Promise((resolve, reject) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const fail = (error) => {
|
|
36
|
+
try { rmSync(destinationPath, { force: true }); } catch {}
|
|
37
|
+
reject(error);
|
|
38
|
+
};
|
|
39
|
+
const req = request(
|
|
40
|
+
url,
|
|
41
|
+
{
|
|
42
|
+
headers: {
|
|
43
|
+
Accept: "application/octet-stream",
|
|
44
|
+
Connection: "keep-alive",
|
|
45
|
+
"User-Agent": "@bloret-crew/blora-agent",
|
|
46
|
+
},
|
|
47
|
+
timeout: DOWNLOAD_TIMEOUT_MS,
|
|
48
|
+
},
|
|
49
|
+
(response) => {
|
|
50
|
+
if ([301, 302, 303, 307, 308].includes(response.statusCode)) {
|
|
51
|
+
response.resume();
|
|
52
|
+
return download(response.headers.location, destinationPath, redirects + 1).then(resolve, reject);
|
|
53
|
+
}
|
|
54
|
+
if (response.statusCode !== 200) {
|
|
55
|
+
response.resume();
|
|
56
|
+
return fail(new Error(`download failed with HTTP ${response.statusCode}`));
|
|
57
|
+
}
|
|
58
|
+
pipeline(response, output).then(() => resolve(), reject);
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
req.on("timeout", () => req.destroy(new Error(`download timed out after ${DOWNLOAD_TIMEOUT_MS / 1000}s`)));
|
|
62
|
+
req.on("error", fail);
|
|
37
63
|
});
|
|
38
64
|
}
|
|
39
65
|
|
|
66
|
+
async function downloadWithRetry(url, destinationPath) {
|
|
67
|
+
let lastError;
|
|
68
|
+
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt += 1) {
|
|
69
|
+
try {
|
|
70
|
+
await download(url, destinationPath);
|
|
71
|
+
return;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
lastError = error;
|
|
74
|
+
if (error.code === "ECONNRESET" || error.message === "socket hang up") throw error;
|
|
75
|
+
if (attempt < MAX_ATTEMPTS) {
|
|
76
|
+
const delay = 1_000 * 2 ** (attempt - 1);
|
|
77
|
+
console.error(`Download attempt ${attempt} failed (${error.message}); retrying in ${delay / 1000}s…`);
|
|
78
|
+
await wait(delay);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
throw lastError;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function downloadWithCurl(url, destinationPath) {
|
|
86
|
+
await execFileAsync("curl", [
|
|
87
|
+
"--fail",
|
|
88
|
+
"--location",
|
|
89
|
+
"--retry", String(MAX_ATTEMPTS - 1),
|
|
90
|
+
"--retry-delay", "2",
|
|
91
|
+
"--connect-timeout", "20",
|
|
92
|
+
"--max-time", String(DOWNLOAD_TIMEOUT_MS / 1000),
|
|
93
|
+
"--output", destinationPath,
|
|
94
|
+
url,
|
|
95
|
+
]);
|
|
96
|
+
}
|
|
97
|
+
|
|
40
98
|
async function extract(archive, target) {
|
|
41
99
|
if (process.platform === "win32") {
|
|
42
100
|
const { execFile } = require("node:child_process");
|
|
43
101
|
await new Promise((resolve, reject) => {
|
|
44
|
-
execFile(
|
|
102
|
+
execFile(
|
|
103
|
+
"powershell.exe",
|
|
104
|
+
[
|
|
105
|
+
"-NoProfile",
|
|
106
|
+
"-NonInteractive",
|
|
107
|
+
"-Command",
|
|
108
|
+
`Expand-Archive -LiteralPath '${archive.replaceAll("'", "''")}' -DestinationPath '${target.replaceAll("'", "''")}' -Force`,
|
|
109
|
+
],
|
|
110
|
+
(error) => (error ? reject(error) : resolve()),
|
|
111
|
+
);
|
|
45
112
|
});
|
|
46
113
|
return;
|
|
47
114
|
}
|
|
48
115
|
const { execFile } = require("node:child_process");
|
|
49
116
|
await new Promise((resolve, reject) => {
|
|
50
|
-
execFile("tar", ["-xzf", archive, "-C", target], (error) => error ? reject(error) : resolve());
|
|
117
|
+
execFile("tar", ["-xzf", archive, "-C", target], (error) => (error ? reject(error) : resolve()));
|
|
51
118
|
});
|
|
52
119
|
}
|
|
53
120
|
|
|
@@ -60,7 +127,13 @@ async function main() {
|
|
|
60
127
|
const url = `https://github.com/${repo}/releases/download/v${version}/${asset}`;
|
|
61
128
|
try {
|
|
62
129
|
console.log(`Downloading Blora ${version} for ${process.platform}/${process.arch}…`);
|
|
63
|
-
|
|
130
|
+
try {
|
|
131
|
+
await downloadWithRetry(url, archive);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (error.code !== "ECONNRESET" && error.message !== "socket hang up") throw error;
|
|
134
|
+
console.error(`Node download failed (${error.message}); retrying with curl…`);
|
|
135
|
+
await downloadWithCurl(url, archive);
|
|
136
|
+
}
|
|
64
137
|
await extract(archive, target);
|
|
65
138
|
const extracted = path.join(target, executable);
|
|
66
139
|
if (!existsSync(extracted)) throw new Error(`release archive does not contain ${executable}`);
|
|
@@ -72,8 +145,8 @@ async function main() {
|
|
|
72
145
|
console.error(`Install the binary from https://github.com/${repo}/releases/tag/v${version}`);
|
|
73
146
|
process.exitCode = 1;
|
|
74
147
|
} finally {
|
|
75
|
-
try {
|
|
76
|
-
try {
|
|
148
|
+
try { rmSync(archive, { force: true }); } catch {}
|
|
149
|
+
try { rmSync(target, { recursive: true, force: true }); } catch {}
|
|
77
150
|
}
|
|
78
151
|
}
|
|
79
152
|
|