From 8b2b54d3d30c58418e2698397d310ac95a9f43df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 17 Nov 2024 09:55:13 +0100 Subject: [PATCH] fix: `after` should show the same ttl as the rate limit headers (#394) * fix: after should show the same ttl as the rate limit headers * use direct functions --- index.js | 18 ++++-------------- test/exponential-backoff.test.js | 4 ++-- test/global-rate-limit.test.js | 2 +- test/group-rate-limit.test.js | 4 ++-- test/route-rate-limit.test.js | 2 +- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 7d14ec5..9f51856 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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' ) { @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -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) { diff --git a/test/exponential-backoff.test.js b/test/exponential-backoff.test.js index 185b608..3073b27 100644 --- a/test/exponential-backoff.test.js +++ b/test/exponential-backoff.test.js @@ -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) ) @@ -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) ) diff --git a/test/global-rate-limit.test.js b/test/global-rate-limit.test.js index 7509e1b..be8f045 100644 --- a/test/global-rate-limit.test.js +++ b/test/global-rate-limit.test.js @@ -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) ) diff --git a/test/group-rate-limit.test.js b/test/group-rate-limit.test.js index 8156639..0852846 100644 --- a/test/group-rate-limit.test.js +++ b/test/group-rate-limit.test.js @@ -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) ) @@ -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) ) diff --git a/test/route-rate-limit.test.js b/test/route-rate-limit.test.js index db070b3..7db6d93 100644 --- a/test/route-rate-limit.test.js +++ b/test/route-rate-limit.test.js @@ -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) )