Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

servicePreflight.ts

58 lines1.7 KB

1import packageJson from "../../package.json" with { type: "json" };
2import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
3​
4export type ServicePreflightResult =
5 | {
6 readonly status: "ready";
7 readonly version: string;
8 readonly launcherProtocol: typeof SERVICE_LAUNCHER_PROTOCOL;
9 }
10 | {
11 readonly status: "blocked";
12 readonly version: string;
13 readonly reason: string;
14 };
15​
16export function runServicePreflight(input: {
17 readonly databasePath: string;
18 readonly launcherProtocol: number;
19 readonly version?: string;
20}): ServicePreflightResult {
21 const version = input.version ?? packageJson.version;
22 if (input.launcherProtocol !== SERVICE_LAUNCHER_PROTOCOL) {
23 return {
24 status: "blocked",
25 version,
26 reason:
27 "This release requires a newer T3 Code service launcher. Update it on the server machine.",
28 };
29 }
30​
31 return { status: "ready", version, launcherProtocol: SERVICE_LAUNCHER_PROTOCOL };
33​
34export function decodeServicePreflightResult(value: unknown): ServicePreflightResult | undefined {
35 if (typeof value !== "object" || value === null) {
36 return undefined;
37 }
38 const record = value as Record<string, unknown>;
39 if (
40 record.status === "ready" &&
41 record.launcherProtocol === SERVICE_LAUNCHER_PROTOCOL &&
42 typeof record.version === "string"
43 ) {
44 return {
45 status: "ready",
46 version: record.version,
47 launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
48 };
49 }
50 if (
51 record.status === "blocked" &&
52 typeof record.version === "string" &&
53 typeof record.reason === "string"
54 ) {
55 return { status: "blocked", version: record.version, reason: record.reason };
56 }
57 return undefined;