Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example leakage error correction #3205

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"is-localhost-ip": "^1.4.0",
"isbot": "^5.1.16",
"kafkajs": "^2.1.0",
"maxmind": "^4.3.6",
"maxmind": "^4.3.24",
"md5": "^2.3.0",
"next": "15.0.4",
"next-basics": "^0.39.0",
Expand Down
38 changes: 30 additions & 8 deletions src/lib/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ import {
} from './constants';
import { NextApiRequestCollect } from 'pages/api/send';

let lookup;

// 1. Initialize lookup OUTSIDE any function, but make it a Promise.
let lookupPromise: any = null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more specific type than 'any' for lookupPromise, such as 'Promise<maxmind.Reader> | null'


// 2. Create an initialization function.
async function initializeMaxmind() {
if (!lookupPromise) {
// eslint-disable-next-line no-console
console.log('debug: loading GeoLite2-City.mmdb');
const dir = path.join(process.cwd(), 'geo');
const dbPath = path.resolve(dir, 'GeoLite2-City.mmdb');

// Use try/catch for error handling during DB loading
try {
lookupPromise = maxmind.open(dbPath);
} catch (error) {
console.error("Error loading GeoLite2 database:", error);
// CRITICAL: You MUST handle the error here. Throwing an error
// will cause the server to crash, which is better than running
// without the database.
throw error; // Re-throw to prevent the app from starting.
}
}
return lookupPromise;
}

initializeMaxmind()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This initialization should be awaited and handled properly during application startup to prevent race conditions


export function getIpAddress(req: NextApiRequestCollect) {
const customHeader = String(process.env.CLIENT_IP_HEADER).toLowerCase();
Expand Down Expand Up @@ -107,15 +133,11 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) {
};
}

// Database lookup
if (!lookup) {
const dir = path.join(process.cwd(), 'geo');

lookup = await maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb'));
const lookup = await lookupPromise;
if (!lookup) { // This check is likely unnecessary, but good for safety.
throw new Error("Maxmind database not loaded.");
}

const result = lookup.get(ip);

if (result) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: The result variable is referenced but never defined. Need to add const result = lookup.get(ip); before this line.

Suggested change
if (result) {
const result = lookup.get(ip);
if (result) {

const country = result.country?.iso_code ?? result?.registered_country?.iso_code;
const subdivision1 = result.subdivisions?.[0]?.iso_code;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7739,10 +7739,10 @@ mathml-tag-names@^2.1.3:
resolved "https://registry.yarnpkg.com/mathml-tag-names/-/mathml-tag-names-2.1.3.tgz#4ddadd67308e780cf16a47685878ee27b736a0a3"
integrity sha512-APMBEanjybaPzUrfqU0IMU5I0AswKMH7k8OTLs0vvV4KZpExkTkY87nR/zpbuTPj+gARop7aGUbl11pnDfW6xg==

maxmind@^4.3.6:
version "4.3.22"
resolved "https://registry.yarnpkg.com/maxmind/-/maxmind-4.3.22.tgz#8168a2d890d88626613b97eeecbb13fabe0074c4"
integrity sha512-dfLO11mE77ELTEIXNezfW0eslodsFLsZ1lQkLauP+5Zsg1m7kCGtljqRyVOd9E5Ne2RJgvY6UU09qvnVocOZvA==
maxmind@^4.3.24:
version "4.3.24"
resolved "https://registry.yarnpkg.com/maxmind/-/maxmind-4.3.24.tgz#c67a4278777210c857434fa8e82bdd6774e5e661"
integrity sha512-dexrLcjfS2xDGOvdV8XcfQYmyQVpGidMwEG2ld19lXlsB+i+lXRWPzQi81HfwRXR4hxzFr5gT0oAIFyqAAb/Ww==
dependencies:
mmdb-lib "2.1.1"
tiny-lru "11.2.11"
Expand Down