refactor(browser): return complete observation evidence
This commit is contained in:
+7
-46
@@ -22,22 +22,11 @@ export const jsonShapeSchema = z.object({
|
||||
observations: z.number().int().min(1),
|
||||
}),
|
||||
),
|
||||
truncated: z.boolean(),
|
||||
});
|
||||
export type JsonShape = z.infer<typeof jsonShapeSchema>;
|
||||
|
||||
export interface JsonShapeLimits {
|
||||
readonly maximumBytes: number;
|
||||
readonly maximumNodes: number;
|
||||
readonly maximumDepth: number;
|
||||
}
|
||||
|
||||
/** Parse approved JSON and immediately discard values after iterative shape inference. */
|
||||
export const inferJsonShape = (
|
||||
text: string,
|
||||
limits: JsonShapeLimits,
|
||||
): JsonShape | null => {
|
||||
if (Buffer.byteLength(text) > limits.maximumBytes) return null;
|
||||
export const inferJsonShape = (text: string): JsonShape | null => {
|
||||
let root: unknown;
|
||||
try {
|
||||
root = JSON.parse(text);
|
||||
@@ -55,28 +44,13 @@ export const inferJsonShape = (
|
||||
}> = [{ value: root, path: "", depth: 0 }];
|
||||
let nodeCount = 0;
|
||||
let maxDepthObserved = 0;
|
||||
let truncated = false;
|
||||
while (pending.length > 0) {
|
||||
const current = pending.pop();
|
||||
if (current === undefined) break;
|
||||
if (nodeCount >= limits.maximumNodes) {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
nodeCount += 1;
|
||||
maxDepthObserved = Math.max(maxDepthObserved, current.depth);
|
||||
if (current.depth >= limits.maximumDepth) {
|
||||
if (hasChildren(current.value)) truncated = true;
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(current.value)) {
|
||||
const capacity = Math.max(
|
||||
0,
|
||||
limits.maximumNodes - nodeCount - pending.length,
|
||||
);
|
||||
const retained = Math.min(current.value.length, capacity);
|
||||
if (retained < current.value.length) truncated = true;
|
||||
for (let index = retained - 1; index >= 0; index -= 1)
|
||||
for (let index = current.value.length - 1; index >= 0; index -= 1)
|
||||
pending.push({
|
||||
value: current.value[index],
|
||||
path: `${current.path}/*`,
|
||||
@@ -86,17 +60,11 @@ export const inferJsonShape = (
|
||||
}
|
||||
if (!isRecord(current.value)) continue;
|
||||
const allEntries = Object.entries(current.value);
|
||||
const capacity = Math.max(
|
||||
0,
|
||||
limits.maximumNodes - nodeCount - pending.length,
|
||||
);
|
||||
const entries = allEntries.slice(0, capacity);
|
||||
if (entries.length < allEntries.length) truncated = true;
|
||||
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
||||
const entry = entries[index];
|
||||
for (let index = allEntries.length - 1; index >= 0; index -= 1) {
|
||||
const entry = allEntries[index];
|
||||
if (entry === undefined) continue;
|
||||
const [name, value] = entry;
|
||||
const path = `${current.path}/${pointerName(name)}`.slice(0, 2_048);
|
||||
const path = `${current.path}/${pointerName(name)}`;
|
||||
const type = jsonValueType(value);
|
||||
const existing = properties.get(path);
|
||||
if (existing === undefined)
|
||||
@@ -118,9 +86,7 @@ export const inferJsonShape = (
|
||||
types: [...value.types].sort(),
|
||||
observations: value.observations,
|
||||
}))
|
||||
.sort((left, right) => left.path.localeCompare(right.path))
|
||||
.slice(0, limits.maximumNodes),
|
||||
truncated,
|
||||
.sort((left, right) => left.path.localeCompare(right.path)),
|
||||
});
|
||||
};
|
||||
|
||||
@@ -139,13 +105,8 @@ const jsonValueType = (value: unknown): JsonValueType => {
|
||||
}
|
||||
};
|
||||
|
||||
const hasChildren = (value: unknown): boolean =>
|
||||
Array.isArray(value)
|
||||
? value.length > 0
|
||||
: isRecord(value) && Object.keys(value).length > 0;
|
||||
|
||||
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
|
||||
const pointerName = (value: string): string =>
|
||||
value.slice(0, 256).replaceAll("~", "~0").replaceAll("/", "~1");
|
||||
value.replaceAll("~", "~0").replaceAll("/", "~1");
|
||||
|
||||
Reference in New Issue
Block a user