askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
version.ts · 2.9 KB · 62 lines
| 1 | /** |
| 2 | * The browser's version, in two spellings of the same three values: the year and month of the release line (the UTC |
| 3 | * month it was cut, written down when it is cut, never read from the build clock) and a release count from 1. |
| 4 | * |
| 5 | * shown to people and used as the git tag: 2026.11v1, 2026.12v1-beta.2 |
| 6 | * package.json and updaters (semver): 2026.11.1, 2026.12.1-beta.2 (no leading zero on the month: semver forbids it) |
| 7 | * |
| 8 | * package.json holds the semver spelling and is the only place it is written; scripts/version.mjs rewrites it. Plugins |
| 9 | * get a separate plain integer, PLUGIN_API_LEVEL, raised only when what a plugin can rely on changes. |
| 10 | * No imports, so scripts can load this file with Node directly. |
| 11 | */ |
| 12 | |
| 13 | /** What a plugin can rely on. Raise it only when the plugin interface changes; plugins declare `min_api`. */ |
| 14 | export const PLUGIN_API_LEVEL = 2; |
| 15 | |
| 16 | export interface Version { year: number; month: number; n: number; pre: string } |
| 17 | |
| 18 | const SEMVER = /^(\d{4})\.([1-9]|1[0-2])\.([1-9]\d*)(?:-([0-9A-Za-z.]+))?$/; |
| 19 | const SHOWN = /^(\d{4})\.(0[1-9]|1[0-2])v([1-9]\d*)(?:-([0-9A-Za-z.]+))?$/; |
| 20 | |
| 21 | function parse(re: RegExp, s: string): Version | null { |
| 22 | const m = re.exec(s.trim()); |
| 23 | if (!m) return null; |
| 24 | const [, y = "", mo = "", n = "", pre = ""] = m; |
| 25 | return { year: Number(y), month: Number(mo), n: Number(n), pre }; |
| 26 | } |
| 27 | |
| 28 | /** "2026.11.1" → {2026, 11, 1}; also accepts the shown form. null for anything else (0.1.0, a date, garbage). */ |
| 29 | export function parseVersion(s: string): Version | null { |
| 30 | return parse(SEMVER, s) ?? parse(SHOWN, s); |
| 31 | } |
| 32 | |
| 33 | export function shownVersion(v: Version): string { |
| 34 | return `${v.year}.${String(v.month).padStart(2, "0")}v${v.n}${v.pre ? "-" + v.pre : ""}`; |
| 35 | } |
| 36 | |
| 37 | export function semverVersion(v: Version): string { |
| 38 | return `${v.year}.${v.month}.${v.n}${v.pre ? "-" + v.pre : ""}`; |
| 39 | } |
| 40 | |
| 41 | /** The shown spelling of a package.json version; a version not in this scheme is shown as it is. */ |
| 42 | export function displayVersion(semver: string): string { |
| 43 | const v = parseVersion(semver); |
| 44 | return v ? shownVersion(v) : semver; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The next release cut in `now`'s UTC month: the count goes up within a month and starts again at 1 in a new one. After a |
| 49 | * pre-release, the next is that same release, final (2026.12v1-beta.2 → 2026.12v1). |
| 50 | */ |
| 51 | export function nextVersion(current: string, now: Date): Version { |
| 52 | const year = now.getUTCFullYear(); |
| 53 | const month = now.getUTCMonth() + 1; |
| 54 | const v = parseVersion(current); |
| 55 | if (!v || v.year !== year || v.month !== month) return { year, month, n: 1, pre: "" }; |
| 56 | return { year, month, n: v.pre ? v.n : v.n + 1, pre: "" }; |
| 57 | } |
| 58 | |
| 59 | /** Sorts like the releases happened: by line, then count, and a pre-release before its final. */ |
| 60 | export function compareVersions(a: Version, b: Version): number { |
| 61 | return a.year - b.year || a.month - b.month || a.n - b.n || (a.pre === b.pre ? 0 : !a.pre ? 1 : !b.pre ? -1 : a.pre < b.pre ? -1 : 1); |
| 62 | } |