# tsrpc/tsrpc architecture

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

- Interactive diagram: https://gitdiagram.com/tsrpc/tsrpc
- Repository: https://github.com/tsrpc/tsrpc

## Overview

#### Purpose and Package Entry
TSRPC is a TypeScript RPC framework providing runtime type validation and binary serialization over HTTP and WebSocket, with optional JSON compatibility. The public package surface begins at `src/index.ts`; core shared abstractions and utilities live in `src/models/`.

#### Protocol-Driven RPC Model
Applications define shared TypeScript request/response and message protocol types, then bind API handlers by service name. Server-side calls expose typed request data and response completion through `src/server/base/ApiCall.ts`; one-way/message semantics are represented by `src/server/base/MsgCall.ts`. Object-ID generation for protocol/class identity is centralized in `src/models/getClassObjectId.ts`.

#### Server Core and Transport Boundary
`src/server/base/BaseServer.ts` owns transport-independent server behavior, while `src/server/base/BaseConnection.ts` represents an individual connected peer and `src/server/base/BaseCall.ts` supplies common call lifecycle behavior. Concrete transports adapt raw network events into these abstractions, isolating protocol dispatch and serialization from HTTP/WS details.

#### HTTP Server Path
`src/server/http/HttpServer.ts` hosts HTTP RPC endpoints and creates `src/server/http/HttpConnection.ts` instances. HTTP-specific API and message dispatch are represented by `src/server/http/ApiCallHttp.ts` and `src/server/http/MsgCallHttp.ts`. This is the request/response transport path, including JSON-compatible operation where configured.

#### WebSocket Server Path
`src/server/ws/WsServer.ts` provides persistent bidirectional RPC transport. `src/server/ws/WsConnection.ts` manages socket-level connection state, and inbound API/message frames become `src/server/ws/ApiCallWs.ts` or `src/server/ws/MsgCallWs.ts`. This boundary enables server-initiated messaging in addition to client API calls.

#### Client Transport and Proxies
Clients select a transport-specific implementation: `src/client/http/HttpClient.ts` for HTTP and `src/client/ws/WsClient.ts` for WebSockets. Proxy layers in `src/client/http/HttpProxy.ts` and `src/client/ws/WebSocketProxy.ts` abstract browser/Node-compatible network primitives, separating RPC behavior from the underlying HTTP and WebSocket runtime APIs.

#### In-Process and Operational Support
`src/server/inner/InnerConnection.ts` and `src/server/inner/ApiCallInner.ts` support internal/in-process invocation through the same call model rather than a network transport. Shared resource reuse is handled by `src/models/Pool.ts`; server diagnostics are provided by `src/server/models/PrefixLogger.ts` and `src/server/models/TerminalColorLogger.ts`. Benchmark HTTP/WS runners and servers under `benchmark/` exercise transport performance.

Main entry points: [`src/server/base/BaseServer.ts`](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseServer.ts) (Base server), [`src/server/base/ApiCall.ts`](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/ApiCall.ts) (API call), [`src/server/base/BaseCall.ts`](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseCall.ts) (Base call), [`src/server/http/HttpConnection.ts`](https://github.com/tsrpc/tsrpc/blob/main/src/server/http/HttpConnection.ts) (HTTP connection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_shared["Shared Protocol Model"]
  node_models["Models &amp; utilities<br/>shared abstractions"]
  node_object_id["Class object IDs<br/>protocol identity"]
  node_pool["Resource pool<br/>resource reuse<br/>[Pool.ts]"]
end

subgraph group_server_core["Server Core"]
  node_base_server["Base server<br/>transport-neutral dispatch<br/>[BaseServer.ts]"]
  node_base_connection["Base connection<br/>peer abstraction<br/>[BaseConnection.ts]"]
  node_base_call["Base call<br/>call lifecycle<br/>[BaseCall.ts]"]
  node_api_call["API call<br/>typed request-response<br/>[ApiCall.ts]"]
  node_msg_call["Message call<br/>one-way message<br/>[MsgCall.ts]"]
  node_logger["Server diagnostics<br/>logging<br/>[PrefixLogger.ts]"]
  node_inner_connection["In-process connection<br/>internal transport<br/>[InnerConnection.ts]"]
  node_inner_api["In-process API call<br/>internal API dispatch<br/>[ApiCallInner.ts]"]
end

subgraph group_http["HTTP Transport"]
  node_http_server["HTTP server<br/>HTTP RPC host<br/>[HttpServer.ts]"]
  node_http_connection["HTTP connection<br/>HTTP peer adapter<br/>[HttpConnection.ts]"]
  node_http_api["HTTP API call<br/>HTTP API dispatch<br/>[ApiCallHttp.ts]"]
  node_http_msg["HTTP message call<br/>HTTP message dispatch<br/>[MsgCallHttp.ts]"]
end

subgraph group_ws["WebSocket Transport"]
  node_ws_server["WebSocket server<br/>persistent RPC host<br/>[WsServer.ts]"]
  node_ws_connection["WebSocket connection<br/>socket peer adapter<br/>[WsConnection.ts]"]
  node_ws_api["WebSocket API call<br/>frame API dispatch<br/>[ApiCallWs.ts]"]
  node_ws_msg["WebSocket message call<br/>bidirectional message dispatch<br/>[MsgCallWs.ts]"]
end

subgraph group_client["Client Transport"]
  node_http_client["HTTP client<br/>RPC client<br/>[HttpClient.ts]"]
  node_http_proxy["HTTP proxy<br/>runtime network adapter<br/>[HttpProxy.ts]"]
  node_ws_client["WebSocket client<br/>RPC client<br/>[WsClient.ts]"]
  node_ws_proxy["WebSocket proxy<br/>runtime network adapter<br/>[WebSocketProxy.ts]"]
end

node_entry["Package entry<br/>public API<br/>[index.ts]"]

node_entry -->|"exports shared API"| node_models
node_models -->|"includes"| node_object_id
node_models -->|"includes"| node_pool
node_base_server -->|"manages peers"| node_base_connection
node_base_connection -->|"creates calls"| node_base_call
node_base_call -->|"specializes"| node_api_call
node_base_call -->|"specializes"| node_msg_call
node_http_server -->|"extends core"| node_base_server
node_http_server -->|"creates"| node_http_connection
node_http_connection -->|"dispatches API"| node_http_api
node_http_connection -->|"dispatches messages"| node_http_msg
node_http_api -->|"specializes"| node_api_call
node_http_msg -->|"specializes"| node_msg_call
node_ws_server -->|"extends core"| node_base_server
node_ws_server -->|"creates"| node_ws_connection
node_ws_connection -->|"dispatches API frames"| node_ws_api
node_ws_connection -->|"dispatches message frames"| node_ws_msg
node_ws_api -->|"specializes"| node_api_call
node_ws_msg -->|"specializes"| node_msg_call
node_http_client -->|"uses network adapter"| node_http_proxy
node_http_client -->|"HTTP RPC"| node_http_server
node_ws_client -->|"uses network adapter"| node_ws_proxy
node_ws_client -->|"WebSocket RPC"| node_ws_server
node_inner_connection -->|"uses call model"| node_base_connection
node_inner_connection -->|"dispatches API"| node_inner_api
node_inner_api -->|"specializes"| node_api_call
node_base_server -.->|"emits diagnostics"| node_logger

click node_entry "https://github.com/tsrpc/tsrpc/blob/main/src/index.ts"
click node_object_id "https://github.com/tsrpc/tsrpc/blob/main/src/models/getClassObjectId.ts"
click node_pool "https://github.com/tsrpc/tsrpc/blob/main/src/models/Pool.ts"
click node_base_server "https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseServer.ts"
click node_base_connection "https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseConnection.ts"
click node_base_call "https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseCall.ts"
click node_api_call "https://github.com/tsrpc/tsrpc/blob/main/src/server/base/ApiCall.ts"
click node_msg_call "https://github.com/tsrpc/tsrpc/blob/main/src/server/base/MsgCall.ts"
click node_logger "https://github.com/tsrpc/tsrpc/blob/main/src/server/models/PrefixLogger.ts"
click node_http_server "https://github.com/tsrpc/tsrpc/blob/main/src/server/http/HttpServer.ts"
click node_http_connection "https://github.com/tsrpc/tsrpc/blob/main/src/server/http/HttpConnection.ts"
click node_http_api "https://github.com/tsrpc/tsrpc/blob/main/src/server/http/ApiCallHttp.ts"
click node_http_msg "https://github.com/tsrpc/tsrpc/blob/main/src/server/http/MsgCallHttp.ts"
click node_ws_server "https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/WsServer.ts"
click node_ws_connection "https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/WsConnection.ts"
click node_ws_api "https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/ApiCallWs.ts"
click node_ws_msg "https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/MsgCallWs.ts"
click node_http_client "https://github.com/tsrpc/tsrpc/blob/main/src/client/http/HttpClient.ts"
click node_http_proxy "https://github.com/tsrpc/tsrpc/blob/main/src/client/http/HttpProxy.ts"
click node_ws_client "https://github.com/tsrpc/tsrpc/blob/main/src/client/ws/WsClient.ts"
click node_ws_proxy "https://github.com/tsrpc/tsrpc/blob/main/src/client/ws/WebSocketProxy.ts"
click node_inner_connection "https://github.com/tsrpc/tsrpc/blob/main/src/server/inner/InnerConnection.ts"
click node_inner_api "https://github.com/tsrpc/tsrpc/blob/main/src/server/inner/ApiCallInner.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_models,node_object_id,node_pool toneBlue
class node_base_server,node_base_connection,node_base_call,node_api_call,node_msg_call,node_logger,node_inner_connection,node_inner_api toneAmber
class node_http_server,node_http_connection,node_http_api,node_http_msg toneMint
class node_ws_server,node_ws_connection,node_ws_api,node_ws_msg toneRose
class node_http_client,node_http_proxy,node_ws_client,node_ws_proxy toneIndigo
class node_entry toneNeutral
```

## Components

### Shared Protocol Model

- **Models & utilities**: shared abstractions
- [Class object IDs](https://github.com/tsrpc/tsrpc/blob/main/src/models/getClassObjectId.ts): protocol identity · `src/models/getClassObjectId.ts`
- [Resource pool](https://github.com/tsrpc/tsrpc/blob/main/src/models/Pool.ts): resource reuse · `src/models/Pool.ts`

### Server Core

- [Base server](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseServer.ts): transport-neutral dispatch · `src/server/base/BaseServer.ts`
- [Base connection](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseConnection.ts): peer abstraction · `src/server/base/BaseConnection.ts`
- [Base call](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/BaseCall.ts): call lifecycle · `src/server/base/BaseCall.ts`
- [API call](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/ApiCall.ts): typed request-response · `src/server/base/ApiCall.ts`
- [Message call](https://github.com/tsrpc/tsrpc/blob/main/src/server/base/MsgCall.ts): one-way message · `src/server/base/MsgCall.ts`
- [Server diagnostics](https://github.com/tsrpc/tsrpc/blob/main/src/server/models/PrefixLogger.ts): logging · Prefix and terminal-color loggers. · `src/server/models/PrefixLogger.ts`
- [In-process connection](https://github.com/tsrpc/tsrpc/blob/main/src/server/inner/InnerConnection.ts): internal transport · `src/server/inner/InnerConnection.ts`
- [In-process API call](https://github.com/tsrpc/tsrpc/blob/main/src/server/inner/ApiCallInner.ts): internal API dispatch · `src/server/inner/ApiCallInner.ts`

### HTTP Transport

- [HTTP server](https://github.com/tsrpc/tsrpc/blob/main/src/server/http/HttpServer.ts): HTTP RPC host · Supports configured JSON-compatible operation. · `src/server/http/HttpServer.ts`
- [HTTP connection](https://github.com/tsrpc/tsrpc/blob/main/src/server/http/HttpConnection.ts): HTTP peer adapter · `src/server/http/HttpConnection.ts`
- [HTTP API call](https://github.com/tsrpc/tsrpc/blob/main/src/server/http/ApiCallHttp.ts): HTTP API dispatch · `src/server/http/ApiCallHttp.ts`
- [HTTP message call](https://github.com/tsrpc/tsrpc/blob/main/src/server/http/MsgCallHttp.ts): HTTP message dispatch · `src/server/http/MsgCallHttp.ts`

### WebSocket Transport

- [WebSocket server](https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/WsServer.ts): persistent RPC host · `src/server/ws/WsServer.ts`
- [WebSocket connection](https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/WsConnection.ts): socket peer adapter · `src/server/ws/WsConnection.ts`
- [WebSocket API call](https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/ApiCallWs.ts): frame API dispatch · `src/server/ws/ApiCallWs.ts`
- [WebSocket message call](https://github.com/tsrpc/tsrpc/blob/main/src/server/ws/MsgCallWs.ts): bidirectional message dispatch · `src/server/ws/MsgCallWs.ts`

### Client Transport

- [HTTP client](https://github.com/tsrpc/tsrpc/blob/main/src/client/http/HttpClient.ts): RPC client · `src/client/http/HttpClient.ts`
- [HTTP proxy](https://github.com/tsrpc/tsrpc/blob/main/src/client/http/HttpProxy.ts): runtime network adapter · `src/client/http/HttpProxy.ts`
- [WebSocket client](https://github.com/tsrpc/tsrpc/blob/main/src/client/ws/WsClient.ts): RPC client · `src/client/ws/WsClient.ts`
- [WebSocket proxy](https://github.com/tsrpc/tsrpc/blob/main/src/client/ws/WebSocketProxy.ts): runtime network adapter · `src/client/ws/WebSocketProxy.ts`

### Other components

- [Package entry](https://github.com/tsrpc/tsrpc/blob/main/src/index.ts): public API · `src/index.ts`

## Connections

- Package entry → Models & utilities: exports shared API
- Models & utilities → Class object IDs: includes
- Models & utilities → Resource pool: includes
- Base server → Base connection: manages peers
- Base connection → Base call: creates calls
- Base call → API call: specializes
- Base call → Message call: specializes
- HTTP server → Base server: extends core
- HTTP server → HTTP connection: creates
- HTTP connection → HTTP API call: dispatches API
- HTTP connection → HTTP message call: dispatches messages
- HTTP API call → API call: specializes
- HTTP message call → Message call: specializes
- WebSocket server → Base server: extends core
- WebSocket server → WebSocket connection: creates
- WebSocket connection → WebSocket API call: dispatches API frames
- WebSocket connection → WebSocket message call: dispatches message frames
- WebSocket API call → API call: specializes
- WebSocket message call → Message call: specializes
- HTTP client → HTTP proxy: uses network adapter
- HTTP client → HTTP server: HTTP RPC
- WebSocket client → WebSocket proxy: uses network adapter
- WebSocket client → WebSocket server: WebSocket RPC
- In-process connection → Base connection: uses call model
- In-process connection → In-process API call: dispatches API
- In-process API call → API call: specializes
- Base server → Server diagnostics: emits diagnostics
