Skip to content

Commit d19d2c1

Browse files
author
Dmitry Shirokov
authored
Merge pull request #28 from runk/key-sources
Alternative way of providing the license key
2 parents efa6148 + 8e8a9f6 commit d19d2c1

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ Maxmind's GeoLite2 Free Databases download helper.
77

88
**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.
99

10+
If you don't have access to the environment variables during installation, you can provide license key via `package.json`:
11+
12+
```json
13+
{
14+
...
15+
"geolite2": {
16+
"license-key": "<your license key>"
17+
}
18+
...
19+
}
20+
```
21+
1022
```javascript
1123
var geolite2 = require('geolite2');
1224
var maxmind = require('maxmind');

scripts/postinstall.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
const fs = require("fs");
2-
const https = require("https");
3-
const zlib = require("zlib");
4-
const path = require("path");
5-
const tar = require("tar");
1+
const fs = require('fs');
2+
const https = require('https');
3+
const zlib = require('zlib');
4+
const path = require('path');
5+
const tar = require('tar');
6+
7+
let licenseKey = process.env.MAXMIND_LICENSE_KEY;
8+
if (!licenseKey) {
9+
try {
10+
const packageJsonFilename = path.join(
11+
process.env['INIT_CWD'],
12+
'package.json'
13+
);
14+
const packageJson = JSON.parse(
15+
fs.readFileSync(packageJsonFilename, 'utf8')
16+
);
17+
licenseKey = packageJson['node-geolite2']['license-key'];
18+
} catch (e) {
19+
console.error("Error reading Maxmind license key from 'package.json'");
20+
console.error(e.message);
21+
}
22+
}
623

7-
const licenseKey = process.env.MAXMIND_LICENSE_KEY;
824
if (!licenseKey) {
925
console.error(`Error: License key is not configured.\n
1026
You need to signup for a _free_ Maxmind account to get a license key.
1127
Go to https://www.maxmind.com/en/geolite2/signup, obtain your key and
12-
put it in the MAXMIND_LICENSE_KEY environment variable\n`);
28+
put it in the MAXMIND_LICENSE_KEY environment variable.
29+
30+
If you don not have access to env vars, put this config in your package.json
31+
file (at the root level) like this:
32+
33+
"geolite2": {
34+
"license-key": "<your license key>"
35+
}
36+
`);
1337
process.exit(1);
1438
}
1539

16-
const link = edition =>
40+
const link = (edition) =>
1741
`https://download.maxmind.com/app/geoip_download?edition_id=${edition}&license_key=${licenseKey}&suffix=tar.gz`;
1842

1943
const links = [
@@ -22,22 +46,22 @@ const links = [
2246
link('GeoLite2-ASN'),
2347
];
2448

25-
const downloadPath = path.join(__dirname, "..", "dbs");
49+
const downloadPath = path.join(__dirname, '..', 'dbs');
2650

2751
if (!fs.existsSync(downloadPath)) fs.mkdirSync(downloadPath);
2852

29-
const download = url =>
30-
new Promise(resolve => {
31-
https.get(url, function(response) {
53+
const download = (url) =>
54+
new Promise((resolve) => {
55+
https.get(url, function (response) {
3256
resolve(response.pipe(zlib.createGunzip({})));
3357
});
3458
});
3559

36-
console.log("Downloading maxmind databases...");
37-
links.forEach(url =>
38-
download(url).then(result =>
39-
result.pipe(tar.t()).on("entry", entry => {
40-
if (entry.path.endsWith(".mmdb")) {
60+
console.log('Downloading maxmind databases...');
61+
links.forEach((url) =>
62+
download(url).then((result) =>
63+
result.pipe(tar.t()).on('entry', (entry) => {
64+
if (entry.path.endsWith('.mmdb')) {
4165
const dstFilename = path.join(downloadPath, path.basename(entry.path));
4266
entry.pipe(fs.createWriteStream(dstFilename));
4367
}

0 commit comments

Comments
 (0)