Skip to content

Commit 351b6f7

Browse files
committed
Improve code readability with section separators
Added clear visual section separators to all library files for better code navigation and readability: lib/main.js: - Dependencies - Configuration - Cache Configuration - Helper Functions - IPv4 Lookup Function - IPv6 Lookup Function - IPv4-Mapped IPv6 Handler - Data Loading Functions (IPv4 and IPv6) - Public API - Initialize lib/utils.js: - IPv4 Conversion Functions (aton4, ntoa4) - IPv6 Conversion Functions (aton6, ntoa6) - Comparison Functions (cmp, cmp6) - IP Address Validation (isPrivateIP) lib/fsWatcher.js: - Watcher Management - File System Watch with Debounce - Exports Benefits: - Easier code navigation - Clear logical separation of concerns - Better developer experience - Maintains all functionality and performance All 94 tests pass successfully.
1 parent 2c31f90 commit 351b6f7

File tree

4 files changed

+96
-20
lines changed

4 files changed

+96
-20
lines changed

dist/utils.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/fsWatcher.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
// ============================================================================
2+
// File System Watcher for Auto-Reloading GeoIP Data
3+
// ============================================================================
4+
15
const { access, constants, watch } = require('node:fs');
26
const { join } = require('node:path');
37
const FSWatcher = {};
48

9+
// ============================================================================
10+
// Watcher Management
11+
// ============================================================================
12+
513
/**
614
* Takes an FSWatcher object and closes it.
715
* @param {string} name - The name of the watcher to close.
816
*/
917
const stopWatching = name => FSWatcher[name].close();
1018

19+
// ============================================================================
20+
// File System Watch with Debounce
21+
// ============================================================================
22+
1123
/**
1224
* Takes a directory/file and watch for change. Upon change, call the callback.
1325
*
@@ -66,4 +78,8 @@ const makeFsWatchFilter = (name, directory, filename, cdDelay, callback) => {
6678
FSWatcher[name] = watch(directory, onWatchEvent);
6779
};
6880

81+
// ============================================================================
82+
// Exports
83+
// ============================================================================
84+
6985
module.exports = { makeFsWatchFilter, stopWatching };

lib/main.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// ============================================================================
2+
// Dependencies
3+
// ============================================================================
4+
15
const { open, fstat, read, close, openSync, fstatSync, readSync, closeSync } = require('node:fs');
26
const { join, resolve } = require('node:path');
37
const { isIP } = require('node:net');
@@ -6,6 +10,10 @@ const { aton4, aton6, cmp6, ntoa4, ntoa6, cmp } = require('./utils.js');
610
const fsWatcher = require('./fsWatcher.js');
711
const { version } = require('../package.json');
812

13+
// ============================================================================
14+
// Configuration
15+
// ============================================================================
16+
917
const watcherName = 'dataWatcher';
1018

1119
const geoDataDir = resolve(
@@ -27,6 +35,10 @@ const privateRange4 = [
2735
[aton4('192.168.0.0'), aton4('192.168.255.255')],
2836
];
2937

38+
// ============================================================================
39+
// Cache Configuration
40+
// ============================================================================
41+
3042
const conf4 = {
3143
firstIP: null,
3244
lastIP: null,
@@ -45,20 +57,21 @@ const conf6 = {
4557
recordSize: 48,
4658
};
4759

48-
// Copy original configs
4960
let cache4 = { ...conf4 };
5061
let cache6 = { ...conf6 };
5162

5263
const RECORD_SIZE = 10;
5364
const RECORD_SIZE6 = 34;
5465

55-
// Helper function to remove null terminators
66+
// ============================================================================
67+
// Helper Functions
68+
// ============================================================================
69+
5670
const removeNullTerminator = str => {
5771
const nullIndex = str.indexOf('\0');
5872
return nullIndex === -1 ? str : str.substring(0, nullIndex);
5973
};
6074

61-
// Helper function to read IPv6 addresses from buffer
6275
const readIp6 = (buffer, line, recordSize, offset) => {
6376
const ipArray = [];
6477
for (let i = 0; i < 2; i++) {
@@ -67,6 +80,10 @@ const readIp6 = (buffer, line, recordSize, offset) => {
6780
return ipArray;
6881
};
6982

83+
// ============================================================================
84+
// IPv4 Lookup Function
85+
// ============================================================================
86+
7087
const lookup4 = ip => {
7188
let fline = 0;
7289
let cline = cache4.lastLine;
@@ -146,6 +163,10 @@ const lookup4 = ip => {
146163
}
147164
};
148165

166+
// ============================================================================
167+
// IPv6 Lookup Function
168+
// ============================================================================
169+
149170
const lookup6 = ip => {
150171
const buffer = cache6.mainBuffer;
151172
const recordSize = cache6.recordSize;
@@ -206,6 +227,10 @@ const lookup6 = ip => {
206227
}
207228
};
208229

230+
// ============================================================================
231+
// IPv4-Mapped IPv6 Handler
232+
// ============================================================================
233+
209234
const V6_PREFIX_1 = '0:0:0:0:0:FFFF:';
210235
const V6_PREFIX_2 = '::FFFF:';
211236
const get4mapped = ip => {
@@ -215,6 +240,10 @@ const get4mapped = ip => {
215240
return null;
216241
};
217242

243+
// ============================================================================
244+
// Data Loading Functions - IPv4
245+
// ============================================================================
246+
218247
function preload(callback) {
219248
let datFile;
220249
let datSize;
@@ -337,6 +366,10 @@ function preload(callback) {
337366
}
338367
}
339368

369+
// ============================================================================
370+
// Data Loading Functions - IPv6
371+
// ============================================================================
372+
340373
function preload6(callback) {
341374
let datFile;
342375
let datSize;
@@ -433,6 +466,10 @@ function preload6(callback) {
433466
}
434467
}
435468

469+
// ============================================================================
470+
// Public API
471+
// ============================================================================
472+
436473
module.exports = {
437474
cmp,
438475

@@ -513,6 +550,10 @@ module.exports = {
513550
version,
514551
};
515552

553+
// ============================================================================
554+
// Initialize - Load data on module startup
555+
// ============================================================================
556+
516557
preload();
517558
preload6();
518559

lib/utils.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
// ============================================================================
2+
// Utility Functions for IP Address Conversion and Comparison
3+
// ============================================================================
4+
15
const utils = module.exports = {};
26

7+
// ============================================================================
8+
// IPv4 Conversion Functions
9+
// ============================================================================
10+
311
utils.aton4 = a => {
412
const parts = a.split('.');
513
return ((parseInt(parts[0], 10) << 24) >>> 0) + ((parseInt(parts[1], 10) << 16) >>> 0) + ((parseInt(parts[2], 10) << 8) >>> 0) + (parseInt(parts[3], 10) >>> 0);
614
};
715

16+
utils.ntoa4 = n => {
17+
return ((n >>> 24) & 0xff) + '.' + ((n >>> 16) & 0xff) + '.' + ((n >>> 8) & 0xff) + '.' + (n & 0xff);
18+
};
19+
20+
// ============================================================================
21+
// IPv6 Conversion Functions
22+
// ============================================================================
23+
824
utils.aton6 = a => {
925
a = a.replace(/"/g, '').split(':');
1026

@@ -39,6 +55,21 @@ utils.aton6 = a => {
3955
return r;
4056
};
4157

58+
utils.ntoa6 = n => {
59+
let a = '[';
60+
61+
for (let i = 0; i < n.length; i++) {
62+
a += (n[i] >>> 16).toString(16) + ':';
63+
a += (n[i] & 0xffff).toString(16) + ':';
64+
}
65+
66+
a = a.replace(/:$/, ']').replace(/:0+/g, ':').replace(/::+/, '::');
67+
return a;
68+
};
69+
70+
// ============================================================================
71+
// Comparison Functions
72+
// ============================================================================
4273

4374
utils.cmp = (a, b) => {
4475
if (typeof a === 'number' && typeof b === 'number') return (a < b ? -1 : (a > b ? 1 : 0));
@@ -56,6 +87,10 @@ utils.cmp6 = (a, b) => {
5687
return 0;
5788
};
5889

90+
// ============================================================================
91+
// IP Address Validation
92+
// ============================================================================
93+
5994
utils.isPrivateIP = addr => {
6095
const str = addr.toString();
6196
return str.startsWith('10.') ||
@@ -65,20 +100,4 @@ utils.isPrivateIP = addr => {
65100
str.startsWith('169.254.') ||
66101
str.startsWith('fc00:') ||
67102
str.startsWith('fe80:');
68-
};
69-
70-
utils.ntoa4 = n => {
71-
return ((n >>> 24) & 0xff) + '.' + ((n >>> 16) & 0xff) + '.' + ((n >>> 8) & 0xff) + '.' + (n & 0xff);
72-
};
73-
74-
utils.ntoa6 = n => {
75-
let a = '[';
76-
77-
for (let i = 0; i < n.length; i++) {
78-
a += (n[i] >>> 16).toString(16) + ':';
79-
a += (n[i] & 0xffff).toString(16) + ':';
80-
}
81-
82-
a = a.replace(/:$/, ']').replace(/:0+/g, ':').replace(/::+/, '::');
83-
return a;
84103
};

0 commit comments

Comments
 (0)