# metebykl/spectra architecture

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

- Interactive diagram: https://gitdiagram.com/metebykl/spectra
- Repository: https://github.com/metebykl/spectra

## Overview

##### Main Components Structure

a) Framework Core Layer:
- Router System
- Context Handler
- Request/Response Pipeline
- Type System
- Middleware Manager

b) Runtime Adapters Layer:
- Node.js Adapter
- Bun Adapter

c) Middleware Layer:
- Body Limit
- Compression
- CORS
- Logger
- Request ID
- Timeout
- Powered By

##### Component Relationships

a) Data Flow:
- Client Request → Runtime Adapter → Core Router → Middleware Pipeline → Handler → Response

b) Dependencies:
- Core to Runtime Adapters (composition relationship)
- Middleware to Core (plugin relationship)
- Plugin System to Core (extension relationship)

##### Detailed Components

For each major component, include:

a) Core Module:
**┌─ Core ──────────────────┐**
- Router                │
- Context Manager       │
- Request Handler       │
- Type System          │
- Middleware Pipeline  │
- ┘

b) Runtime Adapters:
**┌─ Runtime Adapters ─────┐**
- ┌─ Node.js ─┐ ┌─ Bun ─┐│
- Listener  │ │Server ││
- Request   │ │Handler││
- ┘ └───────┘│
- ┘

##### Implementation Details

Important implementation aspects:
- Middleware chain execution flow
- Request/Response transformation pipeline
- Runtime adapter interface requirements
- Type system integration points

##### Additional Elements

- Testing infrastructure
- Development helpers
- Streaming capabilities
- Cookie handling
- URL utilities

- The framework's lightweight nature
- Multi-runtime support
- Type safety throughout the system
- Middleware extensibility
- Plugin architecture

Use standardized UML or similar notation where appropriate, but prioritize clarity and readability over strict adherence to formal notation systems.

Remember to maintain a balance between showing enough detail to understand the system's architecture while avoiding overwhelming complexity that might obscure the main concepts.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CL[Client Request]
    end

    subgraph Core["Framework Core"]
        SP["Spectra Framework"]:::core
        RT["Router"]:::core
        CH["Context Handler"]:::core
        PR["Request/Response Pipeline"]:::core
        TS["Type System"]:::core
        MM["Middleware Manager"]:::core
        CK["Cookie Handler"]:::core
        URL["URL Utils"]:::core
        HD["Headers Utils"]:::core
        ST["Status Codes"]:::core
        RH["Request Handler Utils"]:::core
    end

    subgraph Middleware["Middleware Layer"]
        BL["Body Limit"]:::middleware
        CP["Compression"]:::middleware
        CR["CORS"]:::middleware
        LG["Logger"]:::middleware
        RID["Request ID"]:::middleware
        TO["Timeout"]:::middleware
        PB["Powered By"]:::middleware
    end

    subgraph Runtime["Runtime Adapters"]
        NA["Node.js Adapter"]:::runtime
        BA["Bun Adapter"]:::runtime
        FA["Future Adapters"]:::future
    end

    subgraph Utilities["Development & Utilities"]
        DH["Development Helpers"]:::util
        SC["Streaming Capabilities"]:::util
    end

    subgraph Plugins["Plugin System"]
        TR["tRPC Integration"]:::plugin
    end

    %% Relationships
    CL --> NA & BA
    NA & BA --> RT
    RT --> MM
    MM --> BL & CP & CR & LG & RID & TO & PB
    MM --> CH
    CH --> PR
    PR --> RH
    RH --> CK & URL & HD & ST

    %% Click Events
    click SP "https://github.com/metebykl/spectra/blob/main/packages/core/src/spectra.t"
    click RT "https://github.com/metebykl/spectra/blob/main/packages/core/src/router.ts"
    click CH "https://github.com/metebykl/spectra/blob/main/packages/core/src/context.ts"
    click PR "https://github.com/metebykl/spectra/blob/main/packages/core/src/request.ts"
    click TS "https://github.com/metebykl/spectra/blob/main/packages/core/src/types.ts"
    click NA "https://github.com/metebykl/spectra/tree/main/packages/node/src/"
    click BL "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/body-limit/index.ts"
    click CP "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/compression/index.ts"
    click CR "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/cors/index.ts"
    click LG "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/logger/index.ts"
    click RID "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/request-id/index.ts"
    click TO "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/timeout/index.ts"
    click PB "https://github.com/metebykl/spectra/blob/main/packages/core/src/middleware/powered-by/index.ts"
    click DH "https://github.com/metebykl/spectra/blob/main/packages/core/src/helpers/dev/index.ts"
    click SC "https://github.com/metebykl/spectra/blob/main/packages/core/src/helpers/stream/index.ts"
    click CK "https://github.com/metebykl/spectra/blob/main/packages/core/src/cookie.ts"
    click URL "https://github.com/metebykl/spectra/blob/main/packages/core/src/utils/url.ts"
    click TR "https://github.com/metebykl/spectra/blob/main/packages/trpc/src/index.ts"
    click RH "https://github.com/metebykl/spectra/blob/main/packages/core/src/utils/handler.ts"
    click HD "https://github.com/metebykl/spectra/blob/main/packages/core/src/utils/headers.ts"
    click ST "https://github.com/metebykl/spectra/blob/main/packages/core/src/utils/status.ts"

    %% Styling
    classDef core fill:#e1f5fe,stroke:#01579b
    classDef middleware fill:#f3e5f5,stroke:#4a148c
    classDef runtime fill:#e8f5e9,stroke:#1b5e20
    classDef future fill:#fff3e0,stroke:#e65100,stroke-dasharray: 5 5
    classDef util fill:#fce4ec,stroke:#880e4f
    classDef plugin fill:#fff8e1,stroke:#ff6f00

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Middleware"]:::middleware
        L3["Runtime Adapters"]:::runtime
        L4["Future Components"]:::future
        L5["Utilities"]:::util
        L6["Plugins"]:::plugin
    end
```
