How kunkunsh/kkrpc works
Project Type & Purpose
• This is an RPC library meant to simplify bi-directional communications between processes. Its design abstracts “remote” function calls as local ones using a central RPCChannel and pluggable IO adapters.
• It is used as a standalone package but also powers examples in multiple environments (Node, Deno, Bun, web workers, iframes, Chrome extensions and Tauri apps).
Analyzing the File Structure
• At the top level, you have configuration files (e.g., .npmrc, .prettierrc, .gitignore) plus directories for docs, examples, and packages.
• The “packages/kkrpc” directory holds the core implementation including adapters (in src/adapters), utilities, and type definitions. This shows the main business logic centered on the RPCChannel and IoInterface.
• There are also testing folders (e.g., __tests__) and related build and configuration files that support CI/CD and automated documentation generation.
Core Architectural Components
a. Core Library:
• RPCChannel: The central component handling the bidirectional RPC communication.
• IoInterface: The abstract IO definition that is implemented by various adapters.
• Serialization Mechanism: Supporting both “json” and “superjson” formats.
b. Adapters/Implementations (Using IoInterface):
• Standard I/O adapters: e.g., NodeIo, handling stdio streams.
• Web adapters: for workers, iframes, browser modules (ChromeContentIO, ChromeBackgroundIO, WorkerChildIO, WorkerParentIO, IframeChildIO, IframeParentIO).
• HTTP Adapters: HTTPClientIO and HTTPServerIO for REST-like RPC over HTTP.
• Tauri and other runtime-specific adapters: Found under examples/tauri-demo and specific adapter implementations in packages/kkrpc/src/adapters.
c. API Abstractions:
• LocalAPI: The set of functions exposed for remote calls.
• RemoteAPI: The API callable on the local side which represents the other endpoint’s functionality.
• The getAPI() method that converts the remote side’s capabilities into local-callable functions.
d. Supporting Components:
• Testing and examples: Each example demonstrates the integration of specific adapters with the core RPCChannel.
• Build and config files to support TypeScript compilation, dependency management, and CI pipelines.
a. Start by drawing a central block labeled “Core kkrpc Library” that encapsulates:
• RPCChannel
• IoInterface
• Serialization options (json / superjson)
• API Abstractions (LocalAPI and RemoteAPI)
b. Around the central block, create separate nodes for each adapter/environment:
• Stdio Environment:
• NodeIo and similar classes for Node, Deno, Bun.
• Web Environment:
• Worker Adapters (WorkerChildIO, WorkerParentIO)
• Iframe Adapters (IframeChildIO, IframeParentIO)
• HTTP Environment:
• Browser/Chrome Extension:
• ChromeBackgroundIO and ChromeContentIO nodes, indicating communication between a background script and content script.
• Tauri Environment:
• Tauri-specific adapter (e.g., TauriShellStdio) and integration with plugins.
c. How the “Core Library” connects to each adapter demonstrating how RPC messages and IO implementations are passed.
• Bidirectional communication between the server and client components in each environment.
d. For each adapter node, include a brief label or sub-note indicating the underlying technology or runtime specifics (for example, “Node: process.stdin/stdout”, “HTTP: REST-like message transmission”, “Worker: postMessage”).
e. To capture the testing/demonstration aspects:
• That these examples serve as integration tests and usage scenarios that further validate the architecture.