Skip to content

Commit 8dac474

Browse files
author
Dmitry Shirokov
authored
Merge pull request #34 from runk/db-options
create option to set which dbs are downloaded
2 parents 67d8024 + dc63c19 commit 8dac474

5 files changed

Lines changed: 106 additions & 40 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ package-lock.json
3737
# Optional REPL history
3838
.node_repl_history
3939
dbs/*
40+
41+
.DS_Store

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Maxmind's GeoLite2 Free Databases download helper.
55

66
## Configuration
77

8+
### Access Key
9+
810
**IMPORTANT** You must setup `MAXMIND_LICENSE_KEY` environment variable be able to download databases. To do so, go to the https://www.maxmind.com/en/geolite2/signup, create a free account and generate new license key.
911

1012
If you don't have access to the environment variables during installation, you can provide license key via `package.json`:
@@ -24,6 +26,25 @@ If you don't have access to the environment variables during installation, you c
2426

2527
Beware of security risks of adding keys and secrets to your repository!
2628

29+
### Selecting databases to download
30+
31+
You can select the dbs you want downloaded by adding a `selected-dbs` property on `geolite2` via `package.json`.
32+
33+
`selected-dbs` must be an array of strings, one or more of the values `City`, `Country`, `ASN`.
34+
35+
If `selected-dbs` is unset, or is set but empty, all dbs will be downloaded.
36+
37+
```jsonc
38+
{
39+
...
40+
"geolite2": {
41+
"selected-dbs": ["City", "Country", "ASN"]
42+
}
43+
...
44+
}
45+
```
46+
47+
2748
## Usage
2849

2950
```javascript

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
var path = require("path");
1+
const path = require("path");
2+
3+
const {selectedDbs} = require('./utils');
4+
const selected = selectedDbs();
5+
6+
const makePath = (type) => path.resolve(__dirname, `dbs/GeoLite2-${type}.mmdb`);
7+
8+
const paths = selected.reduce((a,c) => {
9+
a[c.toLowerCase()] = makePath(c);
10+
return a;
11+
}, {});
212

313
module.exports = {
4-
paths: {
5-
city: path.resolve(__dirname, "dbs/GeoLite2-City.mmdb"),
6-
country: path.resolve(__dirname, "dbs/GeoLite2-Country.mmdb"),
7-
asn: path.resolve(__dirname, "dbs/GeoLite2-ASN.mmdb")
8-
}
14+
paths
915
};

scripts/postinstall.js

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
11
const fs = require('fs');
22
const https = require('https');
33
const zlib = require('zlib');
4-
const path = require('path');
54
const tar = require('tar');
6-
7-
const getConfig = () => {
8-
const packageJsonFilename = path.join(
9-
process.env['INIT_CWD'],
10-
'package.json'
11-
);
12-
const packageJson = JSON.parse(fs.readFileSync(packageJsonFilename, 'utf8'));
13-
return packageJson['geolite2'] || {};
14-
};
15-
16-
const keyLoaders = [
17-
() => process.env.MAXMIND_LICENSE_KEY,
18-
() => getConfig()['license-key'],
19-
() => {
20-
const configFile = getConfig()['license-file'];
21-
if (!configFile) return;
22-
23-
const filepath = path.join(process.env['INIT_CWD'], configFile);
24-
return fs.existsSync(filepath)
25-
? fs.readFileSync(filepath, 'utf8').trim()
26-
: undefined;
27-
},
28-
];
5+
const path = require('path');
6+
const {getLicense, selectedDbs} = require('../utils');
297

308
let licenseKey;
31-
329
try {
33-
let i = 0;
34-
while (i < keyLoaders.length) {
35-
licenseKey = keyLoaders[i++]();
36-
if (licenseKey) break;
37-
}
10+
licenseKey = getLicense();
3811
} catch (e) {
3912
console.error('geolite2: Error retrieving Maxmind License Key');
4013
console.error(e.message);
@@ -62,11 +35,14 @@ if (!licenseKey) {
6235
const link = (edition) =>
6336
`https://download.maxmind.com/app/geoip_download?edition_id=${edition}&license_key=${licenseKey}&suffix=tar.gz`;
6437

38+
39+
const selected = selectedDbs();
6540
const links = [
66-
link('GeoLite2-City'),
67-
link('GeoLite2-Country'),
68-
link('GeoLite2-ASN'),
69-
];
41+
'City',
42+
'Country',
43+
'ASN',
44+
].filter(e => selected.includes(e))
45+
.map(e => link(`GeoLite2-${e}`));
7046

7147
const downloadPath = path.join(__dirname, '..', 'dbs');
7248

utils.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const cwdPath = (file) => path.join(process.env['INIT_CWD'] || process.cwd(), file);
5+
6+
const getConfig = () => {
7+
const packageJsonFilename = cwdPath('package.json');
8+
const packageJson = require(packageJsonFilename);
9+
return packageJson['geolite2'] || {};
10+
};
11+
12+
const getLicense = () => {
13+
const envKey = process.env.MAXMIND_LICENSE_KEY;
14+
if(envKey) return envKey;
15+
16+
const config = getConfig();
17+
const licenseKey = config['license-key'];
18+
if(licenseKey) return licenseKey;
19+
20+
const configFile = config['license-file'];
21+
if(!configFile) return;
22+
23+
const configFilePath = cwdPath(configFile);
24+
return fs.existsSync(configFilePath)
25+
? fs.readFileSync(configFilePath, 'utf8').trim()
26+
: undefined;
27+
}
28+
29+
const selectedDbs = () => {
30+
const config = getConfig();
31+
const selected = config['selected-dbs'];
32+
const valids = ['City', 'Country', 'ASN'];
33+
if(!selected) return valids;
34+
if(!Array.isArray(selected)) {
35+
console.error('selected-dbs property must have be an array.');
36+
process.exit(1);
37+
}
38+
39+
if(selected.length === 0) return valids;
40+
41+
if(selected.length > 3) {
42+
console.error('selected-dbs has too many values, there are only three valid values: City, Country, ASN.');
43+
process.exit(1);
44+
}
45+
46+
for(const value of selected) {
47+
const index = valids.indexOf(value);
48+
if(index === -1) {
49+
console.error(`Invalid value in selected-dbs: ${value}. The only valid values are: City, Country, ASN.`);
50+
process.exit(1);
51+
}
52+
}
53+
54+
return selected;
55+
}
56+
57+
module.exports = {
58+
getConfig,
59+
getLicense,
60+
selectedDbs
61+
}

0 commit comments

Comments
 (0)