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.15.0 to 0.15.1

83

lib/RateLimiterRedis.js

@@ -54,69 +54,32 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract');

_block(rlKey, initPoints, msDuration) {
_upsert(rlKey, points, msDuration, forceExpire = false) {
return new Promise((resolve, reject) => {
this.redis.set(rlKey, initPoints, 'EX', Math.floor(msDuration / 1000), (err) => {
if (err) {
return this._handleError(err, 'block', resolve, reject, this.parseKey(rlKey), msDuration / 1000);
}
const secDuration = Math.floor(msDuration / 1000);
if (forceExpire) {
this.redis.multi()
.set(rlKey, points, 'EX', secDuration)
.pttl(rlKey)
.exec((err, res) => {
if (err) {
return reject(err);
}
resolve(new RateLimiterRes(0, msDuration, initPoints));
});
});
}
return resolve(['FORCE', points, res[1]]);
});
} else {
this.redis.multi()
.set(rlKey, 0, 'EX', secDuration, 'NX')
.incrby(rlKey, points)
.pttl(rlKey)
.exec((err, res) => {
if (err) {
return reject(err);
}
/**
*
* @param key
* @param pointsToConsume
* @returns {Promise<any>}
*/
consume(key, pointsToConsume = 1) {
return new Promise((resolve, reject) => {
const rlKey = this.getKey(key);
const inmemoryBlockMsBeforeExpire = this.getInmemoryBlockMsBeforeExpire(rlKey);
if (inmemoryBlockMsBeforeExpire > 0) {
return reject(new RateLimiterRes(0, inmemoryBlockMsBeforeExpire));
return resolve(res);
});
}
this.redis.multi()
.set(rlKey, 0, 'EX', this.duration, 'NX')
.incrby(rlKey, pointsToConsume)
.pttl(rlKey)
.exec((err, results) => {
if (err) {
this._handleError(err, 'consume', resolve, reject, key, pointsToConsume);
} else {
this._afterConsume(resolve, reject, rlKey, pointsToConsume, results);
}
});
});
}
penalty(key, points = 1) {
const rlKey = this.getKey(key);
return new Promise((resolve, reject) => {
this.redis.incrby(rlKey, points, (err, consumedPoints) => {
if (err) {
this._handleError(err, 'penalty', resolve, reject, key, points);
} else {
resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints));
}
});
});
}
reward(key, points = 1) {
const rlKey = this.getKey(key);
return new Promise((resolve, reject) => {
this.redis.incrby(rlKey, -points, (err, consumedPoints) => {
if (err) {
this._handleError(err, 'reward', resolve, reject, key, points);
} else {
resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints));
}
});
});
}
_get(rlKey) {

@@ -123,0 +86,0 @@ return new Promise((resolve, reject) => {

@@ -203,3 +203,3 @@ const RateLimiterAbstract = require('./RateLimiterAbstract');

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

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

{
"name": "rate-limiter-flexible",
"version": "0.15.0",
"version": "0.15.1",
"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