Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Path from "effect/Path";
7import * as Ref from "effect/Ref";
8
9import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
10import { readChromiumLocalStorage } from "./chromiumLocalStorage.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12
13/**
14 * The V1 profile is only read, never opened by Chromium, so this works while V1
15 * is still running.
16 */
17export class DesktopLegacyLocalStorage extends Context.Service<
18 DesktopLegacyLocalStorage,
19 {
20 readonly load: (userDataPath: string) => Effect.Effect<void>;
21 readonly take: Effect.Effect<Option.Option<Readonly<Record<string, string>>>>;
22 readonly complete: Effect.Effect<void>;
23 }
24>()("@t3tools/desktop/app/DesktopLegacyLocalStorage") {}
25
26const MARKER_FILE_NAME = "v1-local-storage-imported";
27const V1_PROFILE_NAMES = ["T3 Code (Alpha)", "t3code"];
28
29const make = Effect.gen(function* () {
30 const environment = yield* DesktopEnvironment.DesktopEnvironment;
31 const fs = yield* FileSystem.FileSystem;
32 const path = yield* Path.Path;
33 const items = yield* Ref.make(Option.none<Readonly<Record<string, string>>>());
34 const markerPath = yield* Ref.make(Option.none<string>());
35
36 /** Newest file mtime in a directory; appending to a log leaves the directory's own mtime alone. */
37 const newestFileMtime = Effect.fn("DesktopLegacyLocalStorage.newestFileMtime")(
38 function* (directory: string) {
39 let newest = 0;
40 for (const name of yield* fs.readDirectory(directory)) {
41 const info = yield* fs.stat(path.join(directory, name)).pipe(Effect.option);
42 if (Option.isNone(info)) continue;
43 const mtime = Option.match(info.value.mtime, {
44 onNone: () => 0,
45 onSome: (date) => date.getTime(),
46 });
47 newest = Math.max(newest, mtime);
48 }
49 return newest;
50 },
51 (effect, _directory) => effect.pipe(Effect.option),
52 );
53
54 const findV1LocalStorage = Effect.gen(function* () {
55 let newest: { readonly directory: string; readonly mtime: number } | null = null;
56 for (const name of V1_PROFILE_NAMES) {
57 const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb");
58 const mtime = yield* newestFileMtime(directory);
59 if (Option.isNone(mtime)) continue;
60 if (newest === null || mtime.value > newest.mtime) newest = { directory, mtime: mtime.value };
61 }
62 return newest?.directory ?? null;
63 });
64
65 const writeMarker = Effect.gen(function* () {
66 const marker = yield* Ref.get(markerPath);
67 if (Option.isNone(marker)) return;
68 yield* fs
69 .writeFileString(marker.value, "")
70 .pipe(
71 Effect.catch((error) =>
72 Effect.logWarning("Could not record the V1 Local Storage import", error),
73 ),
74 );
75 });
76
77 const load = Effect.fn("desktop.legacyLocalStorage.load")(function* (userDataPath: string) {
78 if (environment.isDevelopment) return;
79 const marker = path.join(userDataPath, MARKER_FILE_NAME);
80 if (yield* fs.exists(marker).pipe(Effect.orElseSucceed(() => true))) return;
81 yield* Ref.set(markerPath, Option.some(marker));
82 const directory = yield* findV1LocalStorage;
83 if (directory === null) return yield* writeMarker;
84 const origin = ElectronProtocol.getDesktopUrl(false).replace(/\/$/, "");
85 const read = yield* readChromiumLocalStorage(directory, origin).pipe(
86 Effect.provideService(FileSystem.FileSystem, fs),
87 Effect.provideService(Path.Path, path),
88 Effect.tapError((error) =>
89 Effect.logWarning("Could not read V1 Local Storage; will retry next launch", error),
90 ),
91 Effect.option,
92 );
93 if (Option.isNone(read)) return;
94 yield* Effect.logInfo("V1 Local Storage ready to import", {
95 directory,
96 keys: read.value.size,
97 });
98 yield* Ref.set(items, Option.some(Object.fromEntries(read.value)));
99 });
100
101 return DesktopLegacyLocalStorage.of({
102 load,
103 take: Ref.getAndSet(items, Option.none()),
104 complete: writeMarker,
105 });
106});
107
108export const layer = Layer.effect(DesktopLegacyLocalStorage, make);