Skip to content

Commit 8988db3

Browse files
committed
Update
1 parent 3b0d160 commit 8988db3

File tree

3 files changed

+64
-51
lines changed

3 files changed

+64
-51
lines changed

README.md

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div align="center">
2-
<a href="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.png?v=3.0.0-alpha.0" target="_blank" title="Full screen">
3-
<img src="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.png?v=3.0.0-alpha.0" alt="GeoIP-Lite v2.3 banner">
2+
<a href="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.png?v=4.0.0-alpha.0" target="_blank" title="Full screen">
3+
<img src="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.png?v=4.0.0-alpha.0" alt="geoip-lite2 banner">
44
</a>
5-
<br>
5+
<br>
66
<p>
77
A native <a href="https://nodejs.org" target="_blank" title="Open nodejs.org">Node.js</a> API for GeoLite data from MaxMind.<br>
88
This library includes GeoLite data created by MaxMind, available from <a href="https://www.maxmind.com" target="_blank" title="Open www.maxmind.com">maxmind.com</a>.
@@ -20,15 +20,15 @@ Actively maintained and optimized fork of [geoip-lite](https://github.com/geoip-
2020
Fully native JS implementation with synchronous, in-memory lookups for IPv4 and IPv6.
2121
Includes automated test coverage using [Jest](https://www.npmjs.com/package/jest).
2222

23-
> [!WARNING]
24-
> Remember to regularly update the MaxMind database! You will need a token for this.
23+
> [!WARNING]
24+
> Remember to regularly update the MaxMind database! You will need a license key for this.
2525
26-
> [!NOTE]
26+
> [!NOTE]
2727
> This requires a large amount of RAM. It is known to fail on a Digital Ocean or AWS micro instance.
2828
> This behavior is intentional, as the library prioritizes performance by keeping all data in memory.
2929
30-
> [!NOTE]
31-
> Please note that IPv6 geolocation data may be less complete depending on the version of the GeoLite database.
30+
> [!NOTE]
31+
> IPv6 geolocation data may be less complete depending on the version of the GeoLite database.
3232
3333

3434
## 🛠️ Installation
@@ -38,7 +38,15 @@ npm install geoip-lite2
3838
```
3939

4040
### 2. Update the data files (required)
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).
41+
Run the update script with your MaxMind license key (obtainable for free from [maxmind.com](https://support.maxmind.com/hc/en-us/articles/4407111582235-Generate-a-License-Key)):
42+
```shell
43+
cd node_modules/geoip-lite2 && npm run updatedb license_key=YOUR_LICENSE_KEY
44+
```
45+
46+
Or set the key via an environment variable and run from your project root:
47+
```shell
48+
MAXMIND_KEY=YOUR_LICENSE_KEY node node_modules/geoip-lite2/tools/updatedb.js
49+
```
4250

4351
## 📝 Short Example
4452
```js
@@ -77,29 +85,28 @@ Startup may take up to 200 ms while reading and indexing data files into memory.
7785

7886
### Looking up an IP address
7987
If you have an IP address in dotted IPv4 notation, colon IPv6 notation, or a 32-bit unsigned integer (treated as an IPv4 address),
80-
pass it to the `lookup` method. Remember to remove any `[` and `]` around an IPv6 address before passing it to this method.
88+
pass it to the `lookup` method.
8189

8290
```js
8391
const geo = geoIp.lookup(ip);
8492
```
8593

86-
If `ip` is missing (`undefined` or `null`), `lookup` throws a `TypeError`.
94+
If `ip` is `undefined` or `null`, `lookup` throws a `TypeError`.
95+
For any other invalid input (empty string, non-IP string, non-finite or noninteger number), it returns `null`.
8796

8897
If the IP address was found, the `lookup` method returns an object with the following structure:
8998

9099
```js
91100
{
92-
country: 'CC', // 2 letter ISO-3166-1 country code
93-
region: 'RR', // Up to 3 alphanumeric characters as ISO 3166-2 code
94-
// For US states this is the 2 letter state
95-
// For the United Kingdom this could be ENG as a country like "England"
96-
// FIPS 10-4 subcountry code
97-
isEu: true, // true if the country is a member state of the European Union, otherwise false.
98-
timezone: 'Country/Zone', // Timezone from IANA Time Zone Database
101+
country: 'CC', // 2-letter ISO 3166-1 country code
102+
region: 'RR', // Up to 3-character ISO 3166-2 subdivision code
103+
// (e.g. 'NY' for New York, 'ENG' for England)
104+
isEu: true, // true if the country is an EU member state
105+
timezone: 'Country/Zone', // IANA Time Zone Database identifier
99106
city: 'City name', // Full city name
100-
ll: [<latitude>, <longitude>], // Latitude and longitude of the city
101-
metro: <metro code>, // Metro code
102-
area: <accuracy_radius> // Approximate accuracy radius (km)
107+
ll: [<latitude>, <longitude>], // Latitude and longitude of the city, or [null, null]
108+
metro: <metro code>, // Nielsen metro code (0 if unavailable)
109+
area: <accuracy_radius> // Approximate accuracy radius in km
103110
}
104111
```
105112

@@ -113,23 +120,19 @@ You will need, at minimum, a free license key obtained from [maxmind.com](https:
113120

114121
The package stores checksums of MaxMind data and by default downloads them only if they have changed.
115122

116-
### Ways to update data
123+
### Updating data
117124
```shell
125+
# Standard update (skips if data unchanged)
118126
npm run updatedb license_key=YOUR_LICENSE_KEY
119-
npm run updatedb-force license_key=YOUR_LICENSE_KEY
120-
```
121127

122-
You can also run:
123-
```shell
124-
node ./node_modules/geoip-lite2/tools/updatedb.js license_key=YOUR_LICENSE_KEY
128+
# Force update regardless of checksum
129+
npm run updatedb-force license_key=YOUR_LICENSE_KEY
125130
```
126131

127-
### Ways to reload data in your app when update finished
128-
If you have a server running `geoip-lite2`, and you want to reload its geo data, after you finished update, without a restart.
132+
### Reloading data at runtime
133+
If you have a server running `geoip-lite2` and you want to reload its geo data after an update without a restart:
129134

130135
#### Programmatically
131-
You can do it programmatically, calling after scheduled data updates
132-
133136
```js
134137
// Synchronously
135138
geoIp.reloadDataSync();
@@ -143,24 +146,33 @@ geoIp.reloadData(() => {
143146
await geoIp.reloadData();
144147
```
145148

146-
#### Automatic Start and stop watching for data updates
147-
You can enable the data watcher to automatically refresh in-memory geo data when a file changes in the data directory.
149+
#### Automatic file watching
150+
You can enable the built-in file watcher to automatically refresh in-memory geo data whenever a file changes in the data directory:
148151

149152
```js
150153
geoIp.startWatchingDataUpdate();
151-
```
152154

153-
This tool can be used with `npm run updatedb` to periodically update geo data on a running server.
155+
// Optional: receive errors from background reloads
156+
geoIp.startWatchingDataUpdate(err => {
157+
if (err) console.error('[geoip-lite2] Reload failed:', err);
158+
});
159+
160+
// Stop watching
161+
geoIp.stopWatchingDataUpdate();
162+
```
154163

155164
#### Environment variables
156-
The following environment variables can be set.
165+
The following environment variables are supported:
157166

158167
```bash
159-
# Override the default node_modules/geoip-lite2/data dir
160-
GEODATADIR=/some/path
168+
# MaxMind license key (alternative to the license_key= argument)
169+
MAXMIND_KEY=your_license_key
170+
171+
# Override the default data directory (default: node_modules/geoip-lite2/data)
172+
GEOIP_DATA_DIR=/some/path
161173

162-
# Override the default node_modules/geoip-lite2/tmp dir
163-
GEOTMPDIR=/some/path
174+
# Override the default temporary directory used during updates
175+
GEOIP_TMP_DIR=/some/path
164176
```
165177

166178

@@ -182,13 +194,13 @@ console.log(process.memoryUsage());
182194
/**
183195
* Output:
184196
* {
185-
* rss: 234938368,
186-
* heapTotal: 7376896,
187-
* heapUsed: 4486592,
188-
* external: 189088547,
189-
* arrayBuffers: 187514427
197+
* rss: 234938368,
198+
* heapTotal: 7376896,
199+
* heapUsed: 4486592,
200+
* external: 189088547,
201+
* arrayBuffers: 187514427
190202
* }
191-
**/
203+
*/
192204
```
193205

194206

@@ -199,7 +211,8 @@ console.log(process.memoryUsage());
199211

200212

201213
## 👥 Copyright
202-
`GeoIP-Lite` © 2011-2018 **Philip Tellis** <philip@bluesmoon.info>
214+
`GeoIP-Lite` © 2011–2018 **Philip Tellis** <philip@bluesmoon.info>
215+
`GeoIP-Lite2` © 2023–present **Sefinek** <contact@sefinek.net>
203216

204217

205218
## 🔐 License

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const reportReloadError = err => {
1313

1414
const geoDataDir = resolve(
1515
__dirname,
16-
globalThis['geoDataDir'] || process.env.GEODATADIR || './data/'
16+
globalThis['geoDataDir'] || process.env.GEOIP_DATA_DIR || './data/'
1717
);
1818

1919
const dataFiles = {

tools/updatedb.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ if (typeof license_key === 'undefined' && typeof process.env.MAXMIND_KEY !== 'un
7575
}
7676

7777
let geoDataDir = args.find(arg => arg.match(/^geoDataDir=[\w./]+/) !== null);
78-
if (typeof geoDataDir === 'undefined' && typeof process.env.GEODATADIR !== 'undefined') {
79-
geoDataDir = `geoDataDir=${process.env.GEODATADIR}`;
78+
if (typeof geoDataDir === 'undefined' && typeof process.env.GEOIP_DATA_DIR !== 'undefined') {
79+
geoDataDir = `geoDataDir=${process.env.GEOIP_DATA_DIR}`;
8080
}
8181

8282
let dataPath = path.resolve(__dirname, '..', 'data');
@@ -88,7 +88,7 @@ if (typeof geoDataDir !== 'undefined') {
8888
}
8989
}
9090

91-
const tmpPath = process.env.GEOTMPDIR || path.resolve(__dirname, '..', 'tmp');
91+
const tmpPath = process.env.GEOIP_TMP_DIR || path.resolve(__dirname, '..', 'tmp');
9292
const countryLookup = {};
9393
const cityLookup = { NaN: -1 };
9494
const databases = [{

0 commit comments

Comments
 (0)