autotel
Version:
Write Once, Observe Anywhere
23 lines (21 loc) • 943 B
JavaScript
import * as nodeCrypto from "node:crypto";
//#region src/stable-hash.ts
/**
* Deterministic JSON stringify with sorted object keys, so two structurally
* equal values always produce the same string regardless of key insertion
* order. Shared by `defineEvent` and the validation layer for stable schema
* hashes.
*/
function stableStringify(value) {
if (value === null || value === void 0 || typeof value !== "object") return JSON.stringify(value);
if (Array.isArray(value)) return "[" + value.map((v) => stableStringify(v)).join(",") + "]";
const obj = value;
return "{" + Object.keys(obj).toSorted().map((k) => JSON.stringify(k) + ":" + stableStringify(obj[k])).join(",") + "}";
}
/** Stable sha256 of any JSON-serializable value. */
function hashJson(value) {
return nodeCrypto.createHash("sha256").update(stableStringify(value)).digest("hex");
}
//#endregion
export { hashJson as t };
//# sourceMappingURL=stable-hash-ChFBIhNt.js.map