Versions 17 to 318
new file, 133 lines
1+/** Internal values may expose an exact streaming representation without changing public toJSON. */ 2+const streamingValues = new WeakMap<object, () => unknown>(); 3+export function registerJsonChunkValue(value: object, representation: () => unknown): void { 4+ streamingValues.set(value, representation); 7+/** A JSON string backed by bounded, replayable UTF-16 chunks. */ 8+export class JsonStringChunks { 9+ readonly chunks: () => Iterable<string>; 10+ constructor(chunks: () => Iterable<string>) { 11+ this.chunks = chunks; 14+ return [...this.chunks()].join(""); 18+/** JSON.stringify-compatible chunks for canonical data, without a whole-value string. 19+ * Individual input strings and object key lists still belong to the caller. 21+export function* jsonValueChunks( 23+ options: { maxDepth?: number; unboxPrimitives?: boolean } = {}, 24+): IterableIterator<string> { 25+ const active = new Set<object>(); 26+ function* string(value: string): IterableIterator<string> { 28+ for (let from = 0; from < value.length; ) { 29+ let end = Math.min(from + 16384, value.length); 30+ const last = value.charCodeAt(end - 1), 31+ next = value.charCodeAt(end); 32+ if (last >= 0xd800 && last <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) end--; 33+ yield JSON.stringify(value.slice(from, end)).slice(1, -1); 38+ const normalize = (value: unknown, key: string): unknown => { 39+ if (value instanceof JsonStringChunks) return value; 40+ if (value !== null && typeof value === "object") { 41+ const streaming = streamingValues.get(value); 42+ if (streaming) return streaming(); 43+ const toJSON = (value as { toJSON?: unknown }).toJSON; 44+ if (typeof toJSON === "function") value = toJSON.call(value, key); 46+ if (options.unboxPrimitives && (value instanceof String || value instanceof Number || value instanceof Boolean)) 47+ return value.valueOf(); 50+ const omitted = (value: unknown) => value === undefined || typeof value === "function" || typeof value === "symbol"; 51+ function* encode(value: unknown): IterableIterator<string> { 52+ if (value instanceof JsonStringChunks) { 55+ for (const chunk of value.chunks()) { 56+ if (typeof chunk !== "string" || chunk.length > 16384) throw Error("JSON string chunk exceeds bound"); 57+ const text = pending + chunk; 58+ let end = text.length; 59+ const last = text.charCodeAt(end - 1); 60+ if (last >= 0xd800 && last <= 0xdbff) end--; 61+ pending = text.slice(end); 62+ if (end) yield JSON.stringify(text.slice(0, end)).slice(1, -1); 64+ if (pending) yield JSON.stringify(pending).slice(1, -1); 66+ } else if (typeof value === "string") yield* string(value); 67+ else if (value === null || typeof value === "boolean" || typeof value === "number") yield JSON.stringify(value); 68+ else if (typeof value !== "object" || value === null) throw Error("unsupported canonical JSON value"); 70+ if (active.has(value)) throw Error("cyclic canonical JSON value"); 71+ if (active.size >= (options.maxDepth ?? 128)) throw Error("canonical JSON depth exceeds bound"); 74+ if (Array.isArray(value)) { 76+ const length = value.length; 77+ for (let i = 0; i < length; i++) { 79+ const child = normalize(value[i], String(i)); 80+ yield* encode(omitted(child) ? null : child); 86+ for (const key of Object.keys(value)) { 87+ const child = normalize((value as Record<string, unknown>)[key], key); 88+ if (omitted(child)) continue; 89+ if (!first) yield ","; 93+ yield* encode(child); 98+ active.delete(value); 102+ yield* encode(normalize(input, "")); 105+/** Exact serialized-content equality, retaining only the current pair of chunks. */ 106+export function equalJsonValues(left: unknown, right: unknown): boolean { 107+ const a = jsonValueChunks(left), 108+ b = jsonValueChunks(right); 115+ while (!x.done && i === x.value.length) { 119+ while (!y.done && j === y.value.length) { 123+ if (x.done || y.done) return x.done === y.done; 124+ const length = Math.min(x.value.length - i, y.value.length - j); 125+ if (x.value.slice(i, i + length) !== y.value.slice(j, j + length)) return false;