fxa/packages/fxa-geodb
Dan Schomburg 71a3cf4de2
Merge pull request #13484 from mozilla/train-235
Train 235
2022-06-29 09:56:41 -07:00
..
.vscode feat: add vscode tasks for running tests and debugger 2019-10-01 17:27:41 -07:00
db refactor(packages): use workspace references 2020-05-21 10:57:21 -07:00
examples chore(style): added prettier to fxa-geodb 2019-06-25 09:36:41 -07:00
lib chore(deps): Updates maxmind dep 2022-06-27 15:19:28 -04:00
scripts/mmdb refactor(packages): use workspace references 2020-05-21 10:57:21 -07:00
test chore(format): mass reformat with prettier 2 and single config 2020-05-24 10:51:57 -07:00
.eslintrc task(monorepo): eslint consolidation 2019-11-15 23:24:09 +00:00
.nsprc chore(deps): Updates to address nsp advisory 1179 2020-03-19 10:42:24 -07:00
.prettierignore chore(style): added prettier to fxa-geodb 2019-06-25 09:36:41 -07:00
CHANGELOG.md Release 1.235.2 2022-06-29 11:58:27 -04:00
LICENSE added LICENSE file 2016-06-27 16:33:01 -07:00
README.md chore(docs): Replace 'master' with 'main' throughout 2020-06-25 13:44:28 -07:00
package.json Merge pull request #13484 from mozilla/train-235 2022-06-29 09:56:41 -07:00
sources.json fix(tests): Temporary use fxa hosted cities db to get tests working 2019-12-30 14:42:23 -05:00

README.md

Firefox Accounts GeoDB Repo for Geolocation based services

Provides a wrapper around node-maxmind for getting the location data in an easy-to-read format.

Getting started

Install the module by running npm install fxa-geodb --save. Then, include the module in your source file, like so:

var geoDb = require('fxa-geodb')();

You can also pass other options like the path to the database to the require statement, like so:

var geoDb = require('fxa-geodb')({
  dbPath: 'cities-db.mmdb' // Defaults to fxa-geodb/db/cities-db.mmdb
});

--

API

The function returns a location object or throws if the ip was invalid or location data could not be found. Call the function like so:

try {
  const location = geoDb(ip);
  // Use location...
} catch (err) {
  // Handle err
}

The location object has the following properties:

  • accuracy: Accuracy radius in km (number)
  • city: Human readable city name (string)
  • state: Human readable state name (string)
  • stateCode: ISO 3166-2 state code (string)
  • country: Human readable country name (string)
  • countryCode: ISO 3166-1 alpha-2 country code (string)
  • continent: Human readable continent name (string)
  • timeZone: IANA tz database timezone (string)
  • latLong: An object containing two properties:
    • latitude: Latitude (number)
    • longitude: Longitude (number)

For example:

{
  accuracy: 5,
  city: 'Mountain View',
  state: 'California',
  stateCode: 'CA',
  country: 'United States',
  countryCode: 'US',
  continent: 'North America',
  timeZone: 'America/Los_Angeles'
  latLong: {
    latitude: 37.3885,
    longitude: -122.0741
  }
}

A working example is provided in the examples directory.

--

Testing

This package uses Mocha to test its code. By default npm test will test all files under test/.

Test specific tests with the following commands:

# Test only src/test/lib/sentry.spec.ts
npx mocha test/fxa-geodb.js

# Grep for "setupDownloadList"
npx mocha /test/** -g "setupDownloadList"

Refer to Mocha's CLI documentation for more advanced test configuration.

--

Code Coverage

Code coverage is provided with nyc, to run coverage, call npm run-script cover

--

Updating

A Cron job is provided in lib/maxmind-db-downloader, that can be configured and run like so:

  var maxmindDbDownloader = new MaxmindDbDownloader();
  var targetDirPath = maxmindDbDownloader.createTargetDir('db-name');
  var downloadPromiseFunctions = maxmindDbDownloader.setupDownloadList(
    path.join(__dirname, '..','path-to-sources-file'),
    targetDirPath
  );
  maxmindDbDownloader.setupAutoUpdate('30 30 1 * * 3', downloadPromiseFunctions);

By default, the cron job runs every week on Wednesday at 01:30:30 (UTC -7) and updates the Geodata-DB from Maxmind.

--

Getting involved

Interested in contributing to the development of Firefox Accounts GeoDB repo? Great! Head over to the #fxa:mozilla.org room on Matrix with questions, or jump ahead and fix any of the issues we have.

Please review and understand the Mozilla Community Participation Guidelines before contributing to this project. Also, following the commit guidelines is greatly appreciated.

UPDATE: On March 2020, Mozilla moved from IRC to Matrix. For more information on Matrix, check out the following wiki article: https://wiki.mozilla.org/Matrix.

--

Submitting bugs

You can file issues here on GitHub. Please try to include as much information as you can and under what conditions you saw the issue.

--

Attribution

This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com.

--

License

MPL 2.0