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

versionSkew.ts

186 lines6.0 KB

1import type {
2 EnvironmentId,
3 ServerConfig,
4 ServerInstallation,
5 ServerSelfUpdateCapability,
6} from "@t3tools/contracts";
7import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
8import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
9import * as Schema from "effect/Schema";
10​
11import { APP_VERSION } from "./branding";
12import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
13​
14export interface VersionMismatch {
15 readonly clientVersion: string;
16 readonly serverVersion: string;
17 readonly hint: string;
19​
20const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
21​
22const dismissedServerUpdateFailures = new WeakSet<ServerUpdateState>();
23​
24export function isServerUpdateFailureDismissed(state: ServerUpdateState): boolean {
25 return state.status === "failed" && dismissedServerUpdateFailures.has(state);
27​
28export function dismissServerUpdateFailure(state: ServerUpdateState): void {
29 if (state.status === "failed") dismissedServerUpdateFailures.add(state);
31​
32const VersionMismatchDismissalsSchema = Schema.Struct({
33 keys: Schema.Array(Schema.String),
34});
35​
36type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
37​
38function normalizeVersion(version: string | null | undefined): string | null {
39 const trimmed = version?.trim();
40 return trimmed && trimmed.length > 0 ? trimmed : null;
42​
43function versionCore(version: string): string {
44 return version.replace(/[-+].*$/, "");
46​
47export function resolveVersionMismatch(
48 serverVersion: string | null | undefined,
49 clientVersion: string = APP_VERSION,
50): VersionMismatch | null {
51 const normalizedClientVersion = normalizeVersion(clientVersion);
52 const normalizedServerVersion = normalizeVersion(serverVersion);
53 if (!normalizedClientVersion || !normalizedServerVersion) {
54 return null;
55 }
56​
57 const clientCore = versionCore(normalizedClientVersion);
58 const serverCore = versionCore(normalizedServerVersion);
59 const compareNightlyBuilds =
60 parseSemver(normalizedClientVersion)?.prerelease[0] === "nightly" &&
61 parseSemver(normalizedServerVersion)?.prerelease[0] === "nightly";
62 const serverIsBehind =
63 parseSemver(clientCore) && parseSemver(serverCore)
64 ? compareSemverVersions(
65 compareNightlyBuilds ? normalizedServerVersion : serverCore,
66 compareNightlyBuilds ? normalizedClientVersion : clientCore,
67 ) < 0
68 : normalizedServerVersion !== normalizedClientVersion;
69 if (!serverIsBehind) {
70 return null;
71 }
72​
73 return {
74 clientVersion: normalizedClientVersion,
75 serverVersion: normalizedServerVersion,
76 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
77 };
79​
80export function resolveServerConfigVersionMismatch(
81 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
82): VersionMismatch | null {
83 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
85​
86export function resolveServerSelfUpdateCapability(
87 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
88): ServerSelfUpdateCapability | null {
89 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
91​
92export function supportsDesktopAppUpdate(
93 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
94): boolean {
95 return serverConfig?.environment.capabilities.desktopAppUpdate === true;
97​
98export function supportsServerUpdateThreadContinuation(
99 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
100): boolean {
101 return serverConfig?.environment.capabilities.serverUpdateThreadContinuation === true;
103​
104export function manualServerUpdateCommand(
105 targetVersion: string,
106 installation?: ServerInstallation,
107): string {
108 if (installation?.kind === "npm-global") {
109 const prefix = `'${installation.prefix.replaceAll("'", "'\\''")}'`;
110 return `npm install --global --prefix ${prefix} t3@${targetVersion}`;
111 }
112 const runner =
113 installation?.kind === "pnpm-dlx" ? "pnpm dlx" : installation?.kind === "bunx" ? "bunx" : "npx";
114 return `${runner} t3@${targetVersion}`;
116​
117export function serverUpdateGuidance(capability: ServerSelfUpdateCapability): string {
118 return capability === "desktop-managed" ? "Update the desktop app" : "Update to stay in sync";
120​
121export function buildVersionMismatchDismissalKey(
122 environmentId: EnvironmentId,
123 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
124): string {
125 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
127​
128function readVersionMismatchDismissals(): VersionMismatchDismissals {
129 try {
130 return (
131 getLocalStorageItem(
132 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
133 VersionMismatchDismissalsSchema,
134 ) ?? { keys: [] }
135 );
136 } catch (error) {
137 console.error("Could not read version-mismatch dismissals.", error);
138 return { keys: [] };
139 }
141​
142function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
143 try {
144 setLocalStorageItem(
145 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
146 document,
147 VersionMismatchDismissalsSchema,
148 );
149 } catch (error) {
150 console.error("Could not persist version-mismatch dismissals.", error);
151 }
153​
154export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
155 if (!dismissalKey) {
156 return false;
157 }
158 return readVersionMismatchDismissals().keys.includes(dismissalKey);
160​
161export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
162 if (!dismissalKey) {
163 return;
164 }
165 const document = readVersionMismatchDismissals();
166 if (document.keys.includes(dismissalKey)) {
167 return;
168 }
169 writeVersionMismatchDismissals({
170 keys: [...document.keys, dismissalKey],
171 });
173​
174export function appendVersionMismatchHint(
175 message: string | null | undefined,
176 mismatch: VersionMismatch | null | undefined,
177): string | null {
178 const normalizedMessage = normalizeVersion(message);
179 if (!normalizedMessage) {
180 return mismatch?.hint ?? null;
181 }
182 if (!mismatch) {
183 return normalizedMessage;
184 }
185 return `${normalizedMessage} Hint: ${mismatch.hint}`;