Skip to content

Commit 62af1ed

Browse files
committed
feat: (breaking) allow specifying custom db download path #11
1 parent 1e1d61d commit 62af1ed

File tree

6 files changed

+63
-35
lines changed

6 files changed

+63
-35
lines changed

README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const geolite2 = require('geolite2-redist');
3232
const maxmind = require('maxmind');
3333

3434
(async () => {
35+
await geolite2.downloadDbs()
3536
let lookup = await geolite2.open('GeoLite2-City', path => {
3637
return maxmind.open(path);
3738
});
@@ -51,29 +52,34 @@ const geolite2 = require('geolite2-redist');
5152
const maxmind = require('maxmind');
5253
const fs = require('fs');
5354

54-
let lookup = geolite2.open('GeoLite2-City', path => {
55-
let buf = fs.readFileSync(path);
56-
return new maxmind.Reader(buf);
57-
});
55+
geolite2.downloadDbs().then(() => {
56+
let lookup = geolite2.open('GeoLite2-City', path => {
57+
let buf = fs.readFileSync(path);
58+
return new maxmind.Reader(buf);
59+
});
5860

59-
let city = lookup.get('66.6.44.4');
61+
let city = lookup.get('66.6.44.4');
6062

61-
lookup.close();
63+
lookup.close();
64+
})
6265
```
6366

6467
### Advanced usage
6568

66-
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.
69+
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.
6770

6871
Example usage:
6972
```javascript
7073
const geolite2 = require('geolite2-redist');
7174

75+
const dbBasePath = '/tmp/maxmind'
76+
7277
function useGeolite() {
73-
// You can retrieve the path to `.mmdb` files
74-
let cityPath = geolite2.paths['GeoLite2-City'];
78+
// Do something with the databases
7579
}
7680

81+
await geolite2.downloadDbs(dbBasePath)
82+
7783
const dbWatcher = new geolite2.UpdateSubscriber();
7884
dbWatcher.on('update', () => {
7985
useGeolite();
@@ -92,6 +98,7 @@ import geolite2 from 'geolite2-redist';
9298
import maxmind, { CityResponse } from 'maxmind';
9399

94100
(async () => {
101+
await geolite2.downloadDbs()
95102
let lookup = await geolite2.open<CityResponse>('GeoLite2-City', path => {
96103
return maxmind.open(path);
97104
});
@@ -107,6 +114,12 @@ import maxmind, { CityResponse } from 'maxmind';
107114

108115
### Methods
109116

117+
*geolite2.downloadDbs(path?)*
118+
119+
This function returns a Promise that resolves when GeoIP databases have been succesfully retrieved from the redistribution.
120+
121+
- `path`: `(optional) <string>` Filesystem path to use for storing databases. Can be relative, defaults to '../dbs'.
122+
110123
*geolite2.open(database, databaseReader)*
111124

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

117130
### Properties
118131

119-
*geolite2.paths* `<object>`
132+
*geolite2.databases* `<object>`
120133

121-
Full fs paths for each database. **Warning:** you need to let the library update the databases automatically, see Usage section.
122-
- `GeoLite2-ASN`: `<string>`
123-
- `GeoLite2-Country`: `<string>`
124-
- `GeoLite2-City`: `<string>`
134+
See what databases you can use with `open()`. **Warning:** you need to let the library update the databases automatically, see Usage section.
135+
- `GeoLite2-ASN`
136+
- `GeoLite2-Country`
137+
- `GeoLite2-City`
125138

126139
### Classes
127140

index.d.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import { EventEmitter } from "events";
22
import { Reader } from "maxmind";
33

44
declare module "geolite2-redist" {
5-
export const paths: {
6-
"GeoLite2-ASN": string;
7-
"GeoLite2-City": string;
8-
"GeoLite2-Country": string;
9-
}
5+
export const databases: string[]
106
export const open:
117
<T>(database: string, readerBuilder: (database: string) =>
128
(Reader<T> | Promise<Reader<T>>)) => Promise<Reader<T>>
139
export class UpdateSubscriber extends EventEmitter {
1410
public downloading: boolean;
1511
private _checker: ReturnType<typeof setTimeout>;
1612

13+
downloadDbs(path?: string): Promise<void>;
1714
checkUpdates(): Promise<void>;
1815
update(): void;
1916
triggerUpdate(): Promise<void>;

index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const {EventEmitter} = require('events');
44
const rimraf = require('rimraf');
55

66
const downloadHelper = require('./scripts/download-helper.js');
7-
const downloadPath = path.resolve(__dirname, 'dbs');
7+
let downloadPath = path.resolve(__dirname, 'dbs');
88

99
const updateTimer = 2 * 24 * 60 * 60 * 1000; // 48 hours in ms
1010

11-
const paths = {
11+
let paths = {
1212
'GeoLite2-ASN': path.join(downloadPath, 'GeoLite2-ASN.mmdb'),
1313
'GeoLite2-City': path.join(downloadPath, 'GeoLite2-City.mmdb'),
1414
'GeoLite2-Country': path.join(downloadPath, 'GeoLite2-Country.mmdb')
@@ -176,8 +176,36 @@ function open(database, readerBuilder) {
176176
return wrapReader(reader, readerBuilder, database);
177177
}
178178

179+
function downloadDbs(newpath) {
180+
downloadPath = path.resolve(__dirname, 'dbs')
181+
if (newpath) downloadPath = path.resolve(newpath);
182+
if (!fs.existsSync(downloadPath)) {
183+
fs.mkdirSync(downloadPath, { resursive: true })
184+
}
185+
paths = {
186+
'GeoLite2-ASN': path.join(downloadPath, 'GeoLite2-ASN.mmdb'),
187+
'GeoLite2-City': path.join(downloadPath, 'GeoLite2-City.mmdb'),
188+
'GeoLite2-Country': path.join(downloadPath, 'GeoLite2-Country.mmdb')
189+
};
190+
191+
return new Promise(resolve => {
192+
const us = new UpdateSubscriber()
193+
us.once('update', () => {
194+
us.close()
195+
resolve()
196+
})
197+
us.update()
198+
})
199+
}
200+
179201
module.exports = {
180202
open,
203+
downloadDbs,
181204
UpdateSubscriber,
182-
paths
205+
databases: [
206+
'GeoLite2-ASN',
207+
'GeoLite2-City',
208+
'GeoLite2-Country'
209+
],
210+
downloadPath
183211
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "geolite2-redist",
3-
"version": "1.0.7",
3+
"version": "2.0.0",
44
"description": "Redistribution of Maxmind's GeoLite2 Free Databases (without License Key)",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -17,8 +17,8 @@
1717
"geo lookup"
1818
],
1919
"scripts": {
20-
"postinstall": "node scripts/postinstall.js",
21-
"test": "mocha --check-leaks --bail --grep ${npm_config_grep:-''} --recursive --timeout 1s --inline-diffs test"
20+
"preload": "node scripts/preload-all.js",
21+
"test": "npm run preload && mocha --check-leaks --bail --grep ${npm_config_grep:-''} --recursive --timeout 1s --inline-diffs test"
2222
},
2323
"husky": {
2424
"hooks": {
File renamed without changes.

scripts/test

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)