* Derives country code and region from an IP address using GeoIP lookup. * * This function performs a non-blocking geolocation lookup to extract geographic * information before the IP address is masked for privacy compliance. Returns * ISO country codes and region identifiers that are GDPR/HIPAA
(ipAddress: string)
| 52 | * @returns An object containing optional `countryCode` (ISO 3166-1 alpha-2) and `region` (state/province), or empty object if lookup fails or IP is invalid |
| 53 | */ |
| 54 | function getGeolocation(ipAddress: string): { |
| 55 | countryCode?: string |
| 56 | region?: string |
| 57 | } { |
| 58 | if (!isValidIPAddress(ipAddress)) { |
| 59 | return {} |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | const geo = geoip.lookup(ipAddress) |
| 64 | if (!geo) return {} |
| 65 | |
| 66 | return { |
| 67 | countryCode: geo.country, |
| 68 | region: geo.region |
| 69 | } |
| 70 | } catch (error) { |
| 71 | logger.error(`Failed to resolve geolocation for IP: ${error}`) |
| 72 | return {} |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Categories for telemetry events, used for filtering and compliance reporting. |
no test coverage detected