# contentauth/c2pa-node architecture

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

- Interactive diagram: https://gitdiagram.com/contentauth/c2pa-node
- Repository: https://github.com/contentauth/c2pa-node

## Overview

##### Project type and purpose
- This is an open-source Node.js library with a native Rust core (via Neon) that:
- Reads and validates C2PA manifests in media files
- Adds signed manifests to media files
- It ships as a Node module (`c2pa-node`) and includes prebuilt binaries plus a build path for custom platforms.

##### File structure
- Top-level languages:
- js-src/ : TypeScript/JavaScript wrapper code
- src/ : Rust code (the core logic exposed as a native addon)
- Build/config:
- Cargo.toml, Rust build scripts (`pnpm build:rust`, postinstall.js)
- package.json, pnpm-lock.yaml, neon bindings (scripts/postinstall.js)
- API docs under api-docs/
- Tests under tests/ (Jest + integration fixtures)
- CI/workflows (.github/workflows/*)

##### README for additional insights
- Explains dual-language build (Node + Rust)
- Highlights two signing mechanisms: local (Rust) and remote (JS HTTP)
- Emphasizes environment variable `C2PA_LIBRARY_PATH` to load the native module

##### Main components and their interactions
a. Components
- Node.js Consumer App
- `c2pa-node` Package
– JS API Layer (js-src/index.ts + lib/*.ts)
– Neon Loader (c2pa.node native addon)
- Rust Core Library
– manifest building, hashing, signing, thumbnail generation
– modules: asset.rs, ingredient.rs, sign/{local.rs,remote.rs}
- External Services
– Remote signer endpoint (called by `sign/remote.rs` via HTTP)
- Developer Tooling
– Build scripts, CI pipelines, tests, docs
b. Relationships & data flow
1. Consumer App → imports JS API
2. JS API → loads Neon addon (`c2pa.node`)
3. Neon addon → bridges into Rust functions
4. Rust core  ↔ file system (reads/writes media)
5. Rust core → optional HTTP call for remote signing
6. Results bubble back through Neon to JS consumer
c. Architectural patterns & design principles
- Layered architecture (JS façade → native addon → core logic)
- Adapter pattern for local vs. remote signer
- Single responsibility modules in Rust and JS
- Extensibility via interfaces in js-src/lib and Rust traits
d. Key technologies/frameworks
- Node.js + pnpm, TypeScript
- Rust + Neon bindings
- Jest for testing
- GitHub Actions for CI
- API docs generated alongside code

## Diagram (Mermaid)

```mermaid
flowchart TD
    ConsumerApp["Consumer App"]:::external

    subgraph "c2pa-node Package"
        subgraph "JS API Layer"
            JS_Index["index.ts"]:::js
            JS_MB["manifestBuilder.ts"]:::js
            JS_Signer["signer.ts"]:::js
            JS_Hash["hash.ts"]:::js
            JS_Thumb["thumbnail.ts"]:::js
        end
        subgraph "Neon Loader"
            Bindings["bindings.ts"]:::js
            Postinstall["postinstall.js"]:::tooling
        end
        subgraph "Rust Core Library"
            Rust_Lib["lib.rs"]:::native
            Asset["asset.rs"]:::native
            Ingredient["ingredient.rs"]:::native
            Runtime["runtime.rs"]:::native
            Error["error.rs"]:::native
            subgraph "Signer Implementations"
                SignMod["mod.rs"]:::native
                LocalSign["local.rs"]:::native
                RemoteSign["remote.rs"]:::native
            end
        end
    end

    FileSystem["File System"]:::external
    RemoteService["Remote Signer Service"]:::external
    APIDocs["API Documentation"]:::tooling
    CI["CI/CD Workflows"]:::tooling
    Tests["Tests & Fixtures"]:::tooling

    ConsumerApp -->|"imports"| JS_Index
    JS_Index -->|"uses"| JS_MB
    JS_Index -->|"uses"| JS_Signer
    JS_Index -->|"uses"| JS_Hash
    JS_Index -->|"uses"| JS_Thumb
    JS_Index -->|"loads"| Bindings
    Bindings -->|"bridges to"| Rust_Lib
    Rust_Lib --> Asset
    Rust_Lib --> Ingredient
    Rust_Lib --> Runtime
    Rust_Lib --> Error
    Rust_Lib --> SignMod
    SignMod --> LocalSign
    SignMod --> RemoteSign
    Rust_Lib -->|"reads/writes"| FileSystem
    RemoteSign -->|"HTTP call"| RemoteService
    FileSystem -->|"returns data"| Rust_Lib
    RemoteService -->|"returns signed manifest"| RemoteSign
    Rust_Lib -->|"result"| Bindings
    Bindings -->|"returns result"| JS_Index
    JS_Index -->|"result"| ConsumerApp

    ConsumerApp --> APIDocs
    ConsumerApp --> CI
    ConsumerApp --> Tests

    click JS_Index "https://github.com/contentauth/c2pa-node/blob/main/js-src/index.ts"
    click JS_MB "https://github.com/contentauth/c2pa-node/blob/main/js-src/lib/manifestBuilder.ts"
    click JS_Signer "https://github.com/contentauth/c2pa-node/blob/main/js-src/lib/signer.ts"
    click JS_Hash "https://github.com/contentauth/c2pa-node/blob/main/js-src/lib/hash.ts"
    click JS_Thumb "https://github.com/contentauth/c2pa-node/blob/main/js-src/lib/thumbnail.ts"
    click Bindings "https://github.com/contentauth/c2pa-node/blob/main/js-src/bindings.ts"
    click Postinstall "https://github.com/contentauth/c2pa-node/blob/main/scripts/postinstall.js"
    click Rust_Lib "https://github.com/contentauth/c2pa-node/blob/main/src/lib.rs"
    click Asset "https://github.com/contentauth/c2pa-node/blob/main/src/asset.rs"
    click Ingredient "https://github.com/contentauth/c2pa-node/blob/main/src/ingredient.rs"
    click Runtime "https://github.com/contentauth/c2pa-node/blob/main/src/runtime.rs"
    click Error "https://github.com/contentauth/c2pa-node/blob/main/src/error.rs"
    click SignMod "https://github.com/contentauth/c2pa-node/blob/main/src/sign/mod.rs"
    click LocalSign "https://github.com/contentauth/c2pa-node/blob/main/src/sign/local.rs"
    click RemoteSign "https://github.com/contentauth/c2pa-node/blob/main/src/sign/remote.rs"
    click RemoteService "https://github.com/contentauth/c2pa-node/blob/main/tests/mocks/remote-signer.ts"
    click APIDocs "https://github.com/contentauth/c2pa-node/tree/main/api-docs/"
    click CI "https://github.com/contentauth/c2pa-node/blob/main/.github/workflows/*.yml"
    click Tests "https://github.com/contentauth/c2pa-node/tree/main/tests/"

    classDef js fill:#dae8fc,stroke:#6c8ebf,color:#000
    classDef native fill:#ffe6cc,stroke:#d79b00,color:#000
    classDef external fill:#d5e8d4,stroke:#82b366,color:#000
    classDef tooling fill:#f2f2f2,stroke:#999,color:#000
```
