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 🙏

© 2025 – Pkg Stats / Ryan Hefner

iframe-console-relay

v0.1.2

Published

Relay console logs from an iframe to the parent window via postMessage.

Readme

iframe-console-relay

한국어 README 보기

Relay console.* messages from an iframe to the parent window using postMessage.

Works entirely in the browser. The iframe wraps its console methods and posts structured log events to the parent, which can consume them and optionally forward to the parent's console.

Install

npm install iframe-console-relay

Usage

In the iframe (sender)

import { relayConsoleToParent } from 'iframe-console-relay/iframe';

// Start relaying console.* calls from inside the iframe
const teardown = relayConsoleToParent({
  targetOrigin: 'https://your-parent-app.example.com',
  sessionId: 'optional-session-id',
  captureGlobalErrors: true,
});

console.log('Hello from iframe', { foo: 123 });

// Later, to stop relaying and restore original console
// teardown();

Notes:

  • targetOrigin should be the exact origin of the parent page for security. Use '*' only for local development.
  • When captureGlobalErrors is true (default), error and unhandledrejection are also relayed.

In the parent window (receiver)

import { attachIframeConsoleRelay } from 'iframe-console-relay/parent';

// Optional: filter to a specific iframe and origin(s)
const iframeEl = document.getElementById('child') as HTMLIFrameElement | null;

const detach = attachIframeConsoleRelay({
  allowedOrigins: ['https://child-app.example.com'],
  iframe: iframeEl ?? undefined,
  forwardToConsole: true,
  onEvent: (e) => {
    // Custom handling
    // e.level, e.args, e.timestamp, e.frameUrl, e.origin
  },
});

// Later, to stop listening
// detach();

UMD/CDN usage

You can use the UMD bundle(s) directly from a CDN via <script> tags. The globals are:

  • IframeConsoleRelay (index): { relayConsoleToParent, attachIframeConsoleRelay }
  • IframeConsoleRelayIframe (iframe-only): { relayConsoleToParent }
  • IframeConsoleRelayParent (parent-only): { attachIframeConsoleRelay }

Example (index bundle):

<script src="https://unpkg.com/iframe-console-relay/dist/index.umd.min.js"></script>
<script>
  // In iframe context
  const stop = IframeConsoleRelay.relayConsoleToParent({ targetOrigin: 'https://parent.example.com' });

  // In parent context
  const detach = IframeConsoleRelay.attachIframeConsoleRelay({ allowedOrigins: ['https://child.example.com'] });
  console.log('ready');
</script>

API

relayConsoleToParent(options?: IframeRelayOptions): () => void

  • targetOrigin?: string — Parent origin; defaults to '*'.
  • sessionId?: string — Arbitrary identifier included in events.
  • captureGlobalErrors?: boolean — Also relay window errors; default true.
  • levels?: ('log'|'info'|'warn'|'error'|'debug'|'trace')[] — Which console levels to wrap.

Returns a teardown function that restores original console methods and removes listeners.

attachIframeConsoleRelay(options?: ParentAttachOptions): () => void

  • allowedOrigins?: '*' | string[] — Validate message origin(s); default '*'.
  • iframe?: HTMLIFrameElement — If provided, only accept messages from that iframe.
  • forwardToConsole?: boolean — Also log to parent's console; default true.
  • onEvent?: (event) => void — Callback for each relay payload.

Returns a detach function that removes the window message listener.

Message format

The iframe posts a payload with shape:

type RelayPayload = {
  type: 'IFRAME_CONSOLE_RELAY';
  level: 'log'|'info'|'warn'|'error'|'debug'|'trace';
  args: unknown[];            // safely-serialized values
  timestamp: number;
  frameUrl?: string;
  sessionId?: string;
}

Arguments are serialized in a safe, best-effort way (errors, functions, circular objects, and DOM nodes are handled without throwing).

Security

  • Always set targetOrigin on the iframe side and allowedOrigins on the parent side in production.
  • Messages are filtered by origin and (optionally) iframe source on the parent side.

License

MIT