@k03mad/ip2geo 8.0.0 → 9.0.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
@@ -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,13 +45,26 @@ if (isPrune) {
42
45
  const cacheFolder = argsExtra[0] || DEFAULT_CACHE_FILE_DIR;
43
46
 
44
47
  try {
45
- const {removedEntries} = await pruneCache(
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
- log(`Removed duplicate cache entries: ${removedEntries}`);
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);
52
68
  } catch (err) {
53
69
  if (err.code !== 'ENOENT') {
54
70
  logErrorExit(err);
@@ -99,7 +99,7 @@ export const writeToFsCache = async (ip, data, cacheDir, cacheFileName, cacheFil
99
99
  debug('set to fs cache: %o %o', cacheFileFull, data);
100
100
 
101
101
  await fs.mkdir(cacheDir, {recursive: true});
102
- await fs.appendFile(cacheFileFull, Object.values(data).join(cacheFileSeparator) + cacheFileNewline);
102
+ await fs.appendFile(cacheFileFull, cacheFileNewline + Object.values(data).join(cacheFileSeparator));
103
103
  };
104
104
 
105
105
  /**
@@ -168,19 +168,35 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
168
168
  .join(''),
169
169
  );
170
170
 
171
- let removedEntries = 0;
171
+ let duplicates = 0;
172
+ let empty = 0;
173
+ const longLinesFiles = new Set();
172
174
 
173
175
  await Promise.all(files.map(async file => {
174
176
  const fullFilePath = path.join(cacheDir, file);
175
177
 
176
178
  const data = await fs.readFile(fullFilePath, {encoding: 'utf8'});
177
- const dataArr = data.split(cacheFileNewline);
179
+ const dataArr = data.split(cacheFileNewline).filter(Boolean);
178
180
 
179
- const uniq = [...new Set(dataArr)].sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
180
- await fs.writeFile(fullFilePath, uniq.join(cacheFileNewline).trim());
181
+ const dataArrRemoveEmpty = dataArr.filter(elem => {
182
+ const splitted = elem.split(cacheFileSeparator);
181
183
 
182
- removedEntries += dataArr.length - uniq.length;
184
+ if (splitted.length > outputKeys.length) {
185
+ longLinesFiles.add(fullFilePath);
186
+ }
187
+
188
+ return splitted.filter(Boolean).length > 1;
189
+ });
190
+
191
+ const uniq = [...new Set(dataArrRemoveEmpty)]
192
+ .sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
193
+
194
+ const fileContent = uniq.join(cacheFileNewline).trim();
195
+ await (fileContent ? fs.writeFile(fullFilePath, fileContent) : fs.rm(fullFilePath));
196
+
197
+ empty += dataArr.length - dataArrRemoveEmpty.length;
198
+ duplicates += dataArrRemoveEmpty.length - uniq.length;
183
199
  }));
184
200
 
185
- return {removedEntries};
201
+ return {duplicates, empty, longLinesFiles};
186
202
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "8.0.0",
3
+ "version": "9.0.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -40,5 +40,5 @@ export const checkCacheFile = async ({
40
40
  const cacheFile = `${response.ip.split(/\.|:/)[0]}_${cacheFileName}`;
41
41
  const data = await fs.readFile(path.join(cacheDir, cacheFile), {encoding: 'utf8'});
42
42
 
43
- assert.equal(data, Object.values(response).join(cacheFileSeparator) + cacheFileNewline);
43
+ assert.equal(data, cacheFileNewline + Object.values(response).join(cacheFileSeparator));
44
44
  };