fix(desktop): V2 imports stashed prompts and drafts from the V1 profile #15072
Mergedjuliusmarmingemerged 5 commits into
main fromrefs/pull/15072/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/desktop/src/app/DesktopLegacyLocalStorage.ts line 49OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +29,21 @@29+ readonly complete: Effect.Effect<void>;30+ }31+>()("@t3tools/desktop/app/DesktopLegacyLocalStorage") {}32+33+const MARKER_FILE_NAME = "v1-local-storage-imported";34+// V1 used "T3 Code (Alpha)" when that folder existed and "t3code" otherwise.35+const V1_PROFILE_NAMES = ["T3 Code (Alpha)", "t3code"];36+37+export const make = Effect.gen(function* () {38+ const environment = yield* DesktopEnvironment.DesktopEnvironment;39+ const fs = yield* FileSystem.FileSystem;40+ const path = yield* Path.Path;41+ const items = yield* Ref.make(Option.none<Readonly<Record<string, string>>>());42+ const markerPath = yield* Ref.make(Option.none<string>());43+44+ /** The V1 profile whose Local Storage was written most recently, if any. */45+ const findV1LocalStorage = Effect.gen(function* () {46+ let newest: { readonly directory: string; readonly mtime: number } | null = null;47+ for (const name of V1_PROFILE_NAMES) {48+ const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb");49+ const info = yield* fs.stat(directory).pipe(Effect.option);apps/desktop/src/ipc/methods/legacyLocalStorage.ts line 28Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +4,21 @@4+5+import * as DesktopLegacyLocalStorage from "../../app/DesktopLegacyLocalStorage.ts";6+import * as IpcChannels from "../channels.ts";7+import { makeIpcMethod, makeSyncIpcMethod } from "../DesktopIpc.ts";8+9+export const takeLegacyLocalStorage = makeSyncIpcMethod({10+ channel: IpcChannels.TAKE_LEGACY_LOCAL_STORAGE_CHANNEL,11+ result: Schema.NullOr(Schema.Record(Schema.String, Schema.String)),12+ handler: Effect.fn("desktop.ipc.legacyLocalStorage.take")(function* () {13+ const legacy = yield* DesktopLegacyLocalStorage.DesktopLegacyLocalStorage;14+ return Option.getOrNull(yield* legacy.pending);15+ }),16+});17+18+export const completeLegacyLocalStorage = makeIpcMethod({19+ channel: IpcChannels.COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL,20+ payload: Schema.Void,21+ result: Schema.Void,22+ handler: Effect.fn("desktop.ipc.legacyLocalStorage.complete")(function* () {23+ const legacy = yield* DesktopLegacyLocalStorage.DesktopLegacyLocalStorage;24+ yield* legacy.complete;@@ -0,0 +150,21 @@150+/**151+ * Splits a LevelDB log (write-ahead log or MANIFEST) into its records. A torn152+ * or corrupt record ends the log, the same point LevelDB's own recovery stops.153+ */154+export function readLogRecords(bytes: Uint8Array): Uint8Array[] {155+ const records: Uint8Array[] = [];156+ let pending: Uint8Array[] | null = null;157+ let offset = 0;158+ while (offset + LOG_HEADER_SIZE <= bytes.length) {159+ const blockRemaining = LOG_BLOCK_SIZE - (offset % LOG_BLOCK_SIZE);160+ if (blockRemaining < LOG_HEADER_SIZE) {161+ offset += blockRemaining;162+ continue;163+ }164+ const length = bytes[offset + 4]! | (bytes[offset + 5]! << 8);165+ const type = bytes[offset + 6]!;166+ const end = offset + LOG_HEADER_SIZE + length;167+ // Type 0 is preallocated zero padding: nothing was written past here.168+ if (type === 0 || end > bytes.length || LOG_HEADER_SIZE + length > blockRemaining) break;169+ const typeAndPayload = bytes.subarray(offset + 6, end);170+ if (crc32c(typeAndPayload) !== unmaskCrc(readUint32(bytes, offset))) break;
juliusmarminge added labelsmacroscope-review
- apps/desktop/src/app/chromiumLocalStorage.ts line 105OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +85,21 @@85+ for (let index = 0; index < 256; index++) {86+ let crc = index;87+ for (let bit = 0; bit < 8; bit++) crc = crc & 1 ? (crc >>> 1) ^ 0x82f63b78 : crc >>> 1;88+ table[index] = crc >>> 0;89+ }90+ return table;91+})();92+93+function crc32c(bytes: Uint8Array): number {94+ let crc = 0xffffffff;95+ for (const byte of bytes) crc = CRC32C_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8);96+ return (crc ^ 0xffffffff) >>> 0;97+}98+99+/** LevelDB stores CRCs masked so a CRC of data containing CRCs stays well distributed. */100+const unmaskCrc = (masked: number) => {101+ const rotated = (masked - 0xa282ead8) >>> 0;102+ return ((rotated >>> 17) | (rotated << 15)) >>> 0;103+};104+105+export function decompressSnappy(input: Uint8Array): Uint8Array {apps/desktop/src/app/chromiumLocalStorage.ts lines 151 to 207Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +176,21 @@176+ } else if (type === 2) {177+ pending = [payload];178+ } else if (type === 3 && pending) {179+ pending.push(payload);180+ } else if (type === 4 && pending) {181+ pending.push(payload);182+ const total = pending.reduce((sum, part) => sum + part.length, 0);183+ const record = new Uint8Array(total);184+ let position = 0;185+ for (const part of pending) {186+ record.set(part, position);187+ position += part.length;188+ }189+ records.push(record);190+ pending = null;191+ } else {192+ pending = null;193+ }194+ }195+ return records;196+}apps/desktop/src/app/DesktopLegacyLocalStorage.ts lines 37 to 58OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +38,21 @@38+ const environment = yield* DesktopEnvironment.DesktopEnvironment;39+ const fs = yield* FileSystem.FileSystem;40+ const path = yield* Path.Path;41+ const items = yield* Ref.make(Option.none<Readonly<Record<string, string>>>());42+ const markerPath = yield* Ref.make(Option.none<string>());43+44+ /** The V1 profile whose Local Storage was written most recently, if any. */45+ const findV1LocalStorage = Effect.gen(function* () {46+ let newest: { readonly directory: string; readonly mtime: number } | null = null;47+ for (const name of V1_PROFILE_NAMES) {48+ const directory = path.join(environment.appDataDirectory, name, "Local Storage", "leveldb");49+ const info = yield* fs.stat(directory).pipe(Effect.option);50+ if (Option.isNone(info) || info.value.type !== "Directory") continue;51+ const mtime = Option.match(info.value.mtime, {52+ onNone: () => 0,53+ onSome: (date) => date.getTime(),54+ });55+ if (newest === null || mtime > newest.mtime) newest = { directory, mtime };56+ }57+ return newest?.directory ?? null;58+ });apps/desktop/src/preload.ts lines 34 to 45Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -30,3 +31,14 @@30313132 exposeClerkBridge({ passkeys: true });323334+// Runs before any app script reads localStorage. See DesktopLegacyLocalStorage.35+try {36+ const legacyItems: unknown = ipcRenderer.sendSync(IpcChannels.TAKE_LEGACY_LOCAL_STORAGE_CHANNEL);37+ if (typeof legacyItems === "object" && legacyItems !== null) {38+ mergeLegacyLocalStorage(window.localStorage, legacyItems as Record<string, string>);39+ void ipcRenderer.invoke(IpcChannels.COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL);40+ }41+} catch {42+ // Best effort: the app still starts on the V2 profile's own storage.43+}44+
- apps/desktop/src/app/chromiumLocalStorage.ts line 259OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +239,21 @@239+ reader.varint();240+ tables.delete(reader.varint());241+ break;242+ case 7: {243+ reader.varint();244+ tables.add(reader.varint());245+ reader.varint();246+ reader.lengthPrefixed();247+ reader.lengthPrefixed();248+ break;249+ }250+ case 9:251+ previousLogNumber = reader.varint();252+ break;253+ default:254+ fail(`unknown manifest tag ${tag}`);255+ }256+ }257+ }258+ const minimumLogNumber =259+ previousLogNumber > 0 ? Math.min(logNumber, previousLogNumber) : logNumber;apps/desktop/src/app/chromiumLocalStorage.ts line 258OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +238,21 @@238+ case 6: // deleted file239+ reader.varint();240+ tables.delete(reader.varint());241+ break;242+ case 7: {243+ reader.varint();244+ tables.add(reader.varint());245+ reader.varint();246+ reader.lengthPrefixed();247+ reader.lengthPrefixed();248+ break;249+ }250+ case 9:251+ previousLogNumber = reader.varint();252+ break;253+ default:254+ fail(`unknown manifest tag ${tag}`);255+ }256+ }257+ }258+ const minimumLogNumber =
juliusmarminge merged thisc408737
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed