{"draft":null,"from":164,"to":165,"files":[{"path":"src/engine/json-value-chunks.ts","added":94,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/json-value-chunks.ts\n@@ -0,0 +1,94 @@\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+\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 !== null && typeof value === \"object\") {\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+\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\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\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":[]}