@withjoy/limiter 0.1.4-test → 0.1.6

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/README.md CHANGED
@@ -28,6 +28,8 @@ Although this package is available on npm, it's no longer actively maintained. W
28
28
 
29
29
  In case we need to update in the future, we can replace the "limitd-redis" directory with the latest version of [limitd-redis](https://github.com/auth0/limitd-redis) from their GitHub repository.
30
30
 
31
+ Note: We need to add dependency from limitd-redis to limiter package json before publish. We are using limitd-redis as directly not package inside limiter.
32
+
31
33
  ## Testing:
32
34
 
33
35
  `npm run test` will run offline tests using a mock.
package/limiter.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var LimitdRedis = require("limitd-redis"); // name to redis matter
3
+ var LimitdRedis = require("./limitd-redis"); // name to redis matter
4
4
  var z = require('zod');
5
5
 
6
6
  const defaultBuckets = {
@@ -10,7 +10,7 @@ const defaultBuckets = {
10
10
  emailsByEventIdRate_Warn: { size: 200, per_hour: 200 },
11
11
  eventsByUserId: { size: 5, per_hour: 5 },
12
12
  eventsByIpRate: { size: 240, per_hour: 240 },
13
- paymentAttempts: { size: 9000 },
13
+ paymentAttempts: { size: 5000 },
14
14
  paymentAttemptsByReceiverId: { size: 900 },
15
15
  // for development testing
16
16
  testPerMinute: { size: 3, per_minute: 1 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@withjoy/limiter",
3
- "version": "0.1.4-test",
3
+ "version": "0.1.6",
4
4
  "description": "Api Rate limiter",
5
5
  "main": "limiter.js",
6
6
  "scripts": {
@@ -10,7 +10,13 @@
10
10
  "author": "services@withjoy.com",
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
- "limitd-redis": "file:./limitd-redis",
13
+ "async": "^2.6.1",
14
+ "disyuntor": "^3.5.0",
15
+ "ioredis": "^4.28.5",
16
+ "lodash": "^4.17.15",
17
+ "lru-cache": "^4.1.5",
18
+ "ms": "^2.1.2",
19
+ "retry": "^0.12.0",
14
20
  "zod": "^3.22.4"
15
21
  },
16
22
  "devDependencies": {
@@ -1,15 +0,0 @@
1
- The ISC License
2
-
3
- Copyright (c) Isaac Z. Schlueter and Contributors
4
-
5
- Permission to use, copy, modify, and/or distribute this software for any
6
- purpose with or without fee is hereby granted, provided that the above
7
- copyright notice and this permission notice appear in all copies.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15
- IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -1,158 +0,0 @@
1
- # lru cache
2
-
3
- A cache object that deletes the least-recently-used items.
4
-
5
- [![Build Status](https://travis-ci.org/isaacs/node-lru-cache.svg?branch=master)](https://travis-ci.org/isaacs/node-lru-cache) [![Coverage Status](https://coveralls.io/repos/isaacs/node-lru-cache/badge.svg?service=github)](https://coveralls.io/github/isaacs/node-lru-cache)
6
-
7
- ## Installation:
8
-
9
- ```javascript
10
- npm install lru-cache --save
11
- ```
12
-
13
- ## Usage:
14
-
15
- ```javascript
16
- var LRU = require("lru-cache")
17
- , options = { max: 500
18
- , length: function (n, key) { return n * 2 + key.length }
19
- , dispose: function (key, n) { n.close() }
20
- , maxAge: 1000 * 60 * 60 }
21
- , cache = LRU(options)
22
- , otherCache = LRU(50) // sets just the max size
23
-
24
- cache.set("key", "value")
25
- cache.get("key") // "value"
26
-
27
- // non-string keys ARE fully supported
28
- // but note that it must be THE SAME object, not
29
- // just a JSON-equivalent object.
30
- var someObject = { a: 1 }
31
- cache.set(someObject, 'a value')
32
- // Object keys are not toString()-ed
33
- cache.set('[object Object]', 'a different value')
34
- assert.equal(cache.get(someObject), 'a value')
35
- // A similar object with same keys/values won't work,
36
- // because it's a different object identity
37
- assert.equal(cache.get({ a: 1 }), undefined)
38
-
39
- cache.reset() // empty the cache
40
- ```
41
-
42
- If you put more stuff in it, then items will fall out.
43
-
44
- If you try to put an oversized thing in it, then it'll fall out right
45
- away.
46
-
47
- ## Options
48
-
49
- * `max` The maximum size of the cache, checked by applying the length
50
- function to all values in the cache. Not setting this is kind of
51
- silly, since that's the whole purpose of this lib, but it defaults
52
- to `Infinity`.
53
- * `maxAge` Maximum age in ms. Items are not pro-actively pruned out
54
- as they age, but if you try to get an item that is too old, it'll
55
- drop it and return undefined instead of giving it to you.
56
- * `length` Function that is used to calculate the length of stored
57
- items. If you're storing strings or buffers, then you probably want
58
- to do something like `function(n, key){return n.length}`. The default is
59
- `function(){return 1}`, which is fine if you want to store `max`
60
- like-sized things. The item is passed as the first argument, and
61
- the key is passed as the second argumnet.
62
- * `dispose` Function that is called on items when they are dropped
63
- from the cache. This can be handy if you want to close file
64
- descriptors or do other cleanup tasks when items are no longer
65
- accessible. Called with `key, value`. It's called *before*
66
- actually removing the item from the internal cache, so if you want
67
- to immediately put it back in, you'll have to do that in a
68
- `nextTick` or `setTimeout` callback or it won't do anything.
69
- * `stale` By default, if you set a `maxAge`, it'll only actually pull
70
- stale items out of the cache when you `get(key)`. (That is, it's
71
- not pre-emptively doing a `setTimeout` or anything.) If you set
72
- `stale:true`, it'll return the stale value before deleting it. If
73
- you don't set this, then it'll return `undefined` when you try to
74
- get a stale entry, as if it had already been deleted.
75
- * `noDisposeOnSet` By default, if you set a `dispose()` method, then
76
- it'll be called whenever a `set()` operation overwrites an existing
77
- key. If you set this option, `dispose()` will only be called when a
78
- key falls out of the cache, not when it is overwritten.
79
-
80
- ## API
81
-
82
- * `set(key, value, maxAge)`
83
- * `get(key) => value`
84
-
85
- Both of these will update the "recently used"-ness of the key.
86
- They do what you think. `maxAge` is optional and overrides the
87
- cache `maxAge` option if provided.
88
-
89
- If the key is not found, `get()` will return `undefined`.
90
-
91
- The key and val can be any value.
92
-
93
- * `peek(key)`
94
-
95
- Returns the key value (or `undefined` if not found) without
96
- updating the "recently used"-ness of the key.
97
-
98
- (If you find yourself using this a lot, you *might* be using the
99
- wrong sort of data structure, but there are some use cases where
100
- it's handy.)
101
-
102
- * `del(key)`
103
-
104
- Deletes a key out of the cache.
105
-
106
- * `reset()`
107
-
108
- Clear the cache entirely, throwing away all values.
109
-
110
- * `has(key)`
111
-
112
- Check if a key is in the cache, without updating the recent-ness
113
- or deleting it for being stale.
114
-
115
- * `forEach(function(value,key,cache), [thisp])`
116
-
117
- Just like `Array.prototype.forEach`. Iterates over all the keys
118
- in the cache, in order of recent-ness. (Ie, more recently used
119
- items are iterated over first.)
120
-
121
- * `rforEach(function(value,key,cache), [thisp])`
122
-
123
- The same as `cache.forEach(...)` but items are iterated over in
124
- reverse order. (ie, less recently used items are iterated over
125
- first.)
126
-
127
- * `keys()`
128
-
129
- Return an array of the keys in the cache.
130
-
131
- * `values()`
132
-
133
- Return an array of the values in the cache.
134
-
135
- * `length`
136
-
137
- Return total length of objects in cache taking into account
138
- `length` options function.
139
-
140
- * `itemCount`
141
-
142
- Return total quantity of objects currently in cache. Note, that
143
- `stale` (see options) items are returned as part of this item
144
- count.
145
-
146
- * `dump()`
147
-
148
- Return an array of the cache entries ready for serialization and usage
149
- with 'destinationCache.load(arr)`.
150
-
151
- * `load(cacheEntriesArray)`
152
-
153
- Loads another cache entries array, obtained with `sourceCache.dump()`,
154
- into the cache. The destination cache is reset before loading new entries
155
-
156
- * `prune()`
157
-
158
- Manually iterates over the entire cache proactively pruning old entries
@@ -1,468 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports = LRUCache
4
-
5
- // This will be a proper iterable 'Map' in engines that support it,
6
- // or a fakey-fake PseudoMap in older versions.
7
- var Map = require('pseudomap')
8
- var util = require('util')
9
-
10
- // A linked list to keep track of recently-used-ness
11
- var Yallist = require('yallist')
12
-
13
- // use symbols if possible, otherwise just _props
14
- var hasSymbol = typeof Symbol === 'function' && process.env._nodeLRUCacheForceNoSymbol !== '1'
15
- var makeSymbol
16
- if (hasSymbol) {
17
- makeSymbol = function (key) {
18
- return Symbol(key)
19
- }
20
- } else {
21
- makeSymbol = function (key) {
22
- return '_' + key
23
- }
24
- }
25
-
26
- var MAX = makeSymbol('max')
27
- var LENGTH = makeSymbol('length')
28
- var LENGTH_CALCULATOR = makeSymbol('lengthCalculator')
29
- var ALLOW_STALE = makeSymbol('allowStale')
30
- var MAX_AGE = makeSymbol('maxAge')
31
- var DISPOSE = makeSymbol('dispose')
32
- var NO_DISPOSE_ON_SET = makeSymbol('noDisposeOnSet')
33
- var LRU_LIST = makeSymbol('lruList')
34
- var CACHE = makeSymbol('cache')
35
-
36
- function naiveLength () { return 1 }
37
-
38
- // lruList is a yallist where the head is the youngest
39
- // item, and the tail is the oldest. the list contains the Hit
40
- // objects as the entries.
41
- // Each Hit object has a reference to its Yallist.Node. This
42
- // never changes.
43
- //
44
- // cache is a Map (or PseudoMap) that matches the keys to
45
- // the Yallist.Node object.
46
- function LRUCache (options) {
47
- if (!(this instanceof LRUCache)) {
48
- return new LRUCache(options)
49
- }
50
-
51
- if (typeof options === 'number') {
52
- options = { max: options }
53
- }
54
-
55
- if (!options) {
56
- options = {}
57
- }
58
-
59
- var max = this[MAX] = options.max
60
- // Kind of weird to have a default max of Infinity, but oh well.
61
- if (!max ||
62
- !(typeof max === 'number') ||
63
- max <= 0) {
64
- this[MAX] = Infinity
65
- }
66
-
67
- var lc = options.length || naiveLength
68
- if (typeof lc !== 'function') {
69
- lc = naiveLength
70
- }
71
- this[LENGTH_CALCULATOR] = lc
72
-
73
- this[ALLOW_STALE] = options.stale || false
74
- this[MAX_AGE] = options.maxAge || 0
75
- this[DISPOSE] = options.dispose
76
- this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false
77
- this.reset()
78
- }
79
-
80
- // resize the cache when the max changes.
81
- Object.defineProperty(LRUCache.prototype, 'max', {
82
- set: function (mL) {
83
- if (!mL || !(typeof mL === 'number') || mL <= 0) {
84
- mL = Infinity
85
- }
86
- this[MAX] = mL
87
- trim(this)
88
- },
89
- get: function () {
90
- return this[MAX]
91
- },
92
- enumerable: true
93
- })
94
-
95
- Object.defineProperty(LRUCache.prototype, 'allowStale', {
96
- set: function (allowStale) {
97
- this[ALLOW_STALE] = !!allowStale
98
- },
99
- get: function () {
100
- return this[ALLOW_STALE]
101
- },
102
- enumerable: true
103
- })
104
-
105
- Object.defineProperty(LRUCache.prototype, 'maxAge', {
106
- set: function (mA) {
107
- if (!mA || !(typeof mA === 'number') || mA < 0) {
108
- mA = 0
109
- }
110
- this[MAX_AGE] = mA
111
- trim(this)
112
- },
113
- get: function () {
114
- return this[MAX_AGE]
115
- },
116
- enumerable: true
117
- })
118
-
119
- // resize the cache when the lengthCalculator changes.
120
- Object.defineProperty(LRUCache.prototype, 'lengthCalculator', {
121
- set: function (lC) {
122
- if (typeof lC !== 'function') {
123
- lC = naiveLength
124
- }
125
- if (lC !== this[LENGTH_CALCULATOR]) {
126
- this[LENGTH_CALCULATOR] = lC
127
- this[LENGTH] = 0
128
- this[LRU_LIST].forEach(function (hit) {
129
- hit.length = this[LENGTH_CALCULATOR](hit.value, hit.key)
130
- this[LENGTH] += hit.length
131
- }, this)
132
- }
133
- trim(this)
134
- },
135
- get: function () { return this[LENGTH_CALCULATOR] },
136
- enumerable: true
137
- })
138
-
139
- Object.defineProperty(LRUCache.prototype, 'length', {
140
- get: function () { return this[LENGTH] },
141
- enumerable: true
142
- })
143
-
144
- Object.defineProperty(LRUCache.prototype, 'itemCount', {
145
- get: function () { return this[LRU_LIST].length },
146
- enumerable: true
147
- })
148
-
149
- LRUCache.prototype.rforEach = function (fn, thisp) {
150
- thisp = thisp || this
151
- for (var walker = this[LRU_LIST].tail; walker !== null;) {
152
- var prev = walker.prev
153
- forEachStep(this, fn, walker, thisp)
154
- walker = prev
155
- }
156
- }
157
-
158
- function forEachStep (self, fn, node, thisp) {
159
- var hit = node.value
160
- if (isStale(self, hit)) {
161
- del(self, node)
162
- if (!self[ALLOW_STALE]) {
163
- hit = undefined
164
- }
165
- }
166
- if (hit) {
167
- fn.call(thisp, hit.value, hit.key, self)
168
- }
169
- }
170
-
171
- LRUCache.prototype.forEach = function (fn, thisp) {
172
- thisp = thisp || this
173
- for (var walker = this[LRU_LIST].head; walker !== null;) {
174
- var next = walker.next
175
- forEachStep(this, fn, walker, thisp)
176
- walker = next
177
- }
178
- }
179
-
180
- LRUCache.prototype.keys = function () {
181
- return this[LRU_LIST].toArray().map(function (k) {
182
- return k.key
183
- }, this)
184
- }
185
-
186
- LRUCache.prototype.values = function () {
187
- return this[LRU_LIST].toArray().map(function (k) {
188
- return k.value
189
- }, this)
190
- }
191
-
192
- LRUCache.prototype.reset = function () {
193
- if (this[DISPOSE] &&
194
- this[LRU_LIST] &&
195
- this[LRU_LIST].length) {
196
- this[LRU_LIST].forEach(function (hit) {
197
- this[DISPOSE](hit.key, hit.value)
198
- }, this)
199
- }
200
-
201
- this[CACHE] = new Map() // hash of items by key
202
- this[LRU_LIST] = new Yallist() // list of items in order of use recency
203
- this[LENGTH] = 0 // length of items in the list
204
- }
205
-
206
- LRUCache.prototype.dump = function () {
207
- return this[LRU_LIST].map(function (hit) {
208
- if (!isStale(this, hit)) {
209
- return {
210
- k: hit.key,
211
- v: hit.value,
212
- e: hit.now + (hit.maxAge || 0)
213
- }
214
- }
215
- }, this).toArray().filter(function (h) {
216
- return h
217
- })
218
- }
219
-
220
- LRUCache.prototype.dumpLru = function () {
221
- return this[LRU_LIST]
222
- }
223
-
224
- /* istanbul ignore next */
225
- LRUCache.prototype.inspect = function (n, opts) {
226
- var str = 'LRUCache {'
227
- var extras = false
228
-
229
- var as = this[ALLOW_STALE]
230
- if (as) {
231
- str += '\n allowStale: true'
232
- extras = true
233
- }
234
-
235
- var max = this[MAX]
236
- if (max && max !== Infinity) {
237
- if (extras) {
238
- str += ','
239
- }
240
- str += '\n max: ' + util.inspect(max, opts)
241
- extras = true
242
- }
243
-
244
- var maxAge = this[MAX_AGE]
245
- if (maxAge) {
246
- if (extras) {
247
- str += ','
248
- }
249
- str += '\n maxAge: ' + util.inspect(maxAge, opts)
250
- extras = true
251
- }
252
-
253
- var lc = this[LENGTH_CALCULATOR]
254
- if (lc && lc !== naiveLength) {
255
- if (extras) {
256
- str += ','
257
- }
258
- str += '\n length: ' + util.inspect(this[LENGTH], opts)
259
- extras = true
260
- }
261
-
262
- var didFirst = false
263
- this[LRU_LIST].forEach(function (item) {
264
- if (didFirst) {
265
- str += ',\n '
266
- } else {
267
- if (extras) {
268
- str += ',\n'
269
- }
270
- didFirst = true
271
- str += '\n '
272
- }
273
- var key = util.inspect(item.key).split('\n').join('\n ')
274
- var val = { value: item.value }
275
- if (item.maxAge !== maxAge) {
276
- val.maxAge = item.maxAge
277
- }
278
- if (lc !== naiveLength) {
279
- val.length = item.length
280
- }
281
- if (isStale(this, item)) {
282
- val.stale = true
283
- }
284
-
285
- val = util.inspect(val, opts).split('\n').join('\n ')
286
- str += key + ' => ' + val
287
- })
288
-
289
- if (didFirst || extras) {
290
- str += '\n'
291
- }
292
- str += '}'
293
-
294
- return str
295
- }
296
-
297
- LRUCache.prototype.set = function (key, value, maxAge) {
298
- maxAge = maxAge || this[MAX_AGE]
299
-
300
- var now = maxAge ? Date.now() : 0
301
- var len = this[LENGTH_CALCULATOR](value, key)
302
-
303
- if (this[CACHE].has(key)) {
304
- if (len > this[MAX]) {
305
- del(this, this[CACHE].get(key))
306
- return false
307
- }
308
-
309
- var node = this[CACHE].get(key)
310
- var item = node.value
311
-
312
- // dispose of the old one before overwriting
313
- // split out into 2 ifs for better coverage tracking
314
- if (this[DISPOSE]) {
315
- if (!this[NO_DISPOSE_ON_SET]) {
316
- this[DISPOSE](key, item.value)
317
- }
318
- }
319
-
320
- item.now = now
321
- item.maxAge = maxAge
322
- item.value = value
323
- this[LENGTH] += len - item.length
324
- item.length = len
325
- this.get(key)
326
- trim(this)
327
- return true
328
- }
329
-
330
- var hit = new Entry(key, value, len, now, maxAge)
331
-
332
- // oversized objects fall out of cache automatically.
333
- if (hit.length > this[MAX]) {
334
- if (this[DISPOSE]) {
335
- this[DISPOSE](key, value)
336
- }
337
- return false
338
- }
339
-
340
- this[LENGTH] += hit.length
341
- this[LRU_LIST].unshift(hit)
342
- this[CACHE].set(key, this[LRU_LIST].head)
343
- trim(this)
344
- return true
345
- }
346
-
347
- LRUCache.prototype.has = function (key) {
348
- if (!this[CACHE].has(key)) return false
349
- var hit = this[CACHE].get(key).value
350
- if (isStale(this, hit)) {
351
- return false
352
- }
353
- return true
354
- }
355
-
356
- LRUCache.prototype.get = function (key) {
357
- return get(this, key, true)
358
- }
359
-
360
- LRUCache.prototype.peek = function (key) {
361
- return get(this, key, false)
362
- }
363
-
364
- LRUCache.prototype.pop = function () {
365
- var node = this[LRU_LIST].tail
366
- if (!node) return null
367
- del(this, node)
368
- return node.value
369
- }
370
-
371
- LRUCache.prototype.del = function (key) {
372
- del(this, this[CACHE].get(key))
373
- }
374
-
375
- LRUCache.prototype.load = function (arr) {
376
- // reset the cache
377
- this.reset()
378
-
379
- var now = Date.now()
380
- // A previous serialized cache has the most recent items first
381
- for (var l = arr.length - 1; l >= 0; l--) {
382
- var hit = arr[l]
383
- var expiresAt = hit.e || 0
384
- if (expiresAt === 0) {
385
- // the item was created without expiration in a non aged cache
386
- this.set(hit.k, hit.v)
387
- } else {
388
- var maxAge = expiresAt - now
389
- // dont add already expired items
390
- if (maxAge > 0) {
391
- this.set(hit.k, hit.v, maxAge)
392
- }
393
- }
394
- }
395
- }
396
-
397
- LRUCache.prototype.prune = function () {
398
- var self = this
399
- this[CACHE].forEach(function (value, key) {
400
- get(self, key, false)
401
- })
402
- }
403
-
404
- function get (self, key, doUse) {
405
- var node = self[CACHE].get(key)
406
- if (node) {
407
- var hit = node.value
408
- if (isStale(self, hit)) {
409
- del(self, node)
410
- if (!self[ALLOW_STALE]) hit = undefined
411
- } else {
412
- if (doUse) {
413
- self[LRU_LIST].unshiftNode(node)
414
- }
415
- }
416
- if (hit) hit = hit.value
417
- }
418
- return hit
419
- }
420
-
421
- function isStale (self, hit) {
422
- if (!hit || (!hit.maxAge && !self[MAX_AGE])) {
423
- return false
424
- }
425
- var stale = false
426
- var diff = Date.now() - hit.now
427
- if (hit.maxAge) {
428
- stale = diff > hit.maxAge
429
- } else {
430
- stale = self[MAX_AGE] && (diff > self[MAX_AGE])
431
- }
432
- return stale
433
- }
434
-
435
- function trim (self) {
436
- if (self[LENGTH] > self[MAX]) {
437
- for (var walker = self[LRU_LIST].tail;
438
- self[LENGTH] > self[MAX] && walker !== null;) {
439
- // We know that we're about to delete this one, and also
440
- // what the next least recently used key will be, so just
441
- // go ahead and set it now.
442
- var prev = walker.prev
443
- del(self, walker)
444
- walker = prev
445
- }
446
- }
447
- }
448
-
449
- function del (self, node) {
450
- if (node) {
451
- var hit = node.value
452
- if (self[DISPOSE]) {
453
- self[DISPOSE](hit.key, hit.value)
454
- }
455
- self[LENGTH] -= hit.length
456
- self[CACHE].delete(hit.key)
457
- self[LRU_LIST].removeNode(node)
458
- }
459
- }
460
-
461
- // classy, since V8 prefers predictable objects.
462
- function Entry (key, value, length, now, maxAge) {
463
- this.key = key
464
- this.value = value
465
- this.length = length
466
- this.now = now
467
- this.maxAge = maxAge || 0
468
- }