Skip to content

Commit 151f54e

Browse files
committed
4.0.0-alpha.1
1 parent 5e56347 commit 151f54e

File tree

8 files changed

+190
-192
lines changed

8 files changed

+190
-192
lines changed

LICENSE

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
There are two licenses, one for the software library, and one for the data.
22
This product includes GeoLite data created by MaxMind, available from https://www.maxmind.com
33

4-
5-
SOFTWARE LICENSE (Node JS library)
4+
------------ SOFTWARE LICENSE (Node JS library)
65
The node-geoip/geoip-lite2 JavaScript library is licensed under the APACHE LICENSE, VERSION 2.0.
76

87
Copyright 2011 Philip Tellis <philip@bluesmoon.info>
@@ -21,8 +20,7 @@ limitations under the License.
2120

2221

2322

24-
DATABASES LICENSE (GeoLite2 databases)
25-
23+
------------ DATABASES LICENSE (GeoLite2 databases)
2624
Copyright (c) 2012-2023 MaxMind, Inc. All Rights Reserved.
2725

2826
The GeoLite2 databases are distributed under the

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ console.log(process.memoryUsage());
230230

231231

232232
## 👥 Copyright
233-
`GeoIP-Lite` © 2011–2018 **Philip Tellis** <philip@bluesmoon.info>
234233
`GeoIP-Lite2` © 2023–present **Sefinek** <contact@sefinek.net>
234+
`GeoIP-Lite` © 2011–2018 **Philip Tellis** <philip@bluesmoon.info>
235235

236236

237237
## 🔐 License

eslint.config.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export default [
3333
'no-shadow': ['error', { allow: ['err', 'resolve', 'reject'] }],
3434
'no-trailing-spaces': 'warn',
3535
'no-unreachable': 'warn',
36-
'no-unused-vars': 'warn',
36+
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
3737
'no-use-before-define': ['error', { functions: false, classes: true }],
3838
'no-var': 'error',
3939
'object-curly-spacing': ['warn', 'always'],
4040
'prefer-const': 'error',
4141
'quotes': ['warn', 'single'],
4242
'semi': ['warn', 'always'],
43-
'sort-vars': 'warn',
4443
'space-before-blocks': 'warn',
4544
'space-before-function-paren': ['warn', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
4645
'space-in-parens': 'warn',
@@ -50,6 +49,6 @@ export default [
5049
'wrap-regex': 'warn',
5150
'yoda': 'error',
5251
},
53-
ignores: ['node_modules', '*min.js', '*bundle*', 'build/*', 'dist/*'],
52+
ignores: ['node_modules', '**/*.min.js', '**/*bundle*', 'build/**', 'dist/**', 'services/translations/**'],
5453
},
5554
];

index.js

Lines changed: 88 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@ const { aton4, aton6, cmp6, removeNullTerminator, readIp6, createGeoData, popula
66
const fsWatcher = require('./scripts/fsWatcher.js');
77
const { version } = require('./package.json');
88

9-
const watcherName = 'dataWatcher';
10-
const reportReloadError = err => {
11-
if (err) console.error('[geoip-lite2] Failed to reload GeoIP data:', err);
9+
const WATCHER_NAME = 'dataWatcher';
10+
11+
const GEO_DATA_DIR = resolve(__dirname, globalThis['geoDataDir'] || process.env.GEOIP_DATA_DIR || './data/');
12+
const DATA_FILES = {
13+
city: join(GEO_DATA_DIR, 'geoip-city.dat'),
14+
city6: join(GEO_DATA_DIR, 'geoip-city6.dat'),
15+
cityNames: join(GEO_DATA_DIR, 'geoip-city-names.dat'),
16+
country: join(GEO_DATA_DIR, 'geoip-country.dat'),
17+
country6: join(GEO_DATA_DIR, 'geoip-country6.dat'),
1218
};
19+
const WATCHED_DATA_FILES = Object.values(DATA_FILES).map(filePath => basename(filePath));
1320

14-
const geoDataDir = resolve(
15-
__dirname,
16-
globalThis['geoDataDir'] || process.env.GEOIP_DATA_DIR || './data/'
17-
);
18-
19-
const dataFiles = {
20-
city: join(geoDataDir, 'geoip-city.dat'),
21-
city6: join(geoDataDir, 'geoip-city6.dat'),
22-
cityNames: join(geoDataDir, 'geoip-city-names.dat'),
23-
country: join(geoDataDir, 'geoip-country.dat'),
24-
country6: join(geoDataDir, 'geoip-country6.dat'),
25-
};
26-
const watchedDataFiles = Object.values(dataFiles).map(filePath => basename(filePath));
27-
28-
const privateRange4 = [
21+
const PRIVATE_RANGE4 = [
2922
[aton4('10.0.0.0'), aton4('10.255.255.255')],
3023
[aton4('172.16.0.0'), aton4('172.31.255.255')],
3124
[aton4('192.168.0.0'), aton4('192.168.255.255')],
3225
];
3326

34-
const conf4 = {
27+
const RECORD_SIZE = 10;
28+
const RECORD_SIZE6 = 34;
29+
30+
const CONF4 = {
3531
firstIP: null,
3632
lastIP: null,
3733
lastRecordIdx: 0,
@@ -41,19 +37,18 @@ const conf4 = {
4137
recordSize: 24,
4238
};
4339

44-
const conf6 = {
40+
const CONF6 = {
4541
firstIP: null,
4642
lastIP: null,
4743
lastRecordIdx: 0,
4844
mainBuffer: null,
4945
recordSize: 48,
5046
};
5147

52-
let cache4 = { ...conf4 };
53-
let cache6 = { ...conf6 };
48+
let cache4 = { ...CONF4 };
49+
let cache6 = { ...CONF6 };
5450

55-
const RECORD_SIZE = 10;
56-
const RECORD_SIZE6 = 34;
51+
const reportReloadError = err => err ? console.error('[geoip-lite2] Failed to reload GeoIP data:', err) : null;
5752

5853
const lookup4 = ip => {
5954
if (!cache4.mainBuffer) return null;
@@ -66,7 +61,7 @@ const lookup4 = ip => {
6661

6762
const buffer = cache4.mainBuffer;
6863
const locBuffer = cache4.locationBuffer;
69-
const privateRange = privateRange4;
64+
const privateRange = PRIVATE_RANGE4;
7065
const recordSize = cache4.recordSize;
7166
const locRecordSize = cache4.locationRecordSize;
7267

@@ -191,23 +186,23 @@ const readFileBuffer = async filePath => {
191186
const isExpectedMissingDataError = err => err?.code === 'ENOENT' || err?.code === 'EBADF';
192187

193188
const preloadAsync = async () => {
194-
const asyncCache = { ...conf4 };
189+
const asyncCache = { ...CONF4 };
195190
let mainData;
196191

197192
try {
198-
const cityNamesData = await readFileBuffer(dataFiles.cityNames);
193+
const cityNamesData = await readFileBuffer(DATA_FILES.cityNames);
199194
if (cityNamesData.size === 0) {
200195
const emptyFileError = new Error('geoip-city-names.dat is empty');
201196
emptyFileError.code = 'ENOENT';
202197
throw emptyFileError;
203198
}
204199

205200
asyncCache.locationBuffer = cityNamesData.buffer;
206-
mainData = await readFileBuffer(dataFiles.city);
201+
mainData = await readFileBuffer(DATA_FILES.city);
207202
} catch (err) {
208203
if (!isExpectedMissingDataError(err)) throw err;
209204
asyncCache.locationBuffer = null;
210-
mainData = await readFileBuffer(dataFiles.country);
205+
mainData = await readFileBuffer(DATA_FILES.country);
211206
asyncCache.recordSize = RECORD_SIZE;
212207
}
213208

@@ -219,54 +214,50 @@ const preloadAsync = async () => {
219214
};
220215

221216
const preload = callback => {
222-
if (typeof callback === 'function') {
223-
preloadAsync().then(() => callback()).catch(callback);
224-
} else {
225-
let datFile;
226-
let datSize;
227-
try {
228-
datFile = openSync(dataFiles.cityNames, 'r');
229-
datSize = fstatSync(datFile).size;
230-
if (datSize === 0) {
231-
closeSync(datFile);
232-
datFile = openSync(dataFiles.country, 'r');
233-
datSize = fstatSync(datFile).size;
234-
cache4.recordSize = RECORD_SIZE;
235-
} else {
236-
cache4.locationBuffer = Buffer.alloc(datSize);
237-
readSync(datFile, cache4.locationBuffer, 0, datSize, 0);
238-
closeSync(datFile);
217+
if (typeof callback === 'function') return preloadAsync().then(() => callback()).catch(callback);
239218

240-
datFile = openSync(dataFiles.city, 'r');
241-
datSize = fstatSync(datFile).size;
242-
}
243-
} catch (err) {
244-
if (err.code !== 'ENOENT' && err.code !== 'EBADF') {
245-
throw err;
246-
}
247-
248-
cache4.locationBuffer = null;
249-
datFile = openSync(dataFiles.country, 'r');
219+
let datFile;
220+
let datSize;
221+
try {
222+
datFile = openSync(DATA_FILES.cityNames, 'r');
223+
datSize = fstatSync(datFile).size;
224+
if (datSize === 0) {
225+
closeSync(datFile);
226+
datFile = openSync(DATA_FILES.country, 'r');
250227
datSize = fstatSync(datFile).size;
251228
cache4.recordSize = RECORD_SIZE;
252-
}
229+
} else {
230+
cache4.locationBuffer = Buffer.alloc(datSize);
231+
readSync(datFile, cache4.locationBuffer, 0, datSize, 0);
232+
closeSync(datFile);
253233

254-
cache4.mainBuffer = Buffer.alloc(datSize);
255-
readSync(datFile, cache4.mainBuffer, 0, datSize, 0);
256-
closeSync(datFile);
234+
datFile = openSync(DATA_FILES.city, 'r');
235+
datSize = fstatSync(datFile).size;
236+
}
237+
} catch (err) {
238+
if (err.code !== 'ENOENT' && err.code !== 'EBADF') throw err;
257239

258-
cache4.lastRecordIdx = (datSize / cache4.recordSize) - 1;
259-
cache4.lastIP = cache4.mainBuffer.readUInt32BE((cache4.lastRecordIdx * cache4.recordSize) + 4);
260-
cache4.firstIP = cache4.mainBuffer.readUInt32BE(0);
240+
cache4.locationBuffer = null;
241+
datFile = openSync(DATA_FILES.country, 'r');
242+
datSize = fstatSync(datFile).size;
243+
cache4.recordSize = RECORD_SIZE;
261244
}
245+
246+
cache4.mainBuffer = Buffer.alloc(datSize);
247+
readSync(datFile, cache4.mainBuffer, 0, datSize, 0);
248+
closeSync(datFile);
249+
250+
cache4.lastRecordIdx = (datSize / cache4.recordSize) - 1;
251+
cache4.lastIP = cache4.mainBuffer.readUInt32BE((cache4.lastRecordIdx * cache4.recordSize) + 4);
252+
cache4.firstIP = cache4.mainBuffer.readUInt32BE(0);
262253
};
263254

264255
const preload6Async = async () => {
265-
const asyncCache6 = { ...conf6 };
256+
const asyncCache6 = { ...CONF6 };
266257
let mainData;
267258

268259
try {
269-
const cityData = await readFileBuffer(dataFiles.city6);
260+
const cityData = await readFileBuffer(DATA_FILES.city6);
270261
if (cityData.size === 0) {
271262
const emptyFileError = new Error('geoip-city6.dat is empty');
272263
emptyFileError.code = 'ENOENT';
@@ -276,7 +267,7 @@ const preload6Async = async () => {
276267
mainData = cityData;
277268
} catch (err) {
278269
if (!isExpectedMissingDataError(err)) throw err;
279-
mainData = await readFileBuffer(dataFiles.country6);
270+
mainData = await readFileBuffer(DATA_FILES.country6);
280271
asyncCache6.recordSize = RECORD_SIZE6;
281272
}
282273

@@ -288,39 +279,36 @@ const preload6Async = async () => {
288279
};
289280

290281
const preload6 = callback => {
291-
if (typeof callback === 'function') {
292-
preload6Async().then(() => callback()).catch(callback);
293-
} else {
294-
let datFile;
295-
let datSize;
296-
try {
297-
datFile = openSync(dataFiles.city6, 'r');
298-
datSize = fstatSync(datFile).size;
282+
if (typeof callback === 'function') return preload6Async().then(() => callback()).catch(callback);
299283

300-
if (datSize === 0) {
301-
closeSync(datFile);
302-
datFile = openSync(dataFiles.country6, 'r');
303-
datSize = fstatSync(datFile).size;
304-
cache6.recordSize = RECORD_SIZE6;
305-
}
306-
} catch (err) {
307-
if (err.code !== 'ENOENT' && err.code !== 'EBADF') {
308-
throw err;
309-
}
284+
let datFile, datSize;
285+
try {
286+
datFile = openSync(DATA_FILES.city6, 'r');
287+
datSize = fstatSync(datFile).size;
310288

311-
datFile = openSync(dataFiles.country6, 'r');
289+
if (datSize === 0) {
290+
closeSync(datFile);
291+
datFile = openSync(DATA_FILES.country6, 'r');
312292
datSize = fstatSync(datFile).size;
313293
cache6.recordSize = RECORD_SIZE6;
314294
}
295+
} catch (err) {
296+
if (err.code !== 'ENOENT' && err.code !== 'EBADF') {
297+
throw err;
298+
}
315299

316-
cache6.mainBuffer = Buffer.alloc(datSize);
317-
readSync(datFile, cache6.mainBuffer, 0, datSize, 0);
318-
closeSync(datFile);
319-
320-
cache6.lastRecordIdx = (datSize / cache6.recordSize) - 1;
321-
cache6.lastIP = readIp6(cache6.mainBuffer, cache6.lastRecordIdx, cache6.recordSize, 1);
322-
cache6.firstIP = readIp6(cache6.mainBuffer, 0, cache6.recordSize, 0);
300+
datFile = openSync(DATA_FILES.country6, 'r');
301+
datSize = fstatSync(datFile).size;
302+
cache6.recordSize = RECORD_SIZE6;
323303
}
304+
305+
cache6.mainBuffer = Buffer.alloc(datSize);
306+
readSync(datFile, cache6.mainBuffer, 0, datSize, 0);
307+
closeSync(datFile);
308+
309+
cache6.lastRecordIdx = (datSize / cache6.recordSize) - 1;
310+
cache6.lastIP = readIp6(cache6.mainBuffer, cache6.lastRecordIdx, cache6.recordSize, 1);
311+
cache6.firstIP = readIp6(cache6.mainBuffer, 0, cache6.recordSize, 0);
324312
};
325313

326314
const runAsyncReload = callback => {
@@ -352,7 +340,7 @@ module.exports = {
352340
return null;
353341
},
354342
startWatchingDataUpdate: callback => {
355-
fsWatcher.makeFsWatchFilter(watcherName, geoDataDir, watchedDataFiles, 60 * 1000, change => {
343+
fsWatcher.makeFsWatchFilter(WATCHER_NAME, GEO_DATA_DIR, WATCHED_DATA_FILES, 60 * 1000, change => {
356344
if (change?.file) {
357345
console.log(`[geoip-lite2] Detected change in "${change.file}", reloading data...`);
358346
} else {
@@ -366,14 +354,14 @@ module.exports = {
366354
}
367355
});
368356
},
369-
stopWatchingDataUpdate: () => fsWatcher.stopWatching(watcherName),
357+
stopWatchingDataUpdate: () => fsWatcher.stopWatching(WATCHER_NAME),
370358
clear: () => {
371-
cache4 = { ...conf4 };
372-
cache6 = { ...conf6 };
359+
cache4 = { ...CONF4 };
360+
cache6 = { ...CONF6 };
373361
},
374362
reloadDataSync: () => {
375-
preload();
376-
preload6();
363+
void preload();
364+
void preload6();
377365
},
378366
reloadData: callback => {
379367
if (typeof callback === 'function') {
@@ -392,5 +380,5 @@ module.exports = {
392380
version,
393381
};
394382

395-
preload();
396-
preload6();
383+
void preload();
384+
void preload6();

0 commit comments

Comments
 (0)