# stefanwerfling/node-rohc architecture

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

- Interactive diagram: https://gitdiagram.com/stefanwerfling/node-rohc
- Repository: https://github.com/stefanwerfling/node-rohc

## Overview

##### Project type and purpose
- This is a Node.js native‐add-on library (“node-rohc”) written in TypeScript and C++/N-API.
- Purpose: provide JavaScript/TypeScript bindings to the external ROHC (Robust Header Compression) C library so that Node apps can compress and decompress IP packets.

##### File structure
- Top‐level:
- package.json, binding.gyp, tsconfig.json – configuration for npm package, native build, TS build.
- src/ts – TypeScript wrapper (RohcAddon.ts) exposing a high‐level JS API.
- src/cpp – Native C++ implementation (njsrohc.cpp, rohc-napi.cpp) using N-API to bridge JS ⇄ ROHC library.
- dist – transpiled output (JS, declaration files, sourcemaps) ready for publish.
- test – a JS integration test demonstrating usage.
- Key patterns:
- Two‐layer API: TS layer delegates to compiled native module.
- Native binding built with node-gyp (binding.gyp).
- External dependency: system‐installed ROHC C library and its headers.

##### README for additional insights
- Describes how to install the ROHC C library, then install “node-rohc”.
- Shows usage: import {Rohc} from 'node-rohc', call compress()/decompress().
- Mentions versioning, context & packet info methods.

##### Main components & interactions
a. Components:
1. User Application (JS or TS code)
2. node-rohc TS API layer (Rohc class in RohcAddon.ts)
3. Compiled N-API native module (rohc-napi.node)
4. Underlying ROHC C library (librohc)
5. System environment / OS libraries (libtool, libpcap, cmocka, etc.)
b. Relationships:
- App calls Rohc methods → TS API
- TS API uses N-API calls into native .node binary
- Native code invokes functions in the ROHC C library
- ROHC library performs compress/decompress, returns buffers
- Native layer translates results back to JS buffers & metadata
c. Architectural patterns:
- Adapter/Facade: TS layer abstracts complexity of native calls.
- Native‐binding pattern: N-API code wrapping external C library.
- Build pipeline: TS → transpile → dist; C++ → node-gyp → .node binary.
d. Technologies:
- TypeScript for high‐level API
- Node-API (N-API) for native bindings
- Node-gyp / binding.gyp build system
- External ROHC C library

##### Putting it all together
- Layer with directory/file references:
- TS layer: src/ts/RohcAddon.ts → dist/src/ts/RohcAddon.js
- Native layer: src/cpp/rohc-napi.cpp + binding.gyp → build/Release/rohc.node
- External lib: /usr/local/lib/librohc.so

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        App["User Application\n(test/test.js, index.ts)"]:::js
    end

    %% TypeScript Layer
    subgraph "TypeScript Layer"
        TS["Rohc TS API\n(src/ts/RohcAddon.ts)"]:::ts
        DistTS["dist/src/ts/RohcAddon.js"]:::tsDist
        DistTSdts["dist/src/ts/RohcAddon.d.ts"]:::tsDist
        DistTSmap["dist/src/ts/RohcAddon.js.map"]:::tsDist
    end

    %% Native Binding Layer
    subgraph "Native Binding Layer"
        binding["binding.gyp"]:::config
        NativeMod["Native N-API Module\n(build/Release/rohc.node)"]:::native
        CPP["C++ Source\n(src/cpp/njsrohc.cpp,\nsrc/cpp/njsrohc.h,\nsrc/cpp/rohc-napi.cpp,\nsrc/cpp/dbg.h)"]:::native
    end

    %% Distribution Package
    subgraph "Distribution Package"
        DistEntryJS["dist/index.js"]:::dist
        DistEntryDTS["dist/index.d.ts"]:::dist
        DistEntryMap["dist/index.js.map"]:::dist
    end

    %% C Library Layer
    subgraph "C Library Layer"
        CLib["ROHC C Library\n(/usr/local/lib/librohc.so)"]:::external
    end

    %% System Libraries
    subgraph "System Libraries"
        OS["OS Dependencies\n(libtool, libpcap, cmocka)"]:::os
    end

    %% Build Configurations
    tsconfig["tsconfig.json"]:::config
    pkg["package.json"]:::config

    %% Data Flow
    App -->|"calls compress()/decompress()"| TS
    TS -->|"requires .node via N-API"| NativeMod
    NativeMod -->|"invokes functions"| CLib
    CLib -->|"uses system libs"| OS

    %% Build Flow
    tsconfig -.->|"tsc → dist"| DistTS
    tsconfig -.-> DistTSdts
    tsconfig -.-> DistTSmap
    pkg -.-> TS
    binding -.->|"node-gyp build"| NativeMod

    TS --> DistEntryJS
    TS --> DistEntryDTS
    TS --> DistEntryMap

    %% Return Flows
    OS -->|"returns status"| CLib
    CLib -->|"buffers & metadata"| NativeMod
    NativeMod -->|"JS Buffers"| TS
    TS -->|"results/errors"| App

    %% Click Events
    click App "https://github.com/stefanwerfling/node-rohc/blob/main/test/test.js"
    click App "https://github.com/stefanwerfling/node-rohc/blob/main/index.ts"
    click TS "https://github.com/stefanwerfling/node-rohc/blob/main/src/ts/RohcAddon.ts"
    click DistTS "https://github.com/stefanwerfling/node-rohc/blob/main/dist/src/ts/RohcAddon.js"
    click DistTSdts "https://github.com/stefanwerfling/node-rohc/blob/main/dist/src/ts/RohcAddon.d.ts"
    click DistTSmap "https://github.com/stefanwerfling/node-rohc/blob/main/dist/src/ts/RohcAddon.js.map"
    click binding "https://github.com/stefanwerfling/node-rohc/blob/main/binding.gyp"
    click NativeMod "https://github.com/stefanwerfling/node-rohc/blob/main/build/Release/rohc.node"
    click CPP "https://github.com/stefanwerfling/node-rohc/blob/main/src/cpp/njsrohc.cpp"
    click CPP "https://github.com/stefanwerfling/node-rohc/blob/main/src/cpp/njsrohc.h"
    click CPP "https://github.com/stefanwerfling/node-rohc/blob/main/src/cpp/rohc-napi.cpp"
    click CPP "https://github.com/stefanwerfling/node-rohc/blob/main/src/cpp/dbg.h"
    click DistEntryJS "https://github.com/stefanwerfling/node-rohc/blob/main/dist/index.js"
    click DistEntryDTS "https://github.com/stefanwerfling/node-rohc/blob/main/dist/index.d.ts"
    click DistEntryMap "https://github.com/stefanwerfling/node-rohc/blob/main/dist/index.js.map"
    click tsconfig "https://github.com/stefanwerfling/node-rohc/blob/main/tsconfig.json"
    click pkg "https://github.com/stefanwerfling/node-rohc/blob/main/package.json"

    %% Styles
    classDef js fill:#AED6F1,stroke:#21618C,color:#1B2631;
    classDef ts fill:#D6EAF8,stroke:#1B4F72,color:#1B2631;
    classDef tsDist fill:#EBF5FB,stroke:#2980B9,color:#1B2631;
    classDef native fill:#A9DFBF,stroke:#196F3D,color:#1B2631;
    classDef external fill:#F5CBA7,stroke:#B9770E,color:#1B2631;
    classDef os fill:#E5E7E9,stroke:#5D6D7E,color:#1B2631;
    classDef config fill:#FCF3CF,stroke:#B7950B,color:#1B2631;
    classDef dist fill:#E8DAEF,stroke:#7D3C98,color:#1B2631;
```
