@k03mad/ip2geo 4.3.0 → 5.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/README.md +1 -1
- package/app/lib/ip2geo.js +19 -11
- package/package.json +2 -2
- package/tests/cache-map-off.js +33 -0
- /package/tests/{cache-map-default.js → default.js} +0 -0
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ const info = await ip2geo('1.1.1.1', {
|
|
|
45
45
|
cacheFileSeparator: ';;',
|
|
46
46
|
cacheFileNewline: '\n',
|
|
47
47
|
cacheMap: new Map(),
|
|
48
|
-
cacheMapMaxEntries:
|
|
48
|
+
cacheMapMaxEntries: Number.POSITIVE_INFINITY, // store last N requests, 0 — turns cache map off
|
|
49
49
|
rps: 3, // API RPS, useful in Promise.all with IPs array
|
|
50
50
|
});
|
|
51
51
|
|
package/app/lib/ip2geo.js
CHANGED
|
@@ -26,7 +26,7 @@ const DEFAULT_CACHE_FILE_DIR = path.join(os.tmpdir(), '.ip2geo');
|
|
|
26
26
|
const DEFAULT_CACHE_FILE_NAME = 'ips.log';
|
|
27
27
|
const DEFAULT_CACHE_FILE_SEPARATOR = ';;';
|
|
28
28
|
const DEFAULT_CACHE_FILE_NEWLINE = '\n';
|
|
29
|
-
const DEFAULT_CACHE_MAP_MAX_ENTRIES =
|
|
29
|
+
const DEFAULT_CACHE_MAP_MAX_ENTRIES = Number.POSITIVE_INFINITY;
|
|
30
30
|
const DEFAULT_RPS = 3;
|
|
31
31
|
|
|
32
32
|
export const cacheStorage = new Map();
|
|
@@ -112,9 +112,9 @@ const writeToFsCache = async (ip, data, cacheDir, cacheFileName, cacheFileSepara
|
|
|
112
112
|
* @param {string} [opts.cacheFileName]
|
|
113
113
|
* @param {string} [opts.cacheFileSeparator]
|
|
114
114
|
* @param {string} [opts.cacheFileNewline]
|
|
115
|
-
* @param {Map} [opts.cacheMap]
|
|
116
|
-
* @param {number} opts.cacheMapMaxEntries
|
|
117
|
-
* @param {number} opts.rps
|
|
115
|
+
* @param {Map|false} [opts.cacheMap]
|
|
116
|
+
* @param {number} [opts.cacheMapMaxEntries]
|
|
117
|
+
* @param {number} [opts.rps]
|
|
118
118
|
* @returns {Promise<GeoIpOutput>}
|
|
119
119
|
*/
|
|
120
120
|
export const ip2geo = async (ip = '', {
|
|
@@ -127,11 +127,13 @@ export const ip2geo = async (ip = '', {
|
|
|
127
127
|
rps = DEFAULT_RPS,
|
|
128
128
|
} = {}) => {
|
|
129
129
|
if (ip) {
|
|
130
|
-
|
|
130
|
+
if (cacheMapMaxEntries > 0) {
|
|
131
|
+
const ipData = cacheMap.get(ip);
|
|
131
132
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
if (ipData) {
|
|
134
|
+
debug('get from map cache: %o', ipData);
|
|
135
|
+
return ipData;
|
|
136
|
+
}
|
|
135
137
|
}
|
|
136
138
|
|
|
137
139
|
const fsCache = await readFromFsCache(ip, cacheDir, cacheFileName);
|
|
@@ -146,7 +148,11 @@ export const ip2geo = async (ip = '', {
|
|
|
146
148
|
const outputData = collectOutputData(fileData);
|
|
147
149
|
debug('get from fs cache: %o', outputData);
|
|
148
150
|
|
|
149
|
-
|
|
151
|
+
if (cacheMapMaxEntries > 0) {
|
|
152
|
+
cacheMap.set(ip, outputData);
|
|
153
|
+
debug('set to map cache: %o', outputData);
|
|
154
|
+
}
|
|
155
|
+
|
|
150
156
|
return outputData;
|
|
151
157
|
}
|
|
152
158
|
}
|
|
@@ -173,8 +179,10 @@ export const ip2geo = async (ip = '', {
|
|
|
173
179
|
|
|
174
180
|
const outputData = collectOutputData(usedData);
|
|
175
181
|
|
|
176
|
-
|
|
177
|
-
|
|
182
|
+
if (cacheMapMaxEntries > 0) {
|
|
183
|
+
cacheMap.set(body.ip, outputData);
|
|
184
|
+
debug('set to map cache: %o', outputData);
|
|
185
|
+
}
|
|
178
186
|
|
|
179
187
|
await writeToFsCache(
|
|
180
188
|
body.ip, usedData,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/ip2geo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "GeoIP library",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Kirill Molchanov <k03.mad@gmail.com"
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"debug": "4.3.4"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@k03mad/eslint-config": "19.
|
|
28
|
+
"@k03mad/eslint-config": "19.5.0",
|
|
29
29
|
"eslint": "8.56.0",
|
|
30
30
|
"husky": "8.0.3",
|
|
31
31
|
"mocha": "10.2.0"
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
|
|
3
|
+
import {describe, it} from 'mocha';
|
|
4
|
+
|
|
5
|
+
import {cacheStorage, ip2geo} from '../app/index.js';
|
|
6
|
+
|
|
7
|
+
import {REQUEST_IPV4} from './helpers/consts.js';
|
|
8
|
+
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
9
|
+
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
10
|
+
|
|
11
|
+
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
+
|
|
13
|
+
describe(testName, () => {
|
|
14
|
+
const opts = {
|
|
15
|
+
cacheDir: getTestFolder(testName),
|
|
16
|
+
cacheMapMaxEntries: 0,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
20
|
+
|
|
21
|
+
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
22
|
+
const data = await ip2geo(REQUEST_IPV4.ip, opts);
|
|
23
|
+
assert.deepEqual(data, REQUEST_IPV4);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should have cache file', () => checkCacheFile({
|
|
27
|
+
response: REQUEST_IPV4,
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
it('should not have cache entries', () => {
|
|
31
|
+
assert.equal(cacheStorage.size, 0);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
File without changes
|