# okaprinarjaya/opj-ssfrs architecture

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

- Interactive diagram: https://gitdiagram.com/okaprinarjaya/opj-ssfrs
- Repository: https://github.com/okaprinarjaya/opj-ssfrs

## Overview

##### Project type and purpose
- Opj-Ssfrs is a lightweight Node.js–based TCP file‐receive server (no browser UI).
- It provides a custom protocol for chunked/multipart file transfer over TCP, records detailed transfer metadata in MongoDB, and can integrate with a Gearman job server (optional) or execute shell commands after each complete receive.

##### File structure
- Top-level directories:
- server/ – the TCP server entry point (`server.js`), utilities (`server_utils.js`), and configuration (`config.js`).
- client/ – a reusable client library (`Opj_Ssfrc.js`) and two test clients (`test_client.js`, `test_client_multipart.js`).
- Config files:
- `server/config.js` – database URI, registered IPs, SSL toggle, Gearman settings.
- CI:
- `.github/workflows/dependabot.yml` – dependency updates.
- Package manifest:
- `package.json` – lists Node.js dependencies (e.g., mongodb, gearman, tls).

##### Key insights from README
- Core features: multipart transfer, post‐receive commands, Gearman job submission, detailed logging, SSL (experimental), IP whitelisting.
- External dependencies: MongoDB (required), Gearman (optional), NodeJS runtime.

##### Main components and their interactions
a. Clients
- Application or script that uses `Opj_Ssfrc.js` to open a TCP (or TLS) socket, chunk files, and stream them.
- Two reference binaries: `test_client.js` (single file) and `test_client_multipart.js` (chunked).
b. Network layer
- Plain TCP or optional TLS socket.
- Custom protocol framing for start, chunk, end messages.
c. Server core
- `server.js` listens on configured port, negotiates protocol, handles per‐connection events.
- Delegates parsing and DB writes to `server_utils.js`.
d. Persistence: MongoDB
- Stores transfer metadata and status (sender IP, file name, size, checksum, timestamps, success/failure).
e. Integration points
- Shell command executor – invoked when a transfer completes.
- Gearman client – pushes a job to Gearman server for async processing (image resize, watermark, SQL import, etc.).
- Gearman workers – external processes that pick up jobs and do the heavy compute.
f. Optional security
- TLS termination inside server if `config.ssl = true`.
- IP filter against `config.registeredIp`.

##### Architectural patterns and design principles
- Event‐driven, nonblocking I/O (Node.js streams + events).
- Modular separation: protocol logic (utils), configuration, integration.
- Pluggable post‐receive handlers (shell or Gearman).
- Fail‐safe logging: every state transition written to DB for replay/retry.

##### Final advice
- Keep labels concise but unambiguous.

## Diagram (Mermaid)

```mermaid
graph TD
  subgraph "Client Layer"
    CLib["Client Library\nOpj_Ssfrc.js"]:::client
    Test1["Test Client\nSingle-File"]:::client
    Test2["Test Client\nMultipart"]:::client
  end

  subgraph "Network Layer"
    TCP["TCP Socket"]:::network
    TLS["TLS Socket\n(experimental SSL)"]:::optional
  end

  subgraph "Server Layer"
    Srv["Server Core\nserver.js"]:::core
    Utils["Protocol Parsing & Utils\nserver_utils.js"]:::core
    Conf["Configuration\nconfig.js"]:::core
  end

  subgraph "Persistence & Integration"
    MDB[(MongoDB)]:::database
    Shell["Shell Executor"]:::integration
    GearSrv["Gearman Server"]:::external
    GearWork["Gearman Workers"]:::external
  end

  subgraph "Repository Files"
    CI["CI Workflow\n.dependabot.yml"]:::repo
    PKG["Package Manifest\npackage.json"]:::repo
    README["Project Overview\nREADME.md"]:::repo
  end

  %% Data Flows
  CLib -->|"1) connect"| TCP
  Test1 -->|"1) connect"| TCP
  Test2 -->|"1) connect"| TCP
  CLib -.->|"optional SSL"| TLS
  Test1 -.->|"optional SSL"| TLS
  Test2 -.->|"optional SSL"| TLS

  TCP -->|"2) chunk stream"| Srv
  TLS -.->|"2) encrypted chunk"| Srv

  Srv -->|"delegate parsing & integration"| Utils
  Conf -->|"config data"| Srv

  Utils -->|"3) write log"| MDB
  Utils -->|"4) on complete"| Shell
  Utils -->|"4) on complete"| GearSrv

  GearSrv -->|"job pulled"| GearWork

  %% Click Events
  click CLib "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/client/Opj_Ssfrc.js"
  click Test1 "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/client/test_client.js"
  click Test2 "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/client/test_client_multipart.js"
  click Srv "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/server/server.js"
  click Utils "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/server/server_utils.js"
  click Conf "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/server/config.js"
  click CI "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/.github/workflows/dependabot.yml"
  click PKG "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/package.json"
  click README "https://github.com/okaprinarjaya/opj-ssfrs/blob/master/README.md"

  %% Styles
  classDef client fill:#D4BFFF,stroke:#888,stroke-width:1px;
  classDef network fill:#E0E0E0,stroke:#888,stroke-width:1px;
  classDef core fill:#C2FABC,stroke:#555,stroke-width:1px;
  classDef database fill:#A0D8EF,stroke:#555,stroke-width:1px;
  classDef integration fill:#FFD8B4,stroke:#555,stroke-width:1px;
  classDef external fill:#FBE4A7,stroke:#555,stroke-width:1px;
  classDef repo fill:#F0F0F0,stroke:#AAA,stroke-width:1px;
  classDef optional fill:#E8E8E8,stroke:#888,stroke-dasharray: 5 5;
```
