|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const rateLimit = require('express-rate-limit'); |
| 4 | +const Tapster = require('@extensionengine/tapster'); |
| 5 | +const { provider, ...options } = require('../../../config/server').store; |
4 | 6 |
|
5 | 7 | const DEFAULT_WINDOW_MS = 15 * 60 * 1000; // every 15 minutes |
6 | 8 |
|
7 | | -function requestLimiter({ max = 10, windowMs = DEFAULT_WINDOW_MS, ...opts } = {}) { |
8 | | - return rateLimit({ max, windowMs, ...opts }); |
| 9 | +// Store must be implemented using the following interface: |
| 10 | +// https://github.com/nfriedly/express-rate-limit/blob/master/README.md#store |
| 11 | +class Store { |
| 12 | + constructor() { |
| 13 | + this.cache = new Tapster({ |
| 14 | + ...options[provider], |
| 15 | + store: provider, |
| 16 | + namespace: 'request-limiter' |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + async incr(key, cb) { |
| 21 | + const initialState = { hits: 0 }; |
| 22 | + const { hits, ...record } = await this.cache.has(key) |
| 23 | + ? await this.cache.get(key) |
| 24 | + : initialState; |
| 25 | + await this.cache.set(key, { ...record, hits: hits + 1 }); |
| 26 | + cb(null, hits); |
| 27 | + } |
| 28 | + |
| 29 | + async decrement(key) { |
| 30 | + const { hits, ...record } = await this.cache.get(key) || {}; |
| 31 | + if (!hits) return; |
| 32 | + return this.cache.set(key, { ...record, hits: hits - 1 }); |
| 33 | + } |
| 34 | + |
| 35 | + resetKey(key) { |
| 36 | + return this.cache.delete(key); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const defaultStore = new Store(); |
| 41 | + |
| 42 | +function requestLimiter({ |
| 43 | + max = 10, |
| 44 | + windowMs = DEFAULT_WINDOW_MS, |
| 45 | + store = defaultStore, |
| 46 | + ...opts |
| 47 | +} = {}) { |
| 48 | + return rateLimit({ max, windowMs, store, ...opts }); |
9 | 49 | } |
10 | 50 |
|
11 | 51 | module.exports = { requestLimiter }; |
0 commit comments