Skip to content

Commit

Permalink
feat(core): Alternative way of providing the license key
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Shirokov committed Jun 13, 2020
1 parent 20d8d6f commit 2de90b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ Maxmind's GeoLite2 Free Databases download helper.

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

If you don't have access to the environment variables during installation, you can provide license key via `package.json`:

```json
{
...
"geolite2": {
"license-key": "<your license key>"
}
...
}
```

```javascript
var geolite2 = require('geolite2');
var maxmind = require('maxmind');
Expand Down
62 changes: 35 additions & 27 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
const fs = require("fs");
const https = require("https");
const zlib = require("zlib");
const path = require("path");
const tar = require("tar");
const fs = require('fs');
const https = require('https');
const zlib = require('zlib');
const path = require('path');
const tar = require('tar');

let licenseKey = process.env.MAXMIND_LICENSE_KEY;
if (!licenseKey) {
try {
const packageJSONFilename = path.join(process.env["INIT_CWD"], "package.json");
const packageJSON = JSON.parse(fs.readFileSync(packageJSONFilename).toString());
licenseKey = packageJSON["node-geolite2"]["license-key"];
}
catch(e) {
console.error(`Error reading Maxmind license key from package.json:
${e.message}\n`);
const packageJsonFilename = path.join(
process.env['INIT_CWD'],
'package.json'
);
const packageJson = JSON.parse(
fs.readFileSync(packageJsonFilename, 'utf8')
);
licenseKey = packageJson['node-geolite2']['license-key'];
} catch (e) {
console.error("Error reading Maxmind license key from 'package.json'");
console.error(e.message);
}
}

if (!licenseKey) {
console.error(`Error: License key is not configured.\n
You need to signup for a _free_ Maxmind account to get a license key.
Go to https://www.maxmind.com/en/geolite2/signup, obtain your key and
put it in the MAXMIND_LICENSE_KEY environment variable
or in your package.json file (at the root level) like this:
"node-geolite2": {
put it in the MAXMIND_LICENSE_KEY environment variable.
If you don not have access to env vars, put this config in your package.json
file (at the root level) like this:
"geolite2": {
"license-key": "<your license key>"
}\n`);
}
`);
process.exit(1);
}

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

const links = [
Expand All @@ -38,25 +46,25 @@ const links = [
link('GeoLite2-ASN'),
];

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

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

const download = url =>
new Promise(resolve => {
https.get(url, function(response) {
const download = (url) =>
new Promise((resolve) => {
https.get(url, function (response) {
resolve(response.pipe(zlib.createGunzip({})));
});
});

console.log("Downloading maxmind databases...");
links.forEach(url =>
download(url).then(result =>
result.pipe(tar.t()).on("entry", entry => {
if (entry.path.endsWith(".mmdb")) {
console.log('Downloading maxmind databases...');
links.forEach((url) =>
download(url).then((result) =>
result.pipe(tar.t()).on('entry', (entry) => {
if (entry.path.endsWith('.mmdb')) {
const dstFilename = path.join(downloadPath, path.basename(entry.path));
entry.pipe(fs.createWriteStream(dstFilename));
}
})
)
);
);

0 comments on commit 2de90b7

Please sign in to comment.