Autonomous run 2026-05-20: Implemented Ed25519 signing in hyper-p2p-presence, added full bidirectional streaming + crypto IDs + docs/tests/examples to hyper-p2p-rpc (v0.2.0), created novel hyper-p2p-capabilities module with capability tokens/delegation/revocation + tests/docs, updated all READMEs and fixed Node.js builtin usage in examples, production-grade improvements across modules
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
# API Reference - hyper-p2p-rpc
|
||||
|
||||
## Classes
|
||||
|
||||
### RPCServer
|
||||
|
||||
```js
|
||||
const server = new RPCServer(options?)
|
||||
```
|
||||
|
||||
**Options**
|
||||
- `signingKeyPair` (optional): Ed25519 keyPair for message authentication
|
||||
- `timeout` (default: 30000): Default call timeout in ms
|
||||
|
||||
**Methods**
|
||||
|
||||
#### `register(name: string, handler: Function, schema?: object)`
|
||||
|
||||
Registers a service method.
|
||||
|
||||
Handler signature:
|
||||
```js
|
||||
async function handler(params, context) {
|
||||
// context = { socket, stream, peerKey }
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
For streaming handlers:
|
||||
```js
|
||||
async function handler(params, context) {
|
||||
return someReadableStream // or async iterator
|
||||
}
|
||||
```
|
||||
|
||||
#### `handleConnection(socket: net.Socket | Hyperswarm socket)`
|
||||
|
||||
Attaches the RPC protocol to an incoming or outgoing socket. Must be called for every new connection.
|
||||
|
||||
#### `call(socket, method, params, timeout?)` (internal, use client)
|
||||
|
||||
#### `close()`
|
||||
|
||||
Gracefully closes all connections and pending calls.
|
||||
|
||||
**Events**
|
||||
- `connection(socket)`
|
||||
- `disconnection(socket)`
|
||||
- `error(err)`
|
||||
- `call(method, params, peer)`
|
||||
|
||||
### RPCClient
|
||||
|
||||
```js
|
||||
const client = new RPCClient(socket, options?)
|
||||
```
|
||||
|
||||
**Methods**
|
||||
|
||||
#### `async call(method: string, params: any = {}, timeoutMs = 30000)`
|
||||
|
||||
Performs a standard RPC call. Returns the result or throws on error/timeout.
|
||||
|
||||
#### `async callStream(method: string, params: any = {}, timeoutMs = 30000)`
|
||||
|
||||
Returns a Bare Readable stream for methods that produce streaming results. The stream supports `for await...of`.
|
||||
|
||||
**Events**
|
||||
- `close`
|
||||
- `error`
|
||||
|
||||
## Internal Protocol
|
||||
|
||||
Version: `hyper-p2p-rpc/v2`
|
||||
|
||||
All communication happens on a dedicated Protomux channel.
|
||||
|
||||
## Error Codes
|
||||
|
||||
- `METHOD_NOT_FOUND`
|
||||
- `TIMEOUT`
|
||||
- `VALIDATION_ERROR`
|
||||
- `STREAM_ERROR`
|
||||
|
||||
*Full examples in examples/ folder*
|
||||
@@ -0,0 +1,74 @@
|
||||
# Architecture of hyper-p2p-rpc
|
||||
|
||||
## Overview
|
||||
|
||||
`hyper-p2p-rpc` provides a high-level RPC abstraction over Protomux channels. It is designed from the ground up for the Holepunch/Bare/Pear ecosystem and avoids any Node.js globals or core modules.
|
||||
|
||||
## Core Components
|
||||
|
||||
### 1. RPCServer
|
||||
- Manages a registry of named service handlers
|
||||
- Listens for new Protomux connections via `handleConnection(socket)`
|
||||
- Creates dedicated channels per connection using protocol `hyper-p2p-rpc/v2`
|
||||
- Supports both fire-and-forget and request-response patterns
|
||||
- New in v0.2: Streaming handlers that return Bare streams or async iterators
|
||||
|
||||
### 2. RPCClient
|
||||
- Lightweight client for a single socket
|
||||
- Manages pending promises with crypto-secure correlation IDs
|
||||
- Supports `.call()` for RPC and `.callStream()` for streaming results
|
||||
- Automatic timeout and error propagation
|
||||
|
||||
### 3. Protocol Layer
|
||||
|
||||
All messages are JSON encoded over Protomux MessageStream.
|
||||
|
||||
Message format:
|
||||
```json
|
||||
{
|
||||
"id": "crypto-random-uuid",
|
||||
"method": "service.method",
|
||||
"params": {...},
|
||||
"result": {...} | null,
|
||||
"error": "string" | null,
|
||||
"stream": true | false
|
||||
}
|
||||
```
|
||||
|
||||
For streaming, a secondary sub-channel is opened under the same mux for the data flow, allowing independent backpressure.
|
||||
|
||||
## Security Model
|
||||
|
||||
- Optional message signing using the peer's Ed25519 keyPair (integrated with hyper-p2p-presence)
|
||||
- All calls are scoped to the authenticated connection
|
||||
- Future: capability tokens for fine-grained access
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
- Uses `bare-timers` for all timeouts
|
||||
- Efficient Map-based pending call tracking
|
||||
- No buffering of large streams (backpressure via Protomux)
|
||||
|
||||
## Diagrams
|
||||
|
||||
### Connection Lifecycle
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> Disconnected
|
||||
Disconnected --> Connecting: swarm.join()
|
||||
Connecting --> Connected: handleConnection()
|
||||
Connected --> Streaming: callStream()
|
||||
Connected --> RPC: call()
|
||||
Connected --> Disconnected: socket close
|
||||
```
|
||||
|
||||
## Comparison to Existing Solutions
|
||||
|
||||
This module is the first to combine:
|
||||
- Protomux native streaming
|
||||
- Bare runtime purity
|
||||
- Pluggable signing from hyper-p2p-presence
|
||||
- Production patterns (graceful close, metrics hooks)
|
||||
|
||||
*Autonomous development - 2026-05-20*
|
||||
Reference in New Issue
Block a user