Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as FileSystem from "effect/FileSystem";
3import * as Path from "effect/Path";
4import * as PlatformError from "effect/PlatformError";
5import * as Schema from "effect/Schema";
6
7export class DesktopUserDataInitializationError extends Schema.TaggedError<DesktopUserDataInitializationError>()(
8 "DesktopUserDataInitializationError",
9 {
10 operation: Schema.Literals(["inspect", "read", "create-directory", "write"]),
11 resourcePath: Schema.String,
12 category: Schema.String,
13 cause: Schema.Defect(),
14 },
15) {
16 override get message() {
17 return `Could not initialize Electron user data during ${this.operation} at ${this.resourcePath} (${this.category}).`;
18 }
19
20 static fromFileSystem(
21 cause: PlatformError.PlatformError,
22 operation: DesktopUserDataInitializationError["operation"],
23 resourcePath: string,
24 ) {
25 return new DesktopUserDataInitializationError({
26 operation,
27 resourcePath,
28 category: cause.reason._tag,
29 cause,
30 });
31 }
32}
33
34export const resolveUserDataPath = Effect.fn("desktop.userData.resolveUserDataPath")(
35 function* (input: {
36 readonly appDataDirectory: string;
37 readonly isDevelopment: boolean;
38 readonly platform: NodeJS.Platform;
39 }) {
40 const fs = yield* FileSystem.FileSystem;
41 const path = yield* Path.Path;
42 const names = input.isDevelopment
43 ? { current: "t3code-dev", legacy: "T3 Code (Dev)" }
44 : { current: "t3code-v2", legacy: "T3 Code (Alpha)" };
45 const destinationPath = path.join(input.appDataDirectory, names.current);
46 const legacyPath = path.join(input.appDataDirectory, names.legacy);
47 const inspect = (resourcePath: string) =>
48 fs
49 .exists(resourcePath)
50 .pipe(
51 Effect.mapError((cause) =>
52 DesktopUserDataInitializationError.fromFileSystem(cause, "inspect", resourcePath),
53 ),
54 );
55 if (input.isDevelopment) {
56 return (yield* inspect(legacyPath)) ? legacyPath : destinationPath;
57 }
58 // Chromium databases require their own profile for each running version.
59 if (input.platform !== "win32") return destinationPath;
60 const destinationState = path.join(destinationPath, "Local State");
61 if (yield* inspect(destinationState)) return destinationPath;
62 const legacyState = path.join(legacyPath, "Local State");
63 const sourceState = (yield* inspect(legacyState))
64 ? legacyState
65 : path.join(input.appDataDirectory, "t3code", "Local State");
66 if (!(yield* inspect(sourceState))) return destinationPath;
67 // Windows safeStorage keys live here. Copy only these preferences, never locked databases.
68 const state = yield* fs
69 .readFileString(sourceState)
70 .pipe(
71 Effect.mapError((cause) =>
72 DesktopUserDataInitializationError.fromFileSystem(cause, "read", sourceState),
73 ),
74 );
75 yield* fs
76 .makeDirectory(destinationPath, { recursive: true })
77 .pipe(
78 Effect.mapError((cause) =>
79 DesktopUserDataInitializationError.fromFileSystem(
80 cause,
81 "create-directory",
82 destinationPath,
83 ),
84 ),
85 );
86 yield* fs.writeFileString(destinationState, state, { flag: "wx" }).pipe(
87 Effect.catchIf(
88 (error) => error.reason._tag === "AlreadyExists",
89 () => Effect.void,
90 ),
91 Effect.mapError((cause) =>
92 DesktopUserDataInitializationError.fromFileSystem(cause, "write", destinationState),
93 ),
94 );
95 return destinationPath;
96 },
97);
98
99const GPU_CACHE_DIRECTORIES = [
100 "GPUCache",
101 "GrShaderCache",
102 "ShaderCache",
103 "DawnGraphiteCache",
104 "DawnWebGPUCache",
105 "GPUPersistentCache",
106];
107
108// Keyed on Electron, not Chromium: Electron patches ship Chromium backports under the same Chrome version.
109export const clearStaleGpuCaches = Effect.fn("desktop.userData.clearStaleGpuCaches")(
110 function* (input: { readonly userDataPath: string; readonly electronVersion: string }) {
111 const fs = yield* FileSystem.FileSystem;
112 const path = yield* Path.Path;
113 const markerPath = path.join(input.userDataPath, "gpu-cache-electron-version");
114 const cachedVersion = yield* fs.readFileString(markerPath).pipe(
115 Effect.catchIf(
116 (error) => error.reason._tag === "NotFound",
117 () => Effect.undefined,
118 ),
119 );
120 if (cachedVersion?.trim() === input.electronVersion) return;
121 for (const name of GPU_CACHE_DIRECTORIES) {
122 yield* fs.remove(path.join(input.userDataPath, name), { recursive: true, force: true });
123 }
124 yield* fs.makeDirectory(input.userDataPath, { recursive: true });
125 yield* fs.writeFileString(markerPath, input.electronVersion);
126 },
127 (effect, input) =>
128 effect.pipe(
129 Effect.catch((error) =>
130 Effect.logWarning("could not clear stale GPU caches", {
131 userDataPath: input.userDataPath,
132 electronVersion: input.electronVersion,
133 error: error.message,
134 }),
135 ),
136 ),
137);