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

silver-bullet

v0.0.4

Published

A modern library for easy and robust postmessage communication

Downloads

8

Readme

Silver Bullet

A modern abstraction for communication using postMessage.

Why?

After quite a bit of experience using postMessage, I finally determined it was very painful. So, I decided to write a library but first I would look around to see if there was already a library that I could use.

I was surprised by number of existing libraries, however, I still didn't find one that I liked. (I'll post a more detailed description of my analysis library in the future). Common problems I found were no tests or insufficient tests, no testability support, lacking features (making you write boilerplate), hard to follow not well structured code, and polluting the global namespace.

Disclaimer

This project is relatively new and the API could change at any moment. When the API is stable, version 1 will be released.

Goals

  • Easy testability.
  • Prevent race conditions.
  • Good performance and no memory leaks.
  • Cross browser support. (Still not sure what browsers yet, though).
  • Provide a clean code base that is easy for other programmers to understand.
  • 100% unit test coverage and an integration test for every feature. (Test in multiple browser versions).
  • Support "all" package managers and module loaders.
  • Promise and callback support. (Promises for sending messages, callbacks for subscribing).
  • Support anything that uses postMessage (iframes, windows, web workers)
  • Provide easy debugging / logging

Installation

npm install --save silver-bullet

And then require it:

var silverBullet = require('silver-bullet');

Examples

Simple usage

Main page

  var testSilverBullet = silverBullet.createIframe('http://iframed-page.com');
  testSilverBullet.emit('hi').then(function(result) {
    console.log(result);
  });

Page being iframed

  var testSilverBullet = silverBullet.fromParent('http://parent-page.com');
  testSilverBullet.on('hi', function(data, resolve, reject) {
    respond('hello there');
  });

Creating silver bullets

  // Communicate with parent window
  var bullet = silverBullet.fromParent('http://parent-page.com');  // Pass in the origin

  // From an existing iframe
  var bullet = silverBullet.fromIframe('http://iframe-page.com/index.html'); // Pass in the URL

  // Creating a new iframe to communicate with
  var bullet = silverBullet.createIframe('http://iframe-page.com/index.html'); // Pass in the URL

Using silver bullets

Sending messages

bullet.emit('say hello', {some: 'data'}).then(function(response) {
  console.log('yay, they responded ' + response);
}).catch(function(rejection) {
  console.log('Ahhh... , ' + rejection);
});

Subscribing to messages

// Resolve the promise the sender created by calling resolve
bullet.on('say hello', function(data, resolve, reject) {
  resolve('hi');
});

// Resolve the promise the sender created by returning a value
bullet.on('say hello', function(data, resolve, reject) {
  return 'hi';
});

// Reject the promise the sender created by calling reject
bullet.on('say hello', function(data, resolve, reject) {
  reject('you suck');
});

// Reject the promise the sender created by throwing
bullet.on('say hello', function(data, resolve, reject) {
  throw('you suck');
});

Note: If you are going to use the promise returned by emit then you should only ever set up on subscriber for a particular topic.

Contributing

The best way you contribute right now is to try out silver-bullet in your own project and open up issues (whether it be bugs or feature requests). This project has only been tried with limited use cases. I'd be very interested to hear about use cases that you need solved. Pull requests are also welcome.

Development

You must have the test-server running:

  gulp test-server

Run tests and build:

  bin/build