rpcs
v1.0.1
Published
Convenient Node.js RPC interfaces
Downloads
6
Readme
rpcs
Provide declarative type-safe Remote Procedure Call interfaces.
Features
- 💪Always type-safe
- 👷♂️Work with Worker thread, Web worker, and MessagePort without pain
- 🆓Keep all actions asynchronously and never block the main thread
- 🤙Declarative remote method & states calling like locals
Installation
npm i rpcs
Usage
// remote.ts
import { connectPipe } from 'rpcs'
import { parentPort } from 'worker_threads'
const pipe = {
name: 'name from remote',
sayHi() {
return "hi, I'm from remote side."
}
}
export type Remote = typeof pipe
connectPipe(pipe, parentPort)
// local.ts
import { connectPipe } from 'rpcs'
import { Worker } from 'worker_threads'
import type { Remote } from './remote' // NOTICE: we only import type from remote for code hints
;(async function main() {
const rpc = connectPipe<Remote, Worker>(new Worker('./remote.js'))
const name = await rpc.name // name from remote side
const hi = await rpc.sayHi('time') // return 'hi, I\'m from remote side.' string
})()
Note: method should always return a value which could be cloned by HTML structured clone algorithm.
Compatibility
The latest version of rpcs only supports runtime with native ECMAScript 6(AKA, ES2015) support.