@k03mad/ip2geo 8.1.0 → 9.1.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/app/cli.js +18 -7
- package/app/helpers/cache.js +21 -4
- package/package.json +3 -2
package/app/cli.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import {log, logErrorExit} from '@k03mad/simple-log';
|
|
4
|
+
import chalk from 'chalk';
|
|
4
5
|
|
|
5
6
|
import {DEFAULT_CACHE_FILE_DIR, DEFAULT_CACHE_FILE_NEWLINE, DEFAULT_CACHE_FILE_SEPARATOR, ip2geo} from './api.js';
|
|
6
7
|
import {pruneCache} from './helpers/cache.js';
|
|
7
8
|
import {codeText, nameText} from './helpers/colors.js';
|
|
8
9
|
|
|
10
|
+
const {blue, red, green} = chalk;
|
|
11
|
+
|
|
9
12
|
const args = process.argv.slice(2);
|
|
10
13
|
const argsExtra = args.filter(arg => !arg.startsWith('-'));
|
|
11
14
|
|
|
@@ -42,18 +45,26 @@ if (isPrune) {
|
|
|
42
45
|
const cacheFolder = argsExtra[0] || DEFAULT_CACHE_FILE_DIR;
|
|
43
46
|
|
|
44
47
|
try {
|
|
45
|
-
const {duplicates,
|
|
48
|
+
const {duplicates, empty, longLinesFiles} = await pruneCache(
|
|
46
49
|
cacheFolder,
|
|
47
50
|
DEFAULT_CACHE_FILE_SEPARATOR,
|
|
48
51
|
DEFAULT_CACHE_FILE_NEWLINE,
|
|
49
52
|
);
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
`Removed duplicate cache entries: ${duplicates}`,
|
|
53
|
-
`Removed empty cache entries: ${
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const message = [
|
|
55
|
+
`Removed duplicate cache entries: ${green(duplicates)}`,
|
|
56
|
+
`Removed empty cache entries: ${green(empty)}`,
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
if (longLinesFiles.size > 0) {
|
|
60
|
+
message.push(
|
|
61
|
+
'',
|
|
62
|
+
red('Something went wrong with these cache files (lines too long):'),
|
|
63
|
+
...[...longLinesFiles].map(elem => blue(`— ${elem}`)),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
log(message);
|
|
57
68
|
} catch (err) {
|
|
58
69
|
if (err.code !== 'ENOENT') {
|
|
59
70
|
logErrorExit(err);
|
package/app/helpers/cache.js
CHANGED
|
@@ -2,6 +2,7 @@ import fs from 'node:fs/promises';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
|
|
4
4
|
import _debug from 'debug';
|
|
5
|
+
import ora from 'ora';
|
|
5
6
|
|
|
6
7
|
const debug = _debug('mad:geoip');
|
|
7
8
|
|
|
@@ -170,6 +171,10 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
|
|
|
170
171
|
|
|
171
172
|
let duplicates = 0;
|
|
172
173
|
let empty = 0;
|
|
174
|
+
let counter = 0;
|
|
175
|
+
const longLinesFiles = new Set();
|
|
176
|
+
|
|
177
|
+
const spinner = ora().start();
|
|
173
178
|
|
|
174
179
|
await Promise.all(files.map(async file => {
|
|
175
180
|
const fullFilePath = path.join(cacheDir, file);
|
|
@@ -179,15 +184,27 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
|
|
|
179
184
|
|
|
180
185
|
const dataArrRemoveEmpty = dataArr.filter(elem => {
|
|
181
186
|
const splitted = elem.split(cacheFileSeparator);
|
|
187
|
+
|
|
188
|
+
if (splitted.length > outputKeys.length) {
|
|
189
|
+
longLinesFiles.add(fullFilePath);
|
|
190
|
+
}
|
|
191
|
+
|
|
182
192
|
return splitted.filter(Boolean).length > 1;
|
|
183
193
|
});
|
|
184
194
|
|
|
185
|
-
const uniq = [...new Set(dataArrRemoveEmpty)]
|
|
186
|
-
|
|
195
|
+
const uniq = [...new Set(dataArrRemoveEmpty)]
|
|
196
|
+
.sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
|
|
197
|
+
|
|
198
|
+
const fileContent = uniq.join(cacheFileNewline).trim();
|
|
199
|
+
await (fileContent ? fs.writeFile(fullFilePath, fileContent) : fs.rm(fullFilePath));
|
|
187
200
|
|
|
188
|
-
duplicates += dataArr.length - uniq.length;
|
|
189
201
|
empty += dataArr.length - dataArrRemoveEmpty.length;
|
|
202
|
+
duplicates += dataArrRemoveEmpty.length - uniq.length;
|
|
203
|
+
|
|
204
|
+
counter++;
|
|
205
|
+
spinner.text = `[${counter}/${files.length}]: ${fullFilePath}`;
|
|
190
206
|
}));
|
|
191
207
|
|
|
192
|
-
|
|
208
|
+
spinner.stop();
|
|
209
|
+
return {duplicates, empty, longLinesFiles};
|
|
193
210
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/ip2geo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "GeoIP library",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Kirill Molchanov <k03.mad@gmail.com"
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"@k03mad/request": "6.1.0",
|
|
23
23
|
"@k03mad/simple-log": "2.3.0",
|
|
24
24
|
"chalk": "5.3.0",
|
|
25
|
-
"debug": "4.3.6"
|
|
25
|
+
"debug": "4.3.6",
|
|
26
|
+
"ora": "8.0.1"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@k03mad/eslint-config": "23.0.0",
|