{"draft":null,"from":4,"to":318,"files":[{"path":"server/src/stored-json.ts","added":155,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/stored-json.ts\n@@ -0,0 +1,155 @@\n+import type { GraphStateStorage } from \"../../src/engine/state-source.ts\";\n+import { StoredText } from \"../../src/engine/stored-text.ts\";\n+import { type JsonEvent, jsonEvents } from \"./json-events.ts\";\n+\n+/** Temporary decoded JSON on disk. A root is published only after complete parsing. */\n+export function storeJson(\n+\tstorage: GraphStateStorage,\n+\tnamespace: string,\n+\tchunks: Iterable<string>,\n+\toptions: { finiteNumbers?: boolean } = {},\n+): number {\n+\treturn storage.transaction(() => {\n+\t\tif (storage.read(namespace, \"root\") !== undefined) throw Error(\"decoded JSON already exists\");\n+\t\tconst events = jsonEvents(chunks, options);\n+\t\tlet nextId = 0;\n+\t\tconst take = (): JsonEvent => {\n+\t\t\tconst event = events.next();\n+\t\t\tif (event.done) throw Error(\"truncated JSON events\");\n+\t\t\treturn event.value;\n+\t\t};\n+\t\tconst text = new StoredText(storage, `${namespace}:text`);\n+\t\tconst value = (event = take()): number => {\n+\t\t\tconst id = nextId++;\n+\t\t\tif (!Number.isSafeInteger(nextId)) throw Error(\"decoded JSON identity overflow\");\n+\t\t\tif (event.kind === \"value\")\n+\t\t\t\tstorage.write(namespace, String(id), {\n+\t\t\t\t\tkind: \"value\",\n+\t\t\t\t\tvalue: typeof event.value === \"number\" ? String(event.value) : event.value,\n+\t\t\t\t\tnumber: typeof event.value === \"number\",\n+\t\t\t\t\tnegativeZero: Object.is(event.value, -0),\n+\t\t\t\t});\n+\t\t\telse if (event.kind === \"string\") {\n+\t\t\t\ttext.writeChunks(\n+\t\t\t\t\tString(id),\n+\t\t\t\t\t(function* () {\n+\t\t\t\t\t\tfor (;;) {\n+\t\t\t\t\t\t\tconst part = take();\n+\t\t\t\t\t\t\tif (part.kind === \"string-end\") return;\n+\t\t\t\t\t\t\tif (part.kind !== \"text\") throw Error(\"invalid JSON string events\");\n+\t\t\t\t\t\t\tyield part.value;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t})(),\n+\t\t\t\t);\n+\t\t\t\tstorage.write(namespace, String(id), { kind: \"string\" });\n+\t\t\t} else if (event.kind === \"array\" || event.kind === \"object\") {\n+\t\t\t\tlet count = 0;\n+\t\t\t\tfor (;;) {\n+\t\t\t\t\tconst child = take();\n+\t\t\t\t\tif (child.kind === \"end\") break;\n+\t\t\t\t\tif (event.kind === \"object\" && child.kind !== \"key\") throw Error(\"invalid JSON object events\");\n+\t\t\t\t\tconst key = child.kind === \"key\" ? child.value : String(count);\n+\t\t\t\t\tconst childId = value(event.kind === \"object\" ? take() : child);\n+\t\t\t\t\tconst edge = JSON.stringify([id, key]);\n+\t\t\t\t\tif (event.kind === \"object\" && storage.read(`${namespace}:edges`, edge) === undefined) {\n+\t\t\t\t\t\tstorage.write(`${namespace}:keys`, JSON.stringify([id, count]), key);\n+\t\t\t\t\t\tcount++;\n+\t\t\t\t\t}\n+\t\t\t\t\tstorage.write(`${namespace}:edges`, edge, childId);\n+\t\t\t\t\tif (event.kind === \"array\") count++;\n+\t\t\t\t}\n+\t\t\t\tstorage.write(namespace, String(id), { kind: event.kind, count });\n+\t\t\t} else throw Error(\"invalid JSON value event\");\n+\t\t\treturn id;\n+\t\t};\n+\t\ttry {\n+\t\t\tconst root = value();\n+\t\t\tif (!events.next().done) throw Error(\"trailing JSON events\");\n+\t\t\tstorage.write(namespace, \"root\", { format: 1, id: root, nodes: nextId });\n+\t\t\treturn root;\n+\t\t} finally {\n+\t\t\tevents.return?.();\n+\t\t}\n+\t});\n+}\n+\n+/** A disk-backed JSON node. Child lookup does not decode siblings or string payloads. */\n+export class StoredJson {\n+\treadonly storage: GraphStateStorage;\n+\treadonly namespace: string;\n+\treadonly id: number;\n+\treadonly nodes: number;\n+\tconstructor(storage: GraphStateStorage, namespace: string, id?: number) {\n+\t\tthis.storage = storage;\n+\t\tthis.namespace = namespace;\n+\t\tconst root = storage.read(namespace, \"root\") as { format: number; id: number; nodes: number } | undefined;\n+\t\tif (root?.format !== 1 || !Number.isSafeInteger(root.nodes) || root.nodes < 1 || root.id !== 0)\n+\t\t\tthrow Error(\"invalid stored JSON root\");\n+\t\tthis.nodes = root.nodes;\n+\t\tthis.id = id ?? root.id;\n+\t\tif (!Number.isSafeInteger(this.id) || this.id < 0 || this.id >= this.nodes)\n+\t\t\tthrow Error(\"invalid stored JSON identity\");\n+\t}\n+\tprivate node(): { kind: string; count?: number; value?: unknown; number?: boolean; negativeZero?: boolean } {\n+\t\tconst node = this.storage.read(this.namespace, String(this.id)) as\n+\t\t\t| { kind: string; count?: number; value?: unknown; number?: boolean; negativeZero?: boolean }\n+\t\t\t| undefined;\n+\t\tif (!node || ![\"array\", \"object\", \"string\", \"value\"].includes(node.kind)) throw Error(\"invalid stored JSON node\");\n+\t\treturn node;\n+\t}\n+\tget kind(): string {\n+\t\treturn this.node().kind;\n+\t}\n+\tget length(): number {\n+\t\tconst node = this.node();\n+\t\tif (node.kind !== \"array\" || !Number.isSafeInteger(node.count) || node.count! < 0)\n+\t\t\tthrow Error(\"invalid stored JSON array\");\n+\t\treturn node.count!;\n+\t}\n+\tchild(key: string | number): StoredJson | undefined {\n+\t\tconst node = this.node();\n+\t\tif (node.kind !== \"object\" && node.kind !== \"array\") throw Error(\"stored JSON node is not a container\");\n+\t\tif (\n+\t\t\tnode.kind === \"array\" &&\n+\t\t\t(typeof key !== \"number\" || !Number.isSafeInteger(key) || key < 0 || key >= this.length)\n+\t\t)\n+\t\t\treturn undefined;\n+\t\tconst id = this.storage.read(`${this.namespace}:edges`, JSON.stringify([this.id, String(key)]));\n+\t\tif (id === undefined) {\n+\t\t\tif (node.kind === \"array\") throw Error(\"missing stored JSON array element\");\n+\t\t\treturn undefined;\n+\t\t}\n+\t\tif (typeof id !== \"number\") throw Error(\"invalid stored JSON edge\");\n+\t\treturn new StoredJson(this.storage, this.namespace, id);\n+\t}\n+\t*elements(): IterableIterator<StoredJson> {\n+\t\tconst length = this.length;\n+\t\tfor (let index = 0; index < length; index++) yield this.child(index)!;\n+\t}\n+\t*properties(): IterableIterator<[string, StoredJson]> {\n+\t\tconst node = this.node();\n+\t\tif (node.kind !== \"object\" || !Number.isSafeInteger(node.count) || node.count! < 0)\n+\t\t\tthrow Error(\"invalid stored JSON object\");\n+\t\tfor (let index = 0; index < node.count!; index++) {\n+\t\t\tconst key = this.storage.read(`${this.namespace}:keys`, JSON.stringify([this.id, index]));\n+\t\t\tif (typeof key !== \"string\") throw Error(\"missing stored JSON object key\");\n+\t\t\tconst child = this.child(key);\n+\t\t\tif (!child) throw Error(\"missing stored JSON object member\");\n+\t\t\tyield [key, child];\n+\t\t}\n+\t}\n+\t*textChunks(): IterableIterator<string> {\n+\t\tif (this.kind !== \"string\") throw Error(\"stored JSON node is not text\");\n+\t\tyield* new StoredText(this.storage, `${this.namespace}:text`).chunks(String(this.id));\n+\t}\n+\tscalar(): number | boolean | null {\n+\t\tconst node = this.node();\n+\t\tif (node.kind !== \"value\") throw Error(\"stored JSON node is not a scalar\");\n+\t\tif (node.number) {\n+\t\t\tif (typeof node.value !== \"string\" || Number.isNaN(Number(node.value))) throw Error(\"invalid stored JSON number\");\n+\t\t\treturn node.negativeZero ? -0 : Number(node.value);\n+\t\t}\n+\t\tif (node.value === null || typeof node.value === \"boolean\" || typeof node.value === \"number\") return node.value;\n+\t\tthrow Error(\"invalid stored JSON scalar\");\n+\t}\n+}"}],"unmatched":[]}