# ladjs/superagent architecture

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

- Interactive diagram: https://gitdiagram.com/ladjs/superagent
- Repository: https://github.com/ladjs/superagent

## Overview

##### Main Components

a. Core Components:
- Request Handler (src/request-base.js)
- Response Handler (src/response-base.js)
- Agent System (src/agent-base.js)
- HTTP/HTTPS Client (src/client.js)

b. Node.js Specific Components:
- Node HTTP Implementation (src/node/index.js)
- HTTP2 Wrapper (src/node/http2wrapper.js)
- Response Parser System (src/node/parsers/*)
- Compression Handling (src/node/decompress.js, src/node/unzip.js)

c. Browser-specific Components:
- Browser-compatible XHR/Fetch implementation
- Browser-specific serialization

**┌─────────────────────────────────────────────────────┐**
- Client Application                  │
- ┬─────────────────────┬───────────────┘

- ┌───────────▼────────┐   ┌───────▼───────┐
- Browser Runtime    │   │  Node Runtime │
- ┬────────┘   └───────┬───────┘

**┌───────────────▼────────────────────▼───────────────┐**
- SuperAgent Core                    │
- ┤
- Request Base                                    │
- Response Base                                   │
- Agent Base                                      │
- Utils                                          │
- ┬────────────────────┬───────────────┘

- ┌───────────▼──────┐   ┌────────▼─────────┐
- Parser System    │   │ Protocol Handlers │
- ┘   └──────────────────┘

##### Key Relationships to Highlight

- Bidirectional flow between the core components and runtime-specific implementations
- Plugin system architecture (mentioned in README)
- Demonstrate the request/response lifecycle
- Integration points with external systems

##### Important Details

- Parser system for different content types (JSON, form-data, etc.)
- Compression/decompression pipeline
- HTTP/2 support in Node.js
- Plugin integration points
- Error handling flow
- Request/response transformation pipeline

##### Additional Elements

- Version compatibility notes
- Supported protocol types (HTTP/1.1, HTTP/2)
- Browser compatibility requirements

##### Implementation Notes

- Notes about polyfill requirements
- Plugin integration points with clear extension mechanisms

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Application"]
        Browser["Browser Runtime"]
        Node["Node Runtime"]
    end

    subgraph Core["SuperAgent Core"]
        ReqBase["Request Base"]:::core
        RespBase["Response Base"]:::core
        AgentBase["Agent Base"]:::core
        Client["HTTP/HTTPS Client"]:::core
        Utils["Utilities"]:::core
    end

    subgraph NodeImpl["Node.js Implementation"]
        NodeHTTP["Node HTTP Implementation"]:::node
        HTTP2["HTTP2 Wrapper"]:::node
        NodeResp["Node Response Handler"]:::node
        NodeAgent["Node Agent"]:::node

        subgraph Compression["Compression System"]
            Decompress["Decompression Handler"]:::node
            Unzip["Unzip Handler"]:::node
        end

        subgraph Parsers["Parser System"]
            ParserIndex["Parser Index"]:::node
            JSONParser["JSON Parser"]:::node
            ImageParser["Image Parser"]:::node
            TextParser["Text Parser"]:::node
            URLParser["URL-encoded Parser"]:::node
        end
    end

    Browser --> ReqBase
    Node --> NodeHTTP
    
    ReqBase --> RespBase
    ReqBase --> AgentBase
    ReqBase --> Client
    
    NodeHTTP --> HTTP2
    NodeHTTP --> NodeResp
    NodeHTTP --> NodeAgent
    
    NodeHTTP --> Parsers
    NodeHTTP --> Compression
    
    ParserIndex --> JSONParser
    ParserIndex --> ImageParser
    ParserIndex --> TextParser
    ParserIndex --> URLParser
    
    Decompress --> Unzip

    %% Click events for component mapping
    click ReqBase "https://github.com/ladjs/superagent/blob/master/src/request-base.js"
    click RespBase "https://github.com/ladjs/superagent/blob/master/src/response-base.js"
    click AgentBase "https://github.com/ladjs/superagent/blob/master/src/agent-base.js"
    click Client "https://github.com/ladjs/superagent/blob/master/src/client.js"
    click NodeHTTP "https://github.com/ladjs/superagent/blob/master/src/node/index.js"
    click HTTP2 "https://github.com/ladjs/superagent/blob/master/src/node/http2wrapper.js"
    click ParserIndex "https://github.com/ladjs/superagent/blob/master/src/node/parsers/index.js"
    click JSONParser "https://github.com/ladjs/superagent/blob/master/src/node/parsers/json.js"
    click ImageParser "https://github.com/ladjs/superagent/blob/master/src/node/parsers/image.js"
    click TextParser "https://github.com/ladjs/superagent/blob/master/src/node/parsers/text.js"
    click URLParser "https://github.com/ladjs/superagent/blob/master/src/node/parsers/urlencoded.js"
    click Decompress "https://github.com/ladjs/superagent/blob/master/src/node/decompress.js"
    click Unzip "https://github.com/ladjs/superagent/blob/master/src/node/unzip.js"
    click NodeResp "https://github.com/ladjs/superagent/blob/master/src/node/response.js"
    click NodeAgent "https://github.com/ladjs/superagent/blob/master/src/node/agent.js"
    click Utils "https://github.com/ladjs/superagent/blob/master/src/utils.js"

    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef node fill:#2ecc71,stroke:#27ae60,color:white
    classDef browser fill:#e67e22,stroke:#d35400,color:white
    
    %% Legend
    subgraph Legend
        CoreLegend["Core Components"]:::core
        NodeLegend["Node.js Components"]:::node
        BrowserLegend["Browser Components"]:::browser
    end
```
