Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unnecessary property timeWindow from RedisStore and LocalStore #363

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ async function fastifyRateLimit (fastify, settings) {
pluginComponent.store = new Store(globalParams)
} else {
if (settings.redis) {
pluginComponent.store = new RedisStore(settings.redis, settings.nameSpace, globalParams.timeWindow, globalParams.continueExceeding)
pluginComponent.store = new RedisStore(globalParams.continueExceeding, settings.redis, settings.nameSpace)
} else {
pluginComponent.store = new LocalStore(settings.cache, globalParams.timeWindow, globalParams.continueExceeding)
pluginComponent.store = new LocalStore(globalParams.continueExceeding, settings.cache)
}
}

Expand Down
6 changes: 3 additions & 3 deletions store/LocalStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const { LruMap: Lru } = require('toad-cache')

function LocalStore (cache = 5000, timeWindow, continueExceeding) {
this.lru = new Lru(cache)
function LocalStore (continueExceeding, cache = 5000) {
this.continueExceeding = continueExceeding
this.lru = new Lru(cache)
}

LocalStore.prototype.incr = function (ip, cb, timeWindow, max) {
Expand Down Expand Up @@ -37,7 +37,7 @@ LocalStore.prototype.incr = function (ip, cb, timeWindow, max) {
}

LocalStore.prototype.child = function (routeOptions) {
return new LocalStore(routeOptions.cache, routeOptions.timeWindow, routeOptions.continueExceeding)
return new LocalStore(routeOptions.continueExceeding, routeOptions.cache)
}

module.exports = LocalStore
7 changes: 3 additions & 4 deletions store/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ const lua = `
return {current, ttl}
`

function RedisStore (redis, key = 'fastify-rate-limit-', timeWindow, continueExceeding) {
function RedisStore (continueExceeding, redis, key = 'fastify-rate-limit-') {
this.continueExceeding = continueExceeding
this.redis = redis
this.key = key
this.timeWindow = timeWindow
this.continueExceeding = continueExceeding

if (!this.redis.rateLimit) {
this.redis.defineCommand('rateLimit', {
Expand All @@ -46,7 +45,7 @@ RedisStore.prototype.incr = function (ip, cb, timeWindow, max) {
}

RedisStore.prototype.child = function (routeOptions) {
return new RedisStore(this.redis, `${this.key}${routeOptions.routeInfo.method}${routeOptions.routeInfo.url}-`, routeOptions.timeWindow, routeOptions.continueExceeding)
return new RedisStore(routeOptions.continueExceeding, this.redis, `${this.key}${routeOptions.routeInfo.method}${routeOptions.routeInfo.url}-`)
}

module.exports = RedisStore
Loading