Socket
Socket
Sign inDemoInstall

rate-limiter-flexible

Package Overview
Dependencies
Maintainers
1
Versions
163
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rate-limiter-flexible - npm Package Compare versions

Comparing version 0.9.1 to 0.9.2

84

lib/RateLimiterRedis.js

@@ -1,19 +0,4 @@

const RateLimiterAbstract = require('./RateLimiterAbstract');
const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract');
const RateLimiterRes = require('./RateLimiterRes');
const BlockedKeys = require('./component/BlockedKeys');
const handleRedisError = function (funcName, resolve, reject, key, pointsToConsume) {
if (!(this.insuranceLimiter instanceof RateLimiterAbstract)) {
reject(new Error('Redis Client error'));
} else {
this.insuranceLimiter[funcName](key, pointsToConsume)
.then((res) => {
resolve(res);
})
.catch((res) => {
reject(res);
});
}
};
const afterConsume = function (resolve, reject, rlKey, results) {

@@ -28,5 +13,6 @@ let [resSet, consumed, resTtlMs] = results;

const res = new RateLimiterRes();
res.consumedPoints = consumed;
res.isFirstInDuration = resSet === 'OK';
res.remainingPoints = Math.max(this.points - consumed, 0);
res.remainingPoints = Math.max(this.points - res.consumedPoints, 0);
if (resTtlMs === -1) { // If rlKey created by incrby() not by set()

@@ -40,5 +26,5 @@ res.isFirstInDuration = true;

if (consumed > this.points) {
if (res.consumedPoints > this.points) {
// Block key for this.blockDuration seconds
if (this.blockOnPointsConsumed > 0 && consumed >= this.blockOnPointsConsumed) {
if (this.blockOnPointsConsumed > 0 && res.consumedPoints >= this.blockOnPointsConsumed) {
this._blockedKeys.add(rlKey, this.blockDuration);

@@ -57,3 +43,3 @@ res.msBeforeNext = this.msBlockDuration;

class RateLimiterRedis extends RateLimiterAbstract {
class RateLimiterRedis extends RateLimiterStoreAbstract {
/**

@@ -63,6 +49,5 @@ *

* Defaults {
* ... see other in RateLimiterAbstract
* ... see other in RateLimiterStoreAbstract
*
* redis: RedisClient
* memoryInsurance: true, // Enable using current process memory to limit rates on Redis error
* }

@@ -74,6 +59,2 @@ */

this.redis = opts.redis;
this.blockOnPointsConsumed = opts.blockOnPointsConsumed;
this.blockDuration = opts.blockDuration;
this.insuranceLimiter = opts.insuranceLimiter;
this._blockedKeys = new BlockedKeys();
}

@@ -92,39 +73,2 @@

get blockOnPointsConsumed() {
return this._blockOnPointsConsumed;
}
set blockOnPointsConsumed(value) {
this._blockOnPointsConsumed = value ? parseInt(value) : 0;
if (this.blockOnPointsConsumed > 0 && this.points >= this.blockOnPointsConsumed) {
throw new Error('blockOnPointsConsumed option must be more than points option');
}
}
get blockDuration() {
return this._blockDuration;
}
get msBlockDuration() {
return this._blockDuration * 1000;
}
set blockDuration(value) {
this._blockDuration = value ? parseInt(value) : 0;
if (this.blockDuration > 0 && this.blockOnPointsConsumed === 0) {
throw new Error('blockOnPointsConsumed option must be set up');
}
}
get insuranceLimiter() {
return this._insuranceLimiter;
}
set insuranceLimiter(value) {
if (typeof value !== 'undefined' && !(value instanceof RateLimiterAbstract)) {
throw new Error('insuranceLimiter must be instance of RateLimiterAbstract');
}
this._insuranceLimiter = value;
}
/**

@@ -140,7 +84,5 @@ *

if (this.blockOnPointsConsumed > 0) {
const msBeforeBlockExpires = this._blockedKeys.msBeforeExpire(rlKey);
if (msBeforeBlockExpires > 0) {
return reject(new RateLimiterRes(0, msBeforeBlockExpires));
}
const blockMsBeforeExpire = this.getBlockMsBeforeExpire(rlKey);
if (blockMsBeforeExpire > 0) {
return reject(new RateLimiterRes(0, blockMsBeforeExpire));
}

@@ -154,3 +96,3 @@

if (err) {
handleRedisError.call(this, 'consume', resolve, reject, key, pointsToConsume);
this.handleError(err, 'consume', resolve, reject, key, pointsToConsume);
} else {

@@ -168,3 +110,3 @@ afterConsume.call(this, resolve, reject, rlKey, results);

if (err) {
handleRedisError.call(this, 'penalty', resolve, reject, key, points);
this.handleError(err, 'penalty', resolve, reject, key, points);
} else {

@@ -182,3 +124,3 @@ resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints));

if (err) {
handleRedisError.call(this, 'reward', resolve, reject, key, points);
this.handleError(err, 'reward', resolve, reject, key, points);
} else {

@@ -185,0 +127,0 @@ resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints));

2

package.json
{
"name": "rate-limiter-flexible",
"version": "0.9.1",
"version": "0.9.2",
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -89,3 +89,3 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible)

// Redis specific
// Redis and Mongo specific
blockOnPointsConsumed: 10, // If 10 points consumed in current duration

@@ -240,3 +240,3 @@ blockDuration: 30, // block for 30 seconds in current process memory

* `keyPrefix` `Default: 'rlflx''` If you need to create several limiters for different purpose
* `keyPrefix` `Default: 'rlflx'` If you need to create several limiters for different purpose

@@ -253,3 +253,3 @@ * `points` `Default: 4` Maximum number of points can be consumed over duration

#### Options specific to Redis
#### Options specific to Redis and Mongo

@@ -256,0 +256,0 @@ * `blockOnPointsConsumed` `Default: 0` Against DDoS attacks. Blocked key isn't checked by requesting Redis.

Sorry, the diff of this file is not supported yet

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