@@ -3,6 +3,7 @@ import { beforeEach, describe, expect, test } from "bun:test";
33import { checkDenyList } from "./deny-list" ;
44import { disableIpDenyList , updateIpDenyList } from "./ip-deny-list" ;
55import { DenyListExtension , IpDenyListKey , IpDenyListStatusKey } from "../types" ;
6+ import { Ratelimit } from ".." ;
67
78describe ( "should update ip deny list status" , async ( ) => {
89 const redis = Redis . fromEnv ( ) ;
@@ -139,6 +140,37 @@ describe("should update ip deny list status", async () => {
139140 expect ( newStatus ) . toBe ( "valid" )
140141 expect ( newStatusTTL ) . toBeGreaterThan ( 1000 )
141142 } )
143+
144+ test ( "should be able to use ratelimit with deny list" , async ( ) => {
145+
146+ /**
147+ * When enableProtection is set to true, there was an error in the
148+ * @upstash /ratelimit 2.0.3 release. Because the @upstash/redis
149+ * uses auto-pipelining by default, the client would send the
150+ * EVALSHA and the protection EVAL at the same time.
151+ *
152+ * When the db loses its scripts after a SCRIPT FLUSH or when
153+ * ratelimit is used in a new DB, the EVALSHA will fail. But
154+ * because since the EVAL and EVALSHA are pipelined, they will
155+ * both fail and EVAL will get the EVALSHA's error.
156+ */
157+ const redis = Redis . fromEnv ( { enableAutoPipelining : true } ) ;
158+
159+ // flush the db to make sure
160+ await redis . scriptFlush ( )
161+ // sleep for two secs
162+ await new Promise ( r => setTimeout ( r , 2000 ) ) ;
163+
164+ const ratelimit = new Ratelimit ( {
165+ limiter : Ratelimit . fixedWindow ( 2 , "1s" ) ,
166+ redis,
167+ enableProtection : true
168+ } )
169+
170+ const { success, remaining } = await ratelimit . limit ( "ip-deny-list" )
171+ expect ( success ) . toBeTrue ( )
172+ expect ( remaining ) . toBe ( 1 )
173+ } )
142174} )
143175
144176describe ( "should only allow threshold values from 1 to 8" , async ( ) => {
@@ -177,4 +209,4 @@ describe("should only allow threshold values from 1 to 8", async () => {
177209 expect ( error . name ) . toEqual ( "ThresholdError" )
178210 }
179211 } )
180- } )
212+ } )
0 commit comments