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

smart-limiter

v1.6.0

Published

Smart rate limiter middleware for both express and koa.

Downloads

13

Readme

smart-limiter

Smart rate limiter middleware for both express and koa.

NPM version Build Status Downloads

Requirements

  • Redis 2.8+ with thunk-redis client

Installation

npm install smart-limiter

Example

koa v2

'use strict'

const Koa = require('koa')
const smartLimiter = require('smart-limiter')

const app = new Koa()

app.use(smartLimiter.koav2({
  redis: 6379,
  duration: 10000,
  getId: function (ctx) {
    return ctx.ip
  },
  policy: {
    'GET': [3, 5000],
    'GET /test': [3, 5000, 3, 10000],
    '/test': 5
  }
}))

app.use((ctx) => {
  ctx.body = ctx.headers
})

app.listen(3000)
console.log('Start at 3000')

express

'use strict'

const express = require('express')
const smartLimiter = require('smart-limiter')

const app = express()

app.use(function (req, res, next) {
  if (req.path !== '/favicon.ico') return next()
  res.end()
})

app.use(smartLimiter({
  redis: 6379,
  duration: 10000,
  getId: function (req) {
    return req.ip
  },
  policy: {
    'GET': [3, 5000],
    'GET /test': [3, 5000, 3, 10000],
    '/test': 5
  }
}))

app.use(function (req, res) {
  res.json(res._headers)
})

app.listen(3000)
console.log('Start at 3000')

koa v1

'use strict'

const Koa = require('koa')
const smartLimiter = require('smart-limiter')

const app = new Koa()

app.use(smartLimiter.koa({
  redis: 6379,
  duration: 10000,
  getId: function (ctx) {
    return ctx.ip
  },
  policy: {
    'GET': [3, 5000],
    'GET /test': [3, 5000, 3, 10000],
    '/test': 5
  }
}))

app.use(function * () {
  this.body = this.headers
})

app.listen(3000)
console.log('Start at 3000')

API

var smartLimiter = require('smart-limiter')

smartLimiter(options)

const limiter = smartLimiter({
  redis: thunkRedisClient,
  duration: 10000,
  getId: function (req) {
    return req.ip
  },
  policy: {
    'GET': [3, 5000],
    'GET /test': [3, 5000, 3, 10000],
    '/test': 5
  }
})
app.use(limiter)

return a express middleware.

  • options.prefix: Optional, Type: String, redis key namespace, default to LIMIT.

  • options.redis: Optional, {Mix}, thunk-redis instance or thunk-redis options

  • options.duration: Optional, {Number}, of limit in milliseconds, default to 3600000

  • options.getId: Required, {Function}, generate a identifier for requests

  • options.policy: Required, {Object}, limit policy

    policy key: It support 3 types: METHOD /path, /path and METHOD. Limiter will try match METHOD /path first, then /path, then METHOD. It means that METHOD /path has highest priority, then fallback to /path and METHOD.

    policy value: If value is a member, it means max count with options.duration. If value is array, it should be a pair of max and duration, support one more pairs.

    The first pair is default limit policy. If someone touch the maximum of default limit, then the next policy will be apply, and so on. So next policy should be stricter than previous one.

    If someone touch the maximum of limit and request again after double current duration time, it will rollback to default policy.

    example policy:

    options.policy = {
      'HEAD': 100,
      'GET': [60, 60000, 30, 60000, 30, 120000],
      'PUT': [40, 60000, 20, 60000, 10, 120000],
      'POST': [40, 60000, 10, 60000],
      'DELETE': [40, 60000, 10, 60000],
      'POST /api/organizations': [10, 60000, 2, 60000],
      'POST /api/projects': [20, 60000, 5, 60000],
      '/api/auth': [10, 60000, 5, 120000],
    }

limiter.get(id, max, duration, max, duration...) => Promise

Return a promise that guarantee a limiter result. it support more max and duration pairs ad limit policy. The first pairs will be used as default. If some trigger limit, then the limiter will apply the next pair policy.

limiter.remove(req, callback)

limiter.remove(req) => Promise

Remove req's rate limit data. Only available when using express middleware.

limiter.remove(req, function (err, res) {
  console.log(err, res) // null, 1
})

Responses

Example 200 with header fields:

HTTP/1.1 200 OK

Connection:keep-alive
Content-Length:111
Content-Type:application/json; charset=utf-8
Date:Thu, 10 Dec 2015 13:21:55 GMT
X-Powered-By:Express
X-RateLimit-Limit:3
X-RateLimit-Remaining:2
X-RateLimit-Reset:1449753721

Example 429 with header fields:

HTTP/1.1 429 Too Many Requests

Connection:keep-alive
Content-Length:39
Content-Type:text/html; charset=utf-8
Date:Thu, 10 Dec 2015 13:22:36 GMT
Retry-After:3
X-Powered-By:Express
X-RateLimit-Limit:3
X-RateLimit-Remaining:-1
X-RateLimit-Reset:1449753759