import path from "path"; import fs from "fs"; const MMDB_PATH = process.env.NEXREDIRECT_GEOIP_PATH || path.join(process.cwd(), "data", "GeoLite2-Country.mmdb"); type CountryResponse = { country?: { iso_code?: string } }; type Reader = { get: (ip: string) => CountryResponse | null }; let _reader: Reader | null = null; let _loadAttempted = false; async function getReader(): Promise { if (_reader) return _reader; if (_loadAttempted) return null; _loadAttempted = true; if (!fs.existsSync(MMDB_PATH)) return null; try { const maxmind = await import("maxmind"); _reader = (await maxmind.open(MMDB_PATH)) as unknown as Reader; return _reader; } catch { return null; } } export async function lookupCountry(ip: string): Promise { const r = await getReader(); if (!r) return null; try { const result = r.get(ip); return result?.country?.iso_code ?? null; } catch { return null; } }