# integeralex/hasty-server architecture

> Architecture diagram of the GitHub repository integeralex/hasty-server, 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/integeralex/hasty-server
- Repository: https://github.com/integeralex/hasty-server

## Overview

##### Main Components

a) Core Server Components:
- HTTP Server (TCP Socket Layer)
- Request Parser
- Response Handler
- Router/Middleware System

b) Supporting Libraries:
- HTTP Parser
- MIME Database
- Utility Functions

**[TCP Socket Layer]**

**[HTTP Parser] ←→ [MIME Database]**

**[Request Parser] → [Router] → [Response Handler]**

**[User Routes and Handlers]**

##### Detailed Component Descriptions

a) HTTP Server (server/index.js):
- Entry point for all incoming requests
- TCP socket management
- Request queue handling

b) HTTP Parser (lib/httpParser.js):
- Raw HTTP request parsing
- Header and body extraction
- Request method identification

c) Response Handler (server/response.js):
- Response object creation
- Methods: send(), json(), status()
- CORS handling

d) Supporting Libraries:
- mimeDb.js: MIME type handling
- utils.js: Utility functions
- parser.js: General parsing utilities

##### Key Interactions to Highlight

**Client Request → TCP Socket → HTTP Parser → Router → Handler → Response → Client**

##### Additional Elements

A section showing:
- Middleware chain flow
- CORS handling mechanism
- File handling for downloads
- Error handling flow

##### Implementation Details

- The framework's lightweight nature
- Direct TCP socket usage
- Express.js-like API interface
- No third-party dependencies

##### Notes

- Keep the core HTTP server implementation visible
- Relationship between parser components
- Middleware chain
- Error handling paths
- CORS implementation

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        UserRoutes["User Routes and Handlers"]:::application
    end

    subgraph "Server Components"
        RequestParser["Request Parser"]:::core
        Router["Router/Middleware System"]:::core
        ResponseHandler["Response Handler"]:::core
        click ResponseHandler "https://github.com/IntegerAlex/hasty-server/blob/master/server/response.js"
    end

    subgraph "Core Processing"
        HTTPParser["HTTP Parser"]:::library
        MIMEDb["MIME Database"]:::library
        Parser["General Parser"]:::library
        Utils["Utility Functions"]:::library

        click HTTPParser "https://github.com/IntegerAlex/hasty-server/blob/master/lib/httpParser.js"
        click MIMEDb "https://github.com/IntegerAlex/hasty-server/blob/master/lib/mimeDb.js"
        click Parser "https://github.com/IntegerAlex/hasty-server/blob/master/lib/parser.js"
        click Utils "https://github.com/IntegerAlex/hasty-server/blob/master/lib/utils.js"

        HTTPParser <--> MIMEDb
        HTTPParser <--> Parser
        HTTPParser <--> Utils
    end

    subgraph "Foundation"
        TCPSocket["HTTP Server (TCP Socket Layer)"]:::foundation
        click TCPSocket "https://github.com/IntegerAlex/hasty-server/blob/master/server/index.js"
    end

    %% Main Flow
    Client[("Client")]:::external
    Client -->|Request| TCPSocket
    TCPSocket -->|Raw Data| HTTPParser
    HTTPParser -->|Parsed Request| RequestParser
    RequestParser -->|Processed Request| Router
    Router -->|Route Handler| UserRoutes
    UserRoutes -->|Response Data| ResponseHandler
    ResponseHandler -->|HTTP Response| TCPSocket
    TCPSocket -->|Response| Client

    %% Error Flow
    Router -.->|Error| ResponseHandler
    UserRoutes -.->|Error| ResponseHandler

    %% Middleware Chain
    Router ---|Middleware Chain| Router

    %% Styling
    classDef external fill:#gray,stroke:#333,stroke-width:2px
    classDef foundation fill:#1a73e8,color:white,stroke:#0d47a1
    classDef core fill:#4caf50,color:white,stroke:#2e7d32
    classDef library fill:#ff9800,color:white,stroke:#f57c00
    classDef application fill:#9c27b0,color:white,stroke:#6a1b9a

    %% Legend
    subgraph Legend
        L1[("External System")]:::external
        L2["Foundation Component"]:::foundation
        L3["Core Component"]:::core
        L4["Library Component"]:::library
        L5["Application Component"]:::application
    end
```
