rate-limiter-flexible 2.2.1 → 2.2.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/LICENSE.md CHANGED
@@ -1,5 +1,7 @@
1
+ ## ISC License (ISC)
2
+
1
3
  Copyright 2019 Roman Voloboev
2
4
 
3
5
  Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
6
 
5
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/lib/index.d.ts CHANGED
@@ -6,7 +6,12 @@ export interface IRateLimiterRes {
6
6
  }
7
7
 
8
8
  export class RateLimiterRes {
9
- constructor(remainingPoints?: number, msBeforeNext?: number, consumedPoints?: number, isFirstInDuration?: boolean);
9
+ constructor(
10
+ remainingPoints?: number,
11
+ msBeforeNext?: number,
12
+ consumedPoints?: number,
13
+ isFirstInDuration?: boolean
14
+ );
10
15
 
11
16
  readonly msBeforeNext: number;
12
17
  readonly remainingPoints: number;
@@ -25,21 +30,177 @@ export class RateLimiterRes {
25
30
  export class RateLimiterAbstract {
26
31
  constructor(opts: IRateLimiterOptions);
27
32
 
28
- consume(key: string | number, pointsToConsume?: number, options?: {[key: string]: any }): Promise<RateLimiterRes>;
29
-
30
- penalty(key: string | number, points?: number, options?: {[key: string]: any }): Promise<RateLimiterRes>;
31
-
32
- reward(key: string | number, points?: number, options?: {[key: string]: any }): Promise<RateLimiterRes>;
33
-
34
- block(key: string | number, secDuration: number, options?: {[key: string]: any }): Promise<RateLimiterRes>;
35
-
36
- get(key: string | number, options?: {[key: string]: any }): Promise<RateLimiterRes|null>;
37
-
38
- set(key: string | number, points: number, secDuration: number, options?: {[key: string]: any }): Promise<RateLimiterRes>;
39
-
40
- delete(key: string | number, options?: {[key: string]: any }): Promise<boolean>;
41
-
33
+ /**
34
+ * Maximum number of points can be consumed over duration. Limiter compares this number with
35
+ * number of consumed points by key to decide if an operation should be rejected or resolved.
36
+ */
37
+ points: number;
38
+
39
+ /**
40
+ * Number of seconds before consumed points are reset.
41
+ * Keys never expire, if duration is 0.
42
+ */
43
+ duration: number;
44
+
45
+ /**
46
+ * duration in milliseconds
47
+ */
48
+ get msDuration(): number;
49
+
50
+ /**
51
+ * If positive number and consumed more than points in current duration, block for blockDuration
52
+ * seconds.
53
+ */
54
+ blockDuration: number;
55
+
56
+ /**
57
+ * blockDuration in milliseconds
58
+ */
59
+ get msBlockDuration(): number;
60
+
61
+ /**
62
+ * Delay action to be executed evenly over duration First action in duration is executed without
63
+ * delay. All next allowed actions in current duration are delayed by formula
64
+ * msBeforeDurationEnd / (remainingPoints + 2) with minimum delay of duration * 1000 / points.
65
+ * It allows to cut off load peaks similar way to Leaky Bucket.
66
+ *
67
+ * Note: it isn't recommended to use it for long duration and few points, as it may delay action
68
+ * for too long with default execEvenlyMinDelayMs.
69
+ */
70
+ execEvenly: boolean;
71
+
72
+ /**
73
+ * Sets minimum delay in milliseconds, when action is delayed with execEvenly
74
+ */
75
+ execEvenlyMinDelayMs: number;
76
+
77
+ /**
78
+ * If you need to create several limiters for different purpose.
79
+ * Set to empty string '', if keys should be stored without prefix.
80
+ */
81
+ keyPrefix: string;
82
+
83
+ /**
84
+ * Returns internal key prefixed with keyPrefix option as it is saved in store.
85
+ */
42
86
  getKey(key: string | number): string;
87
+
88
+ /**
89
+ * Returns internal key without the keyPrefix.
90
+ */
91
+ parseKey(rlKey: string): string;
92
+
93
+ /**
94
+ * @param key is usually IP address or some unique client id
95
+ * @param pointsToConsume number of points consumed. default: 1
96
+ * @param options is object with additional settings:
97
+ * - customDuration expire in seconds for this operation only overwrites limiter's duration. It doesn't work, if key already created.
98
+ * @returns Returns Promise, which:
99
+ * - `resolved` with `RateLimiterRes` when point(s) is consumed, so action can be done
100
+ * - `rejected` only for store and database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
101
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
102
+ * - `rejected` when there is no points to be consumed, where reject reason `rejRes` is `RateLimiterRes` object
103
+ * - `rejected` when key is blocked (if block strategy is set up), where reject reason `rejRes` is `RateLimiterRes` object
104
+ */
105
+ consume(
106
+ key: string | number,
107
+ pointsToConsume?: number,
108
+ options?: { [key: string]: any }
109
+ ): Promise<RateLimiterRes>;
110
+
111
+ /**
112
+ * Fine key by points number of points for one duration.
113
+ *
114
+ * Note: Depending on time penalty may go to next durations
115
+ *
116
+ * @returns Returns Promise, which:
117
+ * - `resolved` with RateLimiterRes
118
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
119
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
120
+ */
121
+ penalty(
122
+ key: string | number,
123
+ points?: number,
124
+ options?: { [key: string]: any }
125
+ ): Promise<RateLimiterRes>;
126
+
127
+ /**
128
+ * Reward key by points number of points for one duration.
129
+ * Note: Depending on time reward may go to next durations
130
+ * @returns Promise, which:
131
+ * - `resolved` with RateLimiterRes
132
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
133
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
134
+ */
135
+ reward(
136
+ key: string | number,
137
+ points?: number,
138
+ options?: { [key: string]: any }
139
+ ): Promise<RateLimiterRes>;
140
+
141
+ /**
142
+ * Get RateLimiterRes in current duration. It always returns RateLimiterRes.isFirstInDuration=false.
143
+ * @param key is usually IP address or some unique client id
144
+ * @returns Promise, which:
145
+ * - `resolved` with RateLimiterRes if key is set
146
+ * - `resolved` with null if key is NOT set or expired
147
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
148
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
149
+ */
150
+ get(
151
+ key: string | number,
152
+ options?: { [key: string]: any }
153
+ ): Promise<RateLimiterRes | null>;
154
+
155
+ /**
156
+ * Set points to key for secDuration seconds.
157
+ * Store it forever, if secDuration is 0.
158
+ * @param key
159
+ * @param points
160
+ * @param secDuration
161
+ * @param options
162
+ * @returns Promise, which:
163
+ * - `resolved` with RateLimiterRes
164
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
165
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
166
+ */
167
+ set(
168
+ key: string | number,
169
+ points: number,
170
+ secDuration: number,
171
+ options?: { [key: string]: any }
172
+ ): Promise<RateLimiterRes>;
173
+
174
+ /**
175
+ * Block key by setting consumed points to points + 1 for secDuration seconds.
176
+ *
177
+ * It force updates expire, if there is already key.
178
+ *
179
+ * Blocked key never expires, if secDuration is 0.
180
+ * @returns Promise, which:
181
+ * - `resolved` with RateLimiterRes
182
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
183
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
184
+ */
185
+ block(
186
+ key: string | number,
187
+ secDuration: number,
188
+ options?: { [key: string]: any }
189
+ ): Promise<RateLimiterRes>;
190
+
191
+ /**
192
+ * Delete all data related to key.
193
+ *
194
+ * For example, previously blocked key is not blocked after delete as there is no data anymore.
195
+ * @returns Promise, which:
196
+ * - `resolved` with boolean, true if data is removed by key, false if there is no such key.
197
+ * - `rejected` only for database limiters if insuranceLimiter isn't setup: when some error happened, where reject reason `rejRes` is Error object
198
+ * - `rejected` only for RateLimiterCluster if insuranceLimiter isn't setup: when timeoutMs exceeded, where reject reason `rejRes` is Error object
199
+ */
200
+ delete(
201
+ key: string | number,
202
+ options?: { [key: string]: any }
203
+ ): Promise<boolean>;
43
204
  }
44
205
 
45
206
  export class RateLimiterStoreAbstract extends RateLimiterAbstract {
@@ -55,11 +216,11 @@ interface IRateLimiterOptions {
55
216
  blockDuration?: number;
56
217
  }
57
218
 
58
- interface IRateLimiterClusterOptions extends IRateLimiterOptions{
219
+ interface IRateLimiterClusterOptions extends IRateLimiterOptions {
59
220
  timeoutMs?: number;
60
221
  }
61
222
 
62
- interface IRateLimiterStoreOptions extends IRateLimiterOptions{
223
+ interface IRateLimiterStoreOptions extends IRateLimiterOptions {
63
224
  storeClient: any;
64
225
  storeType?: string;
65
226
  inmemoryBlockOnConsumed?: number;
@@ -70,9 +231,9 @@ interface IRateLimiterStoreOptions extends IRateLimiterOptions{
70
231
  tableCreated?: boolean;
71
232
  }
72
233
 
73
- interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions{
234
+ interface IRateLimiterMongoOptions extends IRateLimiterStoreOptions {
74
235
  indexKeyPrefix?: {
75
- [key: string]: any
236
+ [key: string]: any;
76
237
  };
77
238
  }
78
239
 
@@ -82,7 +243,7 @@ interface ICallbackReady {
82
243
 
83
244
  interface IRLWrapperBlackAndWhiteOptions {
84
245
  limiter: RateLimiterAbstract;
85
- blackList?: string [] | number[];
246
+ blackList?: string[] | number[];
86
247
  whiteList?: string[] | number[];
87
248
  isBlackListed?(key: any): boolean;
88
249
  isWhiteListed?(key: any): boolean;
@@ -105,31 +266,57 @@ export class RateLimiterClusterMasterPM2 {
105
266
  constructor(pm2: any);
106
267
  }
107
268
 
108
- export class RateLimiterRedis extends RateLimiterStoreAbstract {
109
- }
269
+ export class RateLimiterRedis extends RateLimiterStoreAbstract {}
110
270
 
111
271
  export interface IRateLimiterMongoFunctionOptions {
112
- attrs: {[key: string]: any};
272
+ attrs: { [key: string]: any };
113
273
  }
114
274
 
115
275
  export class RateLimiterMongo extends RateLimiterStoreAbstract {
116
276
  constructor(opts: IRateLimiterMongoOptions);
117
- indexKeyPrefix():Object;
118
- indexKeyPrefix(obj?: Object):void;
119
-
120
- consume(key: string | number, pointsToConsume?: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
121
-
122
- penalty(key: string | number, points?: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
123
-
124
- reward(key: string | number, points?: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
125
-
126
- block(key: string | number, secDuration: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
127
-
128
- get(key: string | number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes|null>;
129
-
130
- set(key: string | number, points: number, secDuration: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
131
-
132
- delete(key: string | number, options?: IRateLimiterMongoFunctionOptions): Promise<boolean>;
277
+ indexKeyPrefix(): Object;
278
+ indexKeyPrefix(obj?: Object): void;
279
+
280
+ consume(
281
+ key: string | number,
282
+ pointsToConsume?: number,
283
+ options?: IRateLimiterMongoFunctionOptions
284
+ ): Promise<RateLimiterRes>;
285
+
286
+ penalty(
287
+ key: string | number,
288
+ points?: number,
289
+ options?: IRateLimiterMongoFunctionOptions
290
+ ): Promise<RateLimiterRes>;
291
+
292
+ reward(
293
+ key: string | number,
294
+ points?: number,
295
+ options?: IRateLimiterMongoFunctionOptions
296
+ ): Promise<RateLimiterRes>;
297
+
298
+ block(
299
+ key: string | number,
300
+ secDuration: number,
301
+ options?: IRateLimiterMongoFunctionOptions
302
+ ): Promise<RateLimiterRes>;
303
+
304
+ get(
305
+ key: string | number,
306
+ options?: IRateLimiterMongoFunctionOptions
307
+ ): Promise<RateLimiterRes | null>;
308
+
309
+ set(
310
+ key: string | number,
311
+ points: number,
312
+ secDuration: number,
313
+ options?: IRateLimiterMongoFunctionOptions
314
+ ): Promise<RateLimiterRes>;
315
+
316
+ delete(
317
+ key: string | number,
318
+ options?: IRateLimiterMongoFunctionOptions
319
+ ): Promise<boolean>;
133
320
  }
134
321
 
135
322
  export class RateLimiterMySQL extends RateLimiterStoreAbstract {
@@ -140,8 +327,7 @@ export class RateLimiterPostgres extends RateLimiterStoreAbstract {
140
327
  constructor(opts: IRateLimiterStoreOptions, cb?: ICallbackReady);
141
328
  }
142
329
 
143
- export class RateLimiterMemcache extends RateLimiterStoreAbstract {
144
- }
330
+ export class RateLimiterMemcache extends RateLimiterStoreAbstract {}
145
331
 
146
332
  export class RateLimiterUnion {
147
333
  constructor(...limiters: RateLimiterAbstract[]);
@@ -154,11 +340,14 @@ export class RLWrapperBlackAndWhite extends RateLimiterAbstract {
154
340
  }
155
341
 
156
342
  interface IRateLimiterQueueOpts {
157
- maxQueueSize?: number,
343
+ maxQueueSize?: number;
158
344
  }
159
345
 
160
346
  export class RateLimiterQueue {
161
- constructor(limiterFlexible: RateLimiterAbstract | BurstyRateLimiter, opts?: IRateLimiterQueueOpts);
347
+ constructor(
348
+ limiterFlexible: RateLimiterAbstract | BurstyRateLimiter,
349
+ opts?: IRateLimiterQueueOpts
350
+ );
162
351
 
163
352
  getTokensRemaining(key?: string | number): Promise<number>;
164
353
 
@@ -166,7 +355,14 @@ export class RateLimiterQueue {
166
355
  }
167
356
 
168
357
  export class BurstyRateLimiter {
169
- constructor(rateLimiter: RateLimiterAbstract, burstLimiter: RateLimiterAbstract)
170
-
171
- consume(key: string | number, pointsToConsume?: number, options?: IRateLimiterMongoFunctionOptions): Promise<RateLimiterRes>;
358
+ constructor(
359
+ rateLimiter: RateLimiterAbstract,
360
+ burstLimiter: RateLimiterAbstract
361
+ );
362
+
363
+ consume(
364
+ key: string | number,
365
+ pointsToConsume?: number,
366
+ options?: IRateLimiterMongoFunctionOptions
367
+ ): Promise<RateLimiterRes>;
172
368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rate-limiter-flexible",
3
- "version": "2.2.1",
3
+ "version": "2.2.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": {