A tRPC-style IPC RPC library for Bun subprocesses using Zod for validation.
bun add bunrpc
// api.ts
import { initRpc } from 'bunrpc';
import { z } from 'zod';
export const rpc = initRpc();
export const appRouter = rpc.router({
greet: rpc.procedure
.input(z.object({ name: z.string() }))
.resolve(({ input }) => {
return `Hello ${input.name}!`;
}),
math: {
add: rpc.procedure
.input(z.object({ a: z.number(), b: z.number() }))
.resolve(({ input }) => {
return input.a + input.b;
})
}
});
export type AppRouter = typeof appRouter;
// child-process.ts
import { serve } from 'bunrpc';
import { appRouter } from './api';
// Start listening for IPC messages
serve(appRouter);
// Keep the process alive
process.stdin.resume();
// parent-process.ts
import { spawnRpcClient } from 'bunrpc';
import type { AppRouter } from './api';
async function main() {
// Spawn child process and create typesafe client
const { client, child } = spawnRpcClient<AppRouter>('bun', ['child-process.ts']);
// Call procedures with full type-safety
const greeting = await client.greet({ name: 'World' });
console.log(greeting); // Hello World!
const sum = await client.math.add({ a: 2, b: 3 });
console.log(sum); // 5
// Terminate the child process when done
child.kill();
}
main().catch(console.error);
- Type-safe RPC between Bun parent and child processes
- Schema validation with Zod
- tRPC-style router definition API
- Promise-based API with support for async procedures
# Install dependencies
bun install
# Run tests
bun test
# Build
bun run build
MIT