# unjs/ohash architecture

> Architecture diagram of the GitHub repository unjs/ohash, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/unjs/ohash
- Repository: https://github.com/unjs/ohash
- Video tour (about a minute): https://gitdiagram.com/unjs/ohash/video

## Overview

##### Project Type and Purpose
- This is an open‐source utility library (npm package) focused on object hashing, serialization, and comparison.
- Its main purpose is to expose simple, yet robust, functions (hash, serialize, digest, isEqual, diff) to process various JavaScript objects and values.

##### File Structure
- Top-level files (e.g., package.json, build.config.ts, tsconfig.json, eslint.config.mjs, and various configuration files) indicate that this is a well-configured, TypeScript-based project.
- The “src” directory is where the core implementation lives:
- src/index.ts likely serves as the entry point that exports the main API functions.
- src/hash.ts, src/serialize.ts provide dedicated functions for generating object hashes and serializing objects.
- The “src/crypto” directory is neatly split into “js” and “node” subdirectories, suggesting that the cryptographic (SHA-256 digest) functionality is implemented differently depending on the runtime environment. They abstract out environment-specific crypto utilities.
- The “src/utils” directory houses utility functions such as diffing (src/utils/diff.ts) and possibly other helper functions like is-equal (src/utils/is-equal.ts).
- The “test” directory reinforces that there is a robust testing suite covering various aspects (benchmarks, unit tests for hash, serialize, crypto, and utils).

##### Core Components
a. API Entry Point:
- Src/index.ts as the primary interface.
- Exported functions: hash, serialize, digest, isEqual.
b. Hashing and Serialization Layer:
- That the “hash” function uses the “serialize” function before generating hashes.
c. Cryptography Module:
- Split into two sub-modules: one for “js” (src/crypto/js/index.ts) and one for “node” (src/crypto/node/index.ts).
d. Utility Functions:
- “Utils Module” with components like diff (src/utils/diff.ts) and is-equal (src/utils/is-equal.ts).
- That these utility functions may internally depend on the serialization logic to compare or produce diff outputs.
e. Testing Suite:
- A component labeled “Test Suite” (from the test directory) to indicate that the system’s behavior is validated via unit tests and benchmarks.

##### Relationships and Interactions
- That “Hash Module” relies on the “Serialization Module” before hashing.
- Conditional or alternative paths within the “Cryptography Module” that direct calls (from “digest”) to either the “js” or “node” implementation depending on the runtime.
- That utility functions (like diff) use both serialization and hash comparisons internally.

##### Architectural Patterns & Important Details
- The design is modular, following separation of concerns – each functionality (hashing, serialization, crypto, utility diffing) is encapsulated in its own module.
- There is a clear abstraction for environment-specific behavior for cryptographic functions, ensuring compatibility with both browsers (JS) and Node.js.
- The project follows best practices for open source libraries by isolating business logic from test code and build configurations.
- Technologies: TypeScript is used, along with Node’s and browser-specific implementations for crypto. The project relies on standard test runners (as seen in the test directory) and uses build and linting tools.

## Diagram (Mermaid)

```mermaid
graph TD
    %% API Layer
    subgraph "API Layer"
        API["API Entry Point (src/index.ts)"]:::api
    end

    %% Core Modules
    subgraph "Core Modules"
        HS["Hash Module (src/hash.ts)"]:::module
        SS["Serialization Module (src/serialize.ts)"]:::module
    end

    %% Cryptography Modules
    subgraph "Cryptography Modules"
        CryptoJS["Crypto JS (src/crypto/js/index.ts)"]:::crypto
        CryptoNode["Crypto Node (src/crypto/node/index.ts)"]:::crypto
    end

    %% Utilities
    subgraph "Utilities"
        Diff["Utility: Diff (src/utils/diff.ts)"]:::utils
        IsEqual["Utility: isEqual (src/utils/is-equal.ts)"]:::utils
    end

    %% Testing Suite
    subgraph "Testing"
        TEST["Test Suite (test)"]:::test
    end

    %% Connections from API Layer to Core Modules
    API -->|"hash"| HS
    API -->|"serialize"| SS

    %% Hash Module depends on Serialization Module
    HS -->|"uses"| SS

    %% Connections from API Layer to Cryptography Modules for digest
    API -->|"digest"| CryptoJS
    API -->|"digest"| CryptoNode

    %% Connection from API Layer to Utilities (isEqual exported)
    API -->|"isEqual"| IsEqual

    %% Utilities dependencies on Core Modules
    Diff -->|"uses_serialization"| SS
    Diff -->|"may_use_hash"| HS
    IsEqual -->|"uses_serialization"| SS

    %% Testing suite validates API
    TEST -->|"validates"| API

    %% Click Events
    click API "https://github.com/unjs/ohash/blob/main/src/index.ts"
    click HS "https://github.com/unjs/ohash/blob/main/src/hash.ts"
    click SS "https://github.com/unjs/ohash/blob/main/src/serialize.ts"
    click CryptoJS "https://github.com/unjs/ohash/blob/main/src/crypto/js/index.ts"
    click CryptoNode "https://github.com/unjs/ohash/blob/main/src/crypto/node/index.ts"
    click Diff "https://github.com/unjs/ohash/blob/main/src/utils/diff.ts"
    click IsEqual "https://github.com/unjs/ohash/blob/main/src/utils/is-equal.ts"
    click TEST "https://github.com/unjs/ohash/tree/main/test"

    %% Styles
    classDef api fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef module fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef crypto fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef utils fill:#F4A460,stroke:#333,stroke-width:2px;
    classDef test fill:#D3D3D3,stroke:#333,stroke-width:2px;
```
