@@ -97,9 +97,8 @@ export class RateLimiter {
97
97
const now = Date . now ( ) ;
98
98
99
99
// Use group-specific options if available, otherwise fall back to global options
100
- const maxRequests = groupOptions . maxRequests ?? this . options . maxRequests ;
101
- const windowSeconds = groupOptions . windowSeconds ??
102
- this . options . windowSeconds ;
100
+ const maxRequests = groupOptions . maxRequests ?? 0 ;
101
+ const windowSeconds = groupOptions . windowSeconds ?? 0 ;
103
102
const onRateLimitExceeded = groupOptions . onRateLimitExceeded ??
104
103
this . options . onRateLimitExceeded ;
105
104
@@ -147,8 +146,7 @@ export class RateLimiter {
147
146
}
148
147
149
148
const now = Date . now ( ) ;
150
- const windowSeconds = groupOptions . windowSeconds ??
151
- this . options . windowSeconds ;
149
+ const windowSeconds = groupOptions . windowSeconds ?? 0 ;
152
150
const windowStart = now - ( windowSeconds * 1000 ) ;
153
151
return bucket . requests . filter ( ( time ) => time > windowStart ) . length ;
154
152
}
@@ -161,7 +159,7 @@ export class RateLimiter {
161
159
public getRemainingRequests ( url : string ) : number {
162
160
const key = this . options . getGroupFunc ( url ) ;
163
161
const groupOptions = this . getGroupOptions ( key ) ;
164
- const maxRequests = groupOptions . maxRequests ?? this . options . maxRequests ;
162
+ const maxRequests = groupOptions . maxRequests ?? 0 ;
165
163
166
164
return Math . max (
167
165
0 ,
@@ -199,12 +197,28 @@ export class RateLimiter {
199
197
}
200
198
201
199
/**
202
- * Gets the options for a specific group.
200
+ * Gets the options for a specific group. Falls back to global options if not set.
203
201
* @param group - The group key
204
202
* @returns The options for the group
205
203
*/
206
204
public getGroupOptions ( group : string ) : GroupRateLimiterOptions {
207
- return this . groupOptions . get ( group ) || { } ;
205
+ const options = this . groupOptions . get ( group ) ;
206
+ if ( ! options ) {
207
+ return {
208
+ maxRequests : this . options . maxRequests ,
209
+ windowSeconds : this . options . windowSeconds ,
210
+ } ;
211
+ }
212
+ return options ;
213
+ }
214
+
215
+ /**
216
+ * Checks if a group has specific options set.
217
+ * @param group - The group key
218
+ * @returns True if the group has options, false otherwise
219
+ */
220
+ public hasGroupOptions ( group : string ) : boolean {
221
+ return this . groupOptions . has ( group ) ;
208
222
}
209
223
210
224
/**
@@ -252,7 +266,10 @@ export class RateLimiter {
252
266
* @param headers - The response headers containing rate limit information
253
267
*/
254
268
public updateFromHeaders ( group : string , headers : Headers ) : void {
255
- const currentOptions = this . getGroupOptions ( group ) ;
269
+ // Get existing group-specific options (not global fallback)
270
+ const currentOptions = this . hasGroupOptions ( group )
271
+ ? this . groupOptions . get ( group ) !
272
+ : { } ;
256
273
const newOptions : GroupRateLimiterOptions = { ...currentOptions } ;
257
274
258
275
// Parse IETF standard rate limit headers first, then fall back to x-ratelimit headers
0 commit comments