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

@teocns/puppeteer-middlewares

v1.0.3

Published

A collection of middlewares for Puppeteer PageEvent

Downloads

2

Readme

Puppeteer Middlewares

A library that currently implements rule-based Request Middlewares for Puppeteer page events.

You can conditionally proxify, block, retry, or just override request options!

Installation

  • Using NPM

    npm i @teocns/puppeteer-middlewares
  • From source

    git clone https://github.com/teocns/puppeteer-middlewares/
    cd puppeteer-middlewares
    npm install && npm run build

Running tests

  • npm run test makes use of Jest to run all tests placed under /test

Request Middleware

⚡ Usage

const puppeteer = require('puppeteer');
const { RequestMiddleware, ConditionRuleMatchType } = require('@teocns/puppeteer-middlewares');

(
    async () => {
        const browser = await puppeteer.launch({
            headless: false,
        });
        const page = await browser.newPage();

        
    
        new RequestMiddleware(
            {
                conditions: { match: 'google.com', type: ConditionRuleMatchType.URL_CONTAINS },
                effects:{
                    proxy: 'http://localhost:8899',
                }
            }
        ).bind(page);


        await page.goto('https://google.com');

        const awaitSleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
        await awaitSleep(10000);
        await browser.close();
    }
)();

❓ How to build rules

Each rule are dictionary objects made of conditions and effects.

Example
{
  conditions: { match: 'google.com', type: ConditionRuleMatchType.URL_CONTAINS },
  effects: {
      proxy: 'http://localhost:8899',
      setHeaders: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
      }
  }
}

Available Condition types

ENTIRE_URL
URL_REGEXP
URL_CONTAINS
RESPONSE_STATUS_CODE

Logical Operators

  • Conditions implement the logical operator property whose values can be either of "OR" | "AND" | "NOR" | "XOR" | "NAND" | "NXOR" | "XNOR".

  • The default operator is OR.

  • ❗Note: you must use a valid javascript RegExp string

Usage

Matches everything starting with https:// and ending with either .com or .net

conditions: {
    operator: 'OR',
    match: ['^https://.+.com$', '^https://.+.net$'],
    type: ConditionRuleMatchType.URL_REGEXP,
}

Match status codes different than 200

conditions: {
    operator: 'NOR'
    match: 200,
    type: ConditionRuleMatchType.URL_REGEXP,
}

Effects

  • block Will block the request
  • proxy proxifies the request. Provide an URL string
  • setHeaders will override request headers
Examples

Override headers and use a proxy for all requests containing google.com in the URL

{
  conditions: { match: 'google.com', type: ConditionRuleMatchType.URL_CONTAINS },
  effects: {
      proxy: 'http://localhost:8899',
      setHeaders: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
      }
  }
}

Block all requests matching https://undesired.com

{
  conditions: { match: 'https://undesired.com', type: ConditionRuleMatchType.ENTIRE_URL},
  effects: {
      block:true
  }
}

♻️ Retry rules

You can conditionally retry requests with specified effects

Usage

In this example, the flow is:

  • Will retry 3 times with a cheap-proxy, if the status code is 5xx
  • Will retry 1 time with an expensive-proxy
{ 
  retryRule:{
      conditions: { match: '^5\\d{2}$', type: ConditionRuleMatchType.RESPONSE_STATUS_CODE },
      effects:{
          proxy: 'http://cheap-proxy:8899',
      }, 
      retryCount:3,
      retryRule: {
          effects:{
              proxy: 'http://expensive-proxy:8899'
          }
      }
  }
}