From c6373adcb1ae3313f9d068209dbd6d7f068d3c3b Mon Sep 17 00:00:00 2001 From: Volodymyr Kravchenko Date: Thu, 6 Feb 2025 00:37:35 +0200 Subject: [PATCH] Use temporary fix --- src/lib/detect.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index c67fbe602..f15949892 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -14,7 +14,17 @@ import { } from './constants'; import { NextApiRequestCollect } from 'pages/api/send'; -let lookup; +let lookupPromise: any = null; + +// This function returns the open DB. If it's already opening/open, just return the existing promise. +export function getLookup() { + if (!lookupPromise) { + console.log('debug: loading GeoLite2-City.mmdb'); + const dir = path.join(process.cwd(), 'geo'); + lookupPromise = maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb')); + } + return lookupPromise; +} export function getIpAddress(req: NextApiRequestCollect) { const customHeader = String(process.env.CLIENT_IP_HEADER).toLowerCase(); @@ -108,16 +118,11 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) { } // Database lookup - if (!lookup) { - // eslint-disable-next-line no-console - console.log('debug: loading GeoLite2-City.mmdb'); - const dir = path.join(process.cwd(), 'geo'); - - lookup = await maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb')); - } - + const lookup = await getLookup(); const result = lookup.get(ip); + console.log('debug_result:', result); + if (result) { const country = result.country?.iso_code ?? result?.registered_country?.iso_code; const subdivision1 = result.subdivisions?.[0]?.iso_code;