# thx/resvg-js architecture

> Architecture diagram of the GitHub repository thx/resvg-js, 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/thx/resvg-js
- Repository: https://github.com/thx/resvg-js

## Overview

##### Main Components
- Rust Core: This is the heart of the project where the rendering logic lives. It is implemented in Rust (files in src such as lib.rs, error.rs, fonts.rs, options.rs, and build.rs for build-time integration).
- Node.js Bindings: These are the JavaScript wrappers (e.g., index.js, js-binding.js, index.d.ts) that use the compiled native addon (.node file) and interact with the Rust core via napi‑rs.
- WebAssembly Module: Provided in the wasm directory and referenced in the README. This module uses wasm‑bindgen and offers a pure WebAssembly runtime for browser scenarios (and works with Deno as well).
- Examples, Tests, and Benchmarks: Demonstrated in directories like “example”, “__test__”, and “benchmark”. They provide usage scenarios and performance comparisons, showing how the system interacts end-to-end.
- Platform-Specific npm Packages: Under the “npm” directory there are subdirectories for different OS/architecture deployments, indicating how the project handles packaging and distribution across platforms.
- External Dependencies and Config: These include configuration files for tooling (such as Cargo.toml, package.json, tsconfig.json, ESLint and Prettier configuration files) that help maintain code quality and integration.

##### Component Relationships and Interactions
- The Client/Consumer (Node.js, Deno, Browser) sends an SVG input through an API.
- The JavaScript layer calls the Node native addon (or the WASM module) via the exposed Resvg class.
- The Node native addon or WASM loader, in turn, invokes the Rust core functionality for rendering SVG into PNG.
- Results (such as PNG buffers, computed image dimensions, or transformed SVG) flow back to the JavaScript layer.
- Platform-specific packaging (as seen under the npm directory) ensures that when the package is installed on different systems, the correct binary is used.

##### Architectural Patterns and Design Principles
- Modular Design: The separation between the JS bindings, Rust core, and the WASM module keeps the concerns well isolated.
- Abstraction Layers: There is a clear division between the high-level JavaScript API and the low-level Rust rendering engine. This allows using different backends (native and WASM) with a consistent API.
- Cross-Platform Support: The npm directory and support for Node.js, Deno, and browsers show platform abstraction and conditional packaging.
- Extensibility: The project is structured to allow future expansion (for example, support for additional image formats) without modifying the core functionality.

##### Technologies, Frameworks, and Libraries
- Rust and Cargo: Core logic, error handling, and performance-critical code.
- napi‑rs: Provides the bridge between Node.js and Rust.
- wasm‑bindgen and wasm‑pack: For building the WASM module that runs in browsers.
- JavaScript/TypeScript: For exposing a user-friendly API and type definitions (index.d.ts, tsconfig.json).
- Node.js, Deno, and Browser Environments: Each being supported by the binding layers.
- Testing and Linting Tools: Shown in configuration (.eslintrc.yml, prettierignore, etc.) and test examples (__test__).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Components
    Client["Client (Node.js/Deno/Browser)"]:::js
    ResvgAPI["Resvg‑JS API"]:::js
    NodeBindings["Node.js Bindings (napi‑rs)"]:::js
    WASMModule["WebAssembly Module (wasm‑bindgen)"]:::wasm
    RustCore["Rust Core Engine (resvg logic)"]:::rust

    %% Main Data Flow
    Client -->|"sendSVGinput"| ResvgAPI
    ResvgAPI -->|"calls"| NodeBindings
    ResvgAPI -->|"calls"| WASMModule
    NodeBindings -->|"invokes"| RustCore
    WASMModule -->|"invokes"| RustCore
    RustCore -->|"returnsPNG/dims"| ResvgAPI
    ResvgAPI -->|"returnsResult"| Client

    %% Supporting Modules Subgraph
    subgraph "Supporting Modules"
        direction TB
        Examples["Examples"]:::support
        Tests["Tests"]:::support
        Benchmarks["Benchmarks"]:::support
        PlatformPackages["Platform Packages"]:::support
        ExternalConfig["External Dependencies & Config"]:::support
    end

    %% Supporting Relationships (dotted arrows)
    Examples -.->|"usage"| ResvgAPI
    Tests -.->|"testing"| ResvgAPI
    Benchmarks -.->|"performance"| RustCore
    PlatformPackages -.->|"packaging"| NodeBindings
    ExternalConfig -.->|"config"| RustCore

    %% Click Events
    click RustCore "https://github.com/thx/resvg-js/tree/main/src"
    click NodeBindings "https://github.com/thx/resvg-js/blob/main/index.js"
    click WASMModule "https://github.com/thx/resvg-js/tree/main/wasm"
    click Examples "https://github.com/thx/resvg-js/tree/main/example"
    click Tests "https://github.com/thx/resvg-js/tree/main/__test__"
    click Benchmarks "https://github.com/thx/resvg-js/blob/main/benchmark/bench.ts"
    click PlatformPackages "https://github.com/thx/resvg-js/tree/main/npm"
    click ExternalConfig "https://github.com/thx/resvg-js/blob/main/Cargo.toml"

    %% Styles
    classDef js fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef rust fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef wasm fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef support fill:#F0E68C,stroke:#000,stroke-width:2px;
```
