{"id":14777,"library":"oc-hash-builder","title":"OC Hash Builder","description":"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.","status":"active","version":"1.0.5","language":"javascript","source_language":"en","source_url":"https://github.com/opencomponents/base-templates","tags":["javascript","oc","opencomponents","typescript"],"install":[{"cmd":"npm install oc-hash-builder","lang":"bash","label":"npm"},{"cmd":"yarn add oc-hash-builder","lang":"bash","label":"yarn"},{"cmd":"pnpm add oc-hash-builder","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Assumed named export based on common utility patterns and TypeScript types. ESM is preferred.","wrong":"const buildHash = require('oc-hash-builder');","symbol":"buildHash","correct":"import { buildHash } from 'oc-hash-builder';"},{"note":"If the utility is exposed as a class for more complex scenarios or configuration.","symbol":"HashBuilder","correct":"import { HashBuilder } from 'oc-hash-builder';"},{"note":"Possible default export for a simpler, direct hashing function if no specific configurations are needed.","symbol":"default","correct":"import hashBuilder from 'oc-hash-builder';"}],"quickstart":{"code":"import { buildHash } from 'oc-hash-builder';\n\n// Example 1: Hashing a simple object\nconst componentConfig = {\n  name: 'my-component',\n  version: '1.0.0',\n  dependencies: ['lodash@4.17.21'],\n  settings: {\n    env: 'production'\n  }\n};\nconst configHash = buildHash(componentConfig);\nconsole.log(`Hash for component config: ${configHash}`);\n\n// Example 2: Hashing a string (e.g., component template content)\nconst templateContent = `<div>Hello, {{name}}!</div>`;\nconst contentHash = buildHash(templateContent);\nconsole.log(`Hash for template content: ${contentHash}`);\n\n// Example 3: Ensuring deterministic hashing\nconst sameConfigDifferentOrder = {\n  settings: {\n    env: 'production'\n  },\n  dependencies: ['lodash@4.17.21'],\n  name: 'my-component',\n  version: '1.0.0'\n};\nconst orderIndependentHash = buildHash(sameConfigDifferentOrder);\nconsole.log(`Hash for reordered config (should be same): ${orderIndependentHash}`);\n\n// In a real OpenComponents context, this might be used internally or exposed\n// via a plugin system for ensuring component integrity and cache keys.\n// For instance, during component publishing or rendering lifecycle.","lang":"typescript","description":"Demonstrates how to use `oc-hash-builder` to create deterministic hashes for objects and strings, which is critical for identifying and caching OpenComponents artifacts."},"warnings":[{"fix":"Always use stable versions and verify output if relying on hash stability across different environments or code changes. Avoid manually changing the input structure if order-dependent hashing is suspected.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Check the underlying hashing algorithm used by `oc-hash-builder`. If it's SHA-1, consider contributing or requesting an update to SHA-256 or a stronger algorithm for new versions, especially if the hashes are used in security-relevant ways. If you need cryptographic strength, use a dedicated crypto library.","message":"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).","severity":"deprecated","affected_versions":"<=1.0.5"},{"fix":"Always hash immutable data structures or create a deep copy of mutable objects before hashing them. Ensure that once an object is hashed and its hash is used as an identifier, the object itself is not modified.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"If 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).","cause":"Attempting to import `buildHash` (or similar) from `oc-hash-builder` using CommonJS `require()` syntax when the package is primarily ESM, or vice-versa.","error":"TypeError: Cannot read properties of undefined (reading 'buildHash')"},{"fix":"Inspect 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.","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.","error":"Hash mismatch for identical component configurations."}],"ecosystem":"npm"}