Files
rea/src/domain/jsonShape.ts
T
morluto 79805f244a fix(web): preserve JSON identity and coverage contracts
Decode base64 JSON body bytes at the shape owner without replacement characters. Keep incomplete request correspondence unknown and use explicitly complete authored inventories for positive comparison controls. Repair semantic truncation and storage fixtures to satisfy their owning contracts without weakening assertions.
2026-10-24 08:01:25 +08:00

180 lines
5.6 KiB
TypeScript

import { z } from "zod";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
import { safeParseJson } from "./safeJson.js";
const jsonValueTypeSchema = z.enum([
"object",
"array",
"string",
"number",
"boolean",
"null",
]);
type JsonValueType = z.infer<typeof jsonValueTypeSchema>;
const jsonShapePathSegmentSchema = z.discriminatedUnion("kind", [
z.object({ kind: z.literal("property"), name: z.string() }),
z.object({ kind: z.literal("array-element") }),
]);
type JsonShapePathSegment = z.infer<typeof jsonShapePathSegmentSchema>;
/** Value-free structural summary of one approved JSON payload. */
export const jsonShapeSchema = z.object({
root_type: jsonValueTypeSchema,
node_count: z.int().min(1),
max_depth_observed: z.int().min(0),
properties: z.array(
z.object({
path: z.array(jsonShapePathSegmentSchema).min(1),
types: z.array(jsonValueTypeSchema).min(1),
observations: z.int().min(1),
}),
),
});
export type JsonShape = z.infer<typeof jsonShapeSchema>;
const arrayElementKey = Symbol("array-element");
interface ShapeNode {
readonly path: JsonShapePathSegment[];
readonly types: Set<JsonValueType>;
observations: number;
children?: Map<string | typeof arrayElementKey, ShapeNode>;
}
/** Parse approved JSON text or exact UTF-8 bytes, then discard values after shape inference. */
export const inferJsonShape = (
source: string | Uint8Array,
): JsonShape | null => {
let root: unknown;
try {
const text =
typeof source === "string"
? source
: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
source,
);
root = JSON.parse(text);
} catch (cause: unknown) {
// Invalid JSON or non-UTF-8 bytes cannot establish a shape.
void cause;
return null;
}
const rootShape: ShapeNode = {
path: [],
types: new Set(),
observations: 0,
};
// Each structural path owns one accumulator, shared by all matching array rows.
const childShape = (
parent: ShapeNode,
key: string | typeof arrayElementKey,
): ShapeNode => {
const children = (parent.children ??= new Map());
const existing = children.get(key);
if (existing !== undefined) return existing;
const node: ShapeNode = {
path: [
...parent.path,
typeof key === "string"
? { kind: "property", name: key }
: { kind: "array-element" },
],
types: new Set(),
observations: 0,
};
children.set(key, node);
return node;
};
const pending: Array<{
readonly value: unknown;
readonly shape: ShapeNode;
}> = [{ value: root, shape: rootShape }];
let nodeCount = 0;
let maxDepthObserved = 0;
while (pending.length > 0) {
const current = pending.pop();
if (current === undefined) break;
nodeCount += 1;
maxDepthObserved = Math.max(maxDepthObserved, current.shape.path.length);
current.shape.types.add(jsonValueType(current.value));
current.shape.observations += 1;
if (Array.isArray(current.value)) {
if (current.value.length === 0) continue;
const shape = childShape(current.shape, arrayElementKey);
for (let index = current.value.length - 1; index >= 0; index -= 1)
pending.push({ value: current.value[index], shape });
continue;
}
if (!isRecord(current.value)) continue;
const allEntries = Object.entries(current.value);
for (let index = allEntries.length - 1; index >= 0; index -= 1) {
const entry = allEntries[index];
if (entry === undefined) continue;
const [name, value] = entry;
pending.push({ value, shape: childShape(current.shape, name) });
}
}
// Preorder over sorted siblings gives canonical paths without repeatedly
// comparing their shared prefixes. Each returned path owns its segments.
const properties: JsonShape["properties"] = [];
const shapes = orderedChildren(rootShape);
while (shapes.length > 0) {
const shape = shapes.pop();
if (shape === undefined) break;
properties.push({
path: shape.path.map((segment) => ({ ...segment })),
types: [...shape.types].sort(),
observations: shape.observations,
});
for (const child of orderedChildren(shape)) shapes.push(child);
}
return {
root_type: jsonValueType(root),
node_count: nodeCount,
max_depth_observed: maxDepthObserved,
properties,
};
};
/** Parse text as a JSON object; anything else, including invalid JSON, is null. */
export const parseJsonObject = (
text: string,
): Readonly<Record<string, unknown>> | null => {
const parsed = safeParseJson(text);
return parsed.ok && isRecord(parsed.value) ? parsed.value : null;
};
/** The JSON type name of one parsed value. */
export const jsonValueType = (value: unknown): JsonValueType => {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
switch (typeof value) {
case "string":
return "string";
case "number":
return "number";
case "boolean":
return "boolean";
default:
return "object";
}
};
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);
/** Reverse sibling order for the traversal stack; array elements sort first. */
const orderedChildren = (node: ShapeNode): ShapeNode[] =>
[...(node.children ?? [])]
.sort(([left], [right]) =>
left === right
? 0
: left === arrayElementKey
? 1
: right === arrayElementKey
? -1
: compareUnicodeCodePoints(right, left),
)
.map(([, child]) => child);