@@ -2,34 +2,35 @@ import { type RateLimiter, type RedisClient, UpstashRateLimit, ValkeyRateLimit }
22import { Redis as UpstashRedis } from "@upstash/redis/cloudflare" ;
33import { Redis as ValkeyRedis } from "iovalkey" ;
44
5- interface RateLimiterConfig {
5+ export interface RateLimiterConfig {
66 points : number ;
77 duration : number ;
88 blockDuration ?: number ;
99 keyPrefix : string ;
1010}
1111
12- interface RateLimiterFactory < T extends RateLimiter > {
13- ( identifier : string ) : T ;
12+ export interface RateLimiterFactory < T extends RateLimiter > {
13+ ( ) : T ;
1414}
1515
1616export interface CreateRateLimiterContext {
1717 isEdgeRuntime : boolean ;
1818 redisClient : RedisClient ;
1919 config : RateLimiterConfig ;
20- identifier : string ;
2120}
2221
2322// Create Upstash rate limiter factory
2423function createUpstashRateLimiter (
2524 redisClient : UpstashRedis ,
2625 config : RateLimiterConfig
2726) : RateLimiterFactory < UpstashRateLimit > {
28- return ( identifier : string ) =>
27+ return ( ) =>
2928 new UpstashRateLimit ( {
3029 redis : redisClient ,
31- prefix : `${ config . keyPrefix } ${ identifier } ` ,
32- limiter : UpstashRateLimit . slidingWindow ( config . points , `${ Math . ceil ( config . duration / 60 ) } s` ) ,
30+ // Do not include the identifier in the prefix; it's passed to limit() per request
31+ prefix : `${ config . keyPrefix } ` ,
32+ // config.duration is in seconds (to match Valkey). Upstash accepts duration strings like "10 s".
33+ limiter : UpstashRateLimit . slidingWindow ( config . points , `${ config . duration } s` ) ,
3334 analytics : true ,
3435 } ) ;
3536}
@@ -39,10 +40,11 @@ function createValkeyRateLimiter(
3940 redisClient : ValkeyRedis ,
4041 config : RateLimiterConfig
4142) : RateLimiterFactory < ValkeyRateLimit > {
42- return ( identifier : string ) =>
43+ return ( ) =>
4344 new ValkeyRateLimit ( {
4445 storeClient : redisClient ,
45- keyPrefix : `${ config . keyPrefix } ${ identifier } ` ,
46+ // Keep prefix stable and pass identifier to consume()
47+ keyPrefix : `${ config . keyPrefix } ` ,
4648 points : config . points ,
4749 duration : config . duration ,
4850 blockDuration : config . blockDuration ,
@@ -51,10 +53,10 @@ function createValkeyRateLimiter(
5153 } ) ;
5254}
5355
54- export function createRateLimiter ( ctx : CreateRateLimiterContext ) : RateLimiter {
56+ export function createRateLimiter ( ctx : CreateRateLimiterContext ) : RateLimiterFactory < RateLimiter > {
5557 if ( ctx . isEdgeRuntime ) {
56- return createUpstashRateLimiter ( ctx . redisClient as UpstashRedis , ctx . config ) ( ctx . identifier ) ;
58+ return createUpstashRateLimiter ( ctx . redisClient as UpstashRedis , ctx . config ) as RateLimiterFactory < RateLimiter > ;
5759 } else {
58- return createValkeyRateLimiter ( ctx . redisClient as ValkeyRedis , ctx . config ) ( ctx . identifier ) ;
60+ return createValkeyRateLimiter ( ctx . redisClient as ValkeyRedis , ctx . config ) as RateLimiterFactory < RateLimiter > ;
5961 }
6062}
0 commit comments