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

@ont-community/window-post-message-proxy

v0.2.14

Published

A library used in place of the native window.postMessage which when used on both the sending and receiving windows allow for a nicer asynchronouse promise messaging between the windows

Downloads

531

Readme

window-post-message-proxy

Travis branch npm Total Downloads Monthly Downloads GitHub tag

A library used in place of the native window.postMessage which when used on both the sending and receiving windows allow for a nicer asynchronous promise messaging between the windows.

When sending messages using the proxy, it will apply a unique id to the message, create a deferred object referenced by the id, and pass the message on to the target window. The target window will also have an instance of the windowPostMessage proxy setup which will send back messages and preserve the unique id. Then the original sending instance receives the response message with id, it will look to see if there is matching id in cache and if so resolve the deferred object with the response.

Documentation

https://microsoft.github.io/window-post-message-proxy

Installation

npm install --save window-post-message-proxy

Basic Usage

// Setup
const iframe = document.getElementById("myFrame");
const windowPostMessageProxy = new WindowPostMessageProxy();

// Send message
const message = {
    key: "Value"
};

windowPostMessageProxy.postMessage(iframe.conentWindow, message)
    .then(response => {
        
    });

Advanced Customization

Customizing how tracking properties are added to the method

By default the windowPostMessage proxy will store the tracking properties as object on the message by known property: windowPostMesssageProxy.

This means if you call:

const message = {
    key: "Value"
};

windowPostMessageProxy.postMessage(iframe.conentWindow, message);

The message is actually modified before it's sent to become:

{
    windowPostMessageProxy: {
        id: "ebixvvlbwa3tvtjra4i"
    },
    key: "Value"
};

If you want to customize how the tracking properties are added to and retrieved from the message you can provide it at construction time as an object with two functions. See the interface below:

export interface IProcessTrackingProperties {
  addTrackingProperties<T>(message: T, trackingProperties: ITrackingProperties): T;
  getTrackingProperties(message: any): ITrackingProperties;
}

addTrackingProperties takes a message and adds the tracking properties object and returns the message. getTrackingProperties takes a message and extracts the tracking properties.

Example:

const customProcessTrackingProperties = {
    addTrackingProperties(message, trackingProperties) {
        message.headers = {
            'tracking-id': trackingProperties.id
        };
        
        return message;
    },
    getTrackingProperties(message): ITrackingProperties {
        return {
            id: message.headers['tracking-id']
        };
    }
};
const windowPostMessageProxy = new WindowPostMessageProxy(customProcessTrackingProperties);

Customizing how messages are detected as error responses.

By default response messages are considered error message if they contain an error property.

You can override this behavior by passing an isErrorMessage function at construction time. See interface:

export interface IIsErrorMessage {
  (message: any): boolean;
}

Example:

function isErrorMessage(message: any) {
    return !(200 <= message.status && message.status < 300);
}

const windowPostMessageProxy = new WindowPostMessageProxy({ isErrorMessage });

Logging messages

By default messages are not logged, but you can override this behavior by passing logMessages: true in the options object.

const windowPostMessageProxy = new WindowPostMessageProxy({ logMessages: true });

This will print out a stringified JSON of each object that is received or sent by the specific instance.

Supplying custom name

Each windowPostMessageProxy gives itself a randomly generated name so you can see which instance is communicating in the log messages. Often times you may want to pass a custom name for which window the windowPostMessageProxy instance is running.

You can provided a name by passing name: 'Iframe' in the options object.

const windowPostMessageProxy = new WindowPostMessageProxy({ name: 'Iframe' });

Supress Warning Message about unhandled messages

By default the window post message proxy will warn you if it received a message that was not handled since this is usually an indication of error; however, if you are register multiple window message handlers the message may handled but it's just not able to be known by the windowPostMessageProxy and this warning no longer applies.

const windowPostMessageProxy = new WindowPostMessageProxy({ suppressMessageNotHandledWarning: true });