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

protoframe

v1.1.1

Published

A small (dependency-free) library for 2-way iframe communication.

Downloads

605

Readme

Protoframe

Build Status

A small (dependency-free) library for 2-way iframe communication.

Problem

You are embedding an iframe where you would like to communicate with the following facilities:

  1. From your parent page, you need to use iframe.contentWindow.postMessage
  2. From your iframe, you need to subscribe to these messages using window.addEventListener('message', (ev) => ...)
  3. And vice versa, sometimes at the same time

Solution

This library allows you to define a protocol for this type of communication supporting both fire-and-forget (tell) semantics, as well as request/response (ask) semantics. It provides connector constructors that will facilitate the sending and receiving of these messages across parent and iframe windows.

How?

npm install protoframe --save

ProtoframeDescriptor

import { ProtoframeDescriptor } from 'protoframe';

const cacheProtocol: ProtoframeDescriptor<{
  getFoo: {
    body: { key: string };
    response: { value: string };
  };
  setBar: {
    body: { key: string; value: string };
  };
}> = { type: 'cache' };

The ProtoframeDescriptor defines 2 elements of your protocol:

  1. The message types. The type argument is a structural type that defines the message types as its top level key. For each message type, it defines the body type of the payload (for ask and tell requests), and an optional response types (for ask requests). An ask message must define a response key, a tell message must not
  2. The value of the descriptor ({type: 'cache'}) defines a type key for the protocol, which is used to key on specific types of messages to listen to between the pages

Connectors

The connector allows 2-way communication between a page and its iframe. For example, we have a page that wishes to use an iframe as a "cache" server:

Iframe:

import { ProtoframePubsub } from 'protoframe';

// Will hold our cache state
const data: { [key: string]: string } = {};

// The connector. This function creates a connector that listens for messages
// on `window` and sends messages and responses over `window.parent`
const cache = ProtoframePubsub.iframe(cacheProtocol);

// Implement our handlers for the ask and tell requests defined in the protocol
cache.handleTell('setBar', ({ key, value }) => (data[key] = value));
cache.handleAsk('getFoo', async ({ key }) => {
  const value = key in data ? data[key] : null;
  return { value };
});

Parent:

import { ProtoframePubsub } from 'protoframe';

const iframe = document.getElementById('myCacheServerIframe');
const client = ProtoframePubsub.parent(cacheProtocol, iframe);

// Wait for the iframe page to load and for the connector to be instantiated
ProtoframePubsub.connect(client).then(() => {
  client.tell('setBar', { key: 'my key', value: 'my value' });

  // value = { value: 'my value' }
  const value = await client.ask('getFoo', { key: 'my key' });
});