- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/src/decoded-frames.ts?version=308&format=json
73 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 164, 3 versions changed it
You're reading version 308. The newest is 318, and no version since then changed this file.
1import { CachedStateStorage } from "../../src/engine/state-source.ts";
2import type { WriterAdoptionFrame } from "../../worker/project/project.ts";
3import type { Entry } from "../../worker/project/project-types.ts";
4import type { SqliteLog, SqliteStorage } from "./engine-project.ts";
5import { smallFrameJson } from "./small-frame-json.ts";
6import { StoredJson, storeJson } from "./stored-json.ts";
7import { storedJsonView } from "./stored-json-view.ts";
8import { SqliteWriterState, writerFrameChunksDigest } from "./writer-host.ts";
10/** Keep decoded views alive through the complete page checkpoint, then remove their rows. */
11export function withDecodedFrames<T>(
12 storage: SqliteStorage,
13 namespace: string,
14 log: SqliteLog,
15 input: { from: number; to: number; maxEntries: number; maxBytes: number; digest: string },
16 consume: (frames: Iterable<WriterAdoptionFrame>) => T,
17): { result: T; digest: string } {
18 if (
19 ![input.from, input.to, input.maxEntries, input.maxBytes].every(Number.isSafeInteger) ||
20 input.from < 0 ||
21 input.to < input.from ||
22 input.maxEntries < 1 ||
23 input.maxEntries > 256 ||
24 input.maxBytes < 1
25 )
26 throw Error("invalid decoded frame page bounds");
27 return storage.transactionSync(() => {
28 SqliteWriterState.initialize(storage);
29 const state = new SqliteWriterState(storage, namespace);
30 // Bound cached reads across all disk-backed frames in this page.
31 const reads = new CachedStateStorage(state, 64 * 1024, 256);
32 if (
33 storage.sql.exec("SELECT 1 AS present FROM writer_state WHERE namespace = ? LIMIT 1", namespace).toArray().length
34 )
35 throw Error("decoded frame namespace is occupied");
36 let digest = input.digest;
37 let active = true;
38 const frames = function* (): IterableIterator<WriterAdoptionFrame> {
39 let bytes = 0;
40 for (let seq = input.from; seq < input.to && seq < input.from + input.maxEntries; seq++) {
41 if (!active) throw Error("decoded frame page has ended");
42 const size = log.frameBytes(seq);
43 if (size > input.maxBytes - bytes) {
44 if (bytes) return;
45 throw Error("decoded frame exceeds page byte bound");
46 }
47 digest = writerFrameChunksDigest(digest, seq, log.frameChunks(seq));
48 bytes += size;
49 if (size <= 4096) {
50 yield { seq, bytes: size, entry: smallFrameJson(log.frameChunks(seq)) as Entry };
51 } else {
52 storeJson(state, String(seq), log.frameChunks(seq), { finiteNumbers: true });
53 yield { seq, bytes: size, entry: storedJsonView(new StoredJson(reads, String(seq))) as Entry };
54 }
55 }
56 };
57 try {
58 const result = consume(frames());
59 if (
60 result &&
61 typeof result === "object" &&
62 (typeof (result as { then?: unknown }).then === "function" ||
63 typeof (result as { next?: unknown }).next === "function")
64 )
65 throw Error("decoded frame consumption must finish synchronously");
66 return { result: structuredClone(result), digest };
67 } finally {
68 active = false;
69 reads.clear();
70 storage.sql.exec("DELETE FROM writer_state WHERE namespace = ?", namespace);
71 }
72 });
73}