@bloret-crew/blora-agent 0.1.0-beta.0 → 0.1.0-beta.1

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