{"draft":null,"from":41,"to":318,"files":[{"path":"client/src/client-version.ts","added":73,"removed":7,"diff":"--- a/client/src/client-version.ts\n+++ b/client/src/client-version.ts\n@@ -1,12 +1,14 @@\n-// Which client this is (#29), for `avcs --version` and for the X-Avcs-Client\n+// Which client this is (#29), for `avcs --version` and for the X-Abso-Client\n // header the command-line client sends with each call: plugin.json's version\n // plus the commit checked out where the client runs (0.1.0+ed45dad). The\n // commit is read from .git without running git, since every command pays for\n // its start-up. (The mods carry their own stamp: src/install.ts.)\n \n-import { readFileSync, statSync } from \"node:fs\";\n+import { mkdirSync, readFileSync, statSync, writeFileSync } from \"node:fs\";\n+import { homedir } from \"node:os\";\n import { dirname, join, resolve } from \"node:path\";\n import { fileURLToPath } from \"node:url\";\n+import { homeWritePath } from \"./state-dir.ts\";\n \n export const CLIENT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), \"..\");\n \n@@ -65,14 +67,77 @@\n \treturn typeof stamp === \"string\" && /^[0-9a-f]{7,64}$/.test(stamp) ? stamp.slice(0, 7) : undefined;\n }\n \n+/** Set by the npm package build: the package's version, such as 0.58.0. Absent everywhere else. */\n+function packageVersion(): string | undefined {\n+\tconst stamp = (globalThis as { __ABSO_PACKAGE_VERSION__?: unknown }).__ABSO_PACKAGE_VERSION__;\n+\treturn typeof stamp === \"string\" && /^\\d+\\.\\d+\\.\\d+$/.test(stamp) ? stamp : undefined;\n+}\n+\n+/** A built copy (the npm package or a downloaded abso.mjs), which has no mods to install from, rather than a checkout. */\n+export const builtCopy = (): boolean => packageVersion() !== undefined || bundledCommit() !== undefined;\n+\n let version: string | undefined;\n \n+const NPM_LATEST = \"https://registry.npmjs.org/@abso-cli/abso/latest\";\n+const DAY = 24 * 60 * 60 * 1000;\n+\n+/** Whether x.y.z version a comes after b. */\n+function laterThan(a: string, b: string): boolean {\n+\tconst x = a.split(\".\").map(Number);\n+\tconst y = b.split(\".\").map(Number);\n+\tfor (let i = 0; i < 3; i++) if (x[i] !== y[i]) return (x[i] ?? 0) > (y[i] ?? 0);\n+\treturn false;\n+}\n+\n /**\n- * When this client was bundled for a server and that server is now a different\n- * commit. Undefined when they match, when the server can't be asked, or when\n- * this client runs from a checkout, which abso sync updates and no server serves.\n+ * For the npm package: npm's latest version when it comes after this one. npm\n+ * is asked at most once a day, and its answer is kept in ~/.abso/npm-latest.json,\n+ * so status doesn't wait on the registry each time.\n  */\n-export async function newerCommitNote(server: string): Promise<string | undefined> {\n+async function newerPackageNote(\n+\tmine: string,\n+\thome: string,\n+\tregistry: string,\n+\tnow: number,\n+): Promise<string | undefined> {\n+\tconst file = homeWritePath(home, \"npm-latest.json\");\n+\tlet latest: string | undefined;\n+\ttry {\n+\t\tconst kept = JSON.parse(readFileSync(file, \"utf8\")) as { checked?: unknown; latest?: unknown };\n+\t\tif (typeof kept.checked === \"number\" && now - kept.checked < DAY && typeof kept.latest === \"string\")\n+\t\t\tlatest = kept.latest;\n+\t} catch {}\n+\tif (latest === undefined) {\n+\t\ttry {\n+\t\t\tconst response = await fetch(registry, { signal: AbortSignal.timeout(3000) });\n+\t\t\tconst body = (await response.json()) as { version?: unknown };\n+\t\t\tif (typeof body.version !== \"string\" || !/^\\d+\\.\\d+\\.\\d+$/.test(body.version)) return undefined;\n+\t\t\tlatest = body.version;\n+\t\t} catch {\n+\t\t\treturn undefined;\n+\t\t}\n+\t\ttry {\n+\t\t\tmkdirSync(dirname(file), { recursive: true });\n+\t\t\twriteFileSync(file, `${JSON.stringify({ checked: now, latest })}\\n`);\n+\t\t} catch {}\n+\t}\n+\treturn laterThan(latest, mine)\n+\t\t? `abso ${latest} is out and this is ${mine}: npm install -g @abso-cli/abso updates it.`\n+\t\t: undefined;\n+}\n+\n+/**\n+ * When a built copy of this client is behind: the npm package when npm has a\n+ * later version, or a client bundled for a server when that server is now a\n+ * different commit. Undefined when it isn't, when the answer can't be had, or\n+ * when this client runs from a checkout, which abso sync updates.\n+ */\n+export async function newerClientNote(\n+\tserver: string,\n+\tat: { home?: string; registry?: string; now?: number } = {},\n+): Promise<string | undefined> {\n+\tconst pkg = packageVersion();\n+\tif (pkg) return newerPackageNote(pkg, at.home ?? homedir(), at.registry ?? NPM_LATEST, at.now ?? Date.now());\n \tconst mine = bundledCommit();\n \tif (!mine) return undefined;\n \ttry {\n@@ -86,8 +151,9 @@\n \t}\n }\n \n-/** This client's version: 0.1.0+ed45dad, or plugin.json's alone outside a git checkout. */\n+/** 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. */\n export function clientVersion(): string {\n+\tversion ??= packageVersion();\n \tif (version === undefined) {\n \t\tlet base = \"0.0.0\";\n \t\ttry {"}],"unmatched":[]}