diff --git a/README.md b/README.md index da69de73..7d534699 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ const geolite2 = require('geolite2-redist'); const maxmind = require('maxmind'); (async () => { + await geolite2.downloadDbs() let lookup = await geolite2.open('GeoLite2-City', path => { return maxmind.open(path); }); @@ -51,29 +52,34 @@ const geolite2 = require('geolite2-redist'); const maxmind = require('maxmind'); const fs = require('fs'); -let lookup = geolite2.open('GeoLite2-City', path => { - let buf = fs.readFileSync(path); - return new maxmind.Reader(buf); -}); +geolite2.downloadDbs().then(() => { + let lookup = geolite2.open('GeoLite2-City', path => { + let buf = fs.readFileSync(path); + return new maxmind.Reader(buf); + }); -let city = lookup.get('66.6.44.4'); + let city = lookup.get('66.6.44.4'); -lookup.close(); + lookup.close(); +}) ``` ### Advanced usage -If you do not consume the databases directly, or need more flexible methods, the internal `geolite2.UpdateSubscriber` class is exposed so you can directly listen to database update events. +If you do not consume the databases directly, or need more flexible methods, the internal `geolite2.UpdateSubscriber` class is exposed so you can directly listen to database update events. You can also choose where to download the databases. Example usage: ```javascript const geolite2 = require('geolite2-redist'); +const dbBasePath = '/tmp/maxmind' + function useGeolite() { - // You can retrieve the path to `.mmdb` files - let cityPath = geolite2.paths['GeoLite2-City']; + // Do something with the databases } +await geolite2.downloadDbs(dbBasePath) + const dbWatcher = new geolite2.UpdateSubscriber(); dbWatcher.on('update', () => { useGeolite(); @@ -92,6 +98,7 @@ import geolite2 from 'geolite2-redist'; import maxmind, { CityResponse } from 'maxmind'; (async () => { + await geolite2.downloadDbs() let lookup = await geolite2.open('GeoLite2-City', path => { return maxmind.open(path); }); @@ -107,6 +114,12 @@ import maxmind, { CityResponse } from 'maxmind'; ### Methods +*geolite2.downloadDbs(path?)* + +This function returns a Promise that resolves when GeoIP databases have been succesfully retrieved from the redistribution. + + - `path`: `(optional) ` Filesystem path to use for storing databases. Can be relative, defaults to '../dbs'. + *geolite2.open(database, databaseReader)* - `database`: `` One of `GeoLite2-ASN`, `GeoLite2-Country`, `GeoLite2-City`. @@ -116,12 +129,12 @@ import maxmind, { CityResponse } from 'maxmind'; ### Properties -*geolite2.paths* `` +*geolite2.databases* `` -Full fs paths for each database. **Warning:** you need to let the library update the databases automatically, see Usage section. - - `GeoLite2-ASN`: `` - - `GeoLite2-Country`: `` - - `GeoLite2-City`: `` +See what databases you can use with `open()`. **Warning:** you need to let the library update the databases automatically, see Usage section. + - `GeoLite2-ASN` + - `GeoLite2-Country` + - `GeoLite2-City` ### Classes diff --git a/index.d.ts b/index.d.ts index a23b538b..f3e4aa03 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,11 +2,7 @@ import { EventEmitter } from "events"; import { Reader } from "maxmind"; declare module "geolite2-redist" { - export const paths: { - "GeoLite2-ASN": string; - "GeoLite2-City": string; - "GeoLite2-Country": string; - } + export const databases: string[] export const open: (database: string, readerBuilder: (database: string) => (Reader | Promise>)) => Promise> @@ -14,6 +10,7 @@ declare module "geolite2-redist" { public downloading: boolean; private _checker: ReturnType; + downloadDbs(path?: string): Promise; checkUpdates(): Promise; update(): void; triggerUpdate(): Promise; diff --git a/index.js b/index.js index 8e782631..598c3a4e 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,11 @@ const {EventEmitter} = require('events'); const rimraf = require('rimraf'); const downloadHelper = require('./scripts/download-helper.js'); -const downloadPath = path.resolve(__dirname, 'dbs'); +let downloadPath = path.resolve(__dirname, 'dbs'); const updateTimer = 2 * 24 * 60 * 60 * 1000; // 48 hours in ms -const paths = { +let paths = { 'GeoLite2-ASN': path.join(downloadPath, 'GeoLite2-ASN.mmdb'), 'GeoLite2-City': path.join(downloadPath, 'GeoLite2-City.mmdb'), 'GeoLite2-Country': path.join(downloadPath, 'GeoLite2-Country.mmdb') @@ -176,8 +176,36 @@ function open(database, readerBuilder) { return wrapReader(reader, readerBuilder, database); } +function downloadDbs(newpath) { + downloadPath = path.resolve(__dirname, 'dbs') + if (newpath) downloadPath = path.resolve(newpath); + if (!fs.existsSync(downloadPath)) { + fs.mkdirSync(downloadPath, { resursive: true }) + } + paths = { + 'GeoLite2-ASN': path.join(downloadPath, 'GeoLite2-ASN.mmdb'), + 'GeoLite2-City': path.join(downloadPath, 'GeoLite2-City.mmdb'), + 'GeoLite2-Country': path.join(downloadPath, 'GeoLite2-Country.mmdb') + }; + + return new Promise(resolve => { + const us = new UpdateSubscriber() + us.once('update', () => { + us.close() + resolve() + }) + us.update() + }) +} + module.exports = { open, + downloadDbs, UpdateSubscriber, - paths + databases: [ + 'GeoLite2-ASN', + 'GeoLite2-City', + 'GeoLite2-Country' + ], + downloadPath }; diff --git a/package.json b/package.json index c9ce1139..20782338 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "geolite2-redist", - "version": "1.0.7", + "version": "2.0.0", "description": "Redistribution of Maxmind's GeoLite2 Free Databases (without License Key)", "main": "index.js", "types": "index.d.ts", @@ -17,8 +17,8 @@ "geo lookup" ], "scripts": { - "postinstall": "node scripts/postinstall.js", - "test": "mocha --check-leaks --bail --grep ${npm_config_grep:-''} --recursive --timeout 1s --inline-diffs test" + "preload": "node scripts/preload-all.js", + "test": "npm run preload && mocha --check-leaks --bail --grep ${npm_config_grep:-''} --recursive --timeout 1s --inline-diffs test" }, "husky": { "hooks": { diff --git a/scripts/postinstall.js b/scripts/preload-all.js similarity index 100% rename from scripts/postinstall.js rename to scripts/preload-all.js diff --git a/scripts/test b/scripts/test deleted file mode 100755 index d6c7d1ab..00000000 --- a/scripts/test +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -mocha \ - --check-leaks \ - --bail \ - --grep ${npm_config_grep:-''} \ - --recursive \ - --timeout 1s \ - --inline-diffs \ - test