@traylinx/switchailocal 0.3.0 → 0.6.4

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.js +92 -12
  2. package/package.json +1 -1
package/bin.js CHANGED
@@ -5,7 +5,11 @@
5
5
  // Binary source: GitHub Releases (https://github.com/traylinx/switchAILocal/releases)
6
6
 
7
7
  import { execFileSync, spawnSync } from "node:child_process";
8
- import { createWriteStream, existsSync, mkdirSync, chmodSync, unlinkSync, writeFileSync } from "node:fs";
8
+ import { createHash } from "node:crypto";
9
+ import {
10
+ createWriteStream, existsSync, mkdirSync, mkdtempSync, chmodSync, readdirSync,
11
+ readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync,
12
+ } from "node:fs";
9
13
  import { get as httpsGet } from "node:https";
10
14
  import { homedir, platform, arch } from "node:os";
11
15
  import { join } from "node:path";
@@ -13,6 +17,8 @@ import { join } from "node:path";
13
17
  const REPO = "traylinx/switchAILocal";
14
18
  const BINARY_NAME = "switchAILocal";
15
19
  const DEFAULT_PORT = 18080;
20
+ // Platforms the release workflow publishes (.github/workflows/release.yml).
21
+ const PUBLISHED = new Set(["darwin/arm64", "linux/amd64", "linux/arm64"]);
16
22
 
17
23
  // ── Platform Detection ──────────────────────────────────────────
18
24
 
@@ -24,26 +30,48 @@ function getPlatform() {
24
30
  const archMap = { arm64: "arm64", x64: "amd64", ia32: "386" };
25
31
 
26
32
  const os = osMap[p];
27
- const cpu = archMap[a];
33
+ let cpu = archMap[a];
34
+
35
+ // An x64 Node under Rosetta on Apple Silicon: the arm64 binary runs natively.
36
+ if (os === "darwin" && cpu === "amd64" && runningUnderRosetta()) {
37
+ cpu = "arm64";
38
+ }
28
39
 
29
40
  if (!os || !cpu) {
30
41
  console.error(`Unsupported platform: ${p}/${a}`);
31
42
  process.exit(1);
32
43
  }
33
44
 
45
+ if (!PUBLISHED.has(`${os}/${cpu}`)) {
46
+ console.error(`No pre-built switchAILocal binary is published for ${os}/${cpu}.`);
47
+ console.error(`Published: ${[...PUBLISHED].join(", ")}.`);
48
+ console.error(`Use Docker or build from source: https://github.com/${REPO}#installation`);
49
+ process.exit(1);
50
+ }
51
+
34
52
  const ext = os === "windows" ? ".zip" : ".tar.gz";
35
53
  const bin = os === "windows" ? `${BINARY_NAME}.exe` : BINARY_NAME;
36
54
 
37
55
  return { os, arch: cpu, ext, bin };
38
56
  }
39
57
 
58
+ function runningUnderRosetta() {
59
+ try {
60
+ return execFileSync("sysctl", ["-in", "sysctl.proc_translated"], { stdio: ["ignore", "pipe", "ignore"] })
61
+ .toString().trim() === "1";
62
+ } catch {
63
+ return false;
64
+ }
65
+ }
66
+
40
67
  // ── Version Resolution ──────────────────────────────────────────
41
68
 
42
69
  function parseVersion(args) {
43
70
  for (let i = 0; i < args.length; i++) {
44
71
  if (args[i] === "--version" && args[i + 1]) {
72
+ const ver = args[i + 1];
45
73
  args.splice(i, 2);
46
- return args[i + 1];
74
+ return ver;
47
75
  }
48
76
  if (args[i].startsWith("--version=")) {
49
77
  const ver = args[i].split("=")[1];
@@ -110,6 +138,29 @@ function getCacheDir(version) {
110
138
 
111
139
  // ── Download ────────────────────────────────────────────────────
112
140
 
141
+ function fetchText(url) {
142
+ return followRedirects(url).then((res) => new Promise((resolve, reject) => {
143
+ let data = "";
144
+ res.setEncoding("utf8");
145
+ res.on("data", (c) => { data += c; if (data.length > 1 << 20) reject(new Error("response too large")); });
146
+ res.on("end", () => resolve(data));
147
+ res.on("error", reject);
148
+ }));
149
+ }
150
+
151
+ // Returns the path of the first regular file called `name` below `dir`.
152
+ function findFile(dir, name) {
153
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
154
+ const path = join(dir, entry.name);
155
+ if (entry.isFile() && entry.name === name) return path;
156
+ if (entry.isDirectory()) {
157
+ const found = findFile(path, name);
158
+ if (found) return found;
159
+ }
160
+ }
161
+ return null;
162
+ }
163
+
113
164
  function followRedirects(url) {
114
165
  return new Promise((resolve, reject) => {
115
166
  httpsGet(url, { headers: { "User-Agent": "switchailocal-npx" } }, (res) => {
@@ -159,15 +210,34 @@ async function downloadAndExtract(version, plat, destDir) {
159
210
  res.on("error", reject);
160
211
  });
161
212
 
162
- // Extract — only the binary from the archive
163
- if (plat.ext === ".tar.gz") {
164
- execFileSync("tar", ["xzf", tmpFile, "-C", destDir, "--include", plat.bin], { stdio: "pipe" });
165
- } else {
166
- // Windows: use PowerShell to extract
167
- execFileSync("powershell", [
168
- "-Command",
169
- `Expand-Archive -Path '${tmpFile}' -DestinationPath '${destDir}' -Force`,
170
- ], { stdio: "pipe" });
213
+ // Verify the archive against the release's checksums.txt.
214
+ const sums = await fetchText(`https://github.com/${REPO}/releases/download/${version}/checksums.txt`);
215
+ const line = sums.split("\n").find((l) => l.trim().endsWith(` ${archiveName}`) || l.trim().endsWith(` ${archiveName}`));
216
+ const expected = line && line.trim().split(/\s+/)[0];
217
+ const actual = createHash("sha256").update(readFileSync(tmpFile)).digest("hex");
218
+ if (!expected || expected.toLowerCase() !== actual) {
219
+ throw new Error(`checksum mismatch for ${archiveName}`);
220
+ }
221
+
222
+ // Extract into a scratch directory and take the binary from wherever the
223
+ // archive puts it (releases since 0.6 nest it in a versioned folder).
224
+ const scratch = mkdtempSync(join(destDir, "extract-"));
225
+ try {
226
+ if (plat.ext === ".tar.gz") {
227
+ execFileSync("tar", ["xzf", tmpFile, "-C", scratch], { stdio: "pipe" });
228
+ } else {
229
+ execFileSync("powershell", [
230
+ "-NoProfile", "-Command",
231
+ `Expand-Archive -LiteralPath '${tmpFile}' -DestinationPath '${scratch}' -Force`,
232
+ ], { stdio: "pipe" });
233
+ }
234
+ const found = findFile(scratch, plat.bin);
235
+ if (!found || !statSync(found).isFile()) {
236
+ throw new Error(`${plat.bin} not found in ${archiveName}`);
237
+ }
238
+ renameSync(found, join(destDir, plat.bin));
239
+ } finally {
240
+ rmSync(scratch, { recursive: true, force: true });
171
241
  }
172
242
 
173
243
  chmodSync(join(destDir, plat.bin), 0o755);
@@ -190,6 +260,9 @@ function ensureConfig() {
190
260
 
191
261
  port: ${DEFAULT_PORT}
192
262
 
263
+ # Where OAuth tokens and provider credentials are stored.
264
+ auth-dir: "~/.switchailocal"
265
+
193
266
  # Uncomment and add your API keys:
194
267
  # gemini-api-key:
195
268
  # - api-key: "your-gemini-key"
@@ -252,6 +325,13 @@ async function main() {
252
325
  // Binary might not support --version flag, that's ok
253
326
  }
254
327
 
328
+ // Use ~/.switchailocal/config.yaml (created on first run) unless the caller
329
+ // passes -config or the current directory has its own config.yaml.
330
+ const hasConfigFlag = args.some((a) => /^--?config(=|$)/.test(a));
331
+ if (!hasConfigFlag && !existsSync(join(process.cwd(), "config.yaml"))) {
332
+ args.unshift("-config", ensureConfig());
333
+ }
334
+
255
335
  // Run
256
336
  const result = spawnSync(binaryPath, args, {
257
337
  stdio: "inherit",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@traylinx/switchailocal",
3
- "version": "0.3.0",
3
+ "version": "0.6.4",
4
4
  "description": "One local endpoint. All your AI providers. Install and run switchAILocal instantly.",
5
5
  "bin": {
6
6
  "switchailocal": "bin.js"