Lines 1–14 at 318 (were 1–12 at 39)
1−// Which client this is (#29), for `avcs --version` and for the X-Avcs-Client
1+// Which client this is (#29), for `avcs --version` and for the X-Abso-Client 22// header the command-line client sends with each call: plugin.json's version 33// plus the commit checked out where the client runs (0.1.0+ed45dad). The 44// commit is read from .git without running git, since every command pays for 55// its start-up. (The mods carry their own stamp: src/install.ts.) 7−import { readFileSync, statSync } from "node:fs";
7+import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; 8+import { homedir } from "node:os"; 89import { dirname, join, resolve } from "node:path"; 910import { fileURLToPath } from "node:url"; 11+import { homeWritePath } from "./state-dir.ts"; 1113export const CLIENT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), ".."); Lines 67–143 at 318 (were 65–78 at 39)
6567 return typeof stamp === "string" && /^[0-9a-f]{7,64}$/.test(stamp) ? stamp.slice(0, 7) : undefined; 70+/** Set by the npm package build: the package's version, such as 0.58.0. Absent everywhere else. */ 71+function 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; 76+/** A built copy (the npm package or a downloaded abso.mjs), which has no mods to install from, rather than a checkout. */ 77+export const builtCopy = (): boolean => packageVersion() !== undefined || bundledCommit() !== undefined; 6879let version: string | undefined; 81+const NPM_LATEST = "https://registry.npmjs.org/@abso-cli/abso/latest"; 82+const DAY = 24 * 60 * 60 * 1000; 84+/** Whether x.y.z version a comes after b. */ 85+function 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); 71− * When this client was bundled for a server and that server is now a different
72− * commit. Undefined when they match, when the server can't be asked, or when
73− * this client runs from a checkout, which abso sync updates and no server serves.
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. 75−export async function newerCommitNote(server: string): Promise<string | undefined> {
97+async function newerPackageNote( 102+): Promise<string | undefined> { 103+ const file = homeWritePath(home, "npm-latest.json"); 104+ let latest: string | undefined; 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; 110+ if (latest === undefined) { 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; 120+ mkdirSync(dirname(file), { recursive: true }); 121+ writeFileSync(file, `${JSON.stringify({ checked: now, latest })}\n`); 124+ return laterThan(latest, mine) 125+ ? `abso ${latest} is out and this is ${mine}: npm install -g @abso-cli/abso updates it.` 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. 135+export async function newerClientNote( 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()); 76141 const mine = bundledCommit(); 77142 if (!mine) return undefined; Lines 151–159 at 318 (were 86–93 at 39)
89−/** This client's version: 0.1.0+ed45dad, or plugin.json's alone outside a git checkout. */
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. */ 90155export function clientVersion(): string { 156+ version ??= packageVersion(); 91157 if (version === undefined) { 92158 let base = "0.0.0";