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

stackable-crawler

v0.0.3

Published

middleware based lightweight crawler framework

Downloads

6

Readme

stackable-crawler Build Status

middleware based lightweight crawler framework.

Features

  • Customizable pre-request, pre-process middleware stacks (it enables to log, cache, normalize, etc...)
  • Cancelable crawler
  • Customizable caching strategy
  • Parallel crawling
  • Pause/Resume crawling (it enables to sleep crawler)
  • Error handling (it enables to retry)

Install

$ npm i -S stackable-crawler

QuickStart

import StackableCrawler, {
  CancelRequest
} from 'stackable-crawler';

const crawler = new StackableCrawler({
  prerequest: [
    options => {
      console.log('options:', options);
      return options;
    }
  ],
  processor([response, body]) {
    return new Promise((resolve, reject) => {
      saveFileFunction(body, error => {
        if (error) return reject(error);
        resolve();
      });
    });
  }
});

crawler.on('error', (error, url) => {
  console.error(error, url);
});

crawler.add('https://github.com/');

What can I do in prerequest?

prerequest middlewares stack can have sideeffect about requesting options. options is request module's request option. If prerequest middleware throw CancelRequest error, to request to that url was canceled.

What can I do in preprocess?

preprocess middlewares stack can have sideeffect about response, body, requestOptions. They are also from request.

Friendly crawler?

Use sleep function

const sleepCrawler = (crawler, sleepTime, interval) => {
  setTimeout(() => {
    crawler.stop();
    crawler.once('stopped', () => {
      setTimeout(() => crawler.start(), sleepTime);
    });
  }, interval);
}

Documents

StackableCrawler class (Default export)

Class of crawler. Extends EventEmitter3.

constructor

Take one argument, configure object.

{
  concurrency: 1, // max # of parallel crawling
  prerequest: [], // prerequest middleware functions
  requestCache() {}, // cache strategy
  preprocess: [], // preprocess middleware functions
  processor() {}, // main callback to handle crawled Document
}
prerequest middleware

Function that process requestOptions. Default argument is { uri: url }. Function must return new (mutated) requestOptions or Promise[requestOptions].

requestCache function

Function that returns cached value or undefined. Type of cached value is T or Promise[T]. If cached value returned, crawler pass through that values to processor function. If undefined returned, crawler fetch document as usual.

preprocess middleware

Function that process [response, body, requestOptions]. response and body are fetching result of request module. It also return new argument or Promise.

processor function

Main function. You can do everything here. It can return promise.

crawler#add(url)

Add url to crawling task queue.

crawler#stop()

Pause crawler. If crawler has one more running tasks, these are still running until finished, but no more run new task.

crawler#start()

Resume crawler if paused.

crawler#on(), crawler#once(), ...

These methods are inherit from EventEmitter3.

available event and args

  • event: error, args: [error, url]
  • event: stopped

CancelRequest class (Named export)

Error class. If crawler throws CancelRequest error throw new CancelRequest(), crawler stop to request that url with no error.

middlewares object (Named export)

Bundled simple middlewares.

middlewares.filterUrl

Filter only valid url. If url is invalid, this middleware throw CancelRequest error.

middlewares.urlCache

Function that returns simple in memory cache middleware. Do urlCache() when use. If requesting url was already cached, this middleware throw CancelRequest error.

middlewares.logger

Very simple console.log middleware

middlewares.body2cheerio

Replace body to cheerio object ($). Next middlewares and processor call with [response, $, requestOptions]

License

MIT