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

dolph

v0.2.0

Published

A rate-limiter middleware for Express

Downloads

10

Readme

Dolph

Dolph is a rate-limiter middleware for Express.js.

Naming

Dolph is named in honour of Dolph Lundgren, the actor who once worked as a nightclub bouncer and who reportedly has a genius level IQ and can speak 5 languages. An intelligent, sophisticated bouncer is exactly what Dolph the middleware aspires to be.

dolph will crush you

Why yet another rate-limiter middleware?

Several similar middlewares already exist. At the time we evaluated the existing ones, none supported Redis-Lua scripting. We specifically wanted a solution that used redis lua scripting as it allows for an extremely simply, yet bug-free solution.

The basis of the rate limiting algorithm is documented at redis.io.

Installing dolph

$ npm install dolph --save

Using dolph

Attach the middleware to the route you would like to rate limit.

var dolph = require('dolph');

var rateLimiter = dolph({
   prefix: 'rate:',
   keyFunction: function() {
     return req.user.id;
   }
 });

app.get('/api/',
  rateLimiter,
  function(req, res) {
    // .. your code goes here
  });

Options

Dolph will take an options hash. The options are:

  • prefix: the Redis key prefix to use
  • keyFunction: a function which will map a request into a string. The string should map a single entity which you would like to apply the limiter to. Using a userId is a good option if the limit is per user. Use a clientId if you will limit by clients. If it's a combination, concatenate the values into a single string. The function should take the form function(req) { }.
  • applyLimit: a function which should return true when the limit should be applied, false otherwise. The function should take the form function(req) { }.
  • expiry: a time in seconds before the rate limit is reset. Can be a function with the form function(req) { }. Defaults to 60 seconds.
  • limit: the number of calls that can occur within the limit (as set by the expiry). Can be a function with the form function(req) { }. Defaults to 100.
  • redisClient: a reference to a redisClient (optional)
  • redis: If a redisClient is not passed in, Dolph will instantiate it's own redis client. This hash allows the port, host and options of the client to be specified.

Using the rate limiter without the middleware

Sometimes it's useful to perform rate limiting outside of a middleware. For this reason, Dolph exposes functionality to do this.

var rateLimiter = require('dolph').rateLimiter({
  prefix: 'rate:',
  redis: { host: 'localhost', port: 6379 },  // Default values, not required
  redisClient: { } // Or, BYO redis client to the party (useful if you're using Sentinel)
});

var key = 'thespotter';
rateLimiter(key, expiry, function(err, count, ttl) {
  if (err) return callback(err);

  // `count` is how many times the key has been called within the window period
  // `ttl` is how long until a new window period starts

  // Do whatever you want with this information here....

});

Requirements

Dolph needs Redis 2.6.0 or above and Express 3.

Information

Written by @suprememoocow for Gitter.