The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Path from "effect/Path"; |
| 4 | import * as PlatformError from "effect/PlatformError"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | /** Read without LevelDB's lock so another app can keep the profile open. */ |
| 8 | |
| 9 | const LOG_BLOCK_SIZE = 32 * 1024; |
| 10 | const LOG_HEADER_SIZE = 7; |
| 11 | const TABLE_FOOTER_SIZE = 48; |
| 12 | const BLOCK_TRAILER_SIZE = 5; |
| 13 | |
| 14 | class LevelDbFormatError extends Schema.TaggedError<LevelDbFormatError>()("LevelDbFormatError", { |
| 15 | detail: Schema.String, |
| 16 | }) {} |
| 17 | |
| 18 | const isLevelDbFormatError = Schema.is(LevelDbFormatError); |
| 19 | |
| 20 | const fail = (detail: string): never => { |
| 21 | throw new LevelDbFormatError({ detail }); |
| 22 | }; |
| 23 | |
| 24 | // ignoreBOM keeps a leading U+FEFF that is part of the stored string. |
| 25 | const textDecoderUtf16 = new TextDecoder("utf-16le", { ignoreBOM: true }); |
| 26 | const textDecoderUtf8 = new TextDecoder(); |
| 27 | |
| 28 | /** True ISO-8859-1. WHATWG's "latin1" label is windows-1252, which remaps 0x80-0x9F. */ |
| 29 | function decodeLatin1(bytes: Uint8Array): string { |
| 30 | let result = ""; |
| 31 | for (let offset = 0; offset < bytes.length; offset += 0x8000) { |
| 32 | result += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000)); |
| 33 | } |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | class Reader { |
| 38 | readonly bytes: Uint8Array; |
| 39 | offset: number; |
| 40 | constructor(bytes: Uint8Array, offset = 0) { |
| 41 | this.bytes = bytes; |
| 42 | this.offset = offset; |
| 43 | } |
| 44 | get done() { |
| 45 | return this.offset >= this.bytes.length; |
| 46 | } |
| 47 | byte(): number { |
| 48 | if (this.offset >= this.bytes.length) fail("unexpected end of data"); |
| 49 | return this.bytes[this.offset++]!; |
| 50 | } |
| 51 | varint(): number { |
| 52 | let result = 0; |
| 53 | let multiplier = 1; |
| 54 | for (let shift = 0; shift < 64; shift += 7) { |
| 55 | const byte = this.byte(); |
| 56 | result += (byte & 0x7f) * multiplier; |
| 57 | if ((byte & 0x80) === 0) return result; |
| 58 | multiplier *= 128; |
| 59 | } |
| 60 | return fail("varint too long"); |
| 61 | } |
| 62 | take(length: number): Uint8Array { |
| 63 | if (length < 0 || this.offset + length > this.bytes.length) fail("length out of range"); |
| 64 | const slice = this.bytes.subarray(this.offset, this.offset + length); |
| 65 | this.offset += length; |
| 66 | return slice; |
| 67 | } |
| 68 | lengthPrefixed(): Uint8Array { |
| 69 | return this.take(this.varint()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const readUint32 = (bytes: Uint8Array, offset: number) => |
| 74 | (bytes[offset]! | (bytes[offset + 1]! << 8) | (bytes[offset + 2]! << 16)) + |
| 75 | bytes[offset + 3]! * 0x1000000; |
| 76 | |
| 77 | const CRC32C_TABLE = (() => { |
| 78 | const table = new Uint32Array(256); |
| 79 | for (let index = 0; index < 256; index++) { |
| 80 | let crc = index; |
| 81 | for (let bit = 0; bit < 8; bit++) crc = crc & 1 ? (crc >>> 1) ^ 0x82f63b78 : crc >>> 1; |
| 82 | table[index] = crc >>> 0; |
| 83 | } |
| 84 | return table; |
| 85 | })(); |
| 86 | |
| 87 | function crc32c(bytes: Uint8Array): number { |
| 88 | let crc = 0xffffffff; |
| 89 | for (const byte of bytes) crc = CRC32C_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8); |
| 90 | return (crc ^ 0xffffffff) >>> 0; |
| 91 | } |
| 92 | |
| 93 | const unmaskCrc = (masked: number) => { |
| 94 | const rotated = (masked - 0xa282ead8) >>> 0; |
| 95 | return ((rotated >>> 17) | (rotated << 15)) >>> 0; |
| 96 | }; |
| 97 | |
| 98 | function decompressSnappy(input: Uint8Array): Uint8Array { |
| 99 | const reader = new Reader(input); |
| 100 | const output = new Uint8Array(reader.varint()); |
| 101 | let written = 0; |
| 102 | while (!reader.done) { |
| 103 | const tag = reader.byte(); |
| 104 | const kind = tag & 3; |
| 105 | if (kind === 0) { |
| 106 | let length = tag >>> 2; |
| 107 | if (length >= 60) { |
| 108 | const extraBytes = length - 59; |
| 109 | length = 0; |
| 110 | for (let index = 0; index < extraBytes; index++) length += reader.byte() * 256 ** index; |
| 111 | } |
| 112 | length += 1; |
| 113 | if (written + length > output.length) fail("snappy literal overflows output"); |
| 114 | output.set(reader.take(length), written); |
| 115 | written += length; |
| 116 | continue; |
| 117 | } |
| 118 | let length: number; |
| 119 | let offset: number; |
| 120 | if (kind === 1) { |
| 121 | length = 4 + ((tag >>> 2) & 7); |
| 122 | offset = ((tag >>> 5) << 8) | reader.byte(); |
| 123 | } else if (kind === 2) { |
| 124 | length = (tag >>> 2) + 1; |
| 125 | offset = reader.byte() | (reader.byte() << 8); |
| 126 | } else { |
| 127 | length = (tag >>> 2) + 1; |
| 128 | offset = readUint32(reader.take(4), 0); |
| 129 | } |
| 130 | if (offset === 0 || offset > written || written + length > output.length) { |
| 131 | fail("snappy copy out of range"); |
| 132 | } |
| 133 | // Copies may overlap their own output, so they go byte by byte. |
| 134 | for (let index = 0; index < length; index++) { |
| 135 | output[written] = output[written - offset]!; |
| 136 | written++; |
| 137 | } |
| 138 | } |
| 139 | if (written !== output.length) fail("snappy output length mismatch"); |
| 140 | return output; |
| 141 | } |
| 142 | |
| 143 | /** Corruption skips the remaining block; a truncated final record ends the log, matching LevelDB recovery. */ |
| 144 | function readLogRecords(bytes: Uint8Array): Uint8Array[] { |
| 145 | const records: Uint8Array[] = []; |
| 146 | let pending: Uint8Array[] | null = null; |
| 147 | let offset = 0; |
| 148 | while (offset + LOG_HEADER_SIZE <= bytes.length) { |
| 149 | const blockRemaining = LOG_BLOCK_SIZE - (offset % LOG_BLOCK_SIZE); |
| 150 | if (blockRemaining < LOG_HEADER_SIZE) { |
| 151 | offset += blockRemaining; |
| 152 | continue; |
| 153 | } |
| 154 | const length = bytes[offset + 4]! | (bytes[offset + 5]! << 8); |
| 155 | const type = bytes[offset + 6]!; |
| 156 | const end = offset + LOG_HEADER_SIZE + length; |
| 157 | const fitsBlock = LOG_HEADER_SIZE + length <= blockRemaining; |
| 158 | if (fitsBlock && end > bytes.length) break; |
| 159 | const typeAndPayload = bytes.subarray(offset + 6, end); |
| 160 | if ( |
| 161 | type === 0 || |
| 162 | !fitsBlock || |
| 163 | crc32c(typeAndPayload) !== unmaskCrc(readUint32(bytes, offset)) |
| 164 | ) { |
| 165 | offset += blockRemaining; |
| 166 | pending = null; |
| 167 | continue; |
| 168 | } |
| 169 | const payload = typeAndPayload.subarray(1); |
| 170 | offset = end; |
| 171 | if (type === 1) { |
| 172 | records.push(payload); |
| 173 | pending = null; |
| 174 | } else if (type === 2) { |
| 175 | pending = [payload]; |
| 176 | } else if (type === 3 && pending) { |
| 177 | pending.push(payload); |
| 178 | } else if (type === 4 && pending) { |
| 179 | pending.push(payload); |
| 180 | const total = pending.reduce((sum, part) => sum + part.length, 0); |
| 181 | const record = new Uint8Array(total); |
| 182 | let position = 0; |
| 183 | for (const part of pending) { |
| 184 | record.set(part, position); |
| 185 | position += part.length; |
| 186 | } |
| 187 | records.push(record); |
| 188 | pending = null; |
| 189 | } else { |
| 190 | pending = null; |
| 191 | } |
| 192 | } |
| 193 | return records; |
| 194 | } |
| 195 | |
| 196 | interface LiveFiles { |
| 197 | readonly tables: ReadonlyArray<number>; |
| 198 | readonly logNumber: number; |
| 199 | readonly previousLogNumber: number; |
| 200 | } |
| 201 | |
| 202 | function readManifest(bytes: Uint8Array): LiveFiles { |
| 203 | const tables = new Set<number>(); |
| 204 | let logNumber = 0; |
| 205 | let previousLogNumber = 0; |
| 206 | // LevelDB refuses to open a MANIFEST missing any of these, and so do we. |
| 207 | const required = new Set([2, 3, 4]); |
| 208 | for (const record of readLogRecords(bytes)) { |
| 209 | const reader = new Reader(record); |
| 210 | while (!reader.done) { |
| 211 | const tag = reader.varint(); |
| 212 | required.delete(tag); |
| 213 | switch (tag) { |
| 214 | case 1: |
| 215 | reader.lengthPrefixed(); |
| 216 | break; |
| 217 | case 2: |
| 218 | logNumber = reader.varint(); |
| 219 | break; |
| 220 | case 3: |
| 221 | case 4: |
| 222 | reader.varint(); |
| 223 | break; |
| 224 | case 5: |
| 225 | reader.varint(); |
| 226 | reader.lengthPrefixed(); |
| 227 | break; |
| 228 | case 6: |
| 229 | reader.varint(); |
| 230 | tables.delete(reader.varint()); |
| 231 | break; |
| 232 | case 7: { |
| 233 | reader.varint(); |
| 234 | tables.add(reader.varint()); |
| 235 | reader.varint(); |
| 236 | reader.lengthPrefixed(); |
| 237 | reader.lengthPrefixed(); |
| 238 | break; |
| 239 | } |
| 240 | case 9: |
| 241 | previousLogNumber = reader.varint(); |
| 242 | break; |
| 243 | default: |
| 244 | fail(`unknown manifest tag ${tag}`); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | if (required.size > 0) fail("incomplete manifest"); |
| 249 | return { tables: [...tables], logNumber, previousLogNumber }; |
| 250 | } |
| 251 | |
| 252 | interface VersionedEntry { |
| 253 | readonly sequence: number; |
| 254 | readonly value: Uint8Array | null; |
| 255 | } |
| 256 | |
| 257 | type EntryMap = Map<string, { readonly key: Uint8Array; readonly entry: VersionedEntry }>; |
| 258 | |
| 259 | const keyId = decodeLatin1; |
| 260 | |
| 261 | function record(entries: EntryMap, key: Uint8Array, entry: VersionedEntry) { |
| 262 | const id = keyId(key); |
| 263 | const existing = entries.get(id); |
| 264 | if (!existing || existing.entry.sequence < entry.sequence) entries.set(id, { key, entry }); |
| 265 | } |
| 266 | |
| 267 | function readWriteBatch(entries: EntryMap, batch: Uint8Array) { |
| 268 | if (batch.length < 12) fail("write batch too short"); |
| 269 | const sequence = readUint32(batch, 0) + readUint32(batch, 4) * 0x100000000; |
| 270 | const count = readUint32(batch, 8); |
| 271 | const reader = new Reader(batch, 12); |
| 272 | for (let index = 0; index < count; index++) { |
| 273 | const type = reader.byte(); |
| 274 | const key = reader.lengthPrefixed(); |
| 275 | if (type === 1) { |
| 276 | record(entries, key, { sequence: sequence + index, value: reader.lengthPrefixed() }); |
| 277 | } else if (type === 0) { |
| 278 | record(entries, key, { sequence: sequence + index, value: null }); |
| 279 | } else { |
| 280 | fail(`unknown write batch record type ${type}`); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | function readBlock(table: Uint8Array, reader: Reader): Uint8Array { |
| 286 | const offset = reader.varint(); |
| 287 | const size = reader.varint(); |
| 288 | if (offset + size + BLOCK_TRAILER_SIZE > table.length) fail("block handle out of range"); |
| 289 | const contents = table.subarray(offset, offset + size); |
| 290 | const compression = table[offset + size]!; |
| 291 | const checksum = unmaskCrc(readUint32(table, offset + size + 1)); |
| 292 | if (crc32c(table.subarray(offset, offset + size + 1)) !== checksum) fail("block checksum"); |
| 293 | if (compression === 0) return contents; |
| 294 | if (compression === 1) return decompressSnappy(contents); |
| 295 | return fail(`unsupported block compression ${compression}`); |
| 296 | } |
| 297 | |
| 298 | function* blockEntries(block: Uint8Array): Generator<[Uint8Array, Uint8Array]> { |
| 299 | if (block.length < 4) fail("block too short"); |
| 300 | const restartCount = readUint32(block, block.length - 4); |
| 301 | const limit = block.length - 4 - restartCount * 4; |
| 302 | if (limit < 0) fail("restart array out of range"); |
| 303 | const reader = new Reader(block.subarray(0, limit)); |
| 304 | let key = new Uint8Array(0); |
| 305 | while (!reader.done) { |
| 306 | const shared = reader.varint(); |
| 307 | const unshared = reader.varint(); |
| 308 | const valueLength = reader.varint(); |
| 309 | if (shared > key.length) fail("block key prefix out of range"); |
| 310 | const nextKey = new Uint8Array(shared + unshared); |
| 311 | nextKey.set(key.subarray(0, shared)); |
| 312 | nextKey.set(reader.take(unshared), shared); |
| 313 | key = nextKey; |
| 314 | yield [key, reader.take(valueLength)]; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | function readTable(entries: EntryMap, table: Uint8Array) { |
| 319 | if (table.length < TABLE_FOOTER_SIZE) fail("table too short"); |
| 320 | const footer = new Reader(table, table.length - TABLE_FOOTER_SIZE); |
| 321 | footer.varint(); |
| 322 | footer.varint(); |
| 323 | const index = readBlock(table, footer); |
| 324 | for (const [, handle] of blockEntries(index)) { |
| 325 | const block = readBlock(table, new Reader(handle)); |
| 326 | for (const [internalKey, value] of blockEntries(block)) { |
| 327 | if (internalKey.length < 8) fail("internal key too short"); |
| 328 | const trailerOffset = internalKey.length - 8; |
| 329 | const trailer = |
| 330 | readUint32(internalKey, trailerOffset) + |
| 331 | readUint32(internalKey, trailerOffset + 4) * 0x100000000; |
| 332 | const type = trailer % 256; |
| 333 | const sequence = Math.floor(trailer / 256); |
| 334 | const key = internalKey.subarray(0, trailerOffset); |
| 335 | record(entries, key, { sequence, value: type === 1 ? value : null }); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** Chromium prefixes each stored string with 0 (UTF-16LE) or 1 (Latin-1). */ |
| 341 | function decodeChromiumString(bytes: Uint8Array): string | null { |
| 342 | if (bytes.length === 0) return null; |
| 343 | const body = bytes.subarray(1); |
| 344 | if (bytes[0] === 1) return decodeLatin1(body); |
| 345 | if (bytes[0] === 0 && body.length % 2 === 0) return textDecoderUtf16.decode(body); |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | export class ChromiumLocalStorageReadError extends Schema.TaggedError<ChromiumLocalStorageReadError>()( |
| 350 | "ChromiumLocalStorageReadError", |
| 351 | { |
| 352 | directory: Schema.String, |
| 353 | cause: Schema.Defect(), |
| 354 | }, |
| 355 | ) { |
| 356 | override get message() { |
| 357 | return `Could not read Chromium Local Storage at ${this.directory}.`; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | export const readChromiumLocalStorage = Effect.fn("desktop.chromiumLocalStorage.read")(function* ( |
| 362 | directory: string, |
| 363 | origin: string, |
| 364 | ) { |
| 365 | const fs = yield* FileSystem.FileSystem; |
| 366 | const path = yield* Path.Path; |
| 367 | const readFile = (name: string) => fs.readFile(path.join(directory, name)); |
| 368 | const wrap = Effect.mapError( |
| 369 | (cause: PlatformError.PlatformError | LevelDbFormatError) => |
| 370 | new ChromiumLocalStorageReadError({ directory, cause }), |
| 371 | ); |
| 372 | const decode = <A>(run: () => A) => |
| 373 | Effect.try({ |
| 374 | try: run, |
| 375 | catch: (cause) => |
| 376 | isLevelDbFormatError(cause) ? cause : new LevelDbFormatError({ detail: String(cause) }), |
| 377 | }); |
| 378 | |
| 379 | const current = textDecoderUtf8.decode(yield* readFile("CURRENT").pipe(wrap)).trim(); |
| 380 | if (!/^MANIFEST-\d+$/.test(current)) { |
| 381 | return yield* new ChromiumLocalStorageReadError({ |
| 382 | directory, |
| 383 | cause: new LevelDbFormatError({ detail: "bad CURRENT" }), |
| 384 | }); |
| 385 | } |
| 386 | const manifest = yield* readFile(current).pipe(wrap); |
| 387 | const live = yield* decode(() => readManifest(manifest)).pipe(wrap); |
| 388 | const logs = (yield* fs.readDirectory(directory).pipe(wrap)) |
| 389 | .map((name) => /^(\d+)\.log$/.exec(name)) |
| 390 | .filter((match) => { |
| 391 | const number = Number(match?.[1]); |
| 392 | return number >= live.logNumber || number === live.previousLogNumber; |
| 393 | }) |
| 394 | .map((match) => match![0]); |
| 395 | |
| 396 | const entries: EntryMap = new Map(); |
| 397 | for (const number of live.tables) { |
| 398 | const name = String(number).padStart(6, "0"); |
| 399 | // Newer LevelDB names tables .ldb; older databases may still hold .sst files. |
| 400 | const bytes = yield* readFile(`${name}.ldb`).pipe( |
| 401 | Effect.catchIf( |
| 402 | (error) => error.reason._tag === "NotFound", |
| 403 | () => readFile(`${name}.sst`), |
| 404 | ), |
| 405 | wrap, |
| 406 | ); |
| 407 | yield* decode(() => readTable(entries, bytes)).pipe(wrap); |
| 408 | } |
| 409 | for (const name of logs) { |
| 410 | const bytes = yield* readFile(name).pipe(wrap); |
| 411 | yield* decode(() => { |
| 412 | for (const batch of readLogRecords(bytes)) readWriteBatch(entries, batch); |
| 413 | }).pipe(wrap); |
| 414 | } |
| 415 | |
| 416 | // Concurrent flushes can move entries beyond this MANIFEST snapshot; detect changes so the caller retries. |
| 417 | const currentAfter = textDecoderUtf8.decode(yield* readFile("CURRENT").pipe(wrap)).trim(); |
| 418 | const manifestAfter = yield* fs.stat(path.join(directory, current)).pipe(wrap); |
| 419 | if (currentAfter !== current || Number(manifestAfter.size) !== manifest.length) { |
| 420 | return yield* new ChromiumLocalStorageReadError({ |
| 421 | directory, |
| 422 | cause: new LevelDbFormatError({ detail: "database changed while reading" }), |
| 423 | }); |
| 424 | } |
| 425 | |
| 426 | const prefix = `_${origin}\u0000`; |
| 427 | const items = new Map<string, string>(); |
| 428 | for (const [id, { key: rawKey, entry }] of entries) { |
| 429 | if (!id.startsWith(prefix) || entry.value === null) continue; |
| 430 | const key = decodeChromiumString(rawKey.subarray(prefix.length)); |
| 431 | const value = decodeChromiumString(entry.value); |
| 432 | if (key !== null && value !== null) items.set(key, value); |
| 433 | } |
| 434 | return items; |
| 435 | }); |