How tsrpc/tsrpc works
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: BaseServer.ts Base server · ApiCall.ts API call · BaseCall.ts Base call · HttpConnection.ts HTTP connection
Components 24
Shared Protocol Model
- Models & utilities
- Class object IDs
src/models/getClassObjectId.ts - Resource pool
src/models/Pool.ts
Server Core
- Base server
src/server/base/BaseServer.ts - Base connection
src/server/base/BaseConnection.ts - Base call
src/server/base/BaseCall.ts - API call
src/server/base/ApiCall.ts - Message call
src/server/base/MsgCall.ts - Server diagnostics · Prefix and terminal-color loggers.
src/server/models/PrefixLogger.ts - In-process connection
src/server/inner/InnerConnection.ts - In-process API call
src/server/inner/ApiCallInner.ts
HTTP Transport
- HTTP server · Supports configured JSON-compatible operation.
src/server/http/HttpServer.ts - HTTP connection
src/server/http/HttpConnection.ts - HTTP API call
src/server/http/ApiCallHttp.ts - HTTP message call
src/server/http/MsgCallHttp.ts
WebSocket Transport
- WebSocket server
src/server/ws/WsServer.ts - WebSocket connection
src/server/ws/WsConnection.ts - WebSocket API call
src/server/ws/ApiCallWs.ts - WebSocket message call
src/server/ws/MsgCallWs.ts
Client Transport
- HTTP client
src/client/http/HttpClient.ts - HTTP proxy
src/client/http/HttpProxy.ts - WebSocket client
src/client/ws/WsClient.ts - WebSocket proxy
src/client/ws/WebSocketProxy.ts
Other components
- Package entry
src/index.ts
Connections 27
- 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