@k03mad/ip2geo 4.3.0 → 5.0.1

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 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: 1000, // store last N requests
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 = 1000;
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();
@@ -113,8 +113,8 @@ const writeToFsCache = async (ip, data, cacheDir, cacheFileName, cacheFileSepara
113
113
  * @param {string} [opts.cacheFileSeparator]
114
114
  * @param {string} [opts.cacheFileNewline]
115
115
  * @param {Map} [opts.cacheMap]
116
- * @param {number} opts.cacheMapMaxEntries
117
- * @param {number} opts.rps
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
- const ipData = cacheMap.get(ip);
130
+ if (cacheMapMaxEntries > 0) {
131
+ const ipData = cacheMap.get(ip);
131
132
 
132
- if (ipData) {
133
- debug('get from map cache: %o', ipData);
134
- return ipData;
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
- cacheMap.set(ip, outputData);
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
- cacheMap.set(body.ip, outputData);
177
- debug('set to map cache: %o', outputData);
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": "4.3.0",
3
+ "version": "5.0.1",
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.4.0",
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,34 @@
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_MAP_OFF_ONLY} 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_MAP_OFF_ONLY.ip}"`, async () => {
22
+ const data = await ip2geo(REQUEST_IPV4_MAP_OFF_ONLY.ip, opts);
23
+ assert.deepEqual(data, REQUEST_IPV4_MAP_OFF_ONLY);
24
+ });
25
+
26
+ it('should have cache file', () => checkCacheFile({
27
+ ...opts,
28
+ response: REQUEST_IPV4_MAP_OFF_ONLY,
29
+ }));
30
+
31
+ it('should not have cache entries', () => {
32
+ assert.equal(cacheStorage.size, 0);
33
+ });
34
+ });
@@ -20,6 +20,18 @@ export const REQUEST_IPV4 = {
20
20
  ispDomain: 'google.com',
21
21
  };
22
22
 
23
+ export const REQUEST_IPV4_MAP_OFF_ONLY = {
24
+ ip: '1.1.1.1',
25
+ emoji: '🇺🇸',
26
+ country: 'United States',
27
+ countryA2: 'US',
28
+ region: 'District of Columbia',
29
+ city: 'Washington',
30
+ org: 'APNIC and Cloudflare DNS Resolver project',
31
+ isp: 'Cloudflare, Inc.',
32
+ ispDomain: 'cloudflare.com',
33
+ };
34
+
23
35
  export const REQUEST_IPV6 = {
24
36
  ip: '2a00:dd80:40:100::',
25
37
  emoji: '🇳🇱',
File without changes