@cero-base/rpc
v0.2.0
Published
RPC client/server for cero-base
Readme
@cero-base/rpc
RPC client/server for @cero-base/core.
Install
npm install @cero-base/rpcClient
import { Client, getRpc } from '@cero-base/rpc'
const rpc = getRpc(ipcStream, spec)
const client = new Client(rpc, schema, spec)
await client.ready()
// Collections (user-defined or built-in profile/members/devices)
const profile = await client.collection('profile').get()
const room = await client.rooms.open(null, { name: 'My Room' })
const members = await room.collection('members').get()
// Local-scope tables (cero's local rooms cache, files, settings, identity,
// counters — plus user-declared `scope: 'local'` collections)
const localRooms = await client.local.collection('rooms').get()
// Device pairing — symmetric with room.invite() and db.invite()
const invite = await client.invite()
// Custom methods auto-forward via Proxy
await client.ping({}) // forwards to rpc.ping({})Extend with custom logic:
class ChatClient extends Client {
constructor(rpc) {
super(rpc, schema, spec)
}
async ping() {
const { time } = await this.rpc.ping({})
return Date.now() - time
}
}Server
import { Server } from '@cero-base/rpc/server'
import { getRpc } from '@cero-base/rpc'
const rpc = getRpc(ipcStream, spec)
const server = new Server(db, rpc)
await server.ready()Extend with custom handlers — define onXxx methods, auto-registered:
class ChatServer extends Server {
onPing() {
return { time: Date.now() }
}
async onStats() {
const rooms = await this.db.rooms.list()
return { rooms: rooms.length }
}
}The base server handles all collection ops generically (insert/find/get/update/delete/subscribe), so any collection — including built-in profile/members/devices — round-trips through client.collection(name) without per-collection wiring.
Build
import { build } from '@cero-base/rpc/build'
await build('./spec', schema)
await build('./spec', schema, { rpc: customRpc }) // with custom RPC typesExports
| Export | Description |
| ------------ | -------------------------------------------------------------------------------------------- |
| Client | RPC client — collection(name), rooms.{open,get,sub}, invite(), seed(), join(input) |
| Server | RPC server that wires handlers to a CeroBase db |
| Room | Client-side room with .collection(name) + .invite() |
| Collection | Client-side collection with put/get/del/sub/dispatch |
| getRpc | Create an RPC instance from an IPC stream + spec |
| rpc | Register base RPC types into a hyperschema build |
| build | Build spec with RPC types included |
License
Apache-2.0
