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

versionSkew.ts

175 lines5.9 KB

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