@k03mad/ip2geo 7.3.1 → 7.5.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/.github/dependabot.yml +4 -0
- package/app/{lib/ip2geo.js → api.js} +1 -1
- package/app/cli.js +40 -13
- package/app/helpers/cache.js +32 -0
- package/package.json +6 -6
- package/tests/cache-file-entries.js +1 -1
- package/tests/cache-file-modify.js +1 -1
- package/tests/cache-file-subfolder.js +1 -1
- package/tests/cache-map-entries.js +1 -1
- package/tests/cache-map-max-entries.js +1 -1
- package/tests/cache-map-off.js +1 -1
- package/tests/default.js +1 -1
- package/tests/ip-multi-requests-cache.js +1 -1
- package/tests/ip-no-data.js +1 -1
- package/tests/ip-v6.js +1 -1
- package/tests/shared/fs.js +1 -1
- package/app/index.js +0 -1
package/.github/dependabot.yml
CHANGED
package/app/cli.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {log} from '@k03mad/simple-log';
|
|
3
|
+
import {log, logErrorExit} from '@k03mad/simple-log';
|
|
4
4
|
|
|
5
|
+
import {DEFAULT_CACHE_FILE_DIR, DEFAULT_CACHE_FILE_NEWLINE, DEFAULT_CACHE_FILE_SEPARATOR, ip2geo} from './api.js';
|
|
6
|
+
import {pruneCache} from './helpers/cache.js';
|
|
5
7
|
import {codeText, nameText} from './helpers/colors.js';
|
|
6
8
|
|
|
7
|
-
import {ip2geo} from './index.js';
|
|
8
|
-
|
|
9
9
|
const args = process.argv.slice(2);
|
|
10
|
-
const
|
|
10
|
+
const argsExtra = args.filter(arg => !arg.startsWith('-'));
|
|
11
|
+
|
|
12
|
+
const isHelp = args.includes('-h') || args.includes('--help');
|
|
13
|
+
const isPrune = args.includes('-p') || args.includes('--prune');
|
|
11
14
|
|
|
12
|
-
if (
|
|
15
|
+
if (isHelp) {
|
|
13
16
|
const cmd = `${codeText('$')} ${nameText('ip2geo')}`;
|
|
14
17
|
|
|
15
18
|
log([
|
|
16
19
|
'',
|
|
17
|
-
|
|
20
|
+
codeText('# current external ip'),
|
|
21
|
+
cmd,
|
|
18
22
|
`${cmd} 1.1.1.1`,
|
|
19
23
|
'',
|
|
20
24
|
`${cmd} -h`,
|
|
@@ -26,19 +30,42 @@ if (args.includes('-h') || args.includes('--help')) {
|
|
|
26
30
|
`${cmd} 1.1.1.1 8.8.8.8`,
|
|
27
31
|
`${cmd} 1.1.1.1,8.8.8.8`,
|
|
28
32
|
'',
|
|
33
|
+
codeText('# remove duplicate cache entries'),
|
|
34
|
+
`${cmd} -p`,
|
|
35
|
+
`${cmd} --prune`,
|
|
29
36
|
]);
|
|
30
37
|
|
|
31
38
|
process.exit(0);
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
if (isPrune) {
|
|
42
|
+
const cacheFolder = argsExtra[0] || DEFAULT_CACHE_FILE_DIR;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const {removedEntries} = await pruneCache(
|
|
46
|
+
cacheFolder,
|
|
47
|
+
DEFAULT_CACHE_FILE_SEPARATOR,
|
|
48
|
+
DEFAULT_CACHE_FILE_NEWLINE,
|
|
49
|
+
);
|
|
37
50
|
|
|
38
|
-
|
|
51
|
+
log(`Removed duplicate cache entries: ${removedEntries}`);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
if (err.code !== 'ENOENT') {
|
|
54
|
+
logErrorExit(err);
|
|
55
|
+
}
|
|
39
56
|
|
|
40
|
-
|
|
41
|
-
|
|
57
|
+
log(`Cache folder empty: ${cacheFolder}`);
|
|
58
|
+
}
|
|
42
59
|
} else {
|
|
43
|
-
|
|
60
|
+
const output = argsExtra.length === 0
|
|
61
|
+
? [await ip2geo()]
|
|
62
|
+
: await Promise.all(argsExtra.map(arg => Promise.all(arg.split(',').map(ip => ip2geo({ip})))));
|
|
63
|
+
|
|
64
|
+
const flatten = output.flat();
|
|
65
|
+
|
|
66
|
+
if (args.includes('--json') || args.includes('-j')) {
|
|
67
|
+
log(JSON.stringify(flatten.length > 1 ? flatten : flatten[0]));
|
|
68
|
+
} else {
|
|
69
|
+
log(flatten);
|
|
70
|
+
}
|
|
44
71
|
}
|
package/app/helpers/cache.js
CHANGED
|
@@ -152,3 +152,35 @@ export const writeToMapCache = (body, cacheMap, cacheMapMaxEntries) => {
|
|
|
152
152
|
removeFromMapCacheIfLimit(cacheMap, cacheMapMaxEntries);
|
|
153
153
|
}
|
|
154
154
|
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {string} cacheDir
|
|
158
|
+
* @param {string} cacheFileSeparator
|
|
159
|
+
* @param {string} cacheFileNewline
|
|
160
|
+
*/
|
|
161
|
+
export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline) => {
|
|
162
|
+
const files = await fs.readdir(cacheDir);
|
|
163
|
+
|
|
164
|
+
const cacheLineToNum = line => Number(
|
|
165
|
+
line.split(cacheFileSeparator)[0]
|
|
166
|
+
.split('.')
|
|
167
|
+
.map(num => `00${num}`.slice(-3))
|
|
168
|
+
.join(''),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
let removedEntries = 0;
|
|
172
|
+
|
|
173
|
+
await Promise.all(files.map(async file => {
|
|
174
|
+
const fullFilePath = path.join(cacheDir, file);
|
|
175
|
+
|
|
176
|
+
const data = await fs.readFile(fullFilePath, {encoding: 'utf8'});
|
|
177
|
+
const dataArr = data.split(cacheFileNewline);
|
|
178
|
+
|
|
179
|
+
const uniq = [...new Set(dataArr)].sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
|
|
180
|
+
await fs.writeFile(fullFilePath, uniq.join(cacheFileNewline));
|
|
181
|
+
|
|
182
|
+
removedEntries += dataArr.length - uniq.length;
|
|
183
|
+
}));
|
|
184
|
+
|
|
185
|
+
return {removedEntries};
|
|
186
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/ip2geo",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.5.0",
|
|
4
4
|
"description": "GeoIP library",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Kirill Molchanov <k03.mad@gmail.com"
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"bin": {
|
|
9
9
|
"ip2geo": "app/cli.js"
|
|
10
10
|
},
|
|
11
|
-
"main": "app/
|
|
11
|
+
"main": "app/api.js",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "git+https://github.com/k03mad/ip2geo.git"
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"node": ">=20"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@k03mad/request": "6.0
|
|
23
|
-
"@k03mad/simple-log": "2.
|
|
22
|
+
"@k03mad/request": "6.1.0",
|
|
23
|
+
"@k03mad/simple-log": "2.3.0",
|
|
24
24
|
"chalk": "5.3.0",
|
|
25
25
|
"debug": "4.3.6"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@k03mad/eslint-config": "
|
|
28
|
+
"@k03mad/eslint-config": "23.0.0",
|
|
29
29
|
"eslint": "8.57.0",
|
|
30
30
|
"husky": "9.1.4",
|
|
31
|
-
"mocha": "10.
|
|
31
|
+
"mocha": "10.7.3",
|
|
32
32
|
"npm-run-all": "4.1.5"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4} from './shared/consts.js';
|
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4} from './shared/consts.js';
|
|
@@ -3,7 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
|
|
4
4
|
import {describe, it} from 'mocha';
|
|
5
5
|
|
|
6
|
-
import {ip2geo} from '../app/
|
|
6
|
+
import {ip2geo} from '../app/api.js';
|
|
7
7
|
|
|
8
8
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
9
9
|
import {REQUEST_IPV4} from './shared/consts.js';
|
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4, REQUEST_IPV6} from './shared/consts.js';
|
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {removeCacheFolder} from './shared/fs.js';
|
package/tests/cache-map-off.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {cacheStorage, ip2geo} from '../app/
|
|
5
|
+
import {cacheStorage, ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4_MAP_OFF_ONLY} from './shared/consts.js';
|
package/tests/default.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {cacheStorage, ip2geo} from '../app/
|
|
5
|
+
import {cacheStorage, ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4} from './shared/consts.js';
|
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4} from './shared/consts.js';
|
package/tests/ip-no-data.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV4} from './shared/consts.js';
|
package/tests/ip-v6.js
CHANGED
|
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
|
|
3
3
|
import {describe, it} from 'mocha';
|
|
4
4
|
|
|
5
|
-
import {ip2geo} from '../app/
|
|
5
|
+
import {ip2geo} from '../app/api.js';
|
|
6
6
|
|
|
7
7
|
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
8
|
import {REQUEST_IPV6} from './shared/consts.js';
|
package/tests/shared/fs.js
CHANGED
package/app/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './lib/ip2geo.js';
|