- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/client-version.ts?version=261&format=json
169 linesFirst import 57% · pinch 43%Last change pinch in version 68, 4 versions changed it
You're reading version 261. The newest is 318, and no version since then changed this file.
1// Which client this is (#29), for `avcs --version` and for the X-Abso-Client
2// header the command-line client sends with each call: plugin.json's version
3// plus the commit checked out where the client runs (0.1.0+ed45dad). The
4// commit is read from .git without running git, since every command pays for
5// its start-up. (The mods carry their own stamp: src/install.ts.)
7import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
8import { homedir } from "node:os";
9import { dirname, join, resolve } from "node:path";
10import { fileURLToPath } from "node:url";
11import { homeWritePath } from "./state-dir.ts";
13export const CLIENT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "..");
15/** The commit checked out in the repository holding `dir`, as 7 hex digits; undefined outside one. */
16export function headCommit(dir: string): string | undefined {
17 for (let d = resolve(dir); ; d = dirname(d)) {
18 const dot = join(d, ".git");
19 let isFile: boolean;
20 try {
21 isFile = statSync(dot).isFile();
22 } catch {
23 if (dirname(d) === d) return undefined;
24 continue;
25 }
26 try {
27 // A worktree's .git is a file naming its folder, whose commondir names the shared one.
28 const gitDir = isFile
29 ? resolve(
30 d,
31 readFileSync(dot, "utf8")
32 .replace(/^gitdir:\s*/, "")
33 .trim(),
34 )
35 : dot;
36 let common = gitDir;
37 try {
38 common = resolve(gitDir, readFileSync(join(gitDir, "commondir"), "utf8").trim());
39 } catch {}
40 const head = readFileSync(join(gitDir, "HEAD"), "utf8").trim();
41 if (!head.startsWith("ref:")) return short(head);
42 const ref = head.slice(4).trim();
43 for (const base of [gitDir, common]) {
44 try {
45 return short(readFileSync(join(base, ...ref.split("/")), "utf8").trim());
46 } catch {}
47 }
48 const packed = readFileSync(join(common, "packed-refs"), "utf8")
49 .split("\n")
50 .find((line) => line.endsWith(` ${ref}`));
51 return packed === undefined ? undefined : short(packed.split(" ")[0]!);
52 } catch {
53 return undefined;
54 }
55 }
56}
58const short = (sha: string) => (/^[0-9a-f]{40,64}$/.test(sha) ? sha.slice(0, 7) : undefined);
60/** Set by the staging bundle (abso issue 109). Absent when this file is run from a checkout. */
61export function clientPlace(): string {
62 return bundledCommit() ? "a downloaded copy" : CLIENT_DIR;
63}
65function bundledCommit(): string | undefined {
66 const stamp = (globalThis as { __ABSO_BUILD_COMMIT__?: unknown }).__ABSO_BUILD_COMMIT__;
67 return typeof stamp === "string" && /^[0-9a-f]{7,64}$/.test(stamp) ? stamp.slice(0, 7) : undefined;
68}
70/** Set by the npm package build: the package's version, such as 0.58.0. Absent everywhere else. */
71function packageVersion(): string | undefined {
72 const stamp = (globalThis as { __ABSO_PACKAGE_VERSION__?: unknown }).__ABSO_PACKAGE_VERSION__;
73 return typeof stamp === "string" && /^\d+\.\d+\.\d+$/.test(stamp) ? stamp : undefined;
74}
76/** A built copy (the npm package or a downloaded abso.mjs), which has no mods to install from, rather than a checkout. */
77export const builtCopy = (): boolean => packageVersion() !== undefined || bundledCommit() !== undefined;
79let version: string | undefined;
81const NPM_LATEST = "https://registry.npmjs.org/@abso-cli/abso/latest";
82const DAY = 24 * 60 * 60 * 1000;
84/** Whether x.y.z version a comes after b. */
85function laterThan(a: string, b: string): boolean {
86 const x = a.split(".").map(Number);
87 const y = b.split(".").map(Number);
88 for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return (x[i] ?? 0) > (y[i] ?? 0);
89 return false;
90}
92/**
93 * For the npm package: npm's latest version when it comes after this one. npm
94 * is asked at most once a day, and its answer is kept in ~/.abso/npm-latest.json,
95 * so status doesn't wait on the registry each time.
96 */
97async function newerPackageNote(
98 mine: string,
99 home: string,
100 registry: string,
101 now: number,
102): Promise<string | undefined> {
103 const file = homeWritePath(home, "npm-latest.json");
104 let latest: string | undefined;
105 try {
106 const kept = JSON.parse(readFileSync(file, "utf8")) as { checked?: unknown; latest?: unknown };
107 if (typeof kept.checked === "number" && now - kept.checked < DAY && typeof kept.latest === "string")
108 latest = kept.latest;
109 } catch {}
110 if (latest === undefined) {
111 try {
112 const response = await fetch(registry, { signal: AbortSignal.timeout(3000) });
113 const body = (await response.json()) as { version?: unknown };
114 if (typeof body.version !== "string" || !/^\d+\.\d+\.\d+$/.test(body.version)) return undefined;
115 latest = body.version;
116 } catch {
117 return undefined;
118 }
119 try {
120 mkdirSync(dirname(file), { recursive: true });
121 writeFileSync(file, `${JSON.stringify({ checked: now, latest })}\n`);
122 } catch {}
123 }
124 return laterThan(latest, mine)
125 ? `abso ${latest} is out and this is ${mine}: npm install -g @abso-cli/abso updates it.`
126 : undefined;
127}
129/**
130 * When a built copy of this client is behind: the npm package when npm has a
131 * later version, or a client bundled for a server when that server is now a
132 * different commit. Undefined when it isn't, when the answer can't be had, or
133 * when this client runs from a checkout, which abso sync updates.
134 */
135export async function newerClientNote(
136 server: string,
137 at: { home?: string; registry?: string; now?: number } = {},
138): Promise<string | undefined> {
139 const pkg = packageVersion();
140 if (pkg) return newerPackageNote(pkg, at.home ?? homedir(), at.registry ?? NPM_LATEST, at.now ?? Date.now());
141 const mine = bundledCommit();
142 if (!mine) return undefined;
143 try {
144 const response = await fetch(new URL("/version", server), { signal: AbortSignal.timeout(3000) });
145 const body = (await response.json()) as { commit?: unknown };
146 const theirs = typeof body.commit === "string" ? body.commit.slice(0, 7) : "";
147 if (theirs === "" || theirs === mine) return undefined;
148 return `This abso is ${mine} and the server is ${theirs}. Download ${new URL("/client/abso.mjs", server)} and check it against /client/abso.mjs.sha256.`;
149 } catch {
150 return undefined;
151 }
152}
154/** This client's version: the npm package's (0.58.0), else 0.1.0+ed45dad, or plugin.json's alone outside a git checkout. */
155export function clientVersion(): string {
156 version ??= packageVersion();
157 if (version === undefined) {
158 let base = "0.0.0";
159 try {
160 base = String(
161 (JSON.parse(readFileSync(join(CLIENT_DIR, ".claude-plugin", "plugin.json"), "utf8")) as { version?: unknown })
162 .version,
163 ).split("+")[0]!;
164 } catch {}
165 const commit = bundledCommit() ?? headCommit(CLIENT_DIR);
166 version = commit ? `${base}+${commit}` : base;
167 }
168 return version;
169}