@k03mad/ip2geo 8.0.0 → 8.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 CHANGED
@@ -42,13 +42,18 @@ if (isPrune) {
42
42
  const cacheFolder = argsExtra[0] || DEFAULT_CACHE_FILE_DIR;
43
43
 
44
44
  try {
45
- const {removedEntries} = await pruneCache(
45
+ const {duplicates, noData} = await pruneCache(
46
46
  cacheFolder,
47
47
  DEFAULT_CACHE_FILE_SEPARATOR,
48
48
  DEFAULT_CACHE_FILE_NEWLINE,
49
49
  );
50
50
 
51
- log(`Removed duplicate cache entries: ${removedEntries}`);
51
+ log([
52
+ `Removed duplicate cache entries: ${duplicates}`,
53
+ `Removed empty cache entries: ${noData}`,
54
+ '',
55
+ `Total: ${duplicates + noData}`,
56
+ ]);
52
57
  } catch (err) {
53
58
  if (err.code !== 'ENOENT') {
54
59
  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,26 @@ 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;
172
173
 
173
174
  await Promise.all(files.map(async file => {
174
175
  const fullFilePath = path.join(cacheDir, file);
175
176
 
176
177
  const data = await fs.readFile(fullFilePath, {encoding: 'utf8'});
177
- const dataArr = data.split(cacheFileNewline);
178
+ const dataArr = data.split(cacheFileNewline).filter(Boolean);
178
179
 
179
- const uniq = [...new Set(dataArr)].sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
180
+ const dataArrRemoveEmpty = dataArr.filter(elem => {
181
+ const splitted = elem.split(cacheFileSeparator);
182
+ return splitted.filter(Boolean).length > 1;
183
+ });
184
+
185
+ const uniq = [...new Set(dataArrRemoveEmpty)].sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
180
186
  await fs.writeFile(fullFilePath, uniq.join(cacheFileNewline).trim());
181
187
 
182
- removedEntries += dataArr.length - uniq.length;
188
+ duplicates += dataArr.length - uniq.length;
189
+ empty += dataArr.length - dataArrRemoveEmpty.length;
183
190
  }));
184
191
 
185
- return {removedEntries};
192
+ return {duplicates, empty};
186
193
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "8.0.0",
3
+ "version": "8.1.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
  };