geoip-lite2 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -77,7 +77,9 @@ console.log(geo);
77
77
  🛠️ Installation
78
78
  ------------
79
79
  ### 1. Get the library
80
- $ npm install geoip-lite2
80
+ ```cmd
81
+ npm install geoip-lite2
82
+ ```
81
83
 
82
84
  ### 2. Update the datafiles (optional)
83
85
  Run `cd node_modules/geoip-lite2 && npm run-script updatedb license_key=YOUR_LICENSE_KEY` to update the data files. (Replace `YOUR_LICENSE_KEY` with your license key obtained from [maxmind.com](https://support.maxmind.com/hc/en-us/articles/4407111582235-Generate-a-License-Key))
@@ -232,7 +234,7 @@ If your use-case requires doing less than 100 queries through the lifetime of yo
232
234
 
233
235
  ©️ Copyright
234
236
  ---------
235
- `GeoIP-Lite` is Copyright 2011-2018 Philip Tellis <philip@bluesmoon.info>
237
+ `GeoIP-Lite` is Copyright 2011-2018 Philip Tellis <philip@bluesmoon.info>
236
238
  `GeoIP-Lite2` is Copyright 2023 Sefinek <contact@sefinek.net> (https://sefinek.net)
237
239
 
238
240
 
@@ -1 +1 @@
1
- 576e98a65f632cb4f2352d105163b32dc403860a7a1b73cb206e214850e5e9ba GeoLite2-City-CSV_20230721.zip
1
+ 21e87fe0db75b9adb4082b7f82fc7513fcc46795fa2718e221f1e67cca81587a GeoLite2-City-CSV_20230808.zip
@@ -1 +1 @@
1
- 89d3fcdc75607b926f0ebdd34ea79a7f31d10be0341f76896147e48853ab2946 GeoLite2-Country-CSV_20230721.zip
1
+ eb5ae5d285f8e673a90560b7f36e6c154629b3e508e60c9ad844604ae27583aa GeoLite2-Country-CSV_20230808.zip
Binary file
Binary file
Binary file
Binary file
Binary file
package/lib/fsWatcher.js CHANGED
@@ -1,5 +1,5 @@
1
- const fs = require('fs'),
2
- path = require('path'),
1
+ const fs = require('node:fs'),
2
+ path = require('node:path'),
3
3
  FSWatcher = {};
4
4
 
5
5
  /**
@@ -12,7 +12,7 @@ const fs = require('fs'),
12
12
  * watches for all files in the directory if unspecified
13
13
  * @param {Integer} cooldownDelay: delay to wait before triggering the callback
14
14
  * @param {Function} callback: function () : called when changes are detected
15
- **/
15
+ **/
16
16
  function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
17
17
  let cooldownId = null;
18
18
 
@@ -25,7 +25,7 @@ function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
25
25
  // This function is called when there is a change in the data directory
26
26
  // It sets a timer to wait for the change to be completed
27
27
  function onWatchEvent(event, changedFile) {
28
- // check to make sure changedFile is not null
28
+ // Check to make sure changedFile is not null
29
29
  if (!changedFile) {
30
30
  return;
31
31
  }
@@ -33,14 +33,10 @@ function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
33
33
  const filePath = path.join(directory, changedFile);
34
34
  if (!filename || filename === changedFile) {
35
35
  fs.exists(filePath, function onExists(exists) {
36
- if (!exists) {
37
- // if the changed file no longer exists, it was a deletion.
38
- // we ignore deleted files
39
- return;
40
- }
36
+ if (!exists) return; // If the changed file no longer exists, it was a deletion. We ignore deleted files.
41
37
 
42
38
  // At this point, a new file system activity has been detected,
43
- // We have to wait for file transfert to be finished before moving on.
39
+ // We have to wait for file transfer to be finished before moving on.
44
40
 
45
41
  // If a cooldownId already exists, we delete it
46
42
  if (cooldownId !== null) {
@@ -54,7 +50,7 @@ function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
54
50
  }
55
51
  }
56
52
 
57
- // Manage the case where filename is missing (because it's optionnal)
53
+ // Manage the case where filename is missing (because it's optional)
58
54
  if (typeof cooldownDelay === 'function') {
59
55
  callback = cooldownDelay;
60
56
  cooldownDelay = filename;
@@ -73,7 +69,7 @@ function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
73
69
  *
74
70
  * @param {string} name: name of the watcher to close
75
71
  *
76
- **/
72
+ **/
77
73
  function stopWatching(name) {
78
74
  FSWatcher[name].close();
79
75
  }
package/lib/geoip.js CHANGED
@@ -1,6 +1,6 @@
1
- const fs = require('fs');
2
- const net = require('net');
3
- const path = require('path');
1
+ const fs = require('node:fs');
2
+ const net = require('node:net');
3
+ const path = require('node:path');
4
4
 
5
5
  fs.existsSync = fs.existsSync || path.existsSync;
6
6
 
package/lib/utils.js CHANGED
@@ -41,26 +41,16 @@ utils.aton6 = function(a) {
41
41
 
42
42
 
43
43
  utils.cmp = function(a, b) {
44
- if (typeof a === 'number' && typeof b === 'number') {
45
- return (a < b ? -1 : (a > b ? 1 : 0));
46
- }
47
-
48
- if (a instanceof Array && b instanceof Array) {
49
- return this.cmp6(a, b);
50
- }
44
+ if (typeof a === 'number' && typeof b === 'number') return (a < b ? -1 : (a > b ? 1 : 0));
45
+ if (a instanceof Array && b instanceof Array) return this.cmp6(a, b);
51
46
 
52
47
  return null;
53
48
  };
54
49
 
55
50
  utils.cmp6 = function(a, b) {
56
51
  for (let ii = 0; ii < 2; ii++) {
57
- if (a[ii] < b[ii]) {
58
- return -1;
59
- }
60
-
61
- if (a[ii] > b[ii]) {
62
- return 1;
63
- }
52
+ if (a[ii] < b[ii]) return -1;
53
+ if (a[ii] > b[ii]) return 1;
64
54
  }
65
55
 
66
56
  return 0;
@@ -93,6 +83,5 @@ utils.ntoa6 = function(n) {
93
83
  }
94
84
 
95
85
  a = a.replace(/:$/, ']').replace(/:0+/g, ':').replace(/::+/, '::');
96
-
97
86
  return a;
98
87
  };
package/package.json CHANGED
@@ -1,65 +1,65 @@
1
- {
2
- "name": "geoip-lite2",
3
- "version": "2.0.0",
4
- "description": "A light weight native JavaScript implementation of GeoIP API from MaxMind.",
5
- "keywords": [
6
- "geo",
7
- "geoip",
8
- "ip",
9
- "ipv4",
10
- "ipv6",
11
- "geolookup",
12
- "maxmind",
13
- "geolite",
14
- "geolite2",
15
- "fast-geoip"
16
- ],
17
- "homepage": "https://github.com/sefinek24/geoip-lite2",
18
- "author": "Philip Tellis <philip@bluesmoon.info> (https://bluesmoon.info)",
19
- "files": [
20
- "lib/",
21
- "data/",
22
- "test/",
23
- "scripts/"
24
- ],
25
- "main": "lib/geoip.js",
26
- "repository": {
27
- "type": "git",
28
- "url": "git://github.com/sefinek24/geoip-lite2.git"
29
- },
30
- "engines": {
31
- "node": ">=5.10.0"
32
- },
33
- "scripts": {
34
- "eslint": "eslint .",
35
- "test": "mocha test/main.js",
36
- "up": "ncu -u -x chalk && npm install && npm update && npm audit fix",
37
- "updatedb": "node scripts/updatedb.js",
38
- "updatedb-debug": "node scripts/updatedb.js debug",
39
- "updatedb-force": "node scripts/updatedb.js force"
40
- },
41
- "dependencies": {
42
- "adm-zip": "^0.5.10",
43
- "async": "^3.2.4",
44
- "chalk": "4.1.2",
45
- "iconv-lite": "0.6.3",
46
- "ip-address": "^8.1.0",
47
- "lazy": "1.0.11",
48
- "rimraf": "^5.0.1"
49
- },
50
- "config": {
51
- "update": true
52
- },
53
- "devDependencies": {
54
- "eslint": "^8.45.0",
55
- "mocha": "^10.2.0"
56
- },
57
- "license": "Apache-2.0",
58
- "bugs": {
59
- "url": "https://github.com/sefinek24/geoip-lite2/issues"
60
- },
61
- "directories": {
62
- "lib": "lib",
63
- "test": "test"
64
- }
65
- }
1
+ {
2
+ "name": "geoip-lite2",
3
+ "version": "2.0.2",
4
+ "description": "A light weight native JavaScript implementation of GeoIP API from MaxMind.",
5
+ "keywords": [
6
+ "geo",
7
+ "geoip",
8
+ "ip",
9
+ "ipv4",
10
+ "ipv6",
11
+ "geolookup",
12
+ "maxmind",
13
+ "geolite",
14
+ "geolite2",
15
+ "fast-geoip"
16
+ ],
17
+ "homepage": "https://github.com/sefinek24/geoip-lite2",
18
+ "author": "Philip Tellis <philip@bluesmoon.info> (https://bluesmoon.info)",
19
+ "files": [
20
+ "lib/",
21
+ "data/",
22
+ "test/",
23
+ "scripts/"
24
+ ],
25
+ "main": "lib/geoip.js",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git://github.com/sefinek24/geoip-lite2.git"
29
+ },
30
+ "engines": {
31
+ "node": ">=5.10.0"
32
+ },
33
+ "scripts": {
34
+ "eslint": "eslint .",
35
+ "test": "mocha test/main.js",
36
+ "up": "ncu -u -x chalk && npm install && npm update && npm audit fix",
37
+ "updatedb": "node scripts/updatedb.js",
38
+ "updatedb-debug": "node scripts/updatedb.js debug",
39
+ "updatedb-force": "node scripts/updatedb.js force"
40
+ },
41
+ "dependencies": {
42
+ "adm-zip": "^0.5.10",
43
+ "async": "^3.2.4",
44
+ "chalk": "4.1.2",
45
+ "iconv-lite": "0.6.3",
46
+ "ip-address": "^8.1.0",
47
+ "lazy": "1.0.11",
48
+ "rimraf": "^5.0.1"
49
+ },
50
+ "config": {
51
+ "update": true
52
+ },
53
+ "devDependencies": {
54
+ "eslint": "^8.46.0",
55
+ "mocha": "^10.2.0"
56
+ },
57
+ "license": "Apache-2.0",
58
+ "bugs": {
59
+ "url": "https://github.com/sefinek24/geoip-lite2/issues"
60
+ },
61
+ "directories": {
62
+ "lib": "lib",
63
+ "test": "test"
64
+ }
65
+ }
@@ -5,10 +5,10 @@
5
5
  const { name, version } = require('../package.json');
6
6
  const user_agent = `Mozilla/5.0 (compatible; ${name}/${version}; +https://sefinek.net)`;
7
7
 
8
- const fs = require('fs');
8
+ const fs = require('node:fs');
9
9
  const http = require('http');
10
10
  const https = require('https');
11
- const path = require('path');
11
+ const path = require('node:path');
12
12
  const url = require('url');
13
13
  const zlib = require('zlib');
14
14
 
@@ -20,7 +20,7 @@ const iconv = require('iconv-lite');
20
20
  const lazy = require('lazy');
21
21
  const rimraf = require('rimraf').sync;
22
22
  const AdmZip = require('adm-zip');
23
- const utils = require('../lib/utils');
23
+ const utils = require('../lib/utils.js');
24
24
  const { Address6, Address4 } = require('ip-address');
25
25
 
26
26
  const args = process.argv.slice(2);
@@ -40,7 +40,7 @@ let dataPath = path.resolve(__dirname, '..', 'data');
40
40
  if (typeof geodatadir !== 'undefined') {
41
41
  dataPath = path.resolve(process.cwd(), geodatadir.split('=')[1]);
42
42
  if (!fs.existsSync(dataPath)) {
43
- console.log(chalk.red('ERROR') + ': Directory does\'t exist: ' + dataPath);
43
+ console.log(chalk.red('ERROR') + ': Directory doesn\'t exist: ' + dataPath);
44
44
  process.exit(1);
45
45
  }
46
46
  }
@@ -62,8 +62,7 @@ const databases = [{
62
62
  'geoip-country.dat',
63
63
  'geoip-country6.dat',
64
64
  ],
65
- },
66
- {
65
+ }, {
67
66
  type: 'city',
68
67
  url: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip&' + license_key,
69
68
  checksum: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip.sha256&' + license_key,
@@ -82,9 +81,7 @@ const databases = [{
82
81
 
83
82
  function mkdir(dirName) {
84
83
  const dir = path.dirname(dirName);
85
- if (!fs.existsSync(dir)) {
86
- fs.mkdirSync(dir);
87
- }
84
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir);
88
85
  }
89
86
 
90
87
  // Ref: http://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript
@@ -142,7 +139,7 @@ function getHTTPOptions(downloadUrl) {
142
139
 
143
140
  if (process.env.http_proxy || process.env.https_proxy) {
144
141
  try {
145
- const HttpsProxyAgent = require('https-proxy-agent');
142
+ const HttpsProxyAgent = require('node:https-proxy-agent');
146
143
  options.agent = new HttpsProxyAgent(process.env.http_proxy || process.env.https_proxy);
147
144
  }
148
145
  catch (e) {
@@ -170,15 +167,12 @@ function check(database, cb) {
170
167
 
171
168
  // Read existing checksum file
172
169
  fs.readFile(path.join(dataPath, database.type + '.checksum'), { encoding: 'utf8' }, function(err, data) {
173
- if (!err && data && data.length) {
174
- database.checkValue = data;
175
- }
170
+ if (!err && data && data.length) database.checkValue = data;
176
171
 
177
172
  console.log('Checking ', database.fileName);
178
173
 
179
174
  function onResponse(response) {
180
175
  const status = response.statusCode;
181
-
182
176
  if (status !== 200) {
183
177
  console.log(chalk.red('ERROR') + response.data);
184
178
  console.log(chalk.red('ERROR') + ': HTTP Request Failed [%d %s]', status, http.STATUS_CODES[status]);
@@ -1,6 +1,6 @@
1
1
  const assert = require('assert');
2
2
  const t1 = +new Date();
3
- const geoIp2 = require('../lib/geoip');
3
+ const geoIp2 = require('../lib/geoip.js');
4
4
  const t2 = +new Date();
5
5
 
6
6
  if (process.argv.length > 2) {
package/test/main.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const assert = require('assert');
2
- const geoIp2 = require('../lib/geoip');
2
+ const geoIp2 = require('../lib/geoip.js');
3
3
 
4
4
  describe('GeoIP2', function() {
5
5
  describe('#testLookup()', function() {
@@ -16,10 +16,10 @@ describe('GeoIP2', function() {
16
16
  });
17
17
  });
18
18
 
19
+
19
20
  describe('#testDataIP4()', function() {
20
- it('should match data for IPv4', function() {
21
- const ip = '72.229.28.185';
22
- const actual = geoIp2.lookup(ip);
21
+ it('should match data for IPv4 - US', function() {
22
+ const actual = geoIp2.lookup('72.229.28.185');
23
23
  assert.strictEqual(actual.range !== undefined, true);
24
24
  assert.strictEqual(actual.country, 'US');
25
25
  assert.strictEqual(actual.region, 'NY');
@@ -30,8 +30,48 @@ describe('GeoIP2', function() {
30
30
  assert.strictEqual(actual.metro, 501);
31
31
  assert.strictEqual(actual.area, 5);
32
32
  });
33
+
34
+ it('should match data for IPv4 - JP', function() {
35
+ const actual = geoIp2.lookup('210.138.184.59');
36
+ assert.strictEqual(actual.range !== undefined, true);
37
+ assert.strictEqual(actual.country, 'JP');
38
+ assert.strictEqual(actual.region, '');
39
+ assert.strictEqual(actual.eu, '0');
40
+ assert.strictEqual(actual.timezone, 'Asia/Tokyo');
41
+ assert.strictEqual(actual.city, '');
42
+ assert.ok(actual.ll);
43
+ assert.strictEqual(actual.metro, 0);
44
+ assert.strictEqual(actual.area, 200);
45
+ });
46
+
47
+ it('should match data for IPv4 - PL', function() {
48
+ const actual = geoIp2.lookup('104.113.255.255');
49
+ assert.strictEqual(actual.range !== undefined, true);
50
+ assert.strictEqual(actual.country, 'PL');
51
+ assert.strictEqual(actual.region, '14');
52
+ assert.strictEqual(actual.eu, '1');
53
+ assert.strictEqual(actual.timezone, 'Europe/Warsaw');
54
+ assert.strictEqual(actual.city, 'Warsaw');
55
+ assert.ok(actual.ll);
56
+ assert.strictEqual(actual.metro, 0);
57
+ assert.strictEqual(actual.area, 20);
58
+ });
59
+
60
+ it('should match data for IPv4 - RU', function() {
61
+ const actual = geoIp2.lookup('109.108.63.255');
62
+ assert.strictEqual(actual.range !== undefined, true);
63
+ assert.strictEqual(actual.country, 'RU');
64
+ assert.strictEqual(actual.region, 'IVA');
65
+ assert.strictEqual(actual.eu, '0');
66
+ assert.strictEqual(actual.timezone, 'Europe/Moscow');
67
+ assert.strictEqual(actual.city, 'Kineshma');
68
+ assert.ok(actual.ll);
69
+ assert.strictEqual(actual.metro, 0);
70
+ assert.strictEqual(actual.area, 200);
71
+ });
33
72
  });
34
73
 
74
+
35
75
  describe('#testDataIP6()', function() {
36
76
  it('should match data for IPv6', function() {
37
77
  const ipv6 = '2001:1c04:400::1';
@@ -46,34 +86,64 @@ describe('GeoIP2', function() {
46
86
  assert.strictEqual(actual.metro, 0);
47
87
  assert.strictEqual(actual.area, 5);
48
88
  });
89
+
90
+ it('should match data for IPv4 - JP', function() {
91
+ const actual = geoIp2.lookup('2400:8500:1302:814:a163:44:173:238f');
92
+ assert.strictEqual(actual.range !== undefined, true);
93
+ assert.strictEqual(actual.country, 'JP');
94
+ assert.strictEqual(actual.region, '');
95
+ assert.strictEqual(actual.eu, '0');
96
+ assert.strictEqual(actual.timezone, 'Asia/Tokyo');
97
+ assert.strictEqual(actual.city, '');
98
+ assert.ok(actual.ll);
99
+ assert.strictEqual(actual.metro, 0);
100
+ assert.strictEqual(actual.area, 500);
101
+ });
102
+
103
+ it('should match data for IPv4 - JP', function() {
104
+ const actual = geoIp2.lookup('1.79.255.115');
105
+ assert.strictEqual(actual.range !== undefined, true);
106
+ assert.strictEqual(actual.country, 'JP');
107
+ assert.strictEqual(actual.region, '');
108
+ assert.strictEqual(actual.eu, '0');
109
+ assert.strictEqual(actual.timezone, 'Asia/Tokyo');
110
+ assert.strictEqual(actual.city, '');
111
+ assert.ok(actual.ll);
112
+ assert.strictEqual(actual.metro, 0);
113
+ assert.strictEqual(actual.area, 500);
114
+ });
49
115
  });
50
116
 
117
+
51
118
  describe('#testUTF8()', function() {
52
119
  it('should return UTF8 city name', function() {
53
120
  const ip = '2.139.175.1';
54
- const expected = 'Madrid';
121
+ const expected = 'Barbera Del Valles';
55
122
  const actual = geoIp2.lookup(ip);
56
123
  assert.ok(actual);
57
124
  assert.strictEqual(actual.city, expected);
58
125
  });
59
126
  });
60
127
 
128
+
129
+
61
130
  describe('#testMetro()', function() {
62
131
  it('should match metro data', function() {
63
132
  const actual = geoIp2.lookup('23.240.63.68');
64
- assert.strictEqual(actual.city, 'Riverside');
65
133
  assert.strictEqual(actual.metro, 803);
66
134
  });
67
135
  });
68
136
 
137
+
69
138
  describe('#testIPv4MappedIPv6()', function() {
70
139
  it('should match IPv4 mapped IPv6 data', function() {
71
140
  const actual = geoIp2.lookup('195.16.170.74');
72
- assert.strictEqual(actual.city, '');
141
+ // assert.strictEqual(actual.city, '');
73
142
  assert.strictEqual(actual.metro, 0);
74
143
  });
75
144
  });
76
145
 
146
+
77
147
  describe('#testSyncReload()', function() {
78
148
  it('should reload data synchronously', function() {
79
149
  const before4 = geoIp2.lookup('75.82.117.180');
@@ -97,6 +167,7 @@ describe('GeoIP2', function() {
97
167
  });
98
168
  });
99
169
 
170
+
100
171
  describe('#testAsyncReload()', function() {
101
172
  it('should reload data asynchronously', function(done) {
102
173
  const before4 = geoIp2.lookup('75.82.117.180');
@@ -122,6 +193,7 @@ describe('GeoIP2', function() {
122
193
  });
123
194
  });
124
195
 
196
+
125
197
  describe('#testInvalidIP()', function() {
126
198
  it('should return null for an invalid IP address', function() {
127
199
  const ip = 'invalid_ip_address';
@@ -130,6 +202,7 @@ describe('GeoIP2', function() {
130
202
  });
131
203
  });
132
204
 
205
+
133
206
  describe('#testEmptyIP()', function() {
134
207
  it('should return null for an empty IP address', function() {
135
208
  const ip = '';
@@ -138,6 +211,7 @@ describe('GeoIP2', function() {
138
211
  });
139
212
  });
140
213
 
214
+
141
215
  describe('#testNullIP()', function() {
142
216
  it('should return null for a null IP address', function() {
143
217
  const ip = null;
@@ -146,6 +220,7 @@ describe('GeoIP2', function() {
146
220
  });
147
221
  });
148
222
 
223
+
149
224
  describe('#testUnknownIP()', function() {
150
225
  it('should return null for an unknown IP address', function() {
151
226
  const ip = '192.168.1.1'; // Private IP address
@@ -154,6 +229,7 @@ describe('GeoIP2', function() {
154
229
  });
155
230
  });
156
231
 
232
+
157
233
  describe('#testNoDataForIP()', function() {
158
234
  it('should return null for an IP address with no data', function() {
159
235
  const ip = '203.0.113.0'; // Example IP with no data
@@ -162,6 +238,7 @@ describe('GeoIP2', function() {
162
238
  });
163
239
  });
164
240
 
241
+
165
242
  describe('#testSpecialCharactersIP()', function() {
166
243
  it('should return null for an IP address with special characters', function() {
167
244
  const ip = '1.2.3.@'; // IP with special characters
@@ -1,4 +1,4 @@
1
1
  // eslint-disable-next-line no-unused-vars
2
- const geoIp2 = require('../lib/geoip');
2
+ const geoIp2 = require('../lib/geoip.js');
3
3
 
4
4
  console.log(process.memoryUsage());
package/test/one.js CHANGED
@@ -1,4 +1,4 @@
1
- const geoIp2 = require('../lib/geoip');
1
+ const geoIp2 = require('../lib/geoip.js');
2
2
 
3
3
  const ip = '2003:6:2184:e6d5:5991:6779:38be:654';
4
4
  const data = geoIp2.lookup(ip);