koa-simple-ratelimit
Advanced tools
Comparing version 1.0.0 to 1.0.1
69
index.js
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
const ms = require('ms'); | ||
function ratelimit(opts) { | ||
opts = opts || {}; | ||
/** | ||
* Expose `ratelimit()`. | ||
*/ | ||
module.exports = ratelimit; | ||
function get(p) { | ||
return new Promise(function(resolve) { | ||
opts.db.get(p, function(err, reply) { | ||
resolve(reply); | ||
}); | ||
function get(db, p) { | ||
return new Promise(function(resolve) { | ||
db.get(p, function(err, reply) { | ||
resolve(reply); | ||
}); | ||
} | ||
}); | ||
} | ||
function pttl(p) { | ||
return new Promise(function(resolve) { | ||
opts.db.pttl(p, function(err, reply) { | ||
resolve(reply); | ||
}); | ||
function pttl(db, p) { | ||
return new Promise(function(resolve) { | ||
db.pttl(p, function(err, reply) { | ||
resolve(reply); | ||
}); | ||
} | ||
}); | ||
} | ||
function finish(ctx, next, n, t) { | ||
ctx.set('X-RateLimit-Limit', opts.max); | ||
ctx.set('X-RateLimit-Remaining', n); | ||
ctx.set('X-RateLimit-Reset', t); | ||
return next(); | ||
} | ||
function finish(ctx, next, max, n, t) { | ||
ctx.set('X-RateLimit-Limit', max); | ||
ctx.set('X-RateLimit-Remaining', n); | ||
ctx.set('X-RateLimit-Reset', t); | ||
return next(); | ||
} | ||
/** | ||
* Initialize a new limiter with `opts`: | ||
* | ||
* - `id` identifier being limited | ||
* - `db` redis connection instance | ||
* | ||
* @param {Object} opts | ||
* @api public | ||
*/ | ||
function ratelimit(opts) { | ||
opts = opts || {}; | ||
return function(ctx, next) { | ||
@@ -35,3 +53,3 @@ let id = opts.id ? opts.id(ctx) : ctx.ip; | ||
let name = `limit:${id}:count`; | ||
return get(name).then(function(cur) { | ||
return get(opts.db, name).then(function(cur) { | ||
let n = ~~cur; | ||
@@ -43,3 +61,3 @@ let ex = opts.duration || 3600000; | ||
if (cur !== null) { | ||
return pttl(name).then(function(ex) { | ||
return pttl(opts.db, name).then(function(ex) { | ||
if (n - 1 >= 0) { | ||
@@ -49,3 +67,3 @@ // existing user | ||
n = n - 1; | ||
return finish(ctx, next, n, t); | ||
return finish(ctx, next, opts.max, n, t); | ||
} | ||
@@ -62,7 +80,6 @@ // user maxed | ||
} | ||
opts.db.set(name, opts.max-1, 'PX', opts.duration || 3600000, 'NX'); | ||
return finish(ctx, next, opts.max-1, t); | ||
opts.db.set(name, opts.max - 1, 'PX', opts.duration || 3600000, 'NX'); | ||
return finish(ctx, next, opts.max, opts.max - 1, t); | ||
}); | ||
}; | ||
} | ||
module.exports = ratelimit; |
@@ -5,3 +5,3 @@ { | ||
"repository": "scttcper/koa-simple-ratelimit", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "koa", |
@@ -6,3 +6,2 @@ | ||
[![build status][travis-image]][travis-url] | ||
[![node version][node-image]][node-url] | ||
@@ -14,3 +13,3 @@ [npm-image]: https://img.shields.io/npm/v/koa-simple-ratelimit.svg?style=flat-square | ||
Rate limiter middleware for koa v2. Differes from koa-ratelimit by not depending on [ratelimiter](https://github.com/tj/node-ratelimiter) and using redis pttl to handle expiration time remaining. This creates only one entry in redis database instead of the three that node-ratelimiter does. | ||
Rate limiter middleware for koa v2. Differs from [koa-ratelimit](https://github.com/koajs/ratelimit) by not depending on [ratelimiter](https://github.com/tj/node-ratelimiter) and using redis pttl to handle expiration time remaining. This creates only one entry in redis instead of the three that node-ratelimiter does. | ||
@@ -29,3 +28,3 @@ ## Installation | ||
var koa = require('koa'); | ||
var app = koa(); | ||
var app = new koa(); | ||
@@ -45,4 +44,4 @@ // apply rate limit | ||
app.use(function *(){ | ||
this.body = 'Stuff!'; | ||
app.use(function (){ | ||
this.body = 'Hello'; | ||
}); | ||
@@ -59,2 +58,3 @@ | ||
- `duration` of limit in milliseconds [3600000] | ||
- `id` id to compare requests [ip] | ||
@@ -61,0 +61,0 @@ ## Responses |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5910
73