autotel
Version:
Write Once, Observe Anywhere
30 lines (28 loc) • 1.14 kB
JavaScript
const require_chunk = require('./chunk-C_NdSu1c.cjs');
let node_crypto = require("node:crypto");
node_crypto = require_chunk.__toESM(node_crypto, 1);
//#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 node_crypto.createHash("sha256").update(stableStringify(value)).digest("hex");
}
//#endregion
Object.defineProperty(exports, 'hashJson', {
enumerable: true,
get: function () {
return hashJson;
}
});
//# sourceMappingURL=stable-hash-brKISGf1.cjs.map