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

versionSkew.ts

188 lines6.5 KB

1import type { EnvironmentId, ServerConfig, ServerSelfUpdateCapability } from "@t3tools/contracts";
2import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
3import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
4import * as Schema from "effect/Schema";
5​
6import { APP_VERSION } from "./branding";
7import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
8​
9export interface VersionMismatch {
10 readonly clientVersion: string;
11 readonly serverVersion: string;
12 readonly hint: string;
14​
15export const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
16​
17// Runtime failures retain their identity until the next attempt. Dismiss only
18// that attempt, across chat remounts, without clearing the error in Settings.
19const dismissedServerUpdateFailures = new WeakSet<ServerUpdateState>();
20​
21export function isServerUpdateFailureDismissed(state: ServerUpdateState): boolean {
22 return state.status === "failed" && dismissedServerUpdateFailures.has(state);
24​
25export function dismissServerUpdateFailure(state: ServerUpdateState): void {
26 if (state.status === "failed") dismissedServerUpdateFailures.add(state);
28​
29const VersionMismatchDismissalsSchema = Schema.Struct({
30 keys: Schema.Array(Schema.String),
31});
32​
33type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
34​
35function normalizeVersion(version: string | null | undefined): string | null {
36 const trimmed = version?.trim();
37 return trimmed && trimmed.length > 0 ? trimmed : null;
39​
40/** Core `major.minor.patch`, dropping any prerelease or build suffix. */
41function versionCore(version: string): string {
42 return version.replace(/[-+].*$/, "");
44​
45/**
46 * The skew a user can act on: the connected server runs an older T3 Code than
47 * this client, so the server is the side that needs updating.
48 *
49 * Two nightly builds compare their full versions, including the date and run.
50 * Other combinations compare their core `major.minor.patch` only, so a stable
51 * build and a nightly build with the same core do not cause an update warning.
52 * A server ahead of the client does not need an update. Versions that do not
53 * parse as semver fall back to plain string inequality.
54 */
55export function resolveVersionMismatch(
56 serverVersion: string | null | undefined,
57): VersionMismatch | null {
58 const normalizedClientVersion = normalizeVersion(APP_VERSION);
59 const normalizedServerVersion = normalizeVersion(serverVersion);
60 if (!normalizedClientVersion || !normalizedServerVersion) {
61 return null;
62 }
63​
64 const clientCore = versionCore(normalizedClientVersion);
65 const serverCore = versionCore(normalizedServerVersion);
66 const compareNightlyBuilds =
67 parseSemver(normalizedClientVersion)?.prerelease[0] === "nightly" &&
68 parseSemver(normalizedServerVersion)?.prerelease[0] === "nightly";
69 const serverIsBehind =
70 parseSemver(clientCore) && parseSemver(serverCore)
71 ? compareSemverVersions(
72 compareNightlyBuilds ? normalizedServerVersion : serverCore,
73 compareNightlyBuilds ? normalizedClientVersion : clientCore,
74 ) < 0
75 : normalizedServerVersion !== normalizedClientVersion;
76 if (!serverIsBehind) {
77 return null;
78 }
79​
80 return {
81 clientVersion: normalizedClientVersion,
82 serverVersion: normalizedServerVersion,
83 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
84 };
86​
87export function resolveServerConfigVersionMismatch(
88 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
89): VersionMismatch | null {
90 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
92​
93/** The update path the connected server offers, or null when it only
94 supports a manual relaunch (older servers, dev checkouts, Windows). */
95export function resolveServerSelfUpdateCapability(
96 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
97): ServerSelfUpdateCapability | null {
98 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
100​
101/** The command to hand users whose server cannot update itself. */
102export function manualServerUpdateCommand(targetVersion: string): string {
103 return `npx t3@${targetVersion}`;
105​
106/** One sentence telling the user how to resolve version skew for a server,
107 matched to the update path it offers. */
108export function serverUpdateGuidance(
109 capability: ServerSelfUpdateCapability | null,
110 serverLabel: string,
111): string {
112 switch (capability) {
113 case "boot-service":
114 case "respawn":
115 return `Update the ${serverLabel} so they stay in sync.`;
116 case "desktop-managed":
117 return `Update the desktop app that runs the ${serverLabel}.`;
118 default:
119 return `Relaunch the ${serverLabel} with the copied command to sync them.`;
120 }
122​
123export function buildVersionMismatchDismissalKey(
124 environmentId: EnvironmentId,
125 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
126): string {
127 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
129​
130function readVersionMismatchDismissals(): VersionMismatchDismissals {
131 try {
132 return (
133 getLocalStorageItem(
134 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
135 VersionMismatchDismissalsSchema,
136 ) ?? { keys: [] }
137 );
138 } catch (error) {
139 console.error("Could not read version-mismatch dismissals.", error);
140 return { keys: [] };
141 }
143​
144function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
145 try {
146 setLocalStorageItem(
147 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
148 document,
149 VersionMismatchDismissalsSchema,
150 );
151 } catch (error) {
152 console.error("Could not persist version-mismatch dismissals.", error);
153 }
155​
156export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
157 if (!dismissalKey) {
158 return false;
159 }
160 return readVersionMismatchDismissals().keys.includes(dismissalKey);
162​
163export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
164 if (!dismissalKey) {
165 return;
166 }
167 const document = readVersionMismatchDismissals();
168 if (document.keys.includes(dismissalKey)) {
169 return;
170 }
171 writeVersionMismatchDismissals({
172 keys: [...document.keys, dismissalKey],
173 });
175​
176export function appendVersionMismatchHint(
177 message: string | null | undefined,
178 mismatch: VersionMismatch | null | undefined,
179): string | null {
180 const normalizedMessage = normalizeVersion(message);
181 if (!normalizedMessage) {
182 return mismatch?.hint ?? null;
183 }
184 if (!mismatch) {
185 return normalizedMessage;
186 }
187 return `${normalizedMessage} Hint: ${mismatch.hint}`;