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

@wranggle/rpc-post-message-transport

v0.4.0-alpha.0

Published

WranggleRpc transport using window.postMessage for windows and iframes

Downloads

11

Readme

PostMessageTransport

A WranggleRpc transport for browser windows (including iframes and web/service workers), sending and receiving messages with message events.

Setup

If you are using the full @wranggle/rpc package, the PostMessageTransport is already bundled within. You can import/require it with:

import { WranggleRpc, PostMessageTransport } from '@wranggle/rpc';
// or
const { WranggleRpc, PostMessageTransport } = require('@wranggle/rpc');

Unbundled Alternative

If you prefer using just the packages you need, the unbundled es6 is also available on NPM:

yarn add @wranggle/rpc-core @wranggle/rpc-post-message-transport
# or
npm install @wranggle/rpc-core @wranggle/rpc-post-message-transport 

Unbundled import:

import WranggleRpc from '@wranggle/rpc-core';
import PostMessageTransport from '@wranggle/rpc-post-message-transport';

Construction

When creating your WranggleRpc endpoint, you can use the postMessage shortcut to also construct this transport. Eg:

const rpc = new WranggleRpc({
  postmessage: opts,
  channel: 'some-channel'
});

Or create a new instance yourself:

const rpc = new WranggleRpc({
  transport: new PostMessageTransport(opts),
  channel: 'some-channel'
});

Options

Send/receive windows

To be able to use postMessage, you need to specify which window to use for sending and for listening.

Note: if using an iframe, remember to pass in its contentWindow (eg, myIframe.contentWindow) not the iframe DOM element.

  • sendingWindow a reference to the window for sending messages. It is the window in: window.postMessage().

  • receivingWindow a reference to the window that listens for messages. The window in: window.addEventListener('message', listener)

  • targetWindow a shortcut that sets both sendingWindow and receivingWindow options to the same window. (Using the current window/global is common.)

Filtering/security

  • sendToOrigin The targetOrigin value used when calling targetWindow.postMessage. Set it the origin / base url of the receiving window. (Eg, https://example.edu) If this option is not set, it will use the current origin (meaning both communicating windows must be on the same origin.)

  • shouldReceive Filter that determines if a received message should be used or ignored. It a function is passed, it is passed the event.origin string of the received message, accepting the message only if the function returns true. If a string is passed, it will expect the message origin to match it exactly. (Eg, https://example.edu) If this option is not set, the transport will only accept messages originating from the same origin.

Example

parent window and iframe on the same origin:

Endpoint for parent window:

const iframe = document.querySelector('#my-iframe');
const rpc = new WranggleRpc({
  channel: 'our-same-origin-frames-channel',
  postMessage: {
    targetWindow: iframe.contentWindow,
  }
});

Endpoint inside iframe:

const rpc = new WranggleRpc({
  channel: 'our-same-origin-frames-channel',
  postMessage: {
    targetWindow: window,
  }
});