{"draft":null,"from":3,"to":318,"files":[{"path":"src/engine/stored-text.ts","added":70,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/stored-text.ts\n@@ -0,0 +1,70 @@\n+import type { GraphStateStorage } from \"./state-source.ts\";\n+\n+/** Exact UTF-16 text storage with bounded values; callers choose streaming or full reads. */\n+export class StoredText {\n+\tstatic readonly chunkCharacters = 16384;\n+\treadonly storage: GraphStateStorage;\n+\treadonly collection: string;\n+\tconstructor(storage: GraphStateStorage, collection: string) {\n+\t\tthis.storage = storage;\n+\t\tthis.collection = collection;\n+\t}\n+\twrite(key: string, text: string): void {\n+\t\tthis.writeChunks(\n+\t\t\tkey,\n+\t\t\t(function* () {\n+\t\t\t\tfor (let offset = 0; offset < text.length; offset += StoredText.chunkCharacters)\n+\t\t\t\t\tyield text.slice(offset, offset + StoredText.chunkCharacters);\n+\t\t\t})(),\n+\t\t);\n+\t}\n+\t/** Input pieces and the retained buffer are bounded independently of total text length. */\n+\twriteChunks(key: string, chunks: Iterable<string>): void {\n+\t\tthis.storage.transaction(() => {\n+\t\t\tif (this.storage.read(this.collection, key) !== undefined) throw Error(\"stored text already exists\");\n+\t\t\tlet pending = \"\",\n+\t\t\t\tcharacters = 0,\n+\t\t\t\tparts = 0;\n+\t\t\tconst persist = (chunk: string) =>\n+\t\t\t\tthis.storage.write(`${this.collection}:chunks`, JSON.stringify([key, parts++]), chunk);\n+\t\t\tfor (const chunk of chunks) {\n+\t\t\t\tif (typeof chunk !== \"string\" || chunk.length > StoredText.chunkCharacters)\n+\t\t\t\t\tthrow Error(\"stored text input chunk exceeds bound\");\n+\t\t\t\tcharacters += chunk.length;\n+\t\t\t\tif (!Number.isSafeInteger(characters)) throw Error(\"stored text length overflow\");\n+\t\t\t\tpending += chunk;\n+\t\t\t\tif (pending.length >= StoredText.chunkCharacters) {\n+\t\t\t\t\tpersist(pending.slice(0, StoredText.chunkCharacters));\n+\t\t\t\t\tpending = pending.slice(StoredText.chunkCharacters);\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (pending) persist(pending);\n+\t\t\tthis.storage.write(this.collection, key, { format: 1, characters, parts });\n+\t\t});\n+\t}\n+\n+\t*chunks(key: string): IterableIterator<string> {\n+\t\tconst header = this.storage.read(this.collection, key) as\n+\t\t\t| { format: number; characters: number; parts: number }\n+\t\t\t| undefined;\n+\t\tif (\n+\t\t\theader?.format !== 1 ||\n+\t\t\t!Number.isSafeInteger(header.characters) ||\n+\t\t\theader.characters < 0 ||\n+\t\t\theader.parts !== Math.ceil(header.characters / StoredText.chunkCharacters)\n+\t\t)\n+\t\t\tthrow Error(\"invalid stored text header\");\n+\t\tfor (let part = 0; part < header.parts; part++) {\n+\t\t\tconst chunk = this.storage.read(`${this.collection}:chunks`, JSON.stringify([key, part]));\n+\t\t\tif (\n+\t\t\t\ttypeof chunk !== \"string\" ||\n+\t\t\t\tchunk.length !== Math.min(StoredText.chunkCharacters, header.characters - part * StoredText.chunkCharacters)\n+\t\t\t)\n+\t\t\t\tthrow Error(\"incomplete stored text\");\n+\t\t\tyield chunk;\n+\t\t}\n+\t}\n+\tread(key: string): string {\n+\t\treturn [...this.chunks(key)].join(\"\");\n+\t}\n+}"}],"unmatched":[]}