1.0.1 • Published 2 years ago

rpcs v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

rpcs

Test cases npm version

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.

License

MIT © Liu Bowen

1.0.1

2 years ago

1.0.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago