New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@orz/rpc

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@orz/rpc

Yet another RPC scaffolding

  • 2.1.4
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

rpc

Yet another RPC scaffolding

Install

Node.js

yarn add @orz/rpc

Deno

import { RpcWire, connectWire, reverseProtocol, rpcProtocol, rpcMethod } from 'https://esm.sh/@orz/rpc@2.1.3'

Example

Edit this example in StackBlitz

import { RpcWire, connectWire, reverseProtocol, rpcProtocol, rpcMethod } from '@orz/rpc'

class WindowMessageRpc extends RpcWire {
  private target!: MessagePort

  public rpcStart(target: MessagePort) {
    this.rpcStop()
    this.target = target
    target.onmessage = this.rpcOnMessage as any
    target.onmessageerror = console.error
  }

  private rpcOnMessage = (e: MessageEvent) => {
    void this.rpcHandleMessage(e.data)
  }

  public rpcStop() {
    if (this.target) this.target.close()
  }

  protected rpcSocketClose(_message?: string, _code?: number): void {
    this.rpcStop()
    this.target.close()
  }

  protected async rpcSocketSend(data: any): Promise<void> {
    this.target.postMessage(data)
  }
}

const demoProtocol = rpcProtocol({
  server: {
    plus: rpcMethod<[number, number], number>(),
  },
  client: {
    notify: rpcMethod<[string], void>(),
  },
})

class DemoClient extends connectWire(demoProtocol, WindowMessageRpc) {
  public constructor(target: MessagePort) {
    super()

    this.on.notify = async (str: string) => {
      console.log(`server notify us: ${str}`)
    }

    this.rpcStart(target)
  }
}

class DemoServer extends connectWire(reverseProtocol(demoProtocol), WindowMessageRpc) {
  public constructor(target: MessagePort) {
    super()

    this.on.plus = async (a: number, b: number) => {
      for (let i = 5; i > 0; i--) {
        void this.notify(`Thanks for your order. Please wait for ${i} seconds ...`)
        await new Promise<void>((r) => {
          setTimeout(r, 1000)
        })
      }
      return a + b
    }

    this.rpcStart(target)
  }
}

;(async () => {
  const channel = new MessageChannel()
  const server = new DemoServer(channel.port1)
  const client = new DemoClient(channel.port2)
  const result = await client.plus(2, 3)
  console.log(`got rpc result: ${result}`)
})()

FAQs

Package last updated on 02 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc