npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

librpc-web-mod

v1.3.0

Published

Promise-based RPC client and server for web workers (forked from @librpc/web)

Downloads

2,917

Readme

Promise-based RPC client and server for web workers

Forked from https://github.com/librpc/web to add transferrables and serialized-error

Table of Contents

Features

  • Promise-based API as easy as possible
  • Load balancing with round robin strategy
  • Transferables support
  • Server events
  • Error handling
  • High performance

Install

npm install --save librpc-web-mod

Usage

// server.js
import { Server as RpcServer } from 'librpc-web-mod'

function wait(time) {
  return new Promise(resolve => setTimeout(resolve, time))
}

self.rpcServer = new RpcServer({
  add({ x, y }) {
    return x + y
  },
  task() {
    return wait(1000)
  },
  error() {
    return err
  },
  transfer(buffer) {
    return { buffer }
  },
})
// client.js
import { Client as RpcClient } from 'librpc-web-mod'

var worker = new window.Worker('server.js')

var rpcClient = new RpcClient({ workers: [worker] })

rpcClient.call('add', { x: 1, y: 1 }).then(res => console.log(res)) // 2

rpcClient.call('length').catch(err => console.log(err)) // Unknown RPC method "length"

rpcClient.call('task', null, { timeout: 100 }).catch(err => console.log(err)) // Timeout exceeded for RPC method "task"

rpcClient.call('error').catch(err => console.log(err)) // ReferenceError: err is not defined

rpcClient
  .call('transfer', new ArrayBuffer(0xff))
  .then(res => console.log(res.buffer)) // ArrayBuffer(255)

API

WebRPC.Server

#constructor(methods: { [string]: (*) => Promise<*> | * })

var server = new RpcServer({
  add({ x, y }) {
    return x + y
  },
  sub({ x, y }) {
    return x - y
  },
  mul({ x, y }) {
    return x * y
  },
  div({ x, y }) {
    return x / y
  },
  pow({ x, y }) {
    return x ** y
  },
})

Every passed method becomes remote procedure. It can return Promise if it is needed. Only ArrayBuffers will be transferred automatically (not TypedArrays). Errors thrown by procedures would be handled by server.

#emit(eventName: string, data: *)

setInterval(() => {
  server.emit('update', Date.now())
}, 50)

Trigger server event.

Note: contents of data is recursively inspected for Transferable objects. For large data, this can sometimes be intensive, so if an object contains an attribute containsNoTransferables that is set to true, transferable inspection (the peekTransferables function) will skip that object.

WebRPC.Client

#constructor(options: { workers: Array<Worker> })

var worker = new window.Worker('server.js')
var client = new RpcClient({ workers: [worker] })

Client could be connected to several workers for better CPU utilization. Requests are sent to an exact worker by round robin algorithm.

#call(method: string, data: *, { timeout = 2000 } = {}): Promise<*>

client.call('pow', { x: 2, y: 10 }).then(result => console.log(result))

Remote procedure call. Only ArrayBuffers will be transferred automatically (not TypedArrays).

Error would be thrown, if:

  • it happened during procedure
  • you try to call an unexisted procedure
  • procedure execution takes more than timeout

#on(eventName: string, listener: (*) => void)

function listener(data) {
  console.log(data)
}
client.on('update', listener)

Start listen to server events.

#off(eventName: string, listener: (*) => void)

client.off('update', listener)

Stop listen to server events.