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

puppet-manager

v1.0.8

Published

A Puppeteer Manager to handle concurrent Puppeteer pages with a single browser instance.

Downloads

2

Readme

Puppeteer Manager

A Puppeteer Manager to handle concurrent Puppeteer pages with a single browser instance.

Introduction

The Puppeteer Manager is a utility class designed to manage multiple concurrent Puppeteer pages efficiently. It allows you to limit the number of open pages, reuse them for processing multiple URLs concurrently, and optimize memory consumption by sharing a single Puppeteer browser instance.

Features

  • Manages concurrent Puppeteer pages with a specified concurrency level.
  • Efficiently reuses pages for batch job processing.
  • Reduces memory consumption by utilizing a single Puppeteer browser instance.
  • Supports customizable Puppeteer launch options.

Installation

Install the puppeteer dependency as a peer dependency:

npm install puppeteer

Install the PuppeteerManager class:

npm install puppet-manager

Usage

const PuppeteerManagerBuilder = require('puppet-manager/PuppeteerManagerBuilder');
const ConfigurationNames = require('./ConfigurationNames');

async function batchJob(page, url) {
  // Your batch job logic goes here using the 'page'
  await page.goto(url, { waitUntil: 'networkidle2' });
  // ... (your other operations on the page)
  return page.content(); // Return the result of your batch job
}

async function main() {
  const batchUrls = [
    'https://example.com/page1',
    'https://example.com/page2',
    'https://example.com/page3',
  ];

  try {
    const builder = new PuppeteerManagerBuilder();

    // Use a predefined configuration using the enum value
    const managerWithBuiltInConfig = builder
      .useConfig(ConfigurationNames.Config1)
      .build();

    const resultsBuiltInConfig = await managerWithBuiltInConfig.runBatchJobs(batchUrls, batchJob);
    console.log('Results from predefined config:', resultsBuiltInConfig);

    // Use a custom configuration using setCustomConfig
    const customConfig = {
      headless: true,
      ignoreHTTPSErrors: true,
      defaultViewport: { width: 1280, height: 720 },
      userDataDir: './custom-user-data',
      args: ['--disable-notifications'],
    };

    const managerWithCustomConfig = builder
      .setCustomConfig(customConfig)
      .build();

    const resultsCustomConfig = await managerWithCustomConfig.runBatchJobs(batchUrls, batchJob);
    console.log('Results from custom config:', resultsCustomConfig);
  } finally {
    if (managerWithBuiltInConfig) {
      await managerWithBuiltInConfig.close();
    }
    if (managerWithCustomConfig) {
      await managerWithCustomConfig.close();
    }
  }
}

main();

API Reference

PuppeteerManager(concurrency, puppeteerOptions)

Creates a new PuppeteerManager instance.

  • concurrency (optional, default: 3): The maximum number of concurrent pages to manage.
  • puppeteerOptions (optional, default: {}): Puppeteer launch options.

async runBatchJobs(urls, jobFunction)

Runs batch jobs concurrently using the available pages.

  • urls: An array of URLs to process in batch jobs.
  • jobFunction: The function that represents the batch job logic, taking a Puppeteer page and URL as input.

async initialize()

Initializes the PuppeteerManager with the browser and pages. (Internal method)

async close()

Closes all pages and the browser instance. (Internal method)

async getPage()

Gets an available page from the pool. If no page is available, waits until one becomes available. (Internal method)

returnPage(page)

Returns a page to the pool for reuse. (Internal method)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines.

Acknowledgments

  • This project is inspired by the need for efficient Puppeteer page management in concurrent tasks.

Support

For any questions, bug reports, or feature requests, please open an issue.

TODOS

  1. Include TypeScript type definitions.
  2. Include default browser configurations for a better performance.
  3. Create consistency with naming.
  4. Fix initialize method page-reopening problem.