{"draft":null,"from":170,"to":318,"files":[{"path":"src/engine/json-value-chunks.ts","added":43,"removed":4,"diff":"--- a/src/engine/json-value-chunks.ts\n+++ b/src/engine/json-value-chunks.ts\n@@ -1,7 +1,27 @@\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-export function* jsonValueChunks(input: unknown): IterableIterator<string> {\n+export function* jsonValueChunks(\n+\tinput: unknown,\n+\toptions: { maxDepth?: number; unboxPrimitives?: boolean } = {},\n+): IterableIterator<string> {\n \tconst active = new Set<object>();\n \tfunction* string(value: string): IterableIterator<string> {\n \t\tyield '\"';\n@@ -16,20 +36,39 @@\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\tif (typeof toJSON === \"function\") value = toJSON.call(value, key);\n \t\t}\n+\t\tif (options.unboxPrimitives && (value instanceof String || value instanceof Number || value instanceof Boolean))\n+\t\t\treturn value.valueOf();\n \t\treturn value;\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 {\n \t\t\tif (active.has(value)) throw Error(\"cyclic canonical JSON value\");\n-\t\t\tif (active.size >= 128) throw Error(\"canonical JSON depth exceeds bound\");\n+\t\t\tif (active.size >= (options.maxDepth ?? 128)) throw Error(\"canonical JSON depth exceeds bound\");\n \t\t\tactive.add(value);\n \t\t\ttry {\n \t\t\t\tif (Array.isArray(value)) {"}],"unmatched":[]}