102.1.0 • Published 3 months ago

@streamr/geoip-location v102.1.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

geoip-location

@streamr/geoip-location is a package that provides a way to detect the latitude and longitude of an IP address using the mmdb-lib reader package and the MaxMind GeoLite2 City database resdistributed by GitSquared/node-geolite2-redist. The library complies with the MaxMind GeoLite2 End User License Agreement by keeping the database up-to-date by periodically downloading the fresh database from the GitSquared/node-geolite2-redist repository.

Differences to the node-geolite2-redist package

  • The period between checks for the updates of the GeoLite2 City database is configurable
  • Distributed as commonjs module for greater compatibility with other packages
  • Better error handling and focus on clean shutdown without lingering timers or memory leaks
  • Fewer dependencies: uses 'fetch' instead of 'got' for downloading the database
  • Only supports the GeoLite2 City database
  • Only returns the latitude and longitude of the IP address

Getting started

Installation

npm install @streamr/geoip-location

Usage

import { GeoIpLocator } from '@streamr/geoip-location'

(async () => {
    
        // Creates a new GeoIpLocator object with the following parameters:
        // geoiIpDatabasePath: string - the path to the directory for the GeoLite2 City database
        // the disrectory will be created if it does not exist. 
        // dbCheckInterval?: number - the interval in milliseconds between the checks 
        // for the updates of the GeoLite2 City database (default: 28 days)
        // dbCheckErrorInterval?: number - the interval in milliseconds between the checks 
        // for the updates of the GeoLite2 City database in case of a failed update (default: 1 day)
         
        const locator = new GeoIpLocator('~/geoipdatabases', 15 * 24 * 60 * 60 * 1000, 60 * 60 * 1000)
        
        // Downloads the GeoLite2 City database if it is not found in the geoiIpDatabasePath 
        // or if the database is outdated. Also starts the db update timer. 

        await locator.start()
  
        // Returns the { latitude, longitude } of the IP address 
        // or undefined if the location cannot be found
        
        const location = locator!.lookup('62.241.198.245')
        
        console.log(location.latitude)         // 60.1695
        console.log(location.longitude)        // 24.9354
        
        // Stops the db update timer, does not delete the GeoLite2 City database

        locator.stop()

        // (optional) Deletes the GeoLite2 City database
       
        fs.unlinkSync(dbDir + '~/geoipdatabases/GeoLite2-City.mmdb')

})()

License

This package is licensed under the Apache-2.0 license (see LICENSE). The databases themselves are provided by MaxMind under CC BY-SA 4.0 and redistributed by GitSquared/node-geolite2-redist

This software package uses GeoLite2 data created by MaxMind, available from https://www.maxmind.com.

103.0.0-rc.0

3 months ago

102.2.0-rc.1

4 months ago

102.2.0-rc.2

3 months ago

102.2.0-rc.3

3 months ago

102.2.0-rc.0

4 months ago

102.1.0

4 months ago

102.0.0

5 months ago

102.0.0-beta.2

5 months ago

102.0.0-beta.3

5 months ago

102.0.0-beta.1

5 months ago

102.0.0-beta.0

8 months ago

101.1.2

10 months ago

101.1.1

10 months ago

101.0.0-beta.0

12 months ago

101.1.0

10 months ago

101.0.1

11 months ago

101.0.0-beta.1

12 months ago

101.0.0

11 months ago

101.0.0-beta.2

12 months ago

101.0.0-beta.3

12 months ago

101.0.0-beta.4

12 months ago

101.1.1-beta.0

10 months ago

101.0.2-beta.0

10 months ago

100.2.5-beta.0

1 year ago

100.2.5-beta.1

1 year ago

101.1.1-beta.1

10 months ago

100.2.4

1 year ago