This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (96 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
// **Github:** https://github.com/thunks/thunk-ratelimiter
//
// **License:** MIT
/**
* inspire by https://github.com/tj/node-ratelimiter
*
*/
const fs = require('fs')
const path = require('path')
const thunk = require('thunks')()
const redis = require('thunk-redis')
const limitScript = fs.readFileSync(path.join(__dirname, 'ratelimiter.lua'), { encoding: 'utf8' })
const slice = Array.prototype.slice
module.exports = Limiter
/**
* Initialize a new limiter with `options`:
*
* - `db` redis connection instance
* - `prefix` redis key namespace
* - `max` max requests within `duration` [2500]
* - `duration` of limit in milliseconds [3600000]
*
* @param {Object} options
* @api public
*/
function Limiter (options) {
options = options || {}
this.prefix = options.prefix || 'LIMIT'
this.max = options.max >= 1 ? Math.floor(options.max) : 2500
this.duration = options.duration >= 100 ? Math.floor(options.duration) : 3600000
}
Limiter.prototype.connect = function (redisClient) {
if (this.redis) return this
if (redisClient) {
this.redis = redisClient
if (this.redis.defineCommand) {
this.redis.defineCommand('ratelimiter', {
numberOfKeys: 2,
lua: limitScript
})
}
} else {
this.redis = redis.createClient.apply(null, arguments)
}
return this
}
/**
* get limit object with `id`
*
* call style: (id, max, duration, max, duration, ...)
* @param {String} `id` {String} identifier being limited
* @param {Number} `max` {Number} max requests within `duration`, default to `this.max`
* @param {Number} `duration` {Number} of limit in milliseconds, default to `this.duration`
*
* or call style: ([id, max, duration, max, duration, ...])
* @api public
*/
Limiter.prototype.get = async function (id) {
const args = slice.call(Array.isArray(id) ? id : arguments)
id = this.prefix + ':' + args[0]
if (args[1] == null) args[1] = this.max
if (args[2] == null) args[2] = this.duration
// check pairs of `max, duration`
for (let i = 1, len = args.length; i < len; i += 2) {
if (!(args[i] > 0 && args[i + 1] > 0)) {
return Promise.reject(new Error(args[i] + ' or ' + args[i + 1] + ' is invalid'))
}
}
args[0] = Date.now()
// if ioredis client or other ...?
if (this.redis.ratelimiter) {
args.unshift(`{${id}}:S`, `${id}`)
const result = await this.redis.ratelimiter(args)
return new Limit(...result)
}
// transfor args to [limitScript, 2, id1, id2, timestamp, max, duration, max, duration, ...]
args.unshift(limitScript, 2, `{${id}}:S`, `${id}`)
return thunk.promise(this.redis.evalauto(args)).then(function (res) {
return new Limit(res[0], res[1], res[2], res[3])
})
}
/**
* remove limit object by `id`
*
* @api public
*/
Limiter.prototype.remove = function (id) {
return thunk.promise(this.redis.del(this.prefix + ':' + id))
}
function Limit (remaining, total, duration, reset) {
this.remaining = remaining
this.duration = duration
this.reset = reset
this.total = total
}