rate-limiter-flexible 3.0.1 → 3.0.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/lib/RateLimiterPostgres.js +27 -10
- package/lib/index.d.ts +10 -6
- package/package.json +1 -1
|
@@ -14,6 +14,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
14
14
|
* storeClient: postgresClient,
|
|
15
15
|
* storeType: 'knex', // required only for Knex instance
|
|
16
16
|
* tableName: 'string',
|
|
17
|
+
* schemaName: 'string', // optional
|
|
17
18
|
* }
|
|
18
19
|
*/
|
|
19
20
|
constructor(opts, cb = null) {
|
|
@@ -23,6 +24,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
23
24
|
this.clientType = opts.storeType;
|
|
24
25
|
|
|
25
26
|
this.tableName = opts.tableName;
|
|
27
|
+
this.schemaName = opts.schemaName;
|
|
26
28
|
|
|
27
29
|
this.clearExpiredByTimeout = opts.clearExpiredByTimeout;
|
|
28
30
|
|
|
@@ -46,17 +48,24 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
46
48
|
}
|
|
47
49
|
});
|
|
48
50
|
} else {
|
|
51
|
+
if (this.clearExpiredByTimeout) {
|
|
52
|
+
this._clearExpiredHourAgo();
|
|
53
|
+
}
|
|
49
54
|
if (typeof cb === 'function') {
|
|
50
55
|
cb();
|
|
51
56
|
}
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
59
|
|
|
60
|
+
_getTableIdentifier() {
|
|
61
|
+
return this.schemaName ? `"${this.schemaName}"."${this.tableName}"` : `"${this.tableName}"`;
|
|
62
|
+
}
|
|
63
|
+
|
|
55
64
|
clearExpired(expire) {
|
|
56
65
|
return new Promise((resolve) => {
|
|
57
66
|
const q = {
|
|
58
67
|
name: 'rlflx-clear-expired',
|
|
59
|
-
text: `DELETE FROM
|
|
68
|
+
text: `DELETE FROM ${this._getTableIdentifier()} WHERE expire < $1`,
|
|
60
69
|
values: [expire],
|
|
61
70
|
};
|
|
62
71
|
this._query(q)
|
|
@@ -150,7 +159,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
150
159
|
}
|
|
151
160
|
|
|
152
161
|
_getCreateTableStmt() {
|
|
153
|
-
return `CREATE TABLE IF NOT EXISTS
|
|
162
|
+
return `CREATE TABLE IF NOT EXISTS ${this._getTableIdentifier()} (
|
|
154
163
|
key varchar(255) PRIMARY KEY,
|
|
155
164
|
points integer NOT NULL DEFAULT 0,
|
|
156
165
|
expire bigint
|
|
@@ -190,8 +199,16 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
190
199
|
this._tableName = typeof value === 'undefined' ? this.keyPrefix : value;
|
|
191
200
|
}
|
|
192
201
|
|
|
202
|
+
get schemaName() {
|
|
203
|
+
return this._schemaName;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
set schemaName(value) {
|
|
207
|
+
this._schemaName = value;
|
|
208
|
+
}
|
|
209
|
+
|
|
193
210
|
get tableCreated() {
|
|
194
|
-
return this._tableCreated
|
|
211
|
+
return this._tableCreated;
|
|
195
212
|
}
|
|
196
213
|
|
|
197
214
|
set tableCreated(value) {
|
|
@@ -252,18 +269,18 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
252
269
|
const expireQ = forceExpire
|
|
253
270
|
? ' $3 '
|
|
254
271
|
: ` CASE
|
|
255
|
-
WHEN
|
|
256
|
-
ELSE
|
|
272
|
+
WHEN ${this._getTableIdentifier()}.expire <= $4 THEN $3
|
|
273
|
+
ELSE ${this._getTableIdentifier()}.expire
|
|
257
274
|
END `;
|
|
258
275
|
|
|
259
276
|
return this._query({
|
|
260
277
|
name: forceExpire ? 'rlflx-upsert-force' : 'rlflx-upsert',
|
|
261
278
|
text: `
|
|
262
|
-
INSERT INTO
|
|
279
|
+
INSERT INTO ${this._getTableIdentifier()} VALUES ($1, $2, $3)
|
|
263
280
|
ON CONFLICT(key) DO UPDATE SET
|
|
264
281
|
points = CASE
|
|
265
|
-
WHEN (
|
|
266
|
-
ELSE
|
|
282
|
+
WHEN (${this._getTableIdentifier()}.expire <= $4 OR 1=${forceExpire ? 1 : 0}) THEN $2
|
|
283
|
+
ELSE ${this._getTableIdentifier()}.points + ($2)
|
|
267
284
|
END,
|
|
268
285
|
expire = ${expireQ}
|
|
269
286
|
RETURNING points, expire;`,
|
|
@@ -280,7 +297,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
280
297
|
this._query({
|
|
281
298
|
name: 'rlflx-get',
|
|
282
299
|
text: `
|
|
283
|
-
SELECT points, expire FROM
|
|
300
|
+
SELECT points, expire FROM ${this._getTableIdentifier()} WHERE key = $1 AND (expire > $2 OR expire IS NULL);`,
|
|
284
301
|
values: [rlKey, Date.now()],
|
|
285
302
|
})
|
|
286
303
|
.then((res) => {
|
|
@@ -302,7 +319,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
|
302
319
|
|
|
303
320
|
return this._query({
|
|
304
321
|
name: 'rlflx-delete',
|
|
305
|
-
text: `DELETE FROM
|
|
322
|
+
text: `DELETE FROM ${this._getTableIdentifier()} WHERE key = $1`,
|
|
306
323
|
values: [rlKey],
|
|
307
324
|
})
|
|
308
325
|
.then(res => res.rowCount > 0);
|
package/lib/index.d.ts
CHANGED
|
@@ -247,10 +247,14 @@ interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions {
|
|
|
247
247
|
};
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
+
interface IRateLimiterPostgresOptions extends IRateLimiterStoreNoAutoExpiryOptions {
|
|
251
|
+
schemaName?: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
250
254
|
interface IRateLimiterRedisOptions extends IRateLimiterStoreOptions {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
255
|
+
rejectIfRedisNotReady?: boolean;
|
|
256
|
+
useRedisPackage?: boolean;
|
|
257
|
+
useRedis3AndLowerPackage?: boolean;
|
|
254
258
|
}
|
|
255
259
|
|
|
256
260
|
interface ICallbackReady {
|
|
@@ -283,7 +287,7 @@ export class RateLimiterClusterMasterPM2 {
|
|
|
283
287
|
}
|
|
284
288
|
|
|
285
289
|
export class RateLimiterRedis extends RateLimiterStoreAbstract {
|
|
286
|
-
|
|
290
|
+
constructor(opts: IRateLimiterRedisOptions);
|
|
287
291
|
}
|
|
288
292
|
|
|
289
293
|
export interface IRateLimiterMongoFunctionOptions {
|
|
@@ -342,10 +346,10 @@ export class RateLimiterMySQL extends RateLimiterStoreAbstract {
|
|
|
342
346
|
}
|
|
343
347
|
|
|
344
348
|
export class RateLimiterPostgres extends RateLimiterStoreAbstract {
|
|
345
|
-
constructor(opts:
|
|
349
|
+
constructor(opts: IRateLimiterPostgresOptions, cb?: ICallbackReady);
|
|
346
350
|
}
|
|
347
351
|
|
|
348
|
-
export class RateLimiterMemcache extends RateLimiterStoreAbstract {}
|
|
352
|
+
export class RateLimiterMemcache extends RateLimiterStoreAbstract { }
|
|
349
353
|
|
|
350
354
|
export class RateLimiterUnion {
|
|
351
355
|
constructor(...limiters: RateLimiterAbstract[]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rate-limiter-flexible",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.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": {
|