Socket
Book a DemoInstallSign in
Socket

ifrpc

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ifrpc

RPC interface for iframes (or popups)

2.0.0
latest
npmnpm
Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created
Source

ifrpc

Iframe (or popup) RPC interface.

About

This library is designed for use in managing frame to frame control. Either in the case of 3rd party parent frames managing an iframe(s). Or in the case of a window managing popup windows it created with window.open().

You must have control over both frames involved (referred to as peers). This library needs to be included in both origins.

Installation

Run:

npm install ifrpc --save

Then include node_modules/ifrpc/src/ifrpc.js in your web build process or as a <script> tag.

Repeat this process for both peers if they are separate web applications.

Setup

The ifrpc system must be initialized by both sides as they use the window.postMessage interface for communicating. Care should be taken in setting the origin correctly from both contexts.

Example from the parent frame who's html includes an iframe with src of https://iframe:

const iframe = document.querySelector('iframe').contentWindow;
const iframeRPC = ifrpc.init(iframe, {peerOrigin: 'https://iframe'});

And in the code for the aforementioned iframe:

const parentRPC = ifrpc.init(self.parent, {peerOrigin: 'https://parent'});

Usage

With setup completed you can now use ifrpc to add command handlers and trigger events. Commands are defined by either application and they are fulfilled by a JavaScript function. The return value (or exception) of these functions will be serialized (via structured clone algo) and sent to the peer frame. The invocation of commands is a Promise based interface so the caller doesn't need to be concerned with the mechanics of the message passing.

The main functions to use are:

  • ifrpc.init(frame, [{options}]):
    • frame should be the frame/window object you wish to communicate with.
    • options is an optional object with config options:
      • peerOrigin: The expected origin of the peer for security.
      • magic: A special string value used to disambiguate postMesssage communication.
    • Returns and RPC object.
  • <RPC>.addCommandHandler(name, callback):
    • name should any string.
    • callback should be a function or async function whose return value will be sent to the invoking peer.
  • <RPC>.addEventListener(name, callback):
    • name should any string.
    • callback should be a function or async function.
  • <RPC>.triggerEvent(name, ...args):
    • name is the event name defined by the peer frame with <RPC>.addEventListener.
  • <RPC>.invokeCommand(name, ...args):
    • name is the command name defined by the peer frame with <RPC>.addCommandHandler.
    • Returns a Promise that resolves with the eventual return value of the command handler's callback. If the command handler throws an exception the Promise will reject with an iprc.RemoteError exception describing the remote error.

Examples

Each of these examples shows code from 2 applications who are presumed to be peers. Peers meaning that one is an iframe of the other.

Simple ping/pong

rpcForIframe.addCommandHandler('ping', () => 'pong');
const pong = await rpcForParent.invokeCommand('ping');
console.assert(pong === 'pong');

Argument passing

rpcForIframe.addCommandHandler('sum', (a, b) => a + b);
const sum = await rpcForParent.invokeCommand('sum', 1, 1);
console.assert(sum === 2);

Async handler

rpcForIframe.addCommandHandler('soon', async () => {
    await somethingAsync();
    return true;
}
const soon = await rpcForParent.invokeCommand('soon');
console.assert(soon === true);

Simple event

rpcForIframe.addEventListener('hello', () => {
    console.warn("The peer frame said hello");
});
rpcForParent.triggerEvent('hello');

Event with args

rpcForIframe.addEventListener('hello', whom => {
    console.warn("The peer frame said hello to", whom);
});
rpcForParent.triggerEvent('hello', 'Bob');

FAQs

Package last updated on 04 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.