fix(javascript): hash graph and evidence JSON incrementally (#646)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
/** Hash canonicalize-compatible JSON without assembling an aggregate string. */
|
||||
export const digestCanonicalValue = (
|
||||
value: unknown,
|
||||
context = "Comparison",
|
||||
): string => {
|
||||
const hash = createHash("sha256");
|
||||
const encoded = emitCanonical(value, (part) => hash.update(part), new Set());
|
||||
if (!encoded) throw new TypeError(`${context} could not canonicalize data`);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
|
||||
type Emit = (part: string) => void;
|
||||
|
||||
const emitCanonical = (
|
||||
value: unknown,
|
||||
emit: Emit,
|
||||
ancestors: Set<object>,
|
||||
): boolean => {
|
||||
if (typeof value === "number" && !Number.isFinite(value))
|
||||
throw new Error(
|
||||
Number.isNaN(value) ? "NaN is not allowed" : "Infinity is not allowed",
|
||||
);
|
||||
if (value === null || typeof value !== "object") {
|
||||
const encoded = JSON.stringify(value);
|
||||
if (encoded === undefined) return false;
|
||||
emit(encoded);
|
||||
return true;
|
||||
}
|
||||
if (ancestors.has(value)) throw new Error("Circular reference detected");
|
||||
ancestors.add(value);
|
||||
try {
|
||||
if ("toJSON" in value && typeof value.toJSON === "function") {
|
||||
const normalized: unknown = value.toJSON();
|
||||
return emitCanonical(normalized, emit, ancestors);
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
emit("[");
|
||||
const length = value.length;
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
if (index > 0) emit(",");
|
||||
// Preserve canonicalize's sparse-array and unsupported-value behavior.
|
||||
if (!(index in value)) continue;
|
||||
const item: unknown = value[index];
|
||||
emitCanonical(
|
||||
item === undefined || typeof item === "symbol" ? null : item,
|
||||
emit,
|
||||
ancestors,
|
||||
);
|
||||
}
|
||||
emit("]");
|
||||
} else {
|
||||
emit("{");
|
||||
let first = true;
|
||||
for (const key of Object.keys(value).sort()) {
|
||||
if (
|
||||
Reflect.get(value, key) === undefined ||
|
||||
typeof Reflect.get(value, key) === "symbol"
|
||||
)
|
||||
continue;
|
||||
const item: unknown = Reflect.get(value, key);
|
||||
if (!first) emit(",");
|
||||
first = false;
|
||||
emit(JSON.stringify(key));
|
||||
emit(":");
|
||||
if (!emitCanonical(item, emit, ancestors)) emit("undefined");
|
||||
}
|
||||
emit("}");
|
||||
}
|
||||
return true;
|
||||
} finally {
|
||||
ancestors.delete(value);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user