forked from ded/express-limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (88 loc) · 3.75 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
module.exports = function (app, db, clientType) {
if (clientType === 'mongodb') {
db.ensureIndex({ 'reset': 1 }, { 'expireAfterSeconds': 0 })
}
return function (opts) {
var middleware = function (req, res, next) {
if (opts.whitelist && opts.whitelist(req)) return next()
opts.lookup = Array.isArray(opts.lookup) ? opts.lookup : [opts.lookup]
opts.onRateLimited = typeof opts.onRateLimited === 'function' ? opts.onRateLimited : function (req, res, next) {
res.status(429).send('Rate limit exceeded')
}
var lookups = opts.lookup.map(function (item) {
return item + ':' + item.split('.').reduce(function (prev, cur) {
return prev[cur]
}, req)
}).join(':')
var path = opts.path || req.path
var method = (opts.method || req.method).toLowerCase()
var key = 'ratelimit:' + path + ':' + method + ':' + lookups
var clientToUse = clientType || 'redis';
if (clientToUse === 'redis') {
db.get(key, function (err, limit) {
if (err && opts.ignoreErrors) return next()
var now = Date.now()
limit = limit ? JSON.parse(limit) : {
total: opts.total,
remaining: opts.total,
reset: now + opts.expire
}
if (now > limit.reset) {
limit.reset = now + opts.expire
limit.remaining = opts.total
}
// do not allow negative remaining
limit.remaining = Math.max(Number(limit.remaining) - 1, -1)
db.set(key, JSON.stringify(limit), 'PX', opts.expire, function (e) {
if (!opts.skipHeaders) {
res.set('X-RateLimit-Limit', limit.total)
res.set('X-RateLimit-Reset', Math.ceil(limit.reset / 1000)) // UTC epoch seconds
res.set('X-RateLimit-Remaining', Math.max(limit.remaining, 0))
}
if (limit.remaining >= 0) return next()
var after = (limit.reset - Date.now()) / 1000
if (!opts.skipHeaders) res.set('Retry-After', after)
opts.onRateLimited(req, res, next)
})
})
} else if (clientToUse === 'mongodb') {
db.findOne({ key: key }, function (err, limit) {
if (err && opts.ignoreErrors) return next()
var now = Date.now()
limit = limit ? limit : {
total: opts.total,
remaining: opts.total,
reset: new Date(now + opts.expire)
}
if (now > limit.reset) {
limit.reset = new Date(now + opts.expire)
limit.remaining = opts.total
}
// do not allow negative remaining
limit.remaining = Math.max(Number(limit.remaining) - 1, -1)
db.update({ key: key }, { $set: { total: limit.total, remaining: limit.remaining, reset: limit.reset } }, { upsert: true }, function(e) {
if (!opts.skipHeaders) {
res.set('X-RateLimit-Limit', limit.total)
res.set('X-RateLimit-Reset', Math.ceil(limit.reset / 1000)) // UTC epoch seconds
res.set('X-RateLimit-Remaining', Math.max(limit.remaining, 0))
}
if (limit.remaining >= 0) return next()
var after = (limit.reset - Date.now()) / 1000
if (!opts.skipHeaders) res.set('Retry-After', after)
opts.onRateLimited(req, res, next)
})
})
}
}
if (typeof(opts.lookup) === 'function') {
var callableLookup = opts.lookup;
middleware = function (middleware, req, res, next) {
return callableLookup(req, res, opts, function () {
return middleware(req, res, next)
})
}.bind(this, middleware)
}
if (opts.method && opts.path) app[opts.method](opts.path, middleware)
return middleware
}
}