Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-simple-ratelimit

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-simple-ratelimit - npm Package Compare versions

Comparing version 2.2.3 to 2.3.0

51

index.js

@@ -1,2 +0,1 @@

'use strict';

@@ -6,2 +5,3 @@ /**

*/
const debug = require('debug')('koa-simple-ratelimit');

@@ -33,2 +33,8 @@ const ms = require('ms');

/**
* Expose `ratelimit()`.
*/
module.exports = ratelimit;
/**
* Initialize ratelimit middleware with the given `opts`:

@@ -49,10 +55,9 @@ *

*/
function ratelimit(opts = {}) {
const {
remaining = 'X-RateLimit-Remaining',
reset = 'X-RateLimit-Reset',
total = 'X-RateLimit-Limit',
} = opts.headers || {};
function ratelimit(opts) {
opts = opts || {};
opts.headers = opts.headers || {};
opts.headers.remaining = opts.headers.remaining || 'X-RateLimit-Remaining';
opts.headers.reset = opts.headers.reset || 'X-RateLimit-Reset';
opts.headers.total = opts.headers.total || 'X-RateLimit-Limit';
return async function ratelimiter(ctx, next) {

@@ -81,11 +86,13 @@ const id = opts.id ? opts.id(ctx) : ctx.ip;

const headers = {};
headers[opts.headers.remaining] = opts.max - 1;
headers[opts.headers.reset] = t;
headers[opts.headers.total] = opts.max;
const headers = {
[remaining]: opts.max - 1,
[reset]: t,
[total]: opts.max,
};
ctx.set(headers);
debug('remaining %s/%s %s', opts.max - 1, opts.max, id);
// not existing in redis
if (cur === null) {
debug('remaining %s/%s %s', opts.max - 1, opts.max, id);
opts.db.set(name, opts.max - 1, 'PX', opts.duration || 3600000, 'NX');

@@ -99,5 +106,3 @@ return next();

opts.db.decr(name);
debug('remaining %s/%s %s', n - 1, opts.max, id);
headers[opts.headers.remaining] = n - 1;
ctx.set(headers);
ctx.set(remaining, n - 1);
return next();

@@ -107,3 +112,2 @@ }

debug(`${name} is stuck. Resetting.`);
debug('remaining %s/%s %s', opts.max - 1, opts.max, id);
opts.db.set(name, opts.max - 1, 'PX', opts.duration || 3600000, 'NX');

@@ -113,8 +117,6 @@ return next();

// user maxed
headers['Retry-After'] = t;
headers[opts.headers.remaining] = n;
ctx.set(headers);
ctx.set(remaining, n);
ctx.set('Retry-After', t);
ctx.status = 429;
const retryTime = ms(expires, { long: true });
ctx.body = `Rate limit exceeded, retry in ${retryTime}`;
ctx.body = opts.errorMessage || `Rate limit exceeded, retry in ${ms(expires, { long: true })}.`;
if (opts.throw) {

@@ -125,6 +127,1 @@ ctx.throw(ctx.status, ctx.body, { headers: headers });

}
/**
* Expose `ratelimit()`.
*/
module.exports = ratelimit;
{
"name": "koa-simple-ratelimit",
"description": "Simple Rate limiter middleware for koa v2",
"version": "2.2.3",
"version": "2.3.0",
"scripts": {

@@ -12,16 +12,16 @@ "test": "NODE_ENV=test node_modules/mocha/bin/mocha --reporter spec",

"dependencies": {
"debug": "^2.6.6",
"debug": "^2.6.8",
"ms": "^2.0.0"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.19.0",
"chai": "^4.1.0",
"eslint": "^4.3.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"koa": "^2.2.0",
"mocha": "^3.4.1",
"nyc": "^10.3.2",
"koa": "^2.3.0",
"mocha": "^3.5.0",
"nyc": "^11.1.0",
"redis": "^2.7.1",

@@ -28,0 +28,0 @@ "supertest": "^3.0.0"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc