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

servicePreflight.ts

96 lines3.1 KB

1import * as NodeSqlite from "node:sqlite";
2​
3import packageJson from "../../package.json" with { type: "json" };
4import { migrationManifest } from "../persistence/Migrations.ts";
5import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
6​
7export type ServicePreflightResult =
8 | {
9 readonly status: "ready";
10 readonly version: string;
11 readonly launcherProtocol: typeof SERVICE_LAUNCHER_PROTOCOL;
12 }
13 | {
14 readonly status: "blocked";
15 readonly version: string;
16 readonly reason: string;
17 };
18​
19const localUpdateReason = (version: string) =>
20 `This version includes a database update and cannot be installed remotely. Run \`npx t3@${version} service update\` on the server machine.`;
21​
22const isMigrationRow = (
23 value: unknown,
24): value is { readonly migration_id: number; readonly name: string } =>
25 typeof value === "object" &&
26 value !== null &&
27 "migration_id" in value &&
28 typeof value.migration_id === "number" &&
29 "name" in value &&
30 typeof value.name === "string";
31​
32export function runServicePreflight(input: {
33 readonly databasePath: string;
34 readonly launcherProtocol: number;
35 readonly version?: string;
36}): ServicePreflightResult {
37 const version = input.version ?? packageJson.version;
38 if (input.launcherProtocol !== SERVICE_LAUNCHER_PROTOCOL) {
39 return {
40 status: "blocked",
41 version,
42 reason:
43 "This release requires a newer T3 Code service launcher. Update it on the server machine.",
44 };
45 }
46​
47 try {
48 const database = new NodeSqlite.DatabaseSync(input.databasePath, { readOnly: true });
49 try {
50 const rows: ReadonlyArray<unknown> = database
51 .prepare("SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id")
52 .all();
53 const exact =
54 rows.length === migrationManifest.length &&
55 rows.every((row, index) => {
56 const expected = migrationManifest[index];
57 return (
58 isMigrationRow(row) && row.migration_id === expected?.[0] && row.name === expected?.[1]
59 );
60 });
61 if (!exact) return { status: "blocked", version, reason: localUpdateReason(version) };
62 } finally {
63 database.close();
64 }
65 } catch {
66 return { status: "blocked", version, reason: localUpdateReason(version) };
67 }
68​
69 return { status: "ready", version, launcherProtocol: SERVICE_LAUNCHER_PROTOCOL };
71​
72export function decodeServicePreflightResult(value: unknown): ServicePreflightResult | undefined {
73 if (typeof value !== "object" || value === null) {
74 return undefined;
75 }
76 const record = value as Record<string, unknown>;
77 if (
78 record.status === "ready" &&
79 record.launcherProtocol === SERVICE_LAUNCHER_PROTOCOL &&
80 typeof record.version === "string"
81 ) {
82 return {
83 status: "ready",
84 version: record.version,
85 launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
86 };
87 }
88 if (
89 record.status === "blocked" &&
90 typeof record.version === "string" &&
91 typeof record.reason === "string"
92 ) {
93 return { status: "blocked", version: record.version, reason: record.reason };
94 }
95 return undefined;