{"draft":null,"from":2,"to":318,"files":[{"path":"server/src/json-chunk-cursor.ts","added":86,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/json-chunk-cursor.ts\n@@ -0,0 +1,86 @@\n+/** Incremental UTF-16 cursor over canonical log chunks. */\n+export class JsonChunkCursor {\n+\treadonly #input: Iterator<string>;\n+\t#chunk = \"\";\n+\t#offset = 0;\n+\tconstructor(chunks: Iterable<string>) {\n+\t\tthis.#input = chunks[Symbol.iterator]();\n+\t}\n+\tpeek(): string | undefined {\n+\t\twhile (this.#offset === this.#chunk.length) {\n+\t\t\tconst next = this.#input.next();\n+\t\t\tif (next.done) return undefined;\n+\t\t\tif (typeof next.value !== \"string\" || next.value.length > 600000) throw Error(\"invalid JSON input chunk\");\n+\t\t\tthis.#chunk = next.value;\n+\t\t\tthis.#offset = 0;\n+\t\t}\n+\t\treturn this.#chunk[this.#offset];\n+\t}\n+\ttake(): string {\n+\t\tconst char = this.peek();\n+\t\tif (char === undefined) throw Error(\"truncated JSON input\");\n+\t\tthis.#offset++;\n+\t\treturn char;\n+\t}\n+\t/** Consume one quoted JSON string, yielding bounded decoded text. */\n+\t*stringChunks(): IterableIterator<string> {\n+\t\tif (this.take() !== '\"') throw Error(\"expected JSON string\");\n+\t\tlet buffer = \"\";\n+\t\tfor (;;) {\n+\t\t\t// Copy ordinary text in bounded slices instead of allocating a string\n+\t\t\t// concatenation for every character of a large canonical field.\n+\t\t\tthis.peek();\n+\t\t\tconst start = this.#offset;\n+\t\t\tconst end = Math.min(this.#chunk.length, start + 16384 - buffer.length);\n+\t\t\twhile (this.#offset < end) {\n+\t\t\t\tconst code = this.#chunk.charCodeAt(this.#offset);\n+\t\t\t\tif (code < 32 || code === 34 || code === 92) break;\n+\t\t\t\tthis.#offset++;\n+\t\t\t}\n+\t\t\tif (this.#offset > start) {\n+\t\t\t\tbuffer += this.#chunk.slice(start, this.#offset);\n+\t\t\t\tif (buffer.length === 16384) {\n+\t\t\t\t\tyield buffer;\n+\t\t\t\t\tbuffer = \"\";\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tlet char = this.take();\n+\t\t\tif (char === '\"') {\n+\t\t\t\tif (buffer) yield buffer;\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tif (char.charCodeAt(0) < 32) throw Error(\"unescaped JSON control character\");\n+\t\t\tif (char === \"\\\\\") {\n+\t\t\t\tconst escape = this.take();\n+\t\t\t\tconst simple: Record<string, string> = {\n+\t\t\t\t\t'\"': '\"',\n+\t\t\t\t\t\"\\\\\": \"\\\\\",\n+\t\t\t\t\t\"/\": \"/\",\n+\t\t\t\t\tb: \"\\b\",\n+\t\t\t\t\tf: \"\\f\",\n+\t\t\t\t\tn: \"\\n\",\n+\t\t\t\t\tr: \"\\r\",\n+\t\t\t\t\tt: \"\\t\",\n+\t\t\t\t};\n+\t\t\t\tif (escape === \"u\") {\n+\t\t\t\t\tlet hex = \"\";\n+\t\t\t\t\tfor (let i = 0; i < 4; i++) hex += this.take();\n+\t\t\t\t\tif (!/^[0-9a-fA-F]{4}$/.test(hex)) throw Error(\"invalid JSON Unicode escape\");\n+\t\t\t\t\tchar = String.fromCharCode(Number.parseInt(hex, 16));\n+\t\t\t\t} else {\n+\t\t\t\t\tif (!Object.hasOwn(simple, escape)) throw Error(\"invalid JSON escape\");\n+\t\t\t\t\tchar = simple[escape]!;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tbuffer += char;\n+\t\t\tif (buffer.length === 16384) {\n+\t\t\t\tyield buffer;\n+\t\t\t\tbuffer = \"\";\n+\t\t\t}\n+\t\t}\n+\t}\n+\tclose(): void {\n+\t\tthis.#input.return?.();\n+\t}\n+}"}],"unmatched":[]}