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

servicePreflight.ts

103 lines4.0 KB

1import packageJson from "../../package.json" with { type: "json" };
2import { BUILD_MIGRATION_HEAD, recordedMigrations } from "../persistence/databaseMigrationHead.ts";
3import { skippedV2Migrations } from "../persistence/Migrations.ts";
4import { reconciledV2PreviewHistory } from "../persistence/reconcileV2PreviewMigration.ts";
5import {
6 isSupportedLauncherProtocol,
7 SERVICE_LAUNCHER_PROTOCOL,
8 type SupportedLauncherProtocol,
9} from "./serviceProtocol.ts";
10​
11export type ServicePreflightResult =
12 | {
13 readonly status: "ready";
14 readonly version: string;
15 /** The launcher protocol the server named; it accepts no other in reply. */
16 readonly launcherProtocol: SupportedLauncherProtocol;
17 }
18 | {
19 readonly status: "blocked";
20 readonly version: string;
21 readonly reason: string;
22 };
23​
24export function runServicePreflight(input: {
25 readonly databasePath: string;
26 readonly launcherProtocol: number;
27 readonly version?: string;
28}): ServicePreflightResult {
29 const version = input.version ?? packageJson.version;
30 const { launcherProtocol } = input;
31 if (!isSupportedLauncherProtocol(launcherProtocol)) {
32 return {
33 status: "blocked",
34 version,
35 // A stale `t3` earlier on PATH (an old npm/nvm global) makes `t3 service install`
36 // reinstall itself and report success, so name the exact install and the path check.
37 reason: [
38 `The installed service launcher speaks protocol ${launcherProtocol}, but t3@${version} requires protocol ${SERVICE_LAUNCHER_PROTOCOL}.`,
39 `On the server machine, install this exact CLI: \`curl -fsSL https://t3.codes/install.sh | T3CODE_VERSION=${version} sh\`.`,
40 `Check that \`t3 --version\` prints ${version} and that \`which -a t3\` lists no older t3 (such as an npm or nvm install) first, then run \`~/.local/bin/t3 service install\` (the installer's default path).`,
41 "Updating the desktop app does not update this CLI, and `t3 service update` only repairs the service with whichever CLI runs it.",
42 ].join(" "),
43 };
44 }
45​
46 const recorded = recordedMigrations(input.databasePath);
47 if (recorded === null) {
48 return {
49 status: "blocked",
50 version,
51 reason: `Could not read this database to confirm t3@${version} supports it.`,
52 };
53 }
54 const newer = recorded
55 .filter(({ id }) => id > BUILD_MIGRATION_HEAD)
56 .map(({ id, name }) => `${id}_${name}`);
57 if (newer.length > 0) {
58 return {
59 status: "blocked",
60 version,
61 reason: `This database has migrations that t3@${version} does not know: ${newer.join(", ")}. A newer build wrote them, and running t3@${version} on this database is not safe. Stay on the newer release, or wait for a release that includes these migrations.`,
62 };
63 }
64​
65 // Startup reconciles V2 preview ids before it checks the history, so check what it will see.
66 const reconciled = reconciledV2PreviewHistory(recorded);
67 const incompatible =
68 reconciled === null ? ["an unexpected V2 preview history"] : skippedV2Migrations(reconciled);
69 if (incompatible.length > 0) {
70 return {
71 status: "blocked",
72 version,
73 reason: `This database has incompatible migration history: ${incompatible.join(", ")}. Start the build that wrote it, or report this migration history so an upgrade path can be added.`,
74 };
75 }
76​
77 return { status: "ready", version, launcherProtocol };
79​
80export function decodeServicePreflightResult(
81 value: unknown,
82 launcherProtocol: SupportedLauncherProtocol,
83): ServicePreflightResult | undefined {
84 if (typeof value !== "object" || value === null) {
85 return undefined;
86 }
87 const record = value as Record<string, unknown>;
88 if (
89 record.status === "ready" &&
90 record.launcherProtocol === launcherProtocol &&
91 typeof record.version === "string"
92 ) {
93 return { status: "ready", version: record.version, launcherProtocol };
94 }
95 if (
96 record.status === "blocked" &&
97 typeof record.version === "string" &&
98 typeof record.reason === "string"
99 ) {
100 return { status: "blocked", version: record.version, reason: record.reason };
101 }
102 return undefined;