forked from fastify/fastify-rate-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (173 loc) · 7.03 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
const fp = require('fastify-plugin')
const FJS = require('fast-json-stringify')
const ms = require('ms')
const LocalStore = require('./store/LocalStore')
const RedisStore = require('./store/RedisStore')
const serializeError = FJS({
type: 'object',
properties: {
statusCode: { type: 'number' },
error: { type: 'string' },
message: { type: 'string' }
}
})
function rateLimitPlugin (fastify, settings, next) {
// create the object that will hold the "main" settings that can be shared during the build
// 'global' will define, if the rate limit should be apply by default on all route. default : true
const globalParams = {
global: (typeof settings.global === 'boolean') ? settings.global : true
}
globalParams.addHeaders = Object.assign({
'x-ratelimit-limit': true,
'x-ratelimit-remaining': true,
'x-ratelimit-reset': true,
'retry-after': true
}, settings.addHeaders)
// define the global maximum of request allowed
globalParams.max = (typeof settings.max === 'number' || typeof settings.max === 'function')
? settings.max
: 1000
// define the global Time Window
globalParams.timeWindow = typeof settings.timeWindow === 'string'
? ms(settings.timeWindow)
: typeof settings.timeWindow === 'number'
? settings.timeWindow
: 1000 * 60
globalParams.whitelist = settings.whitelist || null
globalParams.ban = settings.ban || null
// define the name of the app component. Related to redis, it will be use as a part of the keyname define in redis.
const pluginComponent = {
whitelist: globalParams.whitelist
}
if (settings.store) {
const Store = settings.store
pluginComponent.store = new Store(globalParams)
} else {
if (settings.redis) {
pluginComponent.store = new RedisStore(settings.redis, 'fastify-rate-limit-', globalParams.timeWindow)
} else {
pluginComponent.store = new LocalStore(globalParams.timeWindow, settings.cache, fastify)
}
}
globalParams.keyGenerator = typeof settings.keyGenerator === 'function'
? settings.keyGenerator
: (req) => req.raw.ip
globalParams.errorResponseBuilder = (req, context) => ({ statusCode: context.statusCode, error: 'Too Many Requests', message: `Rate limit exceeded, retry in ${context.after}` })
globalParams.isCustomErrorMessage = false
// define if error message was overwritten with a custom error response callback
if (typeof settings.errorResponseBuilder === 'function') {
globalParams.errorResponseBuilder = settings.errorResponseBuilder
globalParams.isCustomErrorMessage = true
}
// onRoute add the preHandler rate-limit function if needed
fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.config && typeof routeOptions.config.rateLimit !== 'undefined') {
if (typeof routeOptions.config.rateLimit === 'object') {
const current = Object.create(pluginComponent)
const mergedRateLimitParams = makeParams(routeOptions.config.rateLimit)
if (!routeOptions.config.rateLimit.timeWindow) {
// load the global timewindow if it is missing from the route config
routeOptions.config.rateLimit.timeWindow = mergedRateLimitParams.timeWindow
}
current.store = pluginComponent.store.child(routeOptions)
// if the current endpoint have a custom rateLimit configuration ...
buildRouteRate(current, mergedRateLimitParams, routeOptions)
} else if (routeOptions.config.rateLimit === false) {
// don't apply any rate-limit
} else {
throw new Error('Unknown value for route rate-limit configuration')
}
} else if (globalParams.global) {
// if the plugin is set globally ( meaning that all the route will be 'rate limited' )
// As the endpoint, does not have a custom rateLimit configuration, use the global one.
buildRouteRate(pluginComponent, globalParams, routeOptions)
}
})
// Merge the parameters of a route with the global ones
function makeParams (routeParams) {
const result = Object.assign({}, globalParams, routeParams)
if (typeof result.timeWindow === 'string') {
result.timeWindow = ms(result.timeWindow)
}
return result
}
next()
}
function buildRouteRate (pluginComponent, params, routeOptions) {
const after = ms(params.timeWindow, { long: true })
if (Array.isArray(routeOptions.preHandler)) {
routeOptions.preHandler.push(preHandler)
} else if (typeof routeOptions.preHandler === 'function') {
routeOptions.preHandler = [routeOptions.preHandler, preHandler]
} else {
routeOptions.preHandler = [preHandler]
}
// PreHandler function that will be use for current endpoint been processed
function preHandler (req, res, next) {
// We retrieve the key from the generator. (can be the global one, or the one define in the endpoint)
const key = params.keyGenerator(req)
// whitelist doesn't apply any rate limit
if (pluginComponent.whitelist) {
if (typeof pluginComponent.whitelist === 'function') {
if (pluginComponent.whitelist(req, key)) {
next()
return
}
} else if (pluginComponent.whitelist.indexOf(key) > -1) {
next()
return
}
}
// As the key is not whitelist in redis/lru, then we increment the rate-limit of the current request and we call the function "onIncr"
pluginComponent.store.incr(key, onIncr)
function onIncr (err, { current, ttl }) {
if (err && params.skipOnError === false) {
return next(err)
}
const maximum = getMax()
if (current <= maximum) {
res.header('x-ratelimit-limit', maximum)
.header('x-ratelimit-remaining', maximum - current)
.header('x-ratelimit-reset', Math.floor(ttl / 1000))
if (typeof params.onExceeding === 'function') {
params.onExceeding(req)
}
next()
} else {
if (typeof params.onExceeded === 'function') {
params.onExceeded(req)
}
if (!params.isCustomErrorMessage) {
res.type('application/json').serializer(serializeError)
}
if (params.addHeaders['x-ratelimit-limit']) { res.header('x-ratelimit-limit', maximum) }
if (params.addHeaders['x-ratelimit-remaining']) { res.header('x-ratelimit-remaining', 0) }
if (params.addHeaders['x-ratelimit-reset']) { res.header('x-ratelimit-reset', Math.floor(ttl / 1000)) }
if (params.addHeaders['retry-after']) { res.header('retry-after', params.timeWindow) }
const code = params.ban && current - maximum > params.ban ? 403 : 429
res.code(code)
const respCtx = {
statusCode: code,
after,
max: maximum
}
if (code === 403) {
respCtx.ban = true
}
res.send(params.errorResponseBuilder(req, respCtx))
}
function getMax () {
if (typeof params.max === 'number') {
return params.max
} else {
return params.max(req, key)
}
}
}
}
}
module.exports = fp(rateLimitPlugin, {
fastify: '>=2.x',
name: 'fastify-rate-limit'
})