OC Hash Builder
oc-hash-builder is a utility library within the OpenComponents (OC) ecosystem, designed to provide deterministic hashing capabilities. Its primary role is to generate consistent hashes for component-related data, configurations, and content, which is crucial for OpenComponents' internal mechanisms such as caching, component identification, and versioning. As of version 1.0.5, it represents a stable initial release. The library's release cadence is likely tied to the broader OpenComponents project, focusing on stability and integration rather than frequent, independent updates. A key differentiator is its purpose-built nature for the OpenComponents framework, ensuring compatibility and adherence to OC's specific requirements for managing and deploying micro-frontends. It ships with TypeScript types, facilitating its use in modern JavaScript and TypeScript projects.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'buildHash')
cause Attempting to import `buildHash` (or similar) from `oc-hash-builder` using CommonJS `require()` syntax when the package is primarily ESM, or vice-versa.fixIf using Node.js with ESM, ensure your `package.json` has `"type": "module"` or use `.mjs` extension, and use `import { buildHash } from 'oc-hash-builder';`. For CommonJS, try `const { buildHash } = require('oc-hash-builder');` (though ESM is more likely preferred). -
Hash mismatch for identical component configurations.
cause The hashing function might be sensitive to property order in objects or other minor differences (e.g., whitespace in strings, differing number types) that are semantically identical but produce different serialized forms.fixInspect the `oc-hash-builder`'s documentation or source for details on how it handles object serialization. Ensure inputs are consistently formatted (e.g., sorted object keys, normalized whitespace) before hashing. If hashing custom classes, ensure a consistent serialization method like `toJSON()` is implemented if the hash builder relies on it.
Warnings
- gotcha Deterministic hashing is crucial. If the library processes objects, ensure it handles property order consistently (e.g., by sorting keys) to produce identical hashes for logically equivalent inputs.
- deprecated Older hashing algorithms like SHA-1 are cryptographically weak and deprecated for security-sensitive contexts. While `oc-hash-builder`'s primary use might be for unique identification rather than cryptographic security, using stronger algorithms like SHA-256 is a best practice. The OpenComponents ecosystem has shown awareness of SHA-1 deprecation in related projects (e.g., openclaw shifted to SHA-256).
- gotcha Hashing mutable objects can lead to inconsistent hashes if the object changes after being hashed, breaking cache integrity or component identification. The hash should represent a fixed state.
Install
-
npm install oc-hash-builder -
yarn add oc-hash-builder -
pnpm add oc-hash-builder
Imports
- buildHash
const buildHash = require('oc-hash-builder');import { buildHash } from 'oc-hash-builder'; - HashBuilder
import { HashBuilder } from 'oc-hash-builder'; - default
import hashBuilder from 'oc-hash-builder';
Quickstart
import { buildHash } from 'oc-hash-builder';
// Example 1: Hashing a simple object
const componentConfig = {
name: 'my-component',
version: '1.0.0',
dependencies: ['lodash@4.17.21'],
settings: {
env: 'production'
}
};
const configHash = buildHash(componentConfig);
console.log(`Hash for component config: ${configHash}`);
// Example 2: Hashing a string (e.g., component template content)
const templateContent = `<div>Hello, {{name}}!</div>`;
const contentHash = buildHash(templateContent);
console.log(`Hash for template content: ${contentHash}`);
// Example 3: Ensuring deterministic hashing
const sameConfigDifferentOrder = {
settings: {
env: 'production'
},
dependencies: ['lodash@4.17.21'],
name: 'my-component',
version: '1.0.0'
};
const orderIndependentHash = buildHash(sameConfigDifferentOrder);
console.log(`Hash for reordered config (should be same): ${orderIndependentHash}`);
// In a real OpenComponents context, this might be used internally or exposed
// via a plugin system for ensuring component integrity and cache keys.
// For instance, during component publishing or rendering lifecycle.