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

window-bus

v1.1.4

Published

An ultra light library to facilitate communication between iframes regardless of origin

Downloads

25

Readme

Installation

npm install window-bus --save

Description

An ultra light (2Kb) library to facilitate communication between iframes, regardless of origin

Example

You can have a look at the test folder on github which contains a runnable basic example

To run the demo, clone the git repository then run npm install && npm run test

Client

import WindowBus from "window-bus";

const iframe = document.createElement('iframe');
iframe.src = 'some_url';

iframe.onload = () => {
  const bus = new WindowBus(iframe.contentWindow, 'demo'); // demo here is the server channel
  bus.startClient(iframe.src, "optionnaly send something to server").then((whatTheServerSentInStartServer) => {

    const pre = document.body;
    const display = (res) => {
      pre.append(document.createTextNode(JSON.stringify(res)));
      pre.append(document.createElement('br'));
    }

    bus.dispatch('test', {
      somePayload: true
    }).then((res) => {
      display(res);
      return bus.dispatch('test', {
        another: 'payload'
      })
    }).then(display);

    bus.on('print', (msg) => {
      display(msg);
      return "reply from the client";
    });

    bus.dispatch('otherTest', 'hi').then((res) => {
      display(res);
      return bus.dispatch('otherTest', 'hi again')
    }).then(display);
  });
}

document.body.append(iframe);

Server (the page displayed in the iframe)

import WindowBus from "window-bus";

const bus = new WindowBus(window.parent, 'demo'); // The channel is optional, but needs to match on both sides
//const bus = new WindowBus(window.opener, 'demo'); // This will allow both popups and iframes to communicate

bus.startServer(['optional list of allowed origins'], "optionally send something to the client").then((whatTheClientSentInStartClient) => {
  const pre = document.body;
  const display = (res) => {
    pre.append(document.createTextNode(JSON.stringify(res)))
    pre.append(document.createElement('br'));
  }
  const cb = (res) => new Promise((resolve) => {
    setTimeout(() => resolve({ ...res, result1: true }), 100);
  });
  bus.on('test', cb);

  bus.on('test', (res, original) => {
    bus.off('test', cb);
    display(original);
    return { ...res, result2: true };
  });

  bus.once('otherTest', (res) => {
    setTimeout(() => {
      bus.dispatch('print', 'sent from the server').then((msg) => {
        display(msg);
      });
    }, 100);
    return res + ' for the first time';
  });
});

A note on Security

This library assumes the server is a public page and thus any client can connect to it if a CSP is not configured

Here is the header you can serve from the server to only allow https://some-client.com to connect to the server

Content-Security-Policy: frame-ancestors https://some-client.com;

You can also use the first parameter of startServer: bus.startServer(['https://some-client.com']) but the CSP is the most secure option