Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const rateLimiter = chooseRateLimiter(request)
let scope = null
try {
scope = rateLimiter._keyPrefix
await rateLimiter.consume(address)
scope = globalRateLimiter._keyPrefix
await globalRateLimiter.consume('all')
return h.continue
} catch (err) {
let error
if (err instanceof Error) {
// If some Redis error and `insuranceLimiter` is not set
error = boom.internal('Try later')
} else {
// Not enough points to consume
error = boom.tooManyRequests('Rate limit exceeded: ' + scope)
error.output.headers['Retry-After'] = Math.round(err.msBeforeNext / 1000) || 1
}
return error
}
})
}
options.namespace + ':' + realIP(request) + ':' + (request.route.id || request.route.path),
routeLimit.limit,
routeLimit.duration
)
request.plugins.ratelimit = {
limit: rateLimit.limit,
remaining: rateLimit.remaining - 1,
reset: rateLimit.reset
}
if (rateLimit.remaining > 0) {
return h.continue
}
const error = Boom.tooManyRequests('RATE_LIMIT_EXCEEDED')
setHeaders(error.output.headers, request.plugins.ratelimit, options.XHeaders)
error.reformat()
return error
}