{"draft":null,"from":5,"to":318,"files":[{"path":"src/engine/json-value-chunks.ts","added":133,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/json-value-chunks.ts\n@@ -0,0 +1,133 @@\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(\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+\t\tfor (let from = 0; from < value.length; ) {\n+\t\t\tlet end = Math.min(from + 16384, value.length);\n+\t\t\tconst last = value.charCodeAt(end - 1),\n+\t\t\t\tnext = value.charCodeAt(end);\n+\t\t\tif (last >= 0xd800 && last <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) end--;\n+\t\t\tyield JSON.stringify(value.slice(from, end)).slice(1, -1);\n+\t\t\tfrom = end;\n+\t\t}\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\") 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 (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 >= (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)) {\n+\t\t\t\t\tyield \"[\";\n+\t\t\t\t\tconst length = value.length;\n+\t\t\t\t\tfor (let i = 0; i < length; i++) {\n+\t\t\t\t\t\tif (i) yield \",\";\n+\t\t\t\t\t\tconst child = normalize(value[i], String(i));\n+\t\t\t\t\t\tyield* encode(omitted(child) ? null : child);\n+\t\t\t\t\t}\n+\t\t\t\t\tyield \"]\";\n+\t\t\t\t} else {\n+\t\t\t\t\tyield \"{\";\n+\t\t\t\t\tlet first = true;\n+\t\t\t\t\tfor (const key of Object.keys(value)) {\n+\t\t\t\t\t\tconst child = normalize((value as Record<string, unknown>)[key], key);\n+\t\t\t\t\t\tif (omitted(child)) continue;\n+\t\t\t\t\t\tif (!first) yield \",\";\n+\t\t\t\t\t\tfirst = false;\n+\t\t\t\t\t\tyield* string(key);\n+\t\t\t\t\t\tyield \":\";\n+\t\t\t\t\t\tyield* encode(child);\n+\t\t\t\t\t}\n+\t\t\t\t\tyield \"}\";\n+\t\t\t\t}\n+\t\t\t} finally {\n+\t\t\t\tactive.delete(value);\n+\t\t\t}\n+\t\t}\n+\t}\n+\tyield* encode(normalize(input, \"\"));\n+}\n+\n+/** Exact serialized-content equality, retaining only the current pair of chunks. */\n+export function equalJsonValues(left: unknown, right: unknown): boolean {\n+\tconst a = jsonValueChunks(left),\n+\t\tb = jsonValueChunks(right);\n+\tlet x = a.next(),\n+\t\ty = b.next(),\n+\t\ti = 0,\n+\t\tj = 0;\n+\ttry {\n+\t\tfor (;;) {\n+\t\t\twhile (!x.done && i === x.value.length) {\n+\t\t\t\tx = a.next();\n+\t\t\t\ti = 0;\n+\t\t\t}\n+\t\t\twhile (!y.done && j === y.value.length) {\n+\t\t\t\ty = b.next();\n+\t\t\t\tj = 0;\n+\t\t\t}\n+\t\t\tif (x.done || y.done) return x.done === y.done;\n+\t\t\tconst length = Math.min(x.value.length - i, y.value.length - j);\n+\t\t\tif (x.value.slice(i, i + length) !== y.value.slice(j, j + length)) return false;\n+\t\t\ti += length;\n+\t\t\tj += length;\n+\t\t}\n+\t} finally {\n+\t\ta.return?.();\n+\t\tb.return?.();\n+\t}\n+}"}],"unmatched":[]}