rate-limiter-flexible 3.0.0 → 3.0.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.
@@ -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
 
@@ -52,11 +54,15 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
52
54
  }
53
55
  }
54
56
 
57
+ _getTableIdentifier() {
58
+ return this.schemaName ? `"${this.schemaName}"."${this.tableName}"` : `"${this.tableName}"`;
59
+ }
60
+
55
61
  clearExpired(expire) {
56
62
  return new Promise((resolve) => {
57
63
  const q = {
58
64
  name: 'rlflx-clear-expired',
59
- text: `DELETE FROM ${this.tableName} WHERE expire < $1`,
65
+ text: `DELETE FROM ${this._getTableIdentifier()} WHERE expire < $1`,
60
66
  values: [expire],
61
67
  };
62
68
  this._query(q)
@@ -150,7 +156,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
150
156
  }
151
157
 
152
158
  _getCreateTableStmt() {
153
- return `CREATE TABLE IF NOT EXISTS ${this.tableName} (
159
+ return `CREATE TABLE IF NOT EXISTS ${this._getTableIdentifier()} (
154
160
  key varchar(255) PRIMARY KEY,
155
161
  points integer NOT NULL DEFAULT 0,
156
162
  expire bigint
@@ -190,8 +196,16 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
190
196
  this._tableName = typeof value === 'undefined' ? this.keyPrefix : value;
191
197
  }
192
198
 
199
+ get schemaName() {
200
+ return this._schemaName;
201
+ }
202
+
203
+ set schemaName(value) {
204
+ this._schemaName = value;
205
+ }
206
+
193
207
  get tableCreated() {
194
- return this._tableCreated
208
+ return this._tableCreated;
195
209
  }
196
210
 
197
211
  set tableCreated(value) {
@@ -252,18 +266,18 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
252
266
  const expireQ = forceExpire
253
267
  ? ' $3 '
254
268
  : ` CASE
255
- WHEN ${this.tableName}.expire <= $4 THEN $3
256
- ELSE ${this.tableName}.expire
269
+ WHEN ${this._getTableIdentifier()}.expire <= $4 THEN $3
270
+ ELSE ${this._getTableIdentifier()}.expire
257
271
  END `;
258
272
 
259
273
  return this._query({
260
274
  name: forceExpire ? 'rlflx-upsert-force' : 'rlflx-upsert',
261
275
  text: `
262
- INSERT INTO ${this.tableName} VALUES ($1, $2, $3)
276
+ INSERT INTO ${this._getTableIdentifier()} VALUES ($1, $2, $3)
263
277
  ON CONFLICT(key) DO UPDATE SET
264
278
  points = CASE
265
- WHEN (${this.tableName}.expire <= $4 OR 1=${forceExpire ? 1 : 0}) THEN $2
266
- ELSE ${this.tableName}.points + ($2)
279
+ WHEN (${this._getTableIdentifier()}.expire <= $4 OR 1=${forceExpire ? 1 : 0}) THEN $2
280
+ ELSE ${this._getTableIdentifier()}.points + ($2)
267
281
  END,
268
282
  expire = ${expireQ}
269
283
  RETURNING points, expire;`,
@@ -280,7 +294,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
280
294
  this._query({
281
295
  name: 'rlflx-get',
282
296
  text: `
283
- SELECT points, expire FROM ${this.tableName} WHERE key = $1 AND (expire > $2 OR expire IS NULL);`,
297
+ SELECT points, expire FROM ${this._getTableIdentifier()} WHERE key = $1 AND (expire > $2 OR expire IS NULL);`,
284
298
  values: [rlKey, Date.now()],
285
299
  })
286
300
  .then((res) => {
@@ -302,7 +316,7 @@ class RateLimiterPostgres extends RateLimiterStoreAbstract {
302
316
 
303
317
  return this._query({
304
318
  name: 'rlflx-delete',
305
- text: `DELETE FROM ${this.tableName} WHERE key = $1`,
319
+ text: `DELETE FROM ${this._getTableIdentifier()} WHERE key = $1`,
306
320
  values: [rlKey],
307
321
  })
308
322
  .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
- rejectIfRedisNotReady?: boolean;
252
- useRedisPackage?: boolean;
253
- useRedis3AndLowerPackage?: boolean;
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
- constructor(opts: IRateLimiterRedisOptions);
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: IRateLimiterStoreNoAutoExpiryOptions, cb?: ICallbackReady);
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.0",
3
+ "version": "3.0.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": {