rate-limiter-flexible 2.3.12 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -180,6 +180,7 @@ Smooth out traffic picks:
180
180
  Specific:
181
181
  * [indexKeyPrefix](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#indexkeyprefix) Combined indexes of MongoDB.
182
182
  * [timeoutMs](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#timeoutms) For Cluster.
183
+ * [rejectIfRedisNotReady](https://github.com/animir/node-rate-limiter-flexible/wiki/Options#rejectifredisnotready)
183
184
 
184
185
  ## API
185
186
 
@@ -19,6 +19,7 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
19
19
  * ... see other in RateLimiterStoreAbstract
20
20
  *
21
21
  * redis: RedisClient
22
+ * rejectIfRedisNotReady: boolean = false - reject / invoke insuranceLimiter immediately when redis connection is not "ready"
22
23
  * }
23
24
  */
24
25
  constructor(opts) {
@@ -29,6 +30,8 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
29
30
  this.client = opts.storeClient;
30
31
  }
31
32
 
33
+ this._rejectIfRedisNotReady = !!opts.rejectIfRedisNotReady;
34
+
32
35
  if (typeof this.client.defineCommand === 'function') {
33
36
  this.client.defineCommand("rlflxIncr", {
34
37
  numberOfKeys: 1,
@@ -37,6 +40,28 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
37
40
  }
38
41
  }
39
42
 
43
+ /**
44
+ * Prevent actual redis call if redis connection is not ready
45
+ * Because of different connection state checks for ioredis and node-redis, only this clients would be actually checked.
46
+ * For any other clients all the requests would be passed directly to redis client
47
+ * @return {boolean}
48
+ * @private
49
+ */
50
+ _isRedisReady() {
51
+ if (!this._rejectIfRedisNotReady) {
52
+ return true;
53
+ }
54
+ // ioredis client
55
+ if (this.client.status && this.client.status !== 'ready') {
56
+ return false;
57
+ }
58
+ // node-redis client
59
+ if (typeof this.client.isReady === 'function' && !this.client.isReady()) {
60
+ return false;
61
+ }
62
+ return true;
63
+ }
64
+
40
65
  _getRateLimiterRes(rlKey, changedPoints, result) {
41
66
  let [consumed, resTtlMs] = result;
42
67
  // Support ioredis results format
@@ -56,6 +81,10 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
56
81
 
57
82
  _upsert(rlKey, points, msDuration, forceExpire = false) {
58
83
  return new Promise((resolve, reject) => {
84
+ if (!this._isRedisReady()) {
85
+ return reject(new Error('Redis connection is not ready'));
86
+ }
87
+
59
88
  const secDuration = Math.floor(msDuration / 1000);
60
89
  const multi = this.client.multi();
61
90
  if (forceExpire) {
@@ -105,6 +134,10 @@ class RateLimiterRedis extends RateLimiterStoreAbstract {
105
134
 
106
135
  _get(rlKey) {
107
136
  return new Promise((resolve, reject) => {
137
+ if (!this._isRedisReady()) {
138
+ return reject(new Error('Redis connection is not ready'));
139
+ }
140
+
108
141
  this.client
109
142
  .multi()
110
143
  .get(rlKey)
package/lib/index.d.ts CHANGED
@@ -255,6 +255,10 @@ interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions {
255
255
  };
256
256
  }
257
257
 
258
+ interface IRateLimiterRedisOptions extends IRateLimiterStoreOptions {
259
+ rejectIfRedisNotReady?: boolean;
260
+ }
261
+
258
262
  interface ICallbackReady {
259
263
  (error?: Error): void;
260
264
  }
@@ -284,7 +288,9 @@ export class RateLimiterClusterMasterPM2 {
284
288
  constructor(pm2: any);
285
289
  }
286
290
 
287
- export class RateLimiterRedis extends RateLimiterStoreAbstract {}
291
+ export class RateLimiterRedis extends RateLimiterStoreAbstract {
292
+ constructor(opts: IRateLimiterRedisOptions);
293
+ }
288
294
 
289
295
  export interface IRateLimiterMongoFunctionOptions {
290
296
  attrs: { [key: string]: any };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rate-limiter-flexible",
3
- "version": "2.3.12",
3
+ "version": "2.4.1",
4
4
  "description": "Node.js rate limiter by key and protection from DDoS and Brute-Force attacks in process Memory, Redis, MongoDb, Memcached, MySQL, PostgreSQL, Cluster or PM",
5
5
  "main": "index.js",
6
6
  "scripts": {