rate-limiter-flexible 2.2.3 → 2.3.2
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 +13 -0
- package/README.md +1 -0
- package/lib/RateLimiterMongo.js +15 -12
- package/lib/RateLimiterStoreAbstract.js +27 -2
- package/lib/component/BlockedKeys/BlockedKeys.js +15 -0
- package/package.json +1 -1
package/.editorconfig
ADDED
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.
|
package/lib/RateLimiterMongo.js
CHANGED
|
@@ -8,7 +8,12 @@ const RateLimiterRes = require('./RateLimiterRes');
|
|
|
8
8
|
*/
|
|
9
9
|
function getDriverVersion(client) {
|
|
10
10
|
try {
|
|
11
|
-
|
|
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;
|
|
@@ -110,11 +115,7 @@ class RateLimiterMongo extends RateLimiterStoreAbstract {
|
|
|
110
115
|
|
|
111
116
|
let doc;
|
|
112
117
|
if (typeof result.value === 'undefined') {
|
|
113
|
-
|
|
114
|
-
doc = result;
|
|
115
|
-
} else {
|
|
116
|
-
[doc] = result.ops; // ops set on replaceOne
|
|
117
|
-
}
|
|
118
|
+
doc = result;
|
|
118
119
|
} else {
|
|
119
120
|
doc = result.value;
|
|
120
121
|
}
|
|
@@ -202,13 +203,15 @@ class RateLimiterMongo extends RateLimiterStoreAbstract {
|
|
|
202
203
|
key,
|
|
203
204
|
}, docAttrs);
|
|
204
205
|
|
|
205
|
-
const replaceTo =
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
206
|
+
const replaceTo = {
|
|
207
|
+
$set: Object.assign({
|
|
208
|
+
key,
|
|
209
|
+
points,
|
|
210
|
+
expire: msDuration > 0 ? new Date(Date.now() + msDuration) : null,
|
|
211
|
+
}, docAttrs)
|
|
212
|
+
};
|
|
210
213
|
|
|
211
|
-
this._collection.
|
|
214
|
+
this._collection.findOneAndUpdate(
|
|
212
215
|
replaceWhere,
|
|
213
216
|
replaceTo,
|
|
214
217
|
upsertOptions
|
|
@@ -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(
|
|
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
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rate-limiter-flexible",
|
|
3
|
-
"version": "2.2
|
|
3
|
+
"version": "2.3.2",
|
|
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": {
|