{"draft":null,"from":174,"to":175,"files":[{"path":"src/engine/json-value-chunks.ts","added":35,"removed":1,"diff":"--- a/src/engine/json-value-chunks.ts\n+++ b/src/engine/json-value-chunks.ts\n@@ -1,3 +1,20 @@\n+/** Internal values may expose an exact streaming representation without changing public toJSON. */\n+const streamingValues = new WeakMap<object, () => unknown>();\n+export function registerJsonChunkValue(value: object, representation: () => unknown): void {\n+\tstreamingValues.set(value, representation);\n+}\n+\n+/** A JSON string backed by bounded, replayable UTF-16 chunks. */\n+export class JsonStringChunks {\n+\treadonly chunks: () => Iterable<string>;\n+\tconstructor(chunks: () => Iterable<string>) {\n+\t\tthis.chunks = chunks;\n+\t}\n+\ttoJSON(): string {\n+\t\treturn [...this.chunks()].join(\"\");\n+\t}\n+}\n+\n /** JSON.stringify-compatible chunks for canonical data, without a whole-value string.\n  * Individual input strings and object key lists still belong to the caller.\n  */\n@@ -16,7 +33,10 @@\n \t\tyield '\"';\n \t}\n \tconst normalize = (value: unknown, key: string): unknown => {\n+\t\tif (value instanceof JsonStringChunks) return value;\n \t\tif (value !== null && typeof value === \"object\") {\n+\t\t\tconst streaming = streamingValues.get(value);\n+\t\t\tif (streaming) return streaming();\n \t\t\tconst toJSON = (value as { toJSON?: unknown }).toJSON;\n \t\t\tif (typeof toJSON === \"function\") return toJSON.call(value, key);\n \t\t}\n@@ -24,7 +44,21 @@\n \t};\n \tconst omitted = (value: unknown) => value === undefined || typeof value === \"function\" || typeof value === \"symbol\";\n \tfunction* encode(value: unknown): IterableIterator<string> {\n-\t\tif (typeof value === \"string\") yield* string(value);\n+\t\tif (value instanceof JsonStringChunks) {\n+\t\t\tyield '\"';\n+\t\t\tlet pending = \"\";\n+\t\t\tfor (const chunk of value.chunks()) {\n+\t\t\t\tif (typeof chunk !== \"string\" || chunk.length > 16384) throw Error(\"JSON string chunk exceeds bound\");\n+\t\t\t\tconst text = pending + chunk;\n+\t\t\t\tlet end = text.length;\n+\t\t\t\tconst last = text.charCodeAt(end - 1);\n+\t\t\t\tif (last >= 0xd800 && last <= 0xdbff) end--;\n+\t\t\t\tpending = text.slice(end);\n+\t\t\t\tif (end) yield JSON.stringify(text.slice(0, end)).slice(1, -1);\n+\t\t\t}\n+\t\t\tif (pending) yield JSON.stringify(pending).slice(1, -1);\n+\t\t\tyield '\"';\n+\t\t} else if (typeof value === \"string\") yield* string(value);\n \t\telse if (value === null || typeof value === \"boolean\" || typeof value === \"number\") yield JSON.stringify(value);\n \t\telse if (typeof value !== \"object\" || value === null) throw Error(\"unsupported canonical JSON value\");\n \t\telse {"}],"unmatched":[]}