# degitx/rats-search architecture

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

- Interactive diagram: https://gitdiagram.com/degitx/rats-search
- Repository: https://github.com/degitx/rats-search

## Overview

##### Project type and purpose
- This is a hybrid full-stack application: an Electron‐based desktop BitTorrent search client plus a Node.js web server with a browser UI.
- Purpose: crawl and index torrent metadata over P2P and tracker protocols, expose search and statistics via a rich UI (desktop or browser) and a REST/WebSocket API.

##### File-structure analysis
- Top level:
- app/ – static HTML shells for Electron and web UI
- build/ – Webpack and build scripts for production, tests, web vs. desktop bundles
- config/ – JSON environment configs (dev, test, prod)
- src/app/ – renderer process code: UI components, pages, CSS assets
- src/background/ – Electron main process (desktop) and Node.js server code: P2P engine, torrent client, trackers strategies, database patcher, API server
- translations/ – i18n JSON files
- tests/ – unit and integration tests for background logic and strategies
- Dockerfile – containerized web-server deployment
- package.json – dependencies, scripts (start, buildweb, server)

##### README insights
- Two modes of operation: desktop (Electron) and standalone web server (Express + WebSockets + REST).

a. Main components
1) Renderer (UI layer)
– Desktop: Electron renderer process (src/app + app/*.html)
– Web: browser UI served by Node.js server
2) Electron Main / Node.js Server
– P2P engine & built‐in torrent client (src/background/torrentClient.js)
– Tracker strategies plugin modules (src/background/strategies/*.js)
– DHT and custom P2P protocol modules (src/background/bt/*.js)
– Local storage/DB layer (src/background/store.js, dbPatcher.js)
– IPC layer for desktop (electronAppPath.js, background.js)
– HTTP & WebSocket server for web UI (background/server.js, api.js)
3) External systems
– Standard BitTorrent trackers and peers (UDP/TCP)
– Other Rats clients via custom P2P search protocol
4) File system
– Local torrents dropped by user (drag & drop)
– Indexed metadata database
5) Configuration & Build
– Webpack bundles for desktop vs. web
– Environment configs loaded at runtime

b. Relationships & data flows
– UI sends search/filter requests to Main/Server over IPC (desktop) or HTTP/WebSocket (web).
– Main/Server dispatches requests to:
- Local DB for cached metadata
- P2P engine (DHT, custom protocol)
- Tracker strategies modules for tracker-based search
– Results stream back to UI, UI renders pages/components.
– Background process stores new metadata, updates stats, triggers feeds.
– Other Rats clients connect via P2P network; Main/Server exchanges search queries and votes.

c. Architectural patterns & principles
– Multi‐process separation: UI vs. background logic
– Modular “strategy” pattern for adding new trackers
– Single‐source configuration via env JSON
– MVC-like UI: view components and state management in src/app
– Build‐time bundling (Webpack) for code separation: app vs. tests vs. production

d. Key technologies, frameworks, libraries
– Electron (desktop cross‐platform shell)
– Node.js & Express (web server for HTTP/WS API)
– WebSockets for real‐time feed updates
– Webpack + Babel for ES6+ compilation
– Custom P2P modules (DHT, UDP/TCP, BitTorrent wire)
– IPC (Electron) vs. REST (web)
– i18n JSON translations

##### Essential details
- Strategy modules branching from core engine to illustrate plugin architecture.
- WebSocket channel for feed updates.
- Environment configs injected at startup.
- Test suite orthogonally or in a corner, linking to engine modules under test.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "UI Layer"
        DesktopUI["Electron Renderer\nsrc/app"]:::frontend
        WebUI["Browser UI\napp/*.html\nsrc/app"]:::frontend
    end

    subgraph "IPC/API Layer"
        IPC["IPC Layer\nsrc/background/background.js\nsrc/background/electronAppPath.js"]:::core
        HTTP["HTTP & WebSocket API\nsrc/background/server.js\nsrc/background/api.js\nsrc/background/p2p.js"]:::core
    end

    subgraph "Core Engine"
        TorrentClient["Torrent Client\nsrc/background/torrentClient.js"]:::core
        subgraph "Tracker Strategies"
            S1["1337x.js"]:::core
            S2["nyaa.js"]:::core
            S3["rutor.js"]:::core
            S4["rutracker.js"]:::core
        end
        subgraph "P2P Protocol (src/background/bt)"
            BT1["client.js"]:::core
            BT2["spider.js"]:::core
            BT3["udp-tracker-request.js"]:::core
            BT4["wire.js"]:::core
            BT5["peer-queue.js"]:::core
            BT6["table.js"]:::core
            BT7["token.js"]:::core
        end
    end

    subgraph "Storage Layer"
        Store["Store & DB\nsrc/background/store.js\ndbPatcher.js\nmysql.js\nsphinx.js"]:::core
    end

    subgraph "External Systems"
        Trackers["BitTorrent Trackers\nUDP/TCP"]:::external
        Peers["Other Rats Clients\nP2P network"]:::external
        FS["Local Torrent Files\nUser Drop"]:::external
    end

    subgraph "Configuration & Build"
        Env["Env Configs\nconfig/*.json\nsrc/background/config.js"]:::config
        Build["Build Scripts\nbuild/webpack*.js\nsrc/background/webpack*.js"]:::config
    end

    subgraph "Tests"
        Tests["Test Suite\ntests/*.test.js"]:::config
    end

    DesktopUI --> IPC
    WebUI --> HTTP
    IPC --> TorrentClient
    HTTP --> TorrentClient
    TorrentClient -->|uses| S1
    TorrentClient -->|uses| S2
    TorrentClient -->|uses| S3
    TorrentClient -->|uses| S4
    TorrentClient -->|uses| BT1
    TorrentClient -->|uses| BT2
    TorrentClient -->|uses| BT3
    TorrentClient -->|uses| BT4
    TorrentClient -->|uses| BT5
    TorrentClient -->|uses| BT6
    TorrentClient -->|uses| BT7
    TorrentClient -->|read/write| Store
    TorrentClient --> Trackers
    TorrentClient --> Peers
    FS --> Store
    Env --> IPC
    Env --> HTTP
    Env --> TorrentClient
    Tests --> TorrentClient
    Tests --> S1
    Tests --> S2
    Tests --> S3
    Tests --> S4

    click DesktopUI "https://github.com/degitx/rats-search/tree/master/src/app/"
    click WebUI "https://github.com/degitx/rats-search/blob/master/app/app.html"
    click IPC "https://github.com/degitx/rats-search/blob/master/src/background/background.js"
    click IPC "https://github.com/degitx/rats-search/blob/master/src/background/electronAppPath.js"
    click HTTP "https://github.com/degitx/rats-search/blob/master/src/background/server.js"
    click HTTP "https://github.com/degitx/rats-search/blob/master/src/background/api.js"
    click TorrentClient "https://github.com/degitx/rats-search/blob/master/src/background/torrentClient.js"
    click S1 "https://github.com/degitx/rats-search/blob/master/src/background/strategies/1337x.js"
    click S2 "https://github.com/degitx/rats-search/blob/master/src/background/strategies/nyaa.js"
    click S3 "https://github.com/degitx/rats-search/blob/master/src/background/strategies/rutor.js"
    click S4 "https://github.com/degitx/rats-search/blob/master/src/background/strategies/rutracker.js"
    click BT1 "https://github.com/degitx/rats-search/blob/master/src/background/bt/client.js"
    click BT2 "https://github.com/degitx/rats-search/blob/master/src/background/bt/spider.js"
    click BT3 "https://github.com/degitx/rats-search/blob/master/src/background/bt/udp-tracker-request.js"
    click BT4 "https://github.com/degitx/rats-search/blob/master/src/background/bt/wire.js"
    click BT5 "https://github.com/degitx/rats-search/blob/master/src/background/bt/peer-queue.js"
    click BT6 "https://github.com/degitx/rats-search/blob/master/src/background/bt/table.js"
    click BT7 "https://github.com/degitx/rats-search/blob/master/src/background/bt/token.js"
    click Store "https://github.com/degitx/rats-search/blob/master/src/background/store.js"
    click Store "https://github.com/degitx/rats-search/blob/master/src/background/dbPatcher.js"
    click Env "https://github.com/degitx/rats-search/blob/master/config/env_development.json"
    click Env "https://github.com/degitx/rats-search/blob/master/config/env_test.json"
    click Env "https://github.com/degitx/rats-search/blob/master/config/env_production.json"
    click Build "https://github.com/degitx/rats-search/blob/master/build/webpack.base.config.js"
    click Build "https://github.com/degitx/rats-search/blob/master/build/webpack.app.config.js"
    click Build "https://github.com/degitx/rats-search/blob/master/build/webpack.tests.config.js"

    classDef frontend fill:#BBE1FA,stroke:#1B4F72
    classDef core fill:#C2FABC,stroke:#1B5E20
    classDef external fill:#FFE1A8,stroke:#975A16
    classDef config fill:#D3D3D3,stroke:#555
```
