1
1
import type { Duration } from "./duration" ;
2
2
import { ms } from "./duration" ;
3
+ import { fixedWindowScript , slidingWindowScript , tokenBucketScript } from "./lua-scripts/single" ;
4
+ import { Ratelimit } from "./ratelimit" ;
3
5
import type { Algorithm , RegionContext } from "./types" ;
4
6
import type { Redis } from "./types" ;
5
- import { Ratelimit } from "./ratelimit" ;
6
- import { fixedWindowScript , slidingWindowScript , tokenBucketScript } from "./lua-scripts/single" ;
7
7
8
8
export type RegionRatelimitConfig = {
9
9
/**
@@ -128,8 +128,7 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
128
128
// payloadLimit?: number,
129
129
) : Algorithm < RegionContext > {
130
130
const windowDuration = ms ( window ) ;
131
- return async function ( ctx : RegionContext , identifier : string , rate ?: number ) {
132
-
131
+ return async ( ctx : RegionContext , identifier : string , rate ?: number ) => {
133
132
const bucket = Math . floor ( Date . now ( ) / windowDuration ) ;
134
133
const key = [ identifier , bucket ] . join ( ":" ) ;
135
134
if ( ctx . cache ) {
@@ -153,9 +152,9 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
153
152
[ windowDuration , incrementBy ] ,
154
153
) ) as number ;
155
154
156
- let success = usedTokensAfterUpdate <= tokens ;
155
+ const success = usedTokensAfterUpdate <= tokens ;
157
156
158
- let remainingTokens = Math . max ( 0 , tokens - usedTokensAfterUpdate )
157
+ const remainingTokens = Math . max ( 0 , tokens - usedTokensAfterUpdate ) ;
159
158
160
159
const reset = ( bucket + 1 ) * windowDuration ;
161
160
if ( ctx . cache && ! success ) {
@@ -198,9 +197,8 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
198
197
*/
199
198
window : Duration ,
200
199
) : Algorithm < RegionContext > {
201
-
202
200
const windowSize = ms ( window ) ;
203
- return async function ( ctx : RegionContext , identifier : string , rate ?: number ) {
201
+ return async ( ctx : RegionContext , identifier : string , rate ?: number ) => {
204
202
const now = Date . now ( ) ;
205
203
206
204
const currentWindow = Math . floor ( now / windowSize ) ;
@@ -229,7 +227,7 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
229
227
[ tokens , now , windowSize , incrementBy ] ,
230
228
) ) as number ;
231
229
232
- let success = remainingTokens >= 0 ;
230
+ const success = remainingTokens >= 0 ;
233
231
234
232
const reset = ( currentWindow + 1 ) * windowSize ;
235
233
if ( ctx . cache && ! success ) {
@@ -277,7 +275,7 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
277
275
maxTokens : number ,
278
276
) : Algorithm < RegionContext > {
279
277
const intervalDuration = ms ( interval ) ;
280
- return async function ( ctx : RegionContext , identifier : string , rate ?: number ) {
278
+ return async ( ctx : RegionContext , identifier : string , rate ?: number ) => {
281
279
if ( ctx . cache ) {
282
280
const { blocked, reset } = ctx . cache . isBlocked ( identifier ) ;
283
281
if ( blocked ) {
@@ -366,7 +364,7 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
366
364
return r
367
365
` ;
368
366
369
- return async function ( ctx : RegionContext , identifier : string ) {
367
+ return async ( ctx : RegionContext , identifier : string ) => {
370
368
if ( ! ctx . cache ) {
371
369
throw new Error ( "This algorithm requires a cache" ) ;
372
370
}
@@ -381,8 +379,8 @@ export class RegionRatelimit extends Ratelimit<RegionContext> {
381
379
382
380
const pending = success
383
381
? ctx . redis . eval ( script , [ key ] , [ windowDuration ] ) . then ( ( t ) => {
384
- ctx . cache ! . set ( key , t as number ) ;
385
- } )
382
+ ctx . cache ! . set ( key , t as number ) ;
383
+ } )
386
384
: Promise . resolve ( ) ;
387
385
388
386
return {
0 commit comments