geoip-lite2 2.3.0-alpha.0 → 3.0.0-alpha.0
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 +18 -25
- package/data/geoip-city6.dat +0 -0
- package/data/geoip-country6.dat +0 -0
- package/example/lookup-watch.js +15 -0
- package/index.d.ts +5 -6
- package/index.js +164 -338
- package/package.json +32 -41
- package/scripts/fsWatcher.js +70 -0
- package/scripts/utils.js +226 -0
- package/tools/updatedb.js +786 -730
- package/fsWatcher.js +0 -85
- package/utils.js +0 -103
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
# 🚀 Improved GeoIP Module by [Sefinek](https://sefinek.net)
|
|
19
|
-
|
|
19
|
+
Actively maintained and optimized fork of [geoip-lite](https://github.com/geoip-lite/node-geoip), originally created by [Philip Tellis](AUTHORS).
|
|
20
20
|
Fully native JS implementation with synchronous, in-memory lookups for IPv4 and IPv6.
|
|
21
21
|
Includes automated test coverage using [Jest](https://www.npmjs.com/package/jest).
|
|
22
22
|
|
|
@@ -37,7 +37,7 @@ Includes automated test coverage using [Jest](https://www.npmjs.com/package/jest
|
|
|
37
37
|
npm install geoip-lite2
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
### 2. Update the data files (
|
|
40
|
+
### 2. Update the data files (required)
|
|
41
41
|
Run `cd node_modules/geoip-lite2 && npm run 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).
|
|
42
42
|
|
|
43
43
|
## 📝 Short Example
|
|
@@ -51,10 +51,9 @@ console.log(geoIp.lookup(ip));
|
|
|
51
51
|
### Output
|
|
52
52
|
```json
|
|
53
53
|
{
|
|
54
|
-
"range": [ 2450746624, 2450746879 ],
|
|
55
54
|
"country": "PL",
|
|
56
55
|
"region": "14",
|
|
57
|
-
"
|
|
56
|
+
"isEu": true,
|
|
58
57
|
"timezone": "Europe/Warsaw",
|
|
59
58
|
"city": "Warsaw",
|
|
60
59
|
"ll": [ 52.2296, 21.0067 ],
|
|
@@ -66,13 +65,14 @@ console.log(geoIp.lookup(ip));
|
|
|
66
65
|
|
|
67
66
|
## 🌐 Live Demo API
|
|
68
67
|
You can see this module in action using my [official API](https://api.sefinek.net).
|
|
69
|
-
- Specific IP: https://api.sefinek.net/api/v2/geoip/109.207.159.255 (
|
|
68
|
+
- Specific IP: https://api.sefinek.net/api/v2/geoip/109.207.159.255 (not for production use)
|
|
70
69
|
- Client's IP: https://api.sefinek.net/api/v2/geoip/me
|
|
71
70
|
- Documentation: https://api.sefinek.net/docs/v2
|
|
72
71
|
|
|
73
72
|
|
|
74
73
|
## 📚 Library API
|
|
75
|
-
GeoIP-Lite2
|
|
74
|
+
GeoIP-Lite2 performs lookups synchronously in memory (`lookup`). It also provides async data reload methods (`reloadData`) and file watcher callbacks (`startWatchingDataUpdate`).
|
|
75
|
+
All blocking file I/O is performed at startup, so all runtime lookups are fast.
|
|
76
76
|
Startup may take up to 200 ms while reading and indexing data files into memory.
|
|
77
77
|
|
|
78
78
|
### Looking up an IP address
|
|
@@ -83,17 +83,18 @@ pass it to the `lookup` method. Remember to remove any `[` and `]` around an IPv
|
|
|
83
83
|
const geo = geoIp.lookup(ip);
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
+
If `ip` is missing (`undefined` or `null`), `lookup` throws a `TypeError`.
|
|
87
|
+
|
|
86
88
|
If the IP address was found, the `lookup` method returns an object with the following structure:
|
|
87
89
|
|
|
88
90
|
```js
|
|
89
91
|
{
|
|
90
|
-
range: [ <low bound of IP block>, <high bound of IP block> ],
|
|
91
92
|
country: 'CC', // 2 letter ISO-3166-1 country code
|
|
92
93
|
region: 'RR', // Up to 3 alphanumeric characters as ISO 3166-2 code
|
|
93
94
|
// For US states this is the 2 letter state
|
|
94
95
|
// For the United Kingdom this could be ENG as a country like "England"
|
|
95
96
|
// FIPS 10-4 subcountry code
|
|
96
|
-
|
|
97
|
+
isEu: true, // true if the country is a member state of the European Union, otherwise false.
|
|
97
98
|
timezone: 'Country/Zone', // Timezone from IANA Time Zone Database
|
|
98
99
|
city: 'City name', // Full city name
|
|
99
100
|
ll: [<latitude>, <longitude>], // Latitude and longitude of the city
|
|
@@ -104,17 +105,6 @@ If the IP address was found, the `lookup` method returns an object with the foll
|
|
|
104
105
|
|
|
105
106
|
If the IP address was not found, `lookup` returns `null`.
|
|
106
107
|
|
|
107
|
-
### Pretty printing an IP address
|
|
108
|
-
If you have a 32-bit unsigned integer or a number returned as part of the `range` array,
|
|
109
|
-
you can use the `pretty` method to get a human-readable format.
|
|
110
|
-
|
|
111
|
-
```js
|
|
112
|
-
console.log('IP is %s', geoIp.pretty(ip));
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
The method returns a string if the input format is recognized, otherwise it returns the input itself.
|
|
116
|
-
|
|
117
|
-
|
|
118
108
|
## 🔄 Built-in Updater
|
|
119
109
|
This package contains an update script that downloads files from MaxMind and handles CSV conversion.
|
|
120
110
|
A npm script alias has been configured to simplify this process. Internet access is required and MaxMind download limits apply.
|
|
@@ -144,10 +134,13 @@ You can do it programmatically, calling after scheduled data updates
|
|
|
144
134
|
// Synchronously
|
|
145
135
|
geoIp.reloadDataSync();
|
|
146
136
|
|
|
147
|
-
// Asynchronously
|
|
137
|
+
// Asynchronously (callback)
|
|
148
138
|
geoIp.reloadData(() => {
|
|
149
139
|
console.log('Done');
|
|
150
140
|
});
|
|
141
|
+
|
|
142
|
+
// Asynchronously (Promise)
|
|
143
|
+
await geoIp.reloadData();
|
|
151
144
|
```
|
|
152
145
|
|
|
153
146
|
#### Automatic Start and stop watching for data updates
|
|
@@ -163,11 +156,11 @@ This tool can be used with `npm run updatedb` to periodically update geo data on
|
|
|
163
156
|
The following environment variables can be set.
|
|
164
157
|
|
|
165
158
|
```bash
|
|
166
|
-
# Override the default node_modules/geoip-
|
|
167
|
-
GEOTMPDIR=/some/path
|
|
168
|
-
|
|
169
|
-
# Override the default node_modules/geoip-lite/tmp dir
|
|
159
|
+
# Override the default node_modules/geoip-lite2/data dir
|
|
170
160
|
GEODATADIR=/some/path
|
|
161
|
+
|
|
162
|
+
# Override the default node_modules/geoip-lite2/tmp dir
|
|
163
|
+
GEOTMPDIR=/some/path
|
|
171
164
|
```
|
|
172
165
|
|
|
173
166
|
|
|
@@ -210,4 +203,4 @@ console.log(process.memoryUsage());
|
|
|
210
203
|
|
|
211
204
|
|
|
212
205
|
## 🔐 License
|
|
213
|
-
There are two licenses for the code and data. See the [LICENSE](LICENSE) file for details.
|
|
206
|
+
There are two licenses for the code and data. See the [LICENSE](LICENSE) file for details.
|
package/data/geoip-city6.dat
CHANGED
|
Binary file
|
package/data/geoip-country6.dat
CHANGED
|
Binary file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const geoIp = require('../index.js');
|
|
2
|
+
|
|
3
|
+
geoIp.startWatchingDataUpdate(err => {
|
|
4
|
+
if (err) {
|
|
5
|
+
console.error('[geoip-lite2] GeoIP reload failed:', err);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
console.log('[geoip-lite2] Reloaded GeoIP database');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const ipv4 = '79.186.130.100';
|
|
12
|
+
console.log(ipv4, geoIp.lookup(ipv4));
|
|
13
|
+
|
|
14
|
+
const ipv6 = '2a01:11bf:4222:900a:99ae:285f:7432:8f8e';
|
|
15
|
+
console.log(ipv6, geoIp.lookup(ipv6));
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
interface GeoIp2Location {
|
|
2
|
-
range: [number | null, number | null];
|
|
3
2
|
country: string;
|
|
4
3
|
region: string;
|
|
5
|
-
|
|
4
|
+
isEu: boolean;
|
|
6
5
|
timezone: string;
|
|
7
6
|
city: string;
|
|
8
7
|
ll: [number | null, number | null];
|
|
@@ -11,10 +10,10 @@ interface GeoIp2Location {
|
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
export function lookup(ip: string | number): GeoIp2Location | null;
|
|
14
|
-
export function pretty(n: string | number | any[]): string;
|
|
15
13
|
export function reloadDataSync(): void;
|
|
16
|
-
export function reloadData(callback
|
|
17
|
-
export function
|
|
14
|
+
export function reloadData(callback: (err?: Error | null) => void): void;
|
|
15
|
+
export function reloadData(): Promise<void>;
|
|
16
|
+
export function startWatchingDataUpdate(callback?: (err?: Error | null) => void): void;
|
|
18
17
|
export function stopWatchingDataUpdate(): void;
|
|
19
18
|
export function clear(): void;
|
|
20
|
-
export const version: string;
|
|
19
|
+
export const version: string;
|