# alexislours/optimage architecture

> Architecture diagram of the GitHub repository alexislours/optimage, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexislours/optimage
- Repository: https://github.com/alexislours/optimage

## Overview

Project Type: This is a Node.js native module written in Rust that provides image processing capabilities through a JavaScript/TypeScript interface.

##### Interface Layer
- JavaScript/TypeScript API interface (index.d.ts)
- Main `convert` function as the primary entry point

##### Native Bridge
- Connection between JavaScript and Rust (NAPI bridge)
- How the native module is loaded and initialized

##### Core Processing
- Rust implementation (src/lib.rs)
- Image processing pipeline stages

##### Build System
- Relationship between Cargo (Rust) and npm (Node.js) systems

**┌─────────────────────────────────────┐**
- JavaScript API            │
- (TypeScript Definitions / Node.js) │
- ┘
- ↓
**┌─────────────────────────────────────┐**
- Native Bridge Layer         │
- (NAPI Bridge)            │
- ┘
- ↓
**┌─────────────────────────────────────┐**
- Rust Image Processing         │
- (Buffer Handling, Conversion,     │
- Resizing, Optimization)        │
- ┘

##### Processing Details
- Image format conversion options
- Resizing capabilities
- Filter types
- Buffer transformation steps

##### Technical Details
- Supported image formats (webp, avif, jpg, etc.)
- Available filter types (nearest, lanczos3, etc.)
- Platform support (Linux x86, macOS ARM64)

##### Implementation Notes
- Prebuilt binary availability
- Rust toolchain requirements
- Build process flow

Special Considerations:

1. Simplicity of the public API (single `convert` function) while showing the complexity of the underlying implementation.

##### Notes about error handling and buffer processing paths.

Level of Detail:
- Keep the high-level flow clear and simple
- Technical details in sub-components
- Configuration options without cluttering the main flow
- Platform-specific considerations in a separate section

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "JavaScript/TypeScript Layer"
        JSApi["JavaScript API Interface"]:::jsapi
        TypeDefs["TypeScript Definitions"]:::jsapi
    end

    subgraph "Native Bridge Layer"
        NapiBridge["NAPI Bridge"]:::bridge
        InitModule["Module Initialization"]:::bridge
    end

    subgraph "Rust Core Processing"
        RustImpl["Rust Implementation"]:::rust
        ImageProc["Image Processing Pipeline"]:::rust
        BufferHandler["Buffer Handling"]:::rust
        Conversion["Format Conversion"]:::rust
        Resizing["Image Resizing"]:::rust
        Optimization["Image Optimization"]:::rust
    end

    subgraph "Build System"
        CargoSystem["Cargo (Rust)"]:::build
        NpmSystem["npm (Node.js)"]:::build
        BuildScript["Build Scripts"]:::build
    end

    JSApi --> NapiBridge
    TypeDefs --> JSApi
    NapiBridge --> InitModule
    InitModule --> RustImpl
    RustImpl --> BufferHandler
    BufferHandler --> ImageProc
    ImageProc --> Conversion
    ImageProc --> Resizing
    ImageProc --> Optimization
    RustImpl --> CargoSystem
    CargoSystem --> NpmSystem
    NpmSystem --> BuildScript

    %% Component Mappings
    click JSApi "https://github.com/alexislours/optimage/blob/main/index.d.ts"
    click RustImpl "https://github.com/alexislours/optimage/blob/main/src/lib.rs"
    click CargoSystem "https://github.com/alexislours/optimage/blob/main/Cargo.toml"
    click NpmSystem "https://github.com/alexislours/optimage/blob/main/package.json"
    click BuildScript "https://github.com/alexislours/optimage/blob/main/install.js"

    %% Styling
    classDef jsapi fill:#3498db,stroke:#2980b9,color:white
    classDef bridge fill:#e67e22,stroke:#d35400,color:white
    classDef rust fill:#2ecc71,stroke:#27ae60,color:white
    classDef build fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Legend
    subgraph Legend
        L1["JavaScript/TypeScript"]:::jsapi
        L2["Native Bridge"]:::bridge
        L3["Rust Components"]:::rust
        L4["Build System"]:::build
    end
```
