From f59772289207dfc064162e62e653d5da497ea063 Mon Sep 17 00:00:00 2001 From: dusk Date: Thu, 16 Jan 2025 15:47:34 +0300 Subject: [PATCH] fix: use foreach because filter isnt available on prod??? --- src/lib/visits.ts | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/lib/visits.ts b/src/lib/visits.ts index a713f93..552c40a 100644 --- a/src/lib/visits.ts +++ b/src/lib/visits.ts @@ -45,19 +45,14 @@ export const addLastVisitor = (request: Request, cookies: Cookies) => { const _addLastVisitor = (visitors: Map, request: Request, cookies: Cookies) => { const currentTime = Date.now() // filter out old entries - visitors = new Map( - visitors.entries().filter( - ([_, visitor]) => - { return currentTime - visitor.visits[0] < 1000 * VISITOR_EXPIRY_SECONDS } - ).map( - ([id, visitor]) => { - visitor.visits = visitor.visits.filter((since) => { - return currentTime - since < 1000 * VISITOR_EXPIRY_SECONDS - }) - return [id, visitor] - } - ) - ) + visitors.forEach((visitor, id, map) => { + if (currentTime - visitor.visits[0] > 1000 * VISITOR_EXPIRY_SECONDS) + map.delete(id) + else + visitor.visits = visitor.visits.filter((since) => { + return currentTime - since < 1000 * VISITOR_EXPIRY_SECONDS + }) + }) // check whether the request is from a bot or not (this doesnt need to be accurate we just want to filter out honest bots) if (isBot(request)) { return visitors } const scopedCookies = scopeCookies(cookies, '/')