@@ -2,6 +2,7 @@ import { Cache } from "./cache";
22import type { Duration } from "./duration" ;
33import { ms } from "./duration" ;
44import { safeEval } from "./hash" ;
5+ import { RESET_SCRIPT , SCRIPTS } from "./lua-scripts/hash" ;
56import {
67 fixedWindowLimitScript ,
78 fixedWindowRemainingTokensScript ,
@@ -115,8 +116,6 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
115116 ctx : {
116117 regionContexts : config . redis . map ( redis => ( {
117118 redis : redis ,
118- scriptHashes : { } ,
119- cacheScripts : config . cacheScripts ?? true ,
120119 } ) ) ,
121120 cache : config . ephemeralCache ? new Cache ( config . ephemeralCache ) : undefined ,
122121 } ,
@@ -178,8 +177,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
178177 redis : regionContext . redis ,
179178 request : safeEval (
180179 regionContext ,
181- fixedWindowLimitScript ,
182- "limitHash" ,
180+ SCRIPTS . multiRegion . fixedWindow . limit ,
183181 [ key ] ,
184182 [ requestId , windowDuration , incrementBy ] ,
185183 ) as Promise < string [ ] > ,
@@ -284,8 +282,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
284282 redis : regionContext . redis ,
285283 request : safeEval (
286284 regionContext ,
287- fixedWindowRemainingTokensScript ,
288- "getRemainingHash" ,
285+ SCRIPTS . multiRegion . fixedWindow . getRemaining ,
289286 [ key ] ,
290287 [ null ]
291288 ) as Promise < string [ ] > ,
@@ -316,8 +313,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
316313 await Promise . all ( ctx . regionContexts . map ( ( regionContext ) => {
317314 safeEval (
318315 regionContext ,
319- resetScript ,
320- "resetHash" ,
316+ RESET_SCRIPT ,
321317 [ pattern ] ,
322318 [ null ]
323319 ) ;
@@ -385,8 +381,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
385381 redis : regionContext . redis ,
386382 request : safeEval (
387383 regionContext ,
388- slidingWindowLimitScript ,
389- "limitHash" ,
384+ SCRIPTS . multiRegion . slidingWindow . limit ,
390385 [ currentKey , previousKey ] ,
391386 [ tokens , now , windowDuration , requestId , incrementBy ] ,
392387 // lua seems to return `1` for true and `null` for false
@@ -508,8 +503,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
508503 redis : regionContext . redis ,
509504 request : safeEval (
510505 regionContext ,
511- slidingWindowRemainingTokensScript ,
512- "getRemainingHash" ,
506+ SCRIPTS . multiRegion . slidingWindow . getRemaining ,
513507 [ currentKey , previousKey ] ,
514508 [ now , windowSize ] ,
515509 // lua seems to return `1` for true and `null` for false
@@ -532,8 +526,7 @@ export class MultiRegionRatelimit extends Ratelimit<MultiRegionContext> {
532526 await Promise . all ( ctx . regionContexts . map ( ( regionContext ) => {
533527 safeEval (
534528 regionContext ,
535- resetScript ,
536- "resetHash" ,
529+ RESET_SCRIPT ,
537530 [ pattern ] ,
538531 [ null ]
539532 ) ;
0 commit comments