11import type { RateLimit } from '../types/RateLimit'
2- import { isBanExpired } from '../utils/isBanExpired'
32import shieldLog from '../utils/shieldLog'
43import {
54 createError ,
@@ -29,7 +28,7 @@ export default defineEventHandler(async (event) => {
2928 const banKey = `ban:${ requestIP } `
3029 const bannedUntilRaw = await shieldStorage . getItem ( banKey )
3130 const bannedUntil = typeof bannedUntilRaw === 'number' ? bannedUntilRaw : Number ( bannedUntilRaw )
32- if ( bannedUntilRaw && ! isNaN ( bannedUntil ) && Date . now ( ) < bannedUntil ) {
31+ if ( bannedUntilRaw && ! Number . isNaN ( bannedUntil ) && Date . now ( ) < bannedUntil ) {
3332 if ( config . retryAfterHeader ) {
3433 const retryAfter = Math . ceil ( ( bannedUntil - Date . now ( ) ) / 1000 )
3534 event . node . res . setHeader ( 'Retry-After' , retryAfter )
@@ -38,7 +37,8 @@ export default defineEventHandler(async (event) => {
3837 statusCode : 429 ,
3938 message : config . errorMessage ,
4039 } )
41- } else if ( bannedUntilRaw && ! isNaN ( bannedUntil ) && Date . now ( ) >= bannedUntil ) {
40+ }
41+ else if ( bannedUntilRaw && ! Number . isNaN ( bannedUntil ) && Date . now ( ) >= bannedUntil ) {
4242 await shieldStorage . removeItem ( banKey )
4343 await shieldStorage . setItem ( `ip:${ requestIP } ` , {
4444 count : 1 ,
@@ -74,7 +74,6 @@ export default defineEventHandler(async (event) => {
7474 count : 1 ,
7575 time : Date . now ( ) ,
7676 } )
77-
7877
7978 if ( config . retryAfterHeader ) {
8079 event . node . res . setHeader ( 'Retry-After' , config . limit . ban )
@@ -97,17 +96,3 @@ const isRateLimited = (req: RateLimit) => {
9796 // console.log(" ", (Date.now() - req.time) / 1000, "<", options.limit.duration);
9897 return ( Date . now ( ) - req . time ) / 1000 < options . limit . duration
9998}
100-
101- const banDelay = async ( req : RateLimit ) => {
102- const options = useRuntimeConfig ( ) . public . nuxtApiShield
103- // console.log(" delayOnBan is: " + options.delayOnBan);
104- if ( options . delayOnBan && req . count > options . limit . max ) {
105- // INFO Nuxt Devtools will send a new request if the response is slow,
106- // so we get the count incremented twice or more times, based on the ban delay time
107- // console.log(` Applying ban delay for ${(req.count - options.limit.max) * options.limit.ban} sec (${Date.now()})`);
108- await new Promise ( resolve =>
109- setTimeout ( resolve , ( req . count - options . limit . max ) * options . limit . ban * 1000 ) ,
110- )
111- // console.log(` Ban delay completed (${Date.now()})`);
112- }
113- }
0 commit comments