@withjoy/limiter 0.1.3 → 0.1.4-test
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 +4 -0
- package/limitd-redis/LICENSE +21 -0
- package/limitd-redis/README.md +183 -0
- package/limitd-redis/docker-compose.yml +11 -0
- package/limitd-redis/index.js +2 -0
- package/limitd-redis/lib/cb.js +45 -0
- package/limitd-redis/lib/client.js +135 -0
- package/limitd-redis/lib/db.js +501 -0
- package/limitd-redis/lib/db_ping.js +106 -0
- package/limitd-redis/lib/put.lua +31 -0
- package/limitd-redis/lib/take.lua +48 -0
- package/limitd-redis/lib/utils.js +116 -0
- package/limitd-redis/lib/validation.js +64 -0
- package/limitd-redis/node_modules/lru-cache/LICENSE +15 -0
- package/limitd-redis/node_modules/lru-cache/README.md +158 -0
- package/limitd-redis/node_modules/lru-cache/index.js +468 -0
- package/limitd-redis/node_modules/lru-cache/package.json +74 -0
- package/limitd-redis/node_modules/ms/index.js +162 -0
- package/limitd-redis/node_modules/ms/license.md +21 -0
- package/limitd-redis/node_modules/ms/package.json +73 -0
- package/limitd-redis/node_modules/ms/readme.md +59 -0
- package/limitd-redis/node_modules/yallist/LICENSE +15 -0
- package/limitd-redis/node_modules/yallist/README.md +204 -0
- package/limitd-redis/node_modules/yallist/iterator.js +7 -0
- package/limitd-redis/node_modules/yallist/package.json +65 -0
- package/limitd-redis/node_modules/yallist/yallist.js +370 -0
- package/limitd-redis/opslevel.yml +6 -0
- package/limitd-redis/package-lock.json +3484 -0
- package/limitd-redis/package.json +31 -0
- package/limitd-redis/test/cb.tests.js +124 -0
- package/limitd-redis/test/client.tests.js +194 -0
- package/limitd-redis/test/db.tests.js +1318 -0
- package/limitd-redis/test/validation.tests.js +124 -0
- package/limiter.js +57 -1
- package/package.json +3 -2
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const ms = require('ms');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const LRU = require('lru-cache');
|
|
4
|
+
|
|
5
|
+
const INTERVAL_TO_MS = {
|
|
6
|
+
'per_second': ms('1s'),
|
|
7
|
+
'per_minute': ms('1m'),
|
|
8
|
+
'per_hour': ms('1h'),
|
|
9
|
+
'per_day': ms('1d')
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const INTERVAL_SHORTCUTS = Object.keys(INTERVAL_TO_MS);
|
|
13
|
+
|
|
14
|
+
function normalizeTemporals(params) {
|
|
15
|
+
const type = _.pick(params, [
|
|
16
|
+
'per_interval',
|
|
17
|
+
'interval',
|
|
18
|
+
'size',
|
|
19
|
+
'unlimited',
|
|
20
|
+
'skip_n_calls'
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
INTERVAL_SHORTCUTS.forEach(intervalShortcut => {
|
|
24
|
+
if (!params[intervalShortcut]) { return; }
|
|
25
|
+
type.interval = INTERVAL_TO_MS[intervalShortcut];
|
|
26
|
+
type.per_interval = params[intervalShortcut];
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (typeof type.size === 'undefined') {
|
|
30
|
+
type.size = type.per_interval;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (type.per_interval) {
|
|
34
|
+
type.ttl = ((type.size * type.interval) / type.per_interval) / 1000;
|
|
35
|
+
type.ms_per_interval = type.per_interval / type.interval;
|
|
36
|
+
type.drip_interval = type.interval / type.per_interval;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return type;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function normalizeType(params) {
|
|
43
|
+
const type = normalizeTemporals(params);
|
|
44
|
+
|
|
45
|
+
type.overridesMatch = {};
|
|
46
|
+
type.overrides = _.reduce(params.overrides || params.override, (result, overrideDef, name) => {
|
|
47
|
+
const override = normalizeTemporals(overrideDef);
|
|
48
|
+
override.name = name;
|
|
49
|
+
if (overrideDef.until && !(overrideDef.until instanceof Date)) {
|
|
50
|
+
overrideDef.until = new Date(overrideDef.until);
|
|
51
|
+
}
|
|
52
|
+
override.until = overrideDef.until;
|
|
53
|
+
if (overrideDef.match) {
|
|
54
|
+
// TODO: Allow more flags
|
|
55
|
+
override.match = new RegExp(overrideDef.match, 'i');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!override.until || override.until >= new Date()) {
|
|
59
|
+
if (override.match) {
|
|
60
|
+
type.overridesMatch[name] = override;
|
|
61
|
+
} else {
|
|
62
|
+
result[name] = override;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result;
|
|
67
|
+
}, {});
|
|
68
|
+
|
|
69
|
+
if (Object.keys(type.overridesMatch).length > 0) {
|
|
70
|
+
type.overridesCache = new LRU({ max: 50 });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return type;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Load the buckets configuration.
|
|
78
|
+
*
|
|
79
|
+
* @param {Object.<string, type>} bucketsConfig The buckets configuration.
|
|
80
|
+
* @memberof LimitDB
|
|
81
|
+
*/
|
|
82
|
+
function buildBuckets(bucketsConfig) {
|
|
83
|
+
return _.reduce(bucketsConfig, (result, bucket, name) => {
|
|
84
|
+
result[name] = normalizeType(bucket);
|
|
85
|
+
return result;
|
|
86
|
+
}, {});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function buildBucket(bucket) {
|
|
90
|
+
return normalizeType(bucket);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function functionOrFalse(fun) {
|
|
94
|
+
return !!(fun && fun.constructor && fun.call && fun.apply)
|
|
95
|
+
? fun
|
|
96
|
+
: false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function randomBetween(min, max) {
|
|
100
|
+
if (min > max) {
|
|
101
|
+
let tmp = max;
|
|
102
|
+
max = min;
|
|
103
|
+
min = tmp;
|
|
104
|
+
}
|
|
105
|
+
return Math.random() * (max-min) + min;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
buildBuckets,
|
|
110
|
+
buildBucket,
|
|
111
|
+
INTERVAL_SHORTCUTS,
|
|
112
|
+
normalizeTemporals,
|
|
113
|
+
normalizeType,
|
|
114
|
+
functionOrFalse,
|
|
115
|
+
randomBetween
|
|
116
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const { INTERVAL_SHORTCUTS } = require('./utils');
|
|
2
|
+
|
|
3
|
+
class LimitdRedisValidationError extends Error {
|
|
4
|
+
constructor(msg, extra) {
|
|
5
|
+
super();
|
|
6
|
+
this.name = this.constructor.name;
|
|
7
|
+
this.message = msg;
|
|
8
|
+
Error.captureStackTrace(this, this.constructor);
|
|
9
|
+
if (extra) {
|
|
10
|
+
this.extra = extra;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function validateParams(params, buckets) {
|
|
16
|
+
if (typeof params !== 'object') {
|
|
17
|
+
return new LimitdRedisValidationError('params are required', { code: 101 });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof params.type !== 'string') {
|
|
21
|
+
return new LimitdRedisValidationError('type is required', { code: 102 });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (typeof buckets[params.type] === 'undefined') {
|
|
25
|
+
return new LimitdRedisValidationError(`undefined bucket type ${params.type}`, { code: 103 });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof params.key !== 'string') {
|
|
29
|
+
return new LimitdRedisValidationError('key is required', { code: 104 });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (typeof params.configOverride !== 'undefined') {
|
|
33
|
+
try {
|
|
34
|
+
validateOverride(params.configOverride);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function validateOverride(configOverride) {
|
|
42
|
+
if (typeof configOverride !== 'object') {
|
|
43
|
+
throw new LimitdRedisValidationError('configuration overrides must be an object', { code: 105 });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// If size is provided, nothing more is strictly required
|
|
47
|
+
// (as in the case of static bucket configurations)
|
|
48
|
+
if (typeof configOverride.size === 'number') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const interval = Object.keys(configOverride)
|
|
53
|
+
.find(key => INTERVAL_SHORTCUTS.indexOf(key) > -1);
|
|
54
|
+
|
|
55
|
+
// If size is not provided, we *must* have a interval specified
|
|
56
|
+
if (typeof interval === 'undefined') {
|
|
57
|
+
throw new LimitdRedisValidationError('configuration overrides must provide either a size or interval', { code: 106 });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
validateParams,
|
|
63
|
+
LimitdRedisValidationError,
|
|
64
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
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.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# lru cache
|
|
2
|
+
|
|
3
|
+
A cache object that deletes the least-recently-used items.
|
|
4
|
+
|
|
5
|
+
[](https://travis-ci.org/isaacs/node-lru-cache) [](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
|