Repository navigation
feat(rate-limit): make tier quotas configurable and return standard rate-limit headers - #79
Open
produckq-boop wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #50
Summary
Makes token-bucket rate-limit tiers configurable via environment variables while keeping existing defaults, standardizes HTTP rate-limit response headers across both Redis and in-memory paths, and documents the tier quotas, headers, and per-key override flow.
Previously,
TIER_LIMITSand the 60-second refill calculation insrc/middleware/tokenBucket.tswere hardcoded, responses only set legacyX-RateLimit-*headers, andtieredRateLimitinsrc/middleware/rateLimit.tsskippedcheckTokenBucketwheneverREDIS_URLwas unset (even thoughcheckTokenBucketalready has an in-memory token-bucket fallback), which also causedDevApiKey.rateLimitOverrideto be ignored in non-Redis environments.Changes
src/middleware/tokenBucket.ts:DEFAULT_TIER_LIMITS,getTierLimits(tier, env?), andresolveTierLimits(env?)supportingRATE_LIMIT_TIER_<TIER>_LIMITandRATE_LIMIT_TIER_<TIER>_WINDOW_MS(plus optionalRATE_LIMIT_TIER_<TIER>_BURST) forunauthenticated,free,developer,pro, andenterprise, falling back to current defaults when unset or invalid.windowMs(capacity / (windowMs / 1000)) instead of a hardcoded 60-second window, while preservingTIER_LIMITSandperMinutefor backward compatibility.applyRateLimitHeaders()and updatetokenBucketMiddleware()(with a defaultreq.apiKey/ IP tier resolver) to emit standardRateLimit-Limit,RateLimit-Remaining, andRateLimit-Reset(delta seconds) alongside existingX-RateLimit-*headers, plusRetry-Afteron429responses.src/middleware/rateLimit.ts&src/index.ts:tieredRateLimitthroughcheckTokenBucketregardless of whether Redis is configured so tier quotas, endpoint multipliers, andreq.apiKey?.rateLimitOverridebehave consistently across Redis and in-memory execution, while keepingrunLocalRateLimitas an error fallback that also emitsRateLimit-*andRetry-After.RateLimit-Limit,RateLimit-Remaining,RateLimit-Reset,Retry-After, andX-RateLimit-*in CORSexposedHeaders.src/middleware/apiKeyAuth.ts:normalizeRateLimitTier, defaulting unknown values to'free') and ignore non-positiverateLimitOverridevalues..env.example,README.md,package.json:RATE_LIMIT_TIER_*variables in.env.exampleand add a Rate Limiting section inREADME.mdcovering tiers, env vars, endpoint multipliers,DevApiKey.rateLimitOverride/ cache invalidation, and response headers."test"key inpackage.jsonand includetests/api-key-auth.test.ts,tests/rate-limit.test.ts, andtests/token-bucket.test.tsinnpm test.tests/rate-limit.test.ts&tests/token-bucket.test.ts:tests/rate-limit.test.tsand addtests/token-bucket.test.tscovering within-limit remaining quota headers,429responses withRetry-AfterandRateLimit-*headers, custom env limits/windows and invalid-env fallbacks, window refill behavior,apiKeyAuthrateLimitOverrideintegration, endpoint multipliers, and Redis-to-memory fallback.Verification
npm run lint(0errors)npm run build&npm run build -- --noEmit&npm run typecheck:scriptsnpm run validate:prisma&npm run check:mock-gatingnpm test(14test files,268tests passing)