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

puppeteer-ipc

v1.1.0

Published

Lightweight wrapper for mutual communication on Puppeteer, inspired by Electron

Downloads

61

Readme

puppeteer-ipc

NPM Node.js CI

Lightweight wrapper for mutual communication on Puppeteer, inspired by Electron.

Install

npm i puppeteer puppeteer-ipc

Usage

import puppeteer from "puppeteer";
import { IPC } from "puppeteer-ipc/main";

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://example.com");

const ipc = new IPC(page);
await ipc.start();

// ----- Browser side -----
await page.evaluate(`
  const { IPC } = window['puppeteer-ipc/browser'];
  const ipc = new IPC();

  ipc.on('ping', () => {
    ipc.send('pong', 'hello');
  });
`);

// ----- Node.js side -----
ipc.on("pong", (data) => {
  console.log(`Message from the browser: ${data.message}`);
  browser.close();
});
await ipc.send("ping");

// Output: Message from the browser: hello

How It Works

puppeteer-ipc uses APIs below to make mutual communication possible:

API

IPC (puppeteer-ipc/main)

  • page Page Puppeteer's page instance
  • options.distPath string Path for JS file of puppeteer-ipc/browser
  • options.skipBrowserInitialization boolean Whether or not to skip initialization such as loading JS file on browser.

This is a class that controls IPC on Node.js side. Since this class extends EventEmitter, you can also use inherited methods such as on, off and once.

start

  • returns: Promise<void> Nothing

Exposes Node.js gateway methods to the browser and wait for them to be loaded.

send

  • name string Name of the event
  • ...payloads unknown[] Payloads of the event which will be passed to the callback function. These values must be serializable to JSON
  • returns: Promise<void> Nothing

A method that sends an event to the browser from Node.js.

IPC (puppeteer-ipc/browser)

This is a class that controls IPC on the browser side.

IPC.send

A method that sends an event to Node.js from the browser. Behaves exactly the same as puppeteer-ipc/main's send method.

TypeScript

puppeteer-ipc is written in TypeScript and fully supports its major features.

Event Callbacks

You can pass a map of event name and its arguments to IPC as a generic parameter to annotate parameters:

const ipc = new IPC<{
  my_event: [string, number];
  [key: string]: [unknown];
}>(page);

ipc.on("my_event", (a, b) => {
  // `a` and `b` are type-safe.
});

Typing Module on Browser

If you use JS bundler for browsers such as webpack, then you can use import statement and consume the type information.

// Browser side
import { IPC } from "puppeteer-ipc/browser";

Note that in this case, you can pass option skipBrowserInitialization: true to avoid creating IPC instance twice:

// Node.js side
const ipc = new IPC(page, { skipBrowserInitialization: true });

Related Projects

  • Electron ─ A desktop app framework which also uses IPC technique to communicate mutually between Node.js and Chromium process.
  • electron-better-ipc ─ Electron IPC wrapper for synchronous communication.

License

MIT