Skip to content

Commit

Permalink
feat: (breaking) allow specifying custom db download path #11
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared committed Dec 3, 2020
1 parent b9ea94d commit d6d705c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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();
Expand All @@ -92,6 +98,7 @@ import geolite2 from 'geolite2-redist';
import maxmind, { CityResponse } from 'maxmind';

(async () => {
await geolite2.downloadDbs()
let lookup = await geolite2.open<CityResponse>('GeoLite2-City', path => {
return maxmind.open(path);
});
Expand All @@ -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) <string>` Filesystem path to use for storing databases. Can be relative, defaults to '../dbs'.

*geolite2.open(database, databaseReader)*

- `database`: `<string>` One of `GeoLite2-ASN`, `GeoLite2-Country`, `GeoLite2-City`.
Expand All @@ -116,12 +129,12 @@ import maxmind, { CityResponse } from 'maxmind';

### Properties

*geolite2.paths* `<object>`
*geolite2.databases* `<object>`

Full fs paths for each database. **Warning:** you need to let the library update the databases automatically, see Usage section.
- `GeoLite2-ASN`: `<string>`
- `GeoLite2-Country`: `<string>`
- `GeoLite2-City`: `<string>`
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

Expand Down
7 changes: 2 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ 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:
<T>(database: string, readerBuilder: (database: string) =>
(Reader<T> | Promise<Reader<T>>)) => Promise<Reader<T>>
export class UpdateSubscriber extends EventEmitter {
public downloading: boolean;
private _checker: ReturnType<typeof setTimeout>;

downloadDbs(path?: string): Promise<void>;
checkUpdates(): Promise<void>;
update(): void;
triggerUpdate(): Promise<void>;
Expand Down
34 changes: 31 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions scripts/test

This file was deleted.

0 comments on commit d6d705c

Please # to comment.