Skip to content

Commit

Permalink
Added type test and fixed max int lua
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketcodes committed Nov 15, 2024
1 parent 0ac9a5d commit 1bb3034
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion store/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const lua = `
-- If the key is new or if its incremented value has exceeded the max value and exponential backoff is enabled then set its TTL
elseif exponentialBackoff and current > max then
local backoffExponent = current - max - 1
ttl = math.min(timeWindow * (2 ^ backoffExponent), MAX_SAFE_INTEGER)
ttl = math.min(timeWindow * (2.0 ^ backoffExponent), MAX_SAFE_INTEGER)
redis.call('PEXPIRE', key, ttl)
end
Expand Down
12 changes: 12 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,19 @@ const options8: RateLimitPluginOptions = {
hook: 'preValidation'
}

const options9: RateLimitPluginOptions = {
global: true,
max: (req: FastifyRequest<RequestGenericInterface>, key: string) => Promise.resolve(42),
timeWindow: (req: FastifyRequest<RequestGenericInterface>, key: string) => 5000,
store: CustomStore,
hook: 'preValidation',
exponentialBackoff: true
}

appWithImplicitHttp.register(fastifyRateLimit, options1)
appWithImplicitHttp.register(fastifyRateLimit, options2)
appWithImplicitHttp.register(fastifyRateLimit, options5)
appWithImplicitHttp.register(fastifyRateLimit,options9)

appWithImplicitHttp.register(fastifyRateLimit, options3).then(() => {
const preHandler1: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit()
Expand Down Expand Up @@ -164,6 +174,8 @@ appWithHttp2.register(fastifyRateLimit, options3)
appWithHttp2.register(fastifyRateLimit, options5)
appWithHttp2.register(fastifyRateLimit, options7)
appWithHttp2.register(fastifyRateLimit, options8)
appWithHttp2.register(fastifyRateLimit, options9)


appWithHttp2.get('/public', {
config: {
Expand Down

0 comments on commit 1bb3034

Please sign in to comment.