rate-limiter-flexible 2.3.0 → 2.3.4
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/lib/RateLimiterMongo.js +17 -6
- package/lib/RateLimiterStoreAbstract.js +2 -2
- package/lib/component/index.d.ts +9 -0
- package/lib/index.d.ts +6 -0
- package/package.json +1 -1
package/.editorconfig
ADDED
package/lib/RateLimiterMongo.js
CHANGED
|
@@ -4,16 +4,22 @@ const RateLimiterRes = require('./RateLimiterRes');
|
|
|
4
4
|
/**
|
|
5
5
|
* Get MongoDB driver version as upsert options differ
|
|
6
6
|
* @params {Object} Client instance
|
|
7
|
-
* @returns {
|
|
7
|
+
* @returns {Object} Version Object containing major, feature & minor versions.
|
|
8
8
|
*/
|
|
9
9
|
function getDriverVersion(client) {
|
|
10
10
|
try {
|
|
11
|
-
const
|
|
12
|
-
const majorVersion = parseInt(version);
|
|
11
|
+
const _client = client.client ? client.client : client;
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
const { version } = _client.topology.s.options.metadata.driver;
|
|
14
|
+
const _v = version.split('.').map(v => parseInt(v));
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
major: _v[0],
|
|
18
|
+
feature: _v[1],
|
|
19
|
+
patch: _v[2],
|
|
20
|
+
};
|
|
15
21
|
} catch (err) {
|
|
16
|
-
return
|
|
22
|
+
return { major: 0, feature: 0, patch: 0 };
|
|
17
23
|
}
|
|
18
24
|
}
|
|
19
25
|
|
|
@@ -169,7 +175,12 @@ class RateLimiterMongo extends RateLimiterStoreAbstract {
|
|
|
169
175
|
const upsertOptions = {
|
|
170
176
|
upsert: true,
|
|
171
177
|
};
|
|
172
|
-
if (this._driverVersion >= 4)
|
|
178
|
+
if ((this._driverVersion.major >= 4) ||
|
|
179
|
+
(this._driverVersion.major === 3 &&
|
|
180
|
+
(this._driverVersion.feature >=7) ||
|
|
181
|
+
(this._driverVersion.feature >= 6 &&
|
|
182
|
+
this._driverVersion.patch >= 7 )))
|
|
183
|
+
{
|
|
173
184
|
upsertOptions.returnDocument = 'after';
|
|
174
185
|
} else {
|
|
175
186
|
upsertOptions.returnOriginal = false;
|
|
@@ -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
|
}
|
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.3.
|
|
3
|
+
"version": "2.3.4",
|
|
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": {
|