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 ;
824if ( ! 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
1943const 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
2751if ( ! 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