Files
rea/src/domain/canonicalDigest.ts
T
morluto 392f1f3107 refactor: consolidate boundary semantics and prune redundant tests
Share parser modes and collision-free composite keys, enforce architecture guards, and consolidate redundant unit assertions into filesystem, CLI, MCP, and producer-backed workflows. Preserve concurrent upstream boundary fixes and exclude legacy Objective-C class-name bookkeeping labels.

Validation: source/composition suites (3358 passing), focused canonical digest and MCP checks, typecheck/lint, Knip, formatting, architecture guards, and real Foundation archive and Apple legacy simulator CLI/MCP verification. No real Hopper, Ghidra, or IDA verification claim.
2026-10-10 05:56:01 +08:00

93 lines
2.9 KiB
TypeScript

import { createHash } from "node:crypto";
/**
* Streaming SHA-256 over canonical JSON. Hashing owner (avoids assembling
* aggregate strings); `comparisonSemantics.canonicalJson` (canonicalize lib)
* is the string-comparison owner. JSON object keys follow RFC 8785's UTF-16
* code-unit order in both; entity-list ordering is a separate concern.
*/
export const digestCanonicalValue = (
value: unknown,
context = "Comparison",
): string => {
const hash = createHash("sha256");
// Batch small emitted parts to cut hash.update crossings.
// Flush cuts only at part boundaries, so the hashed byte stream is unchanged.
let buffered = "";
const emit = (part: string): void => {
buffered += part;
if (buffered.length >= 8192) {
hash.update(buffered);
buffered = "";
}
};
const encoded = emitCanonical(value, emit, new Set());
if (!encoded) throw new TypeError(`${context} could not canonicalize data`);
if (buffered) hash.update(buffered);
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;
// Match canonicalize/RFC 8785, including supplementary-plane keys.
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);
}
};