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

@datadog/framepost

v0.4.1

Published

Secure parent-child iframe communication

Downloads

4,658

Readme

FramePost

FramePost is a library for parent-child iframe communication. It is designed to handle the details of secure communication while also providing a user-friendly API that makes iframe communcation feel more like using an API. This works by establishing a channel between the parent and child through a mutual handshake process before any other logic may proceed.

Parent Client

The parent client is responsible for initializing a channel with the child. When establishing a channel, the parent may include arbitrary data as context sent to the child during the handshake process. This must be done after the iframe is fully loaded. It is highly recommended that the channel be initialized in an onload event:

import { ParentClient } from '@datadog/framepost';

const client = new ParentClient();

const frame = document.createElement('iframe');

frame.src = 'https://domain.dom/path/to/frame.html';

const context = {
    infoAboutParent: 'it is a demo',
}

frame.onload = () => {
    client.requestChannel(frame, frame.src, context);
}

Child Client

The child client must be initialized in the iframe during initial script execution. The child may provide context to be sent to the parent during the handshake process in the context config option.

import { ChildClient } from '@datadog/framepost';

const context = {
    infoAboutChild: 'It is a demo'
}

const client = new ChildClient({ context });

Shared API

After the initial handshake succeeds, each client will handle communication with its counterpart using a shared API.

client.getContext():

Retrieves the context that the opposite client sent during the handshake process:

// in parent
const contextFromChild = await client.getContext();

// in child
const contextFromParent = await client.getContext();

client.on()

The counterpart to client.send(). Subscribes an event handler that will execute on events of the specified type from the opposite client. Event keys must be unique. Returns an unsubscribe hook.

const unsubscribe = client.on('route_change', newRouteData => {
    ...
})

// later
unsubscribe();

client.send()

The counterpart to client.on(). Sends an event to the opposite client with the specified event key and data. Event keys must be unique:

client.send('event_key', dataAboutEvent);

client.request():

The counterpart to client.onRequest(). A convenience method to send and receive data asynchronously from the opposite client. Designed to function like a promise-based api call. In order for a round-trip request to function correctly, an onRequest handler with a matching request key must be listening in the opposite client before the requeset is initiated. See below.

const data = await client.request('getMyData', requestParams);

client.onRequest():

The counterpart to client.request(). Receives request data send from a client.request call in the opposite client. Executes the provided handler, sending the returned response back to the opposite client.

client.onRequest('getMyData', async requestParams => {
    const responseData = await doSomeStuff());

    return responseData;
})

Advanced

Debug mode

Either client can be initialized in debug mode:

const parentClient = new ParentClient({ debug: true });
const childClient = new ChildClient({ debug: true });

This will print debug statements on various events and error cases.

Profiling

Cool feature alert! FramePost will collect and report timing profiles of message transactions handled by each channel. To enable, pass profile: true to each client on init. Each client will silently collect data that can be requested in the parent with parentClient.getProfile():

// in child
const childClient = new ChildClient({ proile: true });

// in parent
child parentClient = new ParentClient({ profile: true });

// after channel init and a few transactions:

parentClient.getMessageProfile().then(profile => {
    // do whatever you want with profile data
    console.log(profile);
});