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

framebus-esm

v2.0.11

Published

Framebus allows you to easily send messages across frames (and iframes) with a simple bus.

Downloads

1

Readme

Framebus Build Status npm version

Framebus allows you to easily send messages across frames (and iframes) with a simple bus.

In one frame:

var bus = require('framebus');

bus.emit('message', {
  from: 'Ron',
  contents: 'they named it...San Diago'
});

In another frame:

var bus = require('framebus');

bus.on('message', function (data) {
  console.log(data.from + ' said: ' + data.contents);
});

API

target(origin): framebus

returns: a chainable instance of framebus that operates on the chosen origin.

This method is used in conjuction with publish, subscribe, and unsubscribe to restrict their results to the given origin. By default, an origin of '*' is used.

framebus.target('https://example.com').on('my cool event', function () {});
// will ignore all incoming 'my cool event' NOT from 'https://example.com'

| Argument | Type | Description | | ---------------- | -------- | ---------------------------------------------------- | | origin | String | (default: '*') only target frames with this origin |

publish('event' [, arg...] [, callback]): boolean

aliases: pub, trigger, emit

returns: true if the event was successfully published, false otherwise

| Argument | Type | Description | | ---------------- | -------- | ---------------------------------------------------- | | event | String | The name of the event | | arg | any | The data to give to subscribers | | callback(data) | Function | Give subscribers a function for easy, direct replies |

subscribe('event', fn): boolean

alises: sub, on

returns: true if the subscriber was successfully added, false otherwise

Unless already bound to a scope, the listener will be executed with this set to the MessageEvent received over postMessage.

| Argument | Type | Description | | ------------------------------ | -------- | ----------------------------------------------------------- | | event | String | The name of the event | | fn([arg...] [, callback]) | Function | Event handler. Arguments are from the publish invocation | | ↳ this | scope | The MessageEvent object from the underlying postMessage |

unsubscribe('event', fn): boolean

aliases: unsub, off

returns: true if the subscriber was successfully removed, false otherwise

| Argument | Type | Description | | ------------ | -------- | ---------------------------------------------------- | | event | String | The name of the event | | fn | Function | The function that was subscribed |

include(popup): boolean

returns: true if the popup was successfully included, false otherwise

var popup = window.open('https://example.com');

framebus.include(popup);
framebus.emit('hello popup and friends!');

| Argument | Type | Description | | ------------ | -------- | ---------------------------------------------------- | | popup | Window | The popup refrence returned by window.open |

Pitfalls

These are some things to keep in mind while using framebus to handle your event delegation

Cross-site scripting (XSS)

framebus allows convenient event delegation across iframe borders. By default it will broadcast events to all iframes on the page, regardless of origin. Use the optional target() method when you know the exact domain of the iframes you are communicating with. This will protect your event data from malicious domains.

Data is serialized as JSON

framebus operates over postMessage using JSON.parse and JSON.stringify to facilitate message data passing. Keep in mind that not all JavaScript objects serialize cleanly into and out of JSON, such as undefined.

Asynchronicity

Even when the subscriber and publisher are within the same frame, events go through postMessage. Keep in mind that postMessage is an asynchronous protocol and that publication and subscription handling occur on separate iterations of the event loop (MDN).

Published callback functions are an abstraction

When you specify a callback while using publish, the function is not actually given to the subscriber. The subscriber receives a one-time-use function that is generated locally by the subscriber's framebus. This one-time-use callback function is pre-configured to publish an event back to the event origin's domain using a UUID as the event name. The events occur as follows:

  1. http://emitter.example.com publishes an event with a function as the event data

    var callback = function (data) {
        console.log('Got back %s as a reply!', data)
    }
    
    framebus.publish('Marco!', callback, 'http://listener.example.com');
  2. The framebus on http://emitter.example.com generates a UUID as an event name and adds the callback as a subscriber to this event.

  3. The framebus on http://listener.example.com sees that a special callback event is in the event payload. A one-time-use function is created locally and given to subscribers of 'Marco!' as the event data.

  4. The subscriber on http://listener.example.com uses the local one-time-use callback function to send data back to the emitter's origin

    framebus.target('http://emitter.example.com')
      .on('Marco!', function (callback) {
         callback('Polo!');
      });
  5. The one-time-use function on http://listener.example.com publishes an event as the UUID generated in step 2 to the origin that emitted the event.

  6. Back on http://emitter.example.com, the callback is called and unsubscribed from the special UUID event afterward.

Development and contributing

See CONTRIBUTING.md