Skip to content

Commit

Permalink
fix: after should show the same ttl as the rate limit headers (#394)
Browse files Browse the repository at this point in the history
* fix: after should show the same ttl as the rate limit headers

* use direct functions
  • Loading branch information
gurgunday authored Nov 17, 2024
1 parent 09f9648 commit 8b2b54d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 20 deletions.
18 changes: 4 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
const ms = require('@lukeed/ms')
const { parse, format } = require('@lukeed/ms')

const LocalStore = require('./store/LocalStore')
const RedisStore = require('./store/RedisStore')
Expand Down Expand Up @@ -75,7 +75,7 @@ async function fastifyRateLimit (fastify, settings) {
if (Number.isFinite(settings.timeWindow) && settings.timeWindow >= 0) {
globalParams.timeWindow = Math.trunc(settings.timeWindow)
} else if (typeof settings.timeWindow === 'string') {
globalParams.timeWindow = ms.parse(settings.timeWindow)
globalParams.timeWindow = parse(settings.timeWindow)
} else if (
typeof settings.timeWindow === 'function'
) {
Expand Down Expand Up @@ -162,7 +162,7 @@ function mergeParams (...params) {
if (Number.isFinite(result.timeWindow) && result.timeWindow >= 0) {
result.timeWindow = Math.trunc(result.timeWindow)
} else if (typeof result.timeWindow === 'string') {
result.timeWindow = ms.parse(result.timeWindow)
result.timeWindow = parse(result.timeWindow)
} else if (typeof result.timeWindow !== 'function') {
result.timeWindow = defaultTimeWindow
}
Expand Down Expand Up @@ -201,11 +201,6 @@ function addRouteRateHook (pluginComponent, params, routeOptions) {
function rateLimitRequestHandler (pluginComponent, params) {
const { rateLimitRan, store } = pluginComponent

let timeWindowString
if (typeof params.timeWindow === 'number') {
timeWindowString = ms.format(params.timeWindow, true)
}

return async (req, res) => {
if (req[rateLimitRan]) {
return
Expand All @@ -216,7 +211,6 @@ function rateLimitRequestHandler (pluginComponent, params) {
// Retrieve the key from the generator (the global one or the one defined in the endpoint)
let key = await params.keyGenerator(req)
const groupId = req.routeOptions.config?.rateLimit?.groupId

if (groupId) {
key += groupId
}
Expand Down Expand Up @@ -249,10 +243,6 @@ function rateLimitRequestHandler (pluginComponent, params) {
current = res.current
ttl = res.ttl
ttlInSeconds = Math.ceil(res.ttl / 1000)

if (params.exponentialBackoff) {
timeWindowString = ms.format(ttl, true)
}
} catch (err) {
if (!params.skipOnError) {
throw err
Expand Down Expand Up @@ -281,7 +271,7 @@ function rateLimitRequestHandler (pluginComponent, params) {
ban: false,
max,
ttl,
after: timeWindowString ?? ms.format(timeWindow, true)
after: format(ttlInSeconds * 1000, true)
}

if (params.ban !== -1 && current - max > params.ban) {
Expand Down
4 changes: 2 additions & 2 deletions test/exponential-backoff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('Exponential Backoff', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 500 ms'
message: 'Rate limit exceeded, retry in 1 second'
},
JSON.parse(res3.payload)
)
Expand Down Expand Up @@ -109,7 +109,7 @@ test('Global Exponential Backoff', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 500 ms'
message: 'Rate limit exceeded, retry in 1 second'
},
JSON.parse(res.payload)
)
Expand Down
2 changes: 1 addition & 1 deletion test/global-rate-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ test('With CustomStore', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 10 seconds'
message: 'Rate limit exceeded, retry in 7 seconds'
},
JSON.parse(res.payload)
)
Expand Down
4 changes: 2 additions & 2 deletions test/group-rate-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test('No groupId provided', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 500 ms'
message: 'Rate limit exceeded, retry in 1 second'
},
JSON.parse(res.payload)
)
Expand Down Expand Up @@ -174,7 +174,7 @@ test('With multiple routes and custom groupId', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 500 ms'
message: 'Rate limit exceeded, retry in 1 second'
},
JSON.parse(res.payload)
)
Expand Down
2 changes: 1 addition & 1 deletion test/route-rate-limit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ test('With CustomStore', async (t) => {
{
statusCode: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded, retry in 10 seconds'
message: 'Rate limit exceeded, retry in 7 seconds'
},
JSON.parse(res.payload)
)
Expand Down

0 comments on commit 8b2b54d

Please sign in to comment.