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

@widget-kit/rpc

v1.0.0

Published

P2P RPC via postMessage

Downloads

1,898

Readme

@widget-kit/rpc

Establishes connection between two windows allowing both to send and handle requests.

Install

$ npm install --save @widget-kit/rpc

Usage

// Main window:
const hostConnection = createConnection(
  iframe.contentWindow,
  'https://remote.example.com'
);

hostConnection.handle('ping', (respond) => {
  respond('pong');
});

// Iframe (loaded from https://remote.example.com):
const widgetConnection = createConnection(window.parent);
const pingRequest = createRequest('ping');
widgetConnection.send(pingRequest, (response) => {
  console.log(response.payload);  // prints 'pong'
});

API Reference

createConnection(targetWindow, [targetOrigin])

Returns Connection object with send and handle methods.

  • targetWindow: a reference to remote window.
  • targetOrigin: allows to restrict windows communication by origin. If targetWindow origin does not match no request would be sent nor accepted from it.

send(request, [onResponse])

Sends prepared request.

  • request: a plain object containing request method, arguments list and set of service fields. Use createRequest helper to create proper request object.
  • onResponse: response handler. Accepts Response object with payload and optional error fields.

handle(method, handler)

Registers request handler for a given method.

  • method: an unique method identifier. It's recommended to have prefix with your app name for custom methods (like myAwesomeApp.analytics.sendEvent).
  • handler: a handler function that accepts response callback and method arguments. You can call response callback at any time passing to it result or error.

Example:

connection.handle('myapp.divide', (respond, a, b) => {
  if (b === 0) {
    respond(null, 'Whoops, you\'re trying to divide by zero!');
  } else {
    respond(a / b);
  }
});

createRequest(method, ...args)

Creates request object that could be sent later.

  • method: a name of the called method.
  • args: arguments that would be passed to method handler.

Example:

// We can make own "request creator" helper
const sum = (a, b) => createRequest('myapp.sum', a, b);

// And then send this request with different arguments easily
connection.send(sum(1, 1));
connection.send(sum(5, 5));