@k03mad/ip2geo 9.4.0 → 9.7.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 CHANGED
@@ -46,7 +46,7 @@ if (isPrune) {
46
46
  log(blue(cacheFolder));
47
47
 
48
48
  try {
49
- const {duplicates, empty, longLinesFiles} = await pruneCache(
49
+ const {duplicates, empty, entries, longLinesFiles} = await pruneCache(
50
50
  cacheFolder,
51
51
  DEFAULT_CACHE_FILE_SEPARATOR,
52
52
  DEFAULT_CACHE_FILE_NEWLINE,
@@ -66,6 +66,11 @@ if (isPrune) {
66
66
  ...longLinesFiles.map(({file, elem}) => dim(`— ${file}\n|— ${elem}`)),
67
67
  ]);
68
68
  }
69
+
70
+ log([
71
+ '',
72
+ green(`Current cache entries: ${bold(entries)}`),
73
+ ]);
69
74
  } catch (err) {
70
75
  logErrorExit(red(err));
71
76
  }
@@ -2,11 +2,10 @@ import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
 
4
4
  import _debug from 'debug';
5
+ import {isIP} from 'is-ip';
5
6
 
6
7
  const debug = _debug('mad:geoip');
7
8
 
8
- const ipRe = /((25[0-5]|((?:2[0-4]|1\d|[1-9])?)\d)\.?\b){4}/;
9
-
10
9
  const outputKeys = [
11
10
  'ip',
12
11
  'continent',
@@ -171,7 +170,15 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
171
170
  fs.readFile(fullFilePath, {encoding: 'utf8'}),
172
171
  ]);
173
172
 
174
- if (stat.isDirectory() || !ipRe.test(data)) {
173
+ const firstIp = data
174
+ .split(cacheFileNewline)
175
+ .find(Boolean)
176
+ .split(cacheFileSeparator)[0];
177
+
178
+ if (
179
+ stat.isDirectory()
180
+ || !isIP(firstIp)
181
+ ) {
175
182
  throw new Error(`Folder has subfolders or files without IPs, wrong cache folder arg?\n${fullFilePath}`);
176
183
  }
177
184
  }));
@@ -186,6 +193,7 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
186
193
  const duplicates = new Set();
187
194
  const empty = [];
188
195
  const longLinesFiles = new Set();
196
+ let entries = 0;
189
197
 
190
198
  await Promise.all(files.map(async file => {
191
199
  const fullFilePath = path.join(cacheDir, file);
@@ -211,13 +219,20 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
211
219
  .sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
212
220
 
213
221
  const fileContent = uniq.join(cacheFileNewline).trim();
214
- await (fileContent ? fs.writeFile(fullFilePath, fileContent) : fs.rm(fullFilePath));
222
+
223
+ if (fileContent) {
224
+ await fs.writeFile(fullFilePath, fileContent);
225
+ entries += uniq.length;
226
+ } else {
227
+ await fs.rm(fullFilePath);
228
+ }
215
229
 
216
230
  dataArrRemoveEmpty
217
231
  .forEach((elem, i, arr) => arr.indexOf(elem) !== i && duplicates.add(elem));
218
232
  }));
219
233
 
220
234
  return {
235
+ entries,
221
236
  duplicates: [...duplicates],
222
237
  empty,
223
238
  longLinesFiles: [...longLinesFiles],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "9.4.0",
3
+ "version": "9.7.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -20,14 +20,15 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@k03mad/request": "6.1.0",
23
- "@k03mad/simple-log": "3.0.0",
23
+ "@k03mad/simple-log": "4.0.0",
24
24
  "chalk": "5.3.0",
25
- "debug": "4.3.6"
25
+ "debug": "4.3.6",
26
+ "is-ip": "5.0.1"
26
27
  },
27
28
  "devDependencies": {
28
- "@k03mad/eslint-config": "23.0.0",
29
+ "@k03mad/eslint-config": "23.1.0",
29
30
  "eslint": "8.57.0",
30
- "husky": "9.1.4",
31
+ "husky": "9.1.5",
31
32
  "mocha": "10.7.3",
32
33
  "npm-run-all": "4.1.5"
33
34
  },