@withjoy/limiter 0.1.0 → 0.1.1
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 +25 -4
- package/limiter.js +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
# limiter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A wrapper for limitd-redis.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Why ?
|
|
4
7
|
|
|
5
8
|
Provides utilities and rollback functionality for our clients.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
This module was originally built to work with `limitd`.
|
|
11
|
+
The prior solution -- and most of the documentation surrounding our standard rate-limiting Buckets --
|
|
12
|
+
can be found in [joylife/limitd](https://bitbucket.org/joylife/limitd/src/master/).
|
|
13
|
+
That solution is now **deprecated**.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Buckets
|
|
17
|
+
|
|
18
|
+
Each client must define the set of rate-limit Buckets that intends to use.
|
|
19
|
+
|
|
20
|
+
The set of `defaultBuckets` defines the standard set of ones that we've historically determined that we need.
|
|
21
|
+
[This list](https://github.com/joylifeinc/limiter/blob/4183c8ca0f5db7392846dc291e481dd307f985f7/limiter.js#L5-L16) may be old, but that's the general idea.
|
|
22
|
+
Your client can use any subset of these Buckets, but when it does, the Best Practice is to use the exact definition(s) here.
|
|
23
|
+
|
|
24
|
+
However, for flexibility reasons, this is not strictly enforced.
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Testing:
|
|
8
28
|
|
|
9
29
|
`npm run test` will run offline tests using a mock.
|
|
10
30
|
`npm run sanity-check` will run against a limitd service configured to run with the test buckets.
|
|
11
31
|
|
|
32
|
+
|
|
12
33
|
## Quick Test
|
|
34
|
+
|
|
13
35
|
```bash
|
|
14
36
|
node tests/performTestWithTestPerMinute.js
|
|
15
|
-
|
|
16
37
|
```
|
package/limiter.js
CHANGED
|
@@ -38,6 +38,9 @@ class Operation {
|
|
|
38
38
|
let connection = null;
|
|
39
39
|
|
|
40
40
|
class Limiter {
|
|
41
|
+
// static defaultBuckets;
|
|
42
|
+
// static pickDefaultBuckets(keys) { ... };
|
|
43
|
+
|
|
41
44
|
constructor(config) {
|
|
42
45
|
this._console = config.console;
|
|
43
46
|
this._config = config;
|
|
@@ -163,4 +166,19 @@ class Limiter {
|
|
|
163
166
|
}
|
|
164
167
|
}
|
|
165
168
|
|
|
169
|
+
// 'Class static' syntax in Node 10 is not mature
|
|
170
|
+
// and we don't want to upgrade Node at this point due to backwards compat
|
|
171
|
+
Limiter.defaultBuckets = defaultBuckets;
|
|
172
|
+
Limiter.pickDefaultBuckets = function pickDefaultBuckets(keyArray) {
|
|
173
|
+
const buckets = {};
|
|
174
|
+
for (const key of keyArray) {
|
|
175
|
+
const bucket = defaultBuckets[key];
|
|
176
|
+
if (! bucket) {
|
|
177
|
+
throw new Error(`Limiter.pickDefaultBuckets: no such bucket; "${key}"`);
|
|
178
|
+
}
|
|
179
|
+
buckets[key] = defaultBuckets[key];
|
|
180
|
+
}
|
|
181
|
+
return buckets;
|
|
182
|
+
}
|
|
183
|
+
|
|
166
184
|
module.exports = Limiter;
|