geoip-lite2 2.0.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/LICENSE ADDED
@@ -0,0 +1,50 @@
1
+ There are two licenses, one for the software library, and one for the data.
2
+
3
+ This product includes GeoLite data created by MaxMind, available from http://maxmind.com/
4
+
5
+ SOFTWARE LICENSE (Node JS library)
6
+
7
+ The node-geoip JavaScript library is licensed under the Apache License, Version 2.0:
8
+
9
+ Copyright 2011 Philip Tellis <philip@bluesmoon.info>
10
+
11
+ Licensed under the Apache License, Version 2.0 (the "License");
12
+ you may not use this file except in compliance with the License.
13
+ You may obtain a copy of the License at
14
+
15
+ http://www.apache.org/licenses/LICENSE-2.0
16
+
17
+ Unless required by applicable law or agreed to in writing, software
18
+ distributed under the License is distributed on an "AS IS" BASIS,
19
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ See the License for the specific language governing permissions and
21
+ limitations under the License.
22
+
23
+ DATABASES LICENSE (GeoLite2 databases)
24
+
25
+ Copyright (c) 2012-2018 MaxMind, Inc. All Rights Reserved.
26
+
27
+ The GeoLite2 databases are distributed under the
28
+ Creative Commons Attribution-ShareAlike 4.0 International License (the "License");
29
+ you may not use this file except in compliance with the License.
30
+ You may obtain a copy of the License at
31
+
32
+ https://creativecommons.org/licenses/by-sa/4.0/legalcode
33
+
34
+ The attribution requirement may be met by including the following in all
35
+ advertising and documentation mentioning features of or use of this
36
+ database:
37
+
38
+ This product includes GeoLite2 data created by MaxMind, available from
39
+ <a href="http://www.maxmind.com">http://www.maxmind.com</a>.
40
+
41
+ THIS DATABASE IS PROVIDED BY MAXMIND, INC ``AS IS'' AND ANY
42
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44
+ DISCLAIMED. IN NO EVENT SHALL MAXMIND BE LIABLE FOR ANY
45
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
+ DATABASE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,241 @@
1
+ πŸ—ΊοΈ GeoIP-Lite2
2
+ ==========
3
+ A native NodeJS API for the GeoLite data from MaxMind.
4
+
5
+ This product includes GeoLite data created by MaxMind, available from https://www.maxmind.com
6
+
7
+ [![Build Status](https://travis-ci.org/bluesmoon/node-geoip.svg?branch=master "node-geoip on Travis")](https://travis-ci.org/bluesmoon/node-geoip)
8
+
9
+
10
+ πŸš€ Improved GeoIP Module by [Sefinek](https://sefinek.net)
11
+ ------------
12
+ This module is an improved and updated edition of [geoip-lite](https://github.com/geoip-lite/node-geoip), thoughtfully designed to meet the latest programming standards.
13
+
14
+ All components have undergone meticulous updates to ensure peak performance and functionality.
15
+ Notably, it is now powered by the most up-to-date MaxMind database, offering significantly enhanced accuracy and reliability. This translates to approximately 40% more precise geolocation data for any IP address, compared to the previous version which relied on a database from 2019.
16
+
17
+ Furthermore, the testing process has been improved by adopting the testing library Mocha.
18
+ This change enhances testing and contributes to the overall reliability of the module.
19
+
20
+ > I am not the creator of this npm module! All copyright rights belong to its original [Creators](AUTHORS).
21
+
22
+
23
+ πŸ“‘ Introduction
24
+ ------------
25
+ MaxMind provides a set of data files for IP to Geo mapping along with opensource libraries to parse and lookup these data files.
26
+ One would typically write a wrapper around their C API to get access to this data in other languages (like JavaScript).
27
+
28
+ GeoIP-Lite instead attempts to be a fully native JavaScript library. A converter script converts the CSV files from MaxMind into
29
+ an internal binary format (note that this is different from the binary data format provided by MaxMind). The geoip module uses this
30
+ binary file to lookup IP addresses and return the country, region and city that it maps to.
31
+
32
+ Both IPv4 and IPv6 addresses are supported, however since the GeoLite IPv6 database does not currently contain any city or region
33
+ information, city, region and postal code lookups are only supported for IPv4.
34
+
35
+
36
+ πŸ“š Philosophy
37
+ ----------
38
+ I was really aiming for a fast JavaScript native implementation for geomapping of IPs.
39
+ My prime motivator was the fact that it was really hard to get libgeoip built for Mac OSX without using the library from MacPorts.
40
+
41
+
42
+ πŸ•΅οΈβ€β™‚οΈ Why GeoIP-Lite?
43
+ -------------
44
+ GeoIP-Lite is a fully JavaScript implementation of the MaxMind geoip API. It is not as fully featured as bindings that use `libgeoip`.
45
+ By reducing scope, this package is about 40% faster at doing lookups. On average, an IP to Location lookup should take 20 microseconds on
46
+ a Macbook Pro. IPv4 addresses take about 6 microseconds, while IPv6 addresses take about 30 microseconds.
47
+
48
+
49
+ πŸ“ Synopsis
50
+ --------
51
+ ### Script
52
+ ```javascript
53
+ const geoIp2 = require('geoip-lite2');
54
+
55
+ const ip = '207.97.227.239';
56
+ const geo = geoIp2.lookup(ip);
57
+
58
+ console.log(geo);
59
+ ```
60
+
61
+ ### Output
62
+ ```json
63
+ {
64
+ "range": [ 3479298048, 3479300095 ],
65
+ "country": "US",
66
+ "region": "TX",
67
+ "eu": "0",
68
+ "timezone": "America/Chicago",
69
+ "city": "San Antonio",
70
+ "ll": [ 29.4969, -98.4032 ],
71
+ "metro": 641,
72
+ "area": 1000
73
+ }
74
+ ```
75
+
76
+
77
+ πŸ› οΈ Installation
78
+ ------------
79
+ ### 1. Get the library
80
+ $ npm install geoip-lite2
81
+
82
+ ### 2. Update the datafiles (optional)
83
+ 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))
84
+
85
+ You can create maxmind account [here](https://www.maxmind.com/en/geolite2/signup)
86
+
87
+ **NOTE** that this requires a lot of RAM. It is known to fail on a Digital Ocean or AWS micro instance.
88
+ There are no plans to change this. GeoIP-Lite2 stores all data in RAM in order to be fast.
89
+
90
+
91
+ 🧩 API
92
+ ---
93
+ geoip-lite2 is completely synchronous. There are no callbacks involved.
94
+ All blocking file IO is done at startup time, so all runtime calls are executed in-memory and are fast.
95
+ Startup may take up to 200ms while it reads into memory and indexes data files.
96
+
97
+ ### Looking up an IP address
98
+ If you have an IP address in dotted quad notation, IPv6 colon notation, or a 32-bit unsigned integer (treated
99
+ as an IPv4 address), pass it to the `lookup` method. Note that you should remove any `[` and `]` around an
100
+ IPv6 address before passing it to this method.
101
+
102
+ ```javascript
103
+ const geo = geoIp2.lookup(ip);
104
+ ```
105
+
106
+ If the IP address was found, the `lookup` method returns an object with the following structure:
107
+
108
+ ```javascript
109
+ {
110
+ range: [ <low bound of IP block>, <high bound of IP block> ],
111
+ country: 'XX', // 2 letter ISO-3166-1 country code
112
+ region: 'RR', // Up to 3 alphanumeric variable length characters as ISO 3166-2 code
113
+ // For US states this is the 2 letter state
114
+ // For the United Kingdom this could be ENG as a country like β€œEngland
115
+ // FIPS 10-4 subcountry code
116
+ eu: '0', // 1 if the country is a member state of the European Union, 0 otherwise.
117
+ timezone: 'Country/Zone', // Timezone from IANA Time Zone Database
118
+ city: "City Name", // This is the full city name
119
+ ll: [<latitude>, <longitude>], // The latitude and longitude of the city
120
+ metro: <metro code>, // Metro code
121
+ area: <accuracy_radius> // The approximate accuracy radius (km), around the latitude and longitude
122
+ }
123
+ ```
124
+
125
+ The actual values for the `range` array depend on whether the IP is IPv4 or IPv6 and should be
126
+ considered internal to `geoip-lite2`. To get a human-readable format, pass them to `geoip.pretty()`
127
+
128
+ If the IP address was not found, the `lookup` returns `null`
129
+
130
+ ### Pretty printing an IP address
131
+ If you have a 32-bit unsigned integer, or a number returned as part of the `range` array from the `lookup` method,
132
+ the `pretty` method can be used to turn it into a human-readable string.
133
+
134
+ ```javascript
135
+ console.log('The IP is %s', geoip.pretty(ip));
136
+ ```
137
+
138
+ This method returns a string if the input was in a format that `geoip-lite2` can recognise, else it returns the
139
+ input itself.
140
+
141
+
142
+ πŸ”„ Built-in Updater
143
+ ----------------
144
+ This package contains an update script that can pull the files from MaxMind and handle the conversion from CSV.
145
+ A npm script alias has been setup to make this process easy. Please keep in mind this requires internet and MaxMind
146
+ rate limits that amount of downloads on their servers.
147
+
148
+ You will need, at minimum, a free license key obtained from [maxmind.com](https://support.maxmind.com/hc/en-us/articles/4407111582235-Generate-a-License-Key) to run the update script.
149
+
150
+ Package stores checksums of MaxMind data and by default only downloads them if checksums have changed.
151
+
152
+ ### Ways to update data
153
+ ```shell
154
+ # Update data if new data is available
155
+ npm run updatedb license_key=YOUR_LICENSE_KEY
156
+
157
+ # Force update data even if checksums have not changed
158
+ npm run updatedb-force license_key=YOUR_LICENSE_KEY
159
+ ```
160
+
161
+ You can also run it by doing:
162
+ ```bash
163
+ node ./node_modules/geoip-lite2/scripts/updatedb.js license_key=YOUR_LICENSE_KEY
164
+ ```
165
+
166
+ ### Ways to reload data in your app when update finished
167
+ If you have a server running `geoip-lite2`, and you want to reload its geo data, after you finished update, without a restart.
168
+
169
+ #### Programmatically
170
+ You can do it programmatically, calling after scheduled data updates
171
+
172
+ ```javascript
173
+ // Synchronously
174
+ geoip.reloadDataSync();
175
+
176
+ // Asynchronously
177
+ geoip.reloadData(function() {
178
+ console.log('Done');
179
+ });
180
+ ```
181
+
182
+ #### Automatic Start and stop watching for data updates
183
+ You can enable the data watcher to automatically refresh in-memory geo data when a file changes in the data directory.
184
+
185
+ ```javascript
186
+ geoip.startWatchingDataUpdate();
187
+ ```
188
+
189
+ This tool can be used with `npm run updatedb` to periodically update geo data on a running server.
190
+
191
+
192
+ ⚠️ Caveats
193
+ -------
194
+ This package includes the GeoLite database from MaxMind. This database is not the most accurate database available,
195
+ however it is the best available for free. You can use the commercial GeoIP database from MaxMind with better
196
+ accuracy by buying a license from MaxMind, and then using the conversion utility to convert it to a format that
197
+ GeoIP-Lite understands. You will need to use the `.csv` files from MaxMind for conversion.
198
+
199
+ Also note that on occassion, the library may take up to 5 seconds to load into memory. This is largely dependent on
200
+ how busy your disk is at that time. It can take as little as 200ms on a lightly loaded disk. This is a one time
201
+ cost though, and you make it up at run time with very fast lookups.
202
+
203
+ ### Memory usage
204
+ Quick test on memory consumption shows that library uses around 100Mb per process
205
+
206
+ ```javascript
207
+ const geoip2 = require('geoip-lite2');
208
+ console.log(process.memoryUsage());
209
+ /**
210
+ * Outputs:
211
+ * {
212
+ * rss: 126365696,
213
+ * heapTotal: 10305536,
214
+ * heapUsed: 5168944,
215
+ * external: 104347120
216
+ * }
217
+ **/
218
+ ```
219
+
220
+
221
+ πŸ”€ Alternatives
222
+ ----------
223
+ If your use-case requires doing less than 100 queries through the lifetime of your application or if you need really fast latency on start-up, you might want to look into [fast-geoip](https://github.com/onramper/fast-geoip) a package with a compatible API that is optimized for serverless environments and provides faster boot times and lower memory consumption at the expense of longer lookup times.
224
+
225
+
226
+ πŸ”– References
227
+ ----------
228
+ - <a href="http://www.maxmind.com/app/iso3166">Documentation from MaxMind</a>
229
+ - <a href="http://en.wikipedia.org/wiki/ISO_3166">ISO 3166 (1 & 2) codes</a>
230
+ - <a href="http://en.wikipedia.org/wiki/List_of_FIPS_region_codes">FIPS region codes</a>
231
+
232
+
233
+ ©️ Copyright
234
+ ---------
235
+ `GeoIP-Lite` is Copyright 2011-2018 Philip Tellis <philip@bluesmoon.info>
236
+ `GeoIP-Lite2` is Copyright 2023 Sefinek <contact@sefinek.net> (https://sefinek.net)
237
+
238
+
239
+ πŸ” License
240
+ -------
241
+ There are two licenses for the code and data. See the [LICENSE](LICENSE) file for details.
@@ -0,0 +1 @@
1
+ 576e98a65f632cb4f2352d105163b32dc403860a7a1b73cb206e214850e5e9ba GeoLite2-City-CSV_20230721.zip
@@ -0,0 +1 @@
1
+ 89d3fcdc75607b926f0ebdd34ea79a7f31d10be0341f76896147e48853ab2946 GeoLite2-Country-CSV_20230721.zip
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,82 @@
1
+ const fs = require('fs'),
2
+ path = require('path'),
3
+ FSWatcher = {};
4
+
5
+ /**
6
+ * Takes a directory/file and watch for change. Upon change, call the
7
+ * callback.
8
+ *
9
+ * @param {String} name: name of this watcher
10
+ * @param {String} directory: path to the directory to watch
11
+ * @param {String} [filename]: (optional) specific filename to watch for,
12
+ * watches for all files in the directory if unspecified
13
+ * @param {Integer} cooldownDelay: delay to wait before triggering the callback
14
+ * @param {Function} callback: function () : called when changes are detected
15
+ **/
16
+ function makeFsWatchFilter(name, directory, filename, cooldownDelay, callback) {
17
+ let cooldownId = null;
18
+
19
+ // Delete the cooldownId and callback the outer function
20
+ function timeoutCallback() {
21
+ cooldownId = null;
22
+ callback();
23
+ }
24
+
25
+ // This function is called when there is a change in the data directory
26
+ // It sets a timer to wait for the change to be completed
27
+ function onWatchEvent(event, changedFile) {
28
+ // check to make sure changedFile is not null
29
+ if (!changedFile) {
30
+ return;
31
+ }
32
+
33
+ const filePath = path.join(directory, changedFile);
34
+ if (!filename || filename === changedFile) {
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
+ }
41
+
42
+ // 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.
44
+
45
+ // If a cooldownId already exists, we delete it
46
+ if (cooldownId !== null) {
47
+ clearTimeout(cooldownId);
48
+ cooldownId = null;
49
+ }
50
+
51
+ // Once the cooldownDelay has passed, the timeoutCallback function will be called
52
+ cooldownId = setTimeout(timeoutCallback, cooldownDelay);
53
+ });
54
+ }
55
+ }
56
+
57
+ // Manage the case where filename is missing (because it's optionnal)
58
+ if (typeof cooldownDelay === 'function') {
59
+ callback = cooldownDelay;
60
+ cooldownDelay = filename;
61
+ filename = null;
62
+ }
63
+
64
+ if (FSWatcher[name]) {
65
+ stopWatching(name);
66
+ }
67
+
68
+ FSWatcher[name] = fs.watch(directory, onWatchEvent);
69
+ }
70
+
71
+ /**
72
+ * Take a FSWatcher object and close it.
73
+ *
74
+ * @param {string} name: name of the watcher to close
75
+ *
76
+ **/
77
+ function stopWatching(name) {
78
+ FSWatcher[name].close();
79
+ }
80
+
81
+ module.exports.makeFsWatchFilter = makeFsWatchFilter;
82
+ module.exports.stopWatching = stopWatching;