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

iframe-message-proxy

v1.2.0

Published

iframe-message-proxy

Downloads

861

Readme

Build Status

Iframe Message Proxy

This package is used for BLiP platform to handle communications between micro frontends, done by iframes throught postMessages. Basically, we send a message and wait for response. It is possible because every message sended to parent window throught IframeMessageProxy has a cached promise with an ID that is resolved when current window receive a message with the same ID. Jump to Usage section to more details.

Installation

npm i -S iframe-message-proxy

Usage

import { IframeMessageProxy } from 'iframe-message-proxy';

IframeMessageProxy.listen(); // Start listen for post messages

// Sending messages
IframeMessageProxy.sendMessage({
  action: 'customAction',
  content: 'Here is my awesome action',
});

sendMessage method takes an object as param that accept these properties:

| Property | Type | Required | Description | | -------- | ---- | -------- | ----------- | | action | string | true | Action sended to parent iframe. By default, is prefixed by blipEvent: | | content | any | false | Actions can have optional contents added | fireAndForget | boolean | false | Messages can have no response and be just a command to parent iframe | caller | string | false | Every message has a caller. By default, is used child iframe name (passed as attribute on <iframe name="iframe-name">...) but you can set a custom caller name too.

By default, sendMessage method will send a postMessage to parent window and wait for some response, if has one.

// Child iframe
const action = await IframeMessageProxy.sendMessage({
  action: 'customAction',
  content: 'Here is my awesome action',
});

// Parent iframe
const iframe = document.getElementById('my-iframe').contentWindow; // Get iframe caller

// Handle received messages
const handleOnReceiveMessage = msgEvt: MessageEvent => {
  /**
   * Assuming that window can receive many postMessage events,
   * there is a tip to filter only messages camed from our library
   */
  const BLIP_EVENT_PREFIX = 'blipEvent:'
  const shouldHandleMessage = msg =>
    Object.keys(msg)
      .find(k => k == 'action' && msg.action.startsWith(BLIP_EVENT_PREFIX));

  if (!msgEvt.data || !message || !shouldHandleMessage(msgEvt.data.message)) {
    return;
  }

  /**
   * Every message has properties "message" and "trackingProperties".
   * "trackingProperties" is used by Iframe Message Proxy to identify
   * which promise will be resolved after send a postMessage, so
   * if you want to send something back to caller, you have to pass
   * trackingProperties received from child iframe.
   */
  const { message, trackingProperties } = msgEvt.data;

  iframe.postMessage({
    response: 'Success!',
    trackingProperties
  }, '*')
}

window.addEventListener('message', handleOnReceiveMessage);

Handling errors

If you want to send an error message to child iframe, you may also add error property to response object. In this way, the child iframe will reject the promise instead of resolve them.

try {
  doSomethingWrong();
} catch (e) {
  iframe.postMessage({
    error: e.toString(),
    trackingProperties
  }, '*')
}

Configuring

You can also configure defaults by config method:

IframeMessageProxy.config({
  prefix: 'customPrefix:',
  eventCaller: 'jarvis',
})

prefix?: string caller?: string receiveWindow?: Window targetWindow?: Window shouldHandleMessage?: ((message: IIdentifiedMessage) => boolean)

| Property | Type | Default | Description | | -------- | ---- | -------- | ----------- | | prefix | string | blipEvent: | Action prefix | | caller | string | window.name | Caller name | | receiveWindow | Window | window | Window that will receive postMessages responses | | targetWindow | Window | window.parent | Window that we'll request something | | shouldHandleMessage | () => boolean | undefined | You can choose what message will be parsed or not by calling a function that takes a MessageEvent as argument. function(evt) { if (!evt.data) return false }