Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@fleekhq/browser-rpc
Advanced tools
This library allows you exchange messages beetween BrowserRPC instances using window.postMessage API. You can call remote procedures as promises.
This library allows you exchange messages beetween BrowserRPC instances using window.postMessage API. You can call remote procedures as promises.
Using npm:
$ npm install @fleekhq/browser-rpc
Using yarn
$ yarn add @fleekhq/browser-rpc
RPC requires that you setup a client (who calls remote procedures and wait for responses) and a server (who process the calls and send back the responses).
Let's say that your client is going to be placed in your webpage and need to call a remote procedure method called sum
that receives 2 arguments (2 number values to sum).
import { BrowserRPC } from '@fleekhq/browser-rpc';
// Create a new rpc instance
// the name is the identidfier of this rpc instance. Name is injected as part of the call message to identify who is trying to call the method requested.
// the target is the name of the rpc server instance that should handle the request
const client = new BrowserRPC(window, {
name: 'browser-client',
target: 'rpc-server',
timeout: 10000,
});
// Before calling any remote method, you have to start the rpc instance. This way, the rpc instance listen for the incoming responses
client.start();
// Now you can call the remote procedures using the "call" method. Take in mind that "call" method returns a promise that resolves to the response sent back by the rpc server or reejects with an error.
client
.call('sum', [1, 2])
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
// Finally, if you don't need to execute more calls, you can stop the rpc client. This method removes the listeners, which means that any incoming response is not going to be catched by the client. If later you need to call another method, you have to "start" the client again.
client.stop();
We've already setup the client, now we have to setup the server. The server listen for methods calls, process the calls and send the responses back to the client. Take in mind that the server only process the calls coming from the target
specified in the initial config object.
import { BrowserRPC } from '@fleekhq/browser-rpc';
// Create a new rpc instance
// name property is injected as part of the responses messages to identify who processed the response
// target property specifies who can call methods on this new instance and receive the responses. If a different target try to call methods on this instance, the calls are ignored.
const server = new BrowserRPC(window, {
name: 'rpc-server',
target: 'browser-client',
});
/* In order to process the calls coming from the client, you have to expose "handlers".
Handlers are just functions that receive as first argument a callback and the rest of arguments are defined by you.
your handler function has to call the callback once the response is procesed or when an error ocurred. the callback receives as first argumeent an ErrorRes object (please refer to the ErrorRes Type section) type and as second the response (any).
To expose a new handler you have to call the "exposeHandler" method on your rpc instance and pass a string name (this is the identifier for your handler) and your handler function. You can add as many handlers as you want.
*/
server.exposeHandler('sum', (cb, val1, val2) => {
const result = val1 + val2;
cb(null, result);
});
// Finally, in order to start receiving incoming calls, you have to start your rpc instance.
// Same as the client, you can stop the server whenever you want.
server.start();
For types info please refer to the types.ts
file: src/types.ts
new BrowserRPC(window, config)
window
: the window object used by the instance to post messages and add the event listeners.config
: [object] config required by the instance
name
: [string] name of the instance. This name is injected in every message emitted by the instance to identify the caller.target
: [string] the name of the rpc instance that has to handle the call requeststimeout?
: [number] timeout for call methods. 0 means no timeout. by default this value is 5000 mshandlers?
{[name: string]: Handler} object with handlers functions. object key is taken as the name identifier for the every handler function defined in the objectAdd the event listeners required by the instance
Remove the event listeners and rejects all the pending calls.
Add a new handler to the instance
Remove a handler by its name
Call a new remote procedure method. Returns a promise resolving to the response or rejecting with an error.
Your handlers functions receive as first argument a callback function that has to be called with the response (second argument) or with an ErrorRes object as first argument if there is any error.
ErrorRes Type:
export type ErrorRes = {
code: number,
message: string,
data?: any,
};
This error type represents a JSON RPC error object. Please refer to the JSON RPC documentation to get information about error codes, message and data:
FAQs
This library allows you exchange messages beetween BrowserRPC instances using window.postMessage API. You can call remote procedures as promises.
The npm package @fleekhq/browser-rpc receives a total of 4 weekly downloads. As such, @fleekhq/browser-rpc popularity was classified as not popular.
We found that @fleekhq/browser-rpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
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.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.