Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (ip, options = {}) => {
const userLocale = options.userLocale || DEFAULTS.USER_LOCALE;
// check if ip is valid
if (!maxmind.validate(ip)) {
throw new Error(ERRORS.IS_INVALID);
}
const locationData = dbLookup.get(ip);
if (locationData == null) {
throw new Error(ERRORS.UNABLE_TO_FETCH_DATA);
}
// return an object with city, country, continent,
// latitude, and longitude, and timezone
return new Location(locationData, userLocale);
};
};
return this.db.then((reader) => {
if (!reader) {
throw new Error('MMDB reader is not valid');
}
if (!maxmind.validate(ipAddress)) {
throw new Error('Invalid IP address');
}
const result = reader.get(ipAddress);
return (result && result.country && result.country.iso_code) || 'ZZ';
});
}