41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { DAEMON_RELEASE_POLICY } from "./daemon-policy.mjs";
|
|
|
|
export const MINIMUM_CLOUD_DAEMON_VERSION = DAEMON_RELEASE_POLICY.minimumVersion;
|
|
|
|
export type StableVersion = [major: number, minor: number, patch: number];
|
|
|
|
export type ReportedDaemonVersion =
|
|
| { state: "unreported"; version: null }
|
|
| { state: "invalid"; version: null }
|
|
| { state: "incompatible"; version: string }
|
|
| { state: "compatible"; version: string };
|
|
|
|
export function parseStableVersion(value: string): StableVersion | null {
|
|
const version = value.trim();
|
|
const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(version);
|
|
if (!match) return null;
|
|
const parts = match.slice(1).map(Number);
|
|
if (parts.some((part) => !Number.isSafeInteger(part))) return null;
|
|
return parts as StableVersion;
|
|
}
|
|
|
|
export function compareStableVersions(left: StableVersion, right: StableVersion): number {
|
|
for (let index = 0; index < 3; index += 1) {
|
|
if (left[index] !== right[index]) return left[index] - right[index];
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
export function classifyReportedDaemonVersion(value: string | undefined): ReportedDaemonVersion {
|
|
const version = value?.trim() ?? "";
|
|
if (!version) return { state: "unreported", version: null };
|
|
|
|
const parsed = parseStableVersion(version);
|
|
const minimum = parseStableVersion(MINIMUM_CLOUD_DAEMON_VERSION);
|
|
if (!parsed || !minimum) return { state: "invalid", version: null };
|
|
if (compareStableVersions(parsed, minimum) < 0) {
|
|
return { state: "incompatible", version };
|
|
}
|
|
return { state: "compatible", version };
|
|
}
|