Canonical JSON Serializer & Hasher (lib/canonical.mjs)

factory · event-runtime/lib/canonical.mjs · javascript · Apache-2.0
infra, security, tag:json, tag:crypto, tag:deterministic, tag:serialization

Serializes objects with recursively sorted keys and provides SHA‑256 hashing utilities for bytes, JSON strings, and arbitrary values, ensuring deterministic identities across the event runtime.

// Source: https://github.com/watt-mind/factory | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Canonical JSON and content hashes. Every deterministic identity in the
 * runtime — payload hashes, artifact hashes, spec hashes, lifecycle record
 * hashes — derives from these bytes (docs/event-runtime.md §9–§10), so key
 * order and formatting can never matter and two writers can never disagree
 * about the hash of the same value.
 */
import { createHash } from "node:crypto";

/**
 * Serialize with object keys sorted recursively. Undefined object properties
 * are dropped (as JSON.stringify does); undefined array elements and
 * non-finite numbers are errors rather than silent

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key