rate-limiter-flexible 2.2.4 → 2.3.3

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/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_size = 2
5
+ indent_style = space
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+
11
+ [*.md]
12
+ insert_final_newline = false
13
+ trim_trailing_whitespace = false
package/README.md CHANGED
@@ -184,6 +184,7 @@ Read detailed description on Wiki.
184
184
  * [set(key, points, secDuration)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimitersetkey-points-secduration) Set points by key.
185
185
  * [block(key, secDuration)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimiterblockkey-secduration) Block key for `secDuration` seconds.
186
186
  * [delete(key)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimiterdeletekey) Reset consumed points.
187
+ * [deleteInMemoryBlockedAll](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimiterdeleteinmemoryblockedall)
187
188
  * [penalty(key, points = 1)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimiterpenaltykey-points--1) Increase number of consumed points in current duration.
188
189
  * [reward(key, points = 1)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimiterrewardkey-points--1) Decrease number of consumed points in current duration.
189
190
  * [getKey(key)](https://github.com/animir/node-rate-limiter-flexible/wiki/API-methods#ratelimitergetkeykey) Get internal prefixed key.
@@ -8,7 +8,12 @@ const RateLimiterRes = require('./RateLimiterRes');
8
8
  */
9
9
  function getDriverVersion(client) {
10
10
  try {
11
- const { version } = client.topology.s.options.metadata.driver;
11
+ let version;
12
+ if (client.client) {
13
+ ({ version } = client.client.topology.s.options.metadata.driver);
14
+ } else {
15
+ ({ version } = client.topology.s.options.metadata.driver);
16
+ }
12
17
  const majorVersion = parseInt(version);
13
18
 
14
19
  return majorVersion;
@@ -261,8 +261,8 @@ module.exports = class RateLimiterStoreAbstract extends RateLimiterAbstract {
261
261
  return new Promise((resolve, reject) => {
262
262
  this._get(rlKey, options)
263
263
  .then((res) => {
264
- if (res === null) {
265
- resolve(res);
264
+ if (res === null || typeof res === 'undefined') {
265
+ resolve(null);
266
266
  } else {
267
267
  resolve(this._getRateLimiterRes(rlKey, 0, res));
268
268
  }
@@ -284,6 +284,7 @@ module.exports = class RateLimiterStoreAbstract extends RateLimiterAbstract {
284
284
  return new Promise((resolve, reject) => {
285
285
  this._delete(rlKey, options)
286
286
  .then((res) => {
287
+ this._inmemoryBlockedKeys.delete(rlKey);
287
288
  resolve(res);
288
289
  })
289
290
  .catch((err) => {
@@ -292,6 +293,13 @@ module.exports = class RateLimiterStoreAbstract extends RateLimiterAbstract {
292
293
  });
293
294
  }
294
295
 
296
+ /**
297
+ * Cleanup keys no-matter expired or not.
298
+ */
299
+ deleteInMemoryBlockedAll() {
300
+ this._inmemoryBlockedKeys.delete();
301
+ }
302
+
295
303
  /**
296
304
  * Get RateLimiterRes object filled depending on storeResult, which specific for exact store
297
305
  *
@@ -356,4 +364,21 @@ module.exports = class RateLimiterStoreAbstract extends RateLimiterAbstract {
356
364
  _delete(rlKey, options = {}) { // eslint-disable-line no-unused-vars
357
365
  throw new Error("You have to implement the method '_delete'!");
358
366
  }
367
+
368
+ /**
369
+ * Have to be implemented
370
+ * Resolve with object used for {@link _getRateLimiterRes} to generate {@link RateLimiterRes}
371
+ *
372
+ * @param {string} rlKey
373
+ * @param {number} points
374
+ * @param {number} msDuration
375
+ * @param {boolean} forceExpire
376
+ * @param {Object} options
377
+ * @abstract
378
+ *
379
+ * @return Promise<Object>
380
+ */
381
+ _upsert() {
382
+ throw new Error("You have to implement the method '_upsert'!");
383
+ }
359
384
  };
@@ -57,4 +57,19 @@ module.exports = class BlockedKeys {
57
57
 
58
58
  return 0;
59
59
  }
60
+
61
+ /**
62
+ * If key is not given, delete all data in memory
63
+ *
64
+ * @param {string|undefined} key
65
+ */
66
+ delete(key) {
67
+ if (key) {
68
+ delete this._keys[key];
69
+ } else {
70
+ Object.keys(this._keys).forEach((key) => {
71
+ delete this._keys[key];
72
+ });
73
+ }
74
+ }
60
75
  };
@@ -0,0 +1,9 @@
1
+ export class RateLimiterQueueError extends Error {
2
+
3
+ constructor(message?: string, extra?: string);
4
+
5
+ readonly name: string;
6
+ readonly message: string;
7
+ readonly extra: string;
8
+
9
+ }
package/lib/index.d.ts CHANGED
@@ -141,6 +141,7 @@ export class RateLimiterAbstract {
141
141
  /**
142
142
  * Get RateLimiterRes in current duration. It always returns RateLimiterRes.isFirstInDuration=false.
143
143
  * @param key is usually IP address or some unique client id
144
+ * @param options
144
145
  * @returns Promise, which:
145
146
  * - `resolved` with RateLimiterRes if key is set
146
147
  * - `resolved` with null if key is NOT set or expired
@@ -205,6 +206,11 @@ export class RateLimiterAbstract {
205
206
 
206
207
  export class RateLimiterStoreAbstract extends RateLimiterAbstract {
207
208
  constructor(opts: IRateLimiterStoreOptions);
209
+
210
+ /**
211
+ * Cleanup keys blocked in current process memory
212
+ */
213
+ deleteInMemoryBlockedAll(): void;
208
214
  }
209
215
 
210
216
  interface IRateLimiterOptions {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rate-limiter-flexible",
3
- "version": "2.2.4",
3
+ "version": "2.3.3",
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": {