-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
0c2fc95
ddca717
59804a1
6549d37
236699a
01da057
c6373ad
26258b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||
|
||||||||
// 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() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||||||||
|
@@ -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) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: The
Suggested change
|
||||||||
const country = result.country?.iso_code ?? result?.registered_country?.iso_code; | ||||||||
const subdivision1 = result.subdivisions?.[0]?.iso_code; | ||||||||
|
There was a problem hiding this comment.
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'