2026-07-22 20:38:26 +08:00
|
|
|
import { createHash } from "node:crypto";
|
|
|
|
|
|
|
|
|
|
import canonicalize from "canonicalize";
|
|
|
|
|
|
2026-10-09 03:30:31 -07:00
|
|
|
/**
|
|
|
|
|
* Canonically encode one export-shape comparison value.
|
|
|
|
|
*
|
|
|
|
|
* Comparison identifiers hash the full result semantics, including each
|
|
|
|
|
* change's presence object and the per-variant property inventories.
|
|
|
|
|
*/
|
2026-07-22 20:38:26 +08:00
|
|
|
export const canonicalExportShapeValue = (value: unknown): string => {
|
|
|
|
|
const encoded = canonicalize(value);
|
|
|
|
|
if (encoded === undefined)
|
|
|
|
|
throw new TypeError(
|
|
|
|
|
"JavaScript export shape comparison could not canonicalize data",
|
|
|
|
|
);
|
|
|
|
|
return encoded;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Derive one deterministic SHA-256 comparison identifier component. */
|
|
|
|
|
export const digestExportShapeValue = (value: unknown): string =>
|
|
|
|
|
createHash("sha256").update(canonicalExportShapeValue(value)).digest("hex");
|