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

rate-limiter-flexible

Package Overview
Dependencies
Maintainers
1
Versions
164
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.23.7 to 0.24.0

6

lib/RateLimiterAbstract.js

@@ -84,2 +84,8 @@ module.exports = class RateLimiterAbstract {

_getKeySecDuration(options = {}) {
return options && options.customDuration
? options.customDuration
: this.duration;
}
getKey(key) {

@@ -86,0 +92,0 @@ return this.keyPrefix.length > 0 ? `${this.keyPrefix}:${key}` : key;

39

lib/RateLimiterCluster.js

@@ -63,3 +63,3 @@ /**

const workerSendToMaster = function (func, promiseId, key, arg) {
const workerSendToMaster = function (func, promiseId, key, arg, opts) {
const payload = {

@@ -73,2 +73,3 @@ channel,

arg,
opts,
},

@@ -94,18 +95,18 @@ };

case 'consume':
promise = this._rateLimiters[msg.keyPrefix].consume(msg.data.key, msg.data.arg);
promise = this._rateLimiters[msg.keyPrefix].consume(msg.data.key, msg.data.arg, msg.data.opts);
break;
case 'penalty':
promise = this._rateLimiters[msg.keyPrefix].penalty(msg.data.key, msg.data.arg);
promise = this._rateLimiters[msg.keyPrefix].penalty(msg.data.key, msg.data.arg, msg.data.opts);
break;
case 'reward':
promise = this._rateLimiters[msg.keyPrefix].reward(msg.data.key, msg.data.arg);
promise = this._rateLimiters[msg.keyPrefix].reward(msg.data.key, msg.data.arg, msg.data.opts);
break;
case 'block':
promise = this._rateLimiters[msg.keyPrefix].block(msg.data.key, msg.data.arg);
promise = this._rateLimiters[msg.keyPrefix].block(msg.data.key, msg.data.arg, msg.data.opts);
break;
case 'get':
promise = this._rateLimiters[msg.keyPrefix].get(msg.data.key);
promise = this._rateLimiters[msg.keyPrefix].get(msg.data.key, msg.data.opts);
break;
case 'delete':
promise = this._rateLimiters[msg.keyPrefix].delete(msg.data.key);
promise = this._rateLimiters[msg.keyPrefix].delete(msg.data.key, msg.data.opts);
break;

@@ -314,47 +315,47 @@ default:

consume(key, pointsToConsume = 1) {
consume(key, pointsToConsume = 1, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'consume', promiseId, key, pointsToConsume);
workerSendToMaster.call(this, 'consume', promiseId, key, pointsToConsume, options);
});
}
penalty(key, points = 1) {
penalty(key, points = 1, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'penalty', promiseId, key, points);
workerSendToMaster.call(this, 'penalty', promiseId, key, points, options);
});
}
reward(key, points = 1) {
reward(key, points = 1, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'reward', promiseId, key, points);
workerSendToMaster.call(this, 'reward', promiseId, key, points, options);
});
}
block(key, secDuration) {
block(key, secDuration, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'block', promiseId, key, secDuration);
workerSendToMaster.call(this, 'block', promiseId, key, secDuration, options);
});
}
get(key) {
get(key, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'get', promiseId, key);
workerSendToMaster.call(this, 'get', promiseId, key, options);
});
}
delete(key) {
delete(key, options = {}) {
return new Promise((resolve, reject) => {
const promiseId = savePromise.call(this, resolve, reject);
workerSendToMaster.call(this, 'delete', promiseId, key);
workerSendToMaster.call(this, 'delete', promiseId, key, options);
});

@@ -361,0 +362,0 @@ }

@@ -15,8 +15,10 @@ const RateLimiterAbstract = require('./RateLimiterAbstract');

* @param pointsToConsume
* @param {Object} options
* @returns {Promise<RateLimiterRes>}
*/
consume(key, pointsToConsume = 1) {
consume(key, pointsToConsume = 1, options = {}) {
return new Promise((resolve, reject) => {
const rlKey = this.getKey(key);
let res = this._memoryStorage.incrby(rlKey, pointsToConsume, this.duration);
const secDuration = this._getKeySecDuration(options);
let res = this._memoryStorage.incrby(rlKey, pointsToConsume, secDuration);
res.remainingPoints = Math.max(this.points - res.consumedPoints, 0);

@@ -45,6 +47,7 @@

penalty(key, points = 1) {
penalty(key, points = 1, options = {}) {
const rlKey = this.getKey(key);
return new Promise((resolve) => {
const res = this._memoryStorage.incrby(rlKey, points, this.duration);
const secDuration = this._getKeySecDuration(options);
const res = this._memoryStorage.incrby(rlKey, points, secDuration);
res.remainingPoints = this.points - res.consumedPoints;

@@ -55,6 +58,7 @@ resolve(res);

reward(key, points = 1) {
reward(key, points = 1, options = {}) {
const rlKey = this.getKey(key);
return new Promise((resolve) => {
const res = this._memoryStorage.incrby(rlKey, -points, this.duration);
const secDuration = this._getKeySecDuration(options);
const res = this._memoryStorage.incrby(rlKey, -points, secDuration);
res.remainingPoints = this.points - res.consumedPoints;

@@ -61,0 +65,0 @@ resolve(res);

@@ -85,7 +85,7 @@ const RateLimiterAbstract = require('./RateLimiterAbstract');

_handleError(err, funcName, resolve, reject, key, data = false) {
_handleError(err, funcName, resolve, reject, key, data = false, options = {}) {
if (!(this.insuranceLimiter instanceof RateLimiterAbstract)) {
reject(err);
} else {
this.insuranceLimiter[funcName](key, data)
this.insuranceLimiter[funcName](key, data, options)
.then((res) => {

@@ -179,3 +179,3 @@ resolve(res);

this._upsert(rlKey, pointsToConsume, this.msDuration, false, options)
this._upsert(rlKey, pointsToConsume, this._getKeySecDuration(options) * 1000, false, options)
.then((res) => {

@@ -185,3 +185,3 @@ this._afterConsume(resolve, reject, rlKey, pointsToConsume, res);

.catch((err) => {
this._handleError(err, 'consume', resolve, reject, key, pointsToConsume);
this._handleError(err, 'consume', resolve, reject, key, pointsToConsume, options);
});

@@ -201,3 +201,3 @@ });

return new Promise((resolve, reject) => {
this._upsert(rlKey, points, this.msDuration, false, options)
this._upsert(rlKey, points, this._getKeySecDuration(options) * 1000, false, options)
.then((res) => {

@@ -207,3 +207,3 @@ resolve(this._getRateLimiterRes(rlKey, points, res));

.catch((err) => {
this._handleError(err, 'penalty', resolve, reject, key, points);
this._handleError(err, 'penalty', resolve, reject, key, points, options);
});

@@ -223,3 +223,3 @@ });

return new Promise((resolve, reject) => {
this._upsert(rlKey, -points, this.msDuration, false, options)
this._upsert(rlKey, -points, this._getKeySecDuration(options) * 1000, false, options)
.then((res) => {

@@ -229,3 +229,3 @@ resolve(this._getRateLimiterRes(rlKey, -points, res));

.catch((err) => {
this._handleError(err, 'reward', resolve, reject, key, points);
this._handleError(err, 'reward', resolve, reject, key, points, options);
});

@@ -253,3 +253,3 @@ });

.catch((err) => {
this._handleError(err, 'get', resolve, reject, key);
this._handleError(err, 'get', resolve, reject, key, options);
});

@@ -273,3 +273,3 @@ });

.catch((err) => {
this._handleError(err, 'delete', resolve, reject, key);
this._handleError(err, 'delete', resolve, reject, key, options);
});

@@ -309,3 +309,3 @@ });

.catch((err) => {
this._handleError(err, 'block', resolve, reject, this.parseKey(rlKey), msDuration / 1000);
this._handleError(err, 'block', resolve, reject, this.parseKey(rlKey), msDuration / 1000, options);
});

@@ -312,0 +312,0 @@ });

{
"name": "rate-limiter-flexible",
"version": "0.23.7",
"version": "0.24.0",
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications",

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

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