@k03mad/ip2geo 7.5.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 +7 -2
- package/app/helpers/cache.js +14 -7
- package/package.json +1 -1
- package/tests/shared/fs.js +1 -1
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 {
|
|
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(
|
|
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);
|
package/app/helpers/cache.js
CHANGED
|
@@ -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)
|
|
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
|
|
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
|
|
180
|
-
|
|
180
|
+
const dataArrRemoveEmpty = dataArr.filter(elem => {
|
|
181
|
+
const splitted = elem.split(cacheFileSeparator);
|
|
182
|
+
return splitted.filter(Boolean).length > 1;
|
|
183
|
+
});
|
|
181
184
|
|
|
182
|
-
|
|
185
|
+
const uniq = [...new Set(dataArrRemoveEmpty)].sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
|
|
186
|
+
await fs.writeFile(fullFilePath, uniq.join(cacheFileNewline).trim());
|
|
187
|
+
|
|
188
|
+
duplicates += dataArr.length - uniq.length;
|
|
189
|
+
empty += dataArr.length - dataArrRemoveEmpty.length;
|
|
183
190
|
}));
|
|
184
191
|
|
|
185
|
-
return {
|
|
192
|
+
return {duplicates, empty};
|
|
186
193
|
};
|
package/package.json
CHANGED
package/tests/shared/fs.js
CHANGED
|
@@ -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)
|
|
43
|
+
assert.equal(data, cacheFileNewline + Object.values(response).join(cacheFileSeparator));
|
|
44
44
|
};
|