Gathering detailed insights and metrics for worker-rpc
Gathering detailed insights and metrics for worker-rpc
Gathering detailed insights and metrics for worker-rpc
Gathering detailed insights and metrics for worker-rpc
worker-factory
A little factory function to create a JSON-RPC based Web Worker implementation.
rpc-shooter
A tool library for handling window && iframe && worker communication based on the JSON RPC specification
@lynx-js/web-worker-rpc-canary
@lynx-dev/web-worker-rpc-canary
Lynx3 Web Platform runtime core
A simple RPC layer for communicating with web workers and over other transports
npm install worker-rpc
Typescript
Module System
Node Version
NPM Version
99.6
Supply Chain
98.6
Quality
74.9
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
1,142,628,033
Last Day
113,391
Last Week
1,945,116
Last Month
8,756,509
Last Year
106,126,112
MIT License
46 Stars
14 Commits
7 Forks
2 Watchers
1 Branches
3 Contributors
Updated on Apr 11, 2025
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
worker-rpc@0.2.0
Size
6.64 kB
NPM Version
5.6.0
Node Version
8.11.3
Published on
May 19, 2019
Cumulative downloads
Total Downloads
Last Day
-27.3%
113,391
Compared to previous day
Last Week
-12.1%
1,945,116
Compared to previous week
Last Month
7.5%
8,756,509
Compared to previous month
Last Year
-38.2%
106,126,112
Compared to previous year
1
5
This package provides a simple RPC mechanism on top of any transport that transfers
JSON data. It was initially conceived to provide communication with web workers
(and as such supports transferables), but it can be used on top of many other
different transport channels, i.e. postMessage
between frames, websockets via
socket.io
or JSON encoded messages over pipes.
You can install the library into your project via npm
npm install worker-rpc
The library is written in Typescript and will work in any environment that supports ES5 and ES6-style promises (either native or through a shim). No external typings are required for using this library with Typescript (version >= 2).
In this example, we use the library to set up communication with a web worker.
import {RpcProvider} from 'worker-rpc';
const rpcProvider = new RpcProvider(
(message, transfer) => postMessage(message, transfer)
);
onmessage = e => rpcProvider.dispatch(e.data);
rpcProvider.registerRpcHandler('add', ({x, y}) => x + y);
The RPC provider is initialized with a function that dispatches a message. This function will receive an opaque message object as first argument, and a list of transferables as second argument. This allows to leverage transfer of ownership instead of copying between worker and host page.
On incoming messages, dispatch
is called on the RPC provider in order to
handle the message.
Each registered RPC handler is identified by a message ID (add
in this example)
and has a handler function that receives the message object and can return a
result either as an immediate value or as a promise.
import {RpcProvider} from 'worker-rpc';
const worker = new Worker('worker.js'),
rpcProvider = new RpcProvider(
(message, transfer) => worker.postMessage(message, transfer)
);
worker.onmessage = e => rpcProvider.dispatch(e.data);
rpcProvider
.rpc('add', {x: 1, y: 2})
.then(result => console.log(result)); // 3
ES5 / CommonJS
var RpcProvider = require('worker-rpc').RpcProvider;
ES6
import {RpcProvider} from 'worker-rpc';
Typescript
import {RpcProvider, RpcProviderInterface} from 'worker-rpc';
The API is built around the RpcProvider
class. A RpcProvider
acts both as
client and server for RPC calls and event-like signals. The library uses ES6
promises and can consume any A+ compliant promises.
const rpc = new RpcProvider(dispatcher, timeout);
dispatcher
: A function that will be called for dispatching messages. The
first argument will be an opaque message object, and the second argument
an error of Transferable
objects that are to be passed via ownership
transfer (if supported by the transport).timeout
(optional): The timeout for RPC transactions in milliseconds.
Values of 0
or smaller disable the timeout (this is the default).rpc.dispatch(message);
Similar to message dispatch, worker-rpc
does not provide a built-in mechanism
for receiving messages. Instead, incoming messages must be relayed to the provider
by invoking dispatch
.
message
: The received message.rpc.registerRpcHandler(id, handler);
Register a handler function for RPC calls with id id
. Returns the provider instance.
id
: RPC call id. Only a single handler can be registered for any id. Ids should
be strings.handler
: The handler function. This function receives the payload object as
its argument and can return its result either as an immediate value or as a
promise.rpc.registerSignalHandler(id, handler));
Register a handler function for signals with id id
. Returns the provider instance.
id
: Signal id. The namespace for signal ids is seperate from that of RPC ids,
and multiple handlers my be attached tp a single signal. Ids should be stringshandler
: The handler function. This function receives the payload object as
its argument; the result is ignored.const result = rpc.rpc(id, payload, transfer);
Dispatch a RPC call and returns a promise for its result. The promise is rejected if the call times out or if no handler is registered (or if the handler rejects the operation).
id
: RPC call id.payload
(optional): RPC call payload.transfer
(optional): List of Transferables
that will be passed to dispatched
(see above).rpc.signal(id, payload, transfer);
Dispatch a signal. Returns the provider instance.
id
: Signal id.payload
(optional): Signal payload.transfer
(optional): List of Transferables
that will be passed to dispatched
(see above).rpc.deregisterRpcHandler(id, handler);
id
and handler
must be the same arguments used for registerRpcHandler
.
Returns the provider instance.
rpc.deregisterSignalHandler(id, handler);
id
and handler
must be the same arguments used for registerSignalHandler
.
Returns the provider instance.
rpc.error.addHandler(errorHandler);
The error event is dispatched if there is either a local or remote communcation error (timeout, invalid id, etc.). Checkout the microevent.ts documentation for the event API.
Feel free to use this library under the conditions of the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 2/12 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More