{"draft":null,"from":80,"to":318,"files":[{"path":"server/src/json-events.ts","added":79,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/json-events.ts\n@@ -0,0 +1,79 @@\n+import { JsonChunkCursor } from \"./json-chunk-cursor.ts\";\n+\n+export type JsonEvent =\n+\t| { kind: \"object\" | \"array\" | \"end\" | \"string\" | \"string-end\" }\n+\t| { kind: \"key\" | \"text\"; value: string }\n+\t| { kind: \"value\"; value: number | boolean | null };\n+\n+/** Structural JSON decoding. Retains nesting and one bounded string fragment, not a decoded tree. */\n+export function* jsonEvents(\n+\tchunks: Iterable<string>,\n+\toptions: { finiteNumbers?: boolean } = {},\n+): IterableIterator<JsonEvent> {\n+\tconst cursor = new JsonChunkCursor(chunks);\n+\tconst whitespace = () => {\n+\t\twhile (cursor.peek() !== undefined && /[ \\t\\n\\r]/.test(cursor.peek()!)) cursor.take();\n+\t};\n+\tfunction* value(depth: number): IterableIterator<JsonEvent> {\n+\t\tif (depth > 128) throw Error(\"JSON nesting exceeds bound\");\n+\t\twhitespace();\n+\t\tconst first = cursor.peek();\n+\t\tif (first === '\"') {\n+\t\t\tyield { kind: \"string\" };\n+\t\t\tfor (const text of cursor.stringChunks()) yield { kind: \"text\", value: text };\n+\t\t\tyield { kind: \"string-end\" };\n+\t\t\treturn;\n+\t\t}\n+\t\tif (first === \"{\" || first === \"[\") {\n+\t\t\tconst object = cursor.take() === \"{\";\n+\t\t\tconst close = object ? \"}\" : \"]\";\n+\t\t\tyield { kind: object ? \"object\" : \"array\" };\n+\t\t\twhitespace();\n+\t\t\tif (cursor.peek() === close) {\n+\t\t\t\tcursor.take();\n+\t\t\t\tyield { kind: \"end\" };\n+\t\t\t\treturn;\n+\t\t\t}\n+\t\t\tfor (;;) {\n+\t\t\t\tif (object) {\n+\t\t\t\t\twhitespace();\n+\t\t\t\t\tlet key = \"\";\n+\t\t\t\t\tfor (const text of cursor.stringChunks()) {\n+\t\t\t\t\t\tkey += text;\n+\t\t\t\t\t\tif (key.length > 4096) throw Error(\"JSON key exceeds bound\");\n+\t\t\t\t\t}\n+\t\t\t\t\tyield { kind: \"key\", value: key };\n+\t\t\t\t\twhitespace();\n+\t\t\t\t\tif (cursor.take() !== \":\") throw Error(\"expected JSON colon\");\n+\t\t\t\t}\n+\t\t\t\tyield* value(depth + 1);\n+\t\t\t\twhitespace();\n+\t\t\t\tconst separator = cursor.take();\n+\t\t\t\tif (separator === close) break;\n+\t\t\t\tif (separator !== \",\") throw Error(\"expected JSON separator\");\n+\t\t\t}\n+\t\t\tyield { kind: \"end\" };\n+\t\t\treturn;\n+\t\t}\n+\t\tlet token = \"\";\n+\t\twhile (cursor.peek() !== undefined && !/[ \\t\\n\\r,\\]}]/.test(cursor.peek()!)) {\n+\t\t\ttoken += cursor.take();\n+\t\t\tif (token.length > 256) throw Error(\"JSON scalar exceeds bound\");\n+\t\t}\n+\t\tif (token === \"true\" || token === \"false\" || token === \"null\") {\n+\t\t\tyield { kind: \"value\", value: token === \"null\" ? null : token === \"true\" };\n+\t\t} else {\n+\t\t\tif (!/^-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/.test(token)) throw Error(\"invalid JSON scalar\");\n+\t\t\tconst number = Number(token);\n+\t\t\tif (options.finiteNumbers && !Number.isFinite(number)) throw Error(\"non-finite JSON number\");\n+\t\t\tyield { kind: \"value\", value: number };\n+\t\t}\n+\t}\n+\ttry {\n+\t\tyield* value(0);\n+\t\twhitespace();\n+\t\tif (cursor.peek() !== undefined) throw Error(\"trailing JSON input\");\n+\t} finally {\n+\t\tcursor.close();\n+\t}\n+}"}],"unmatched":[]}