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

DesktopLegacyLocalStorage.ts

106 lines4.1 KB

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 = (directory: string) =>
38 Effect.gen(function* () {
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 }).pipe(Effect.option);
51​
52 const findV1LocalStorage = Effect.gen(function* () {
53 let newest: { readonly directory: string; readonly mtime: number } | null = null;
54 for (const name of V1_PROFILE_NAMES) {
55 const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb");
56 const mtime = yield* newestFileMtime(directory);
57 if (Option.isNone(mtime)) continue;
58 if (newest === null || mtime.value > newest.mtime) newest = { directory, mtime: mtime.value };
59 }
60 return newest?.directory ?? null;
61 });
62​
63 const writeMarker = Effect.gen(function* () {
64 const marker = yield* Ref.get(markerPath);
65 if (Option.isNone(marker)) return;
66 yield* fs
67 .writeFileString(marker.value, "")
68 .pipe(
69 Effect.catch((error) =>
70 Effect.logWarning("Could not record the V1 Local Storage import", error),
71 ),
72 );
73 });
74​
75 const load = Effect.fn("desktop.legacyLocalStorage.load")(function* (userDataPath: string) {
76 if (environment.isDevelopment) return;
77 const marker = path.join(userDataPath, MARKER_FILE_NAME);
78 if (yield* fs.exists(marker).pipe(Effect.orElseSucceed(() => true))) return;
79 yield* Ref.set(markerPath, Option.some(marker));
80 const directory = yield* findV1LocalStorage;
81 if (directory === null) return yield* writeMarker;
82 const origin = ElectronProtocol.getDesktopUrl(false).replace(/\/$/, "");
83 const read = yield* readChromiumLocalStorage(directory, origin).pipe(
84 Effect.provideService(FileSystem.FileSystem, fs),
85 Effect.provideService(Path.Path, path),
86 Effect.tapError((error) =>
87 Effect.logWarning("Could not read V1 Local Storage; will retry next launch", error),
88 ),
89 Effect.option,
90 );
91 if (Option.isNone(read)) return;
92 yield* Effect.logInfo("V1 Local Storage ready to import", {
93 directory,
94 keys: read.value.size,
95 });
96 yield* Ref.set(items, Option.some(Object.fromEntries(read.value)));
97 });
98​
99 return DesktopLegacyLocalStorage.of({
100 load,
101 take: Ref.getAndSet(items, Option.none()),
102 complete: writeMarker,
103 });
104});
105​
106export const layer = Layer.effect(DesktopLegacyLocalStorage, make);