@k03mad/ip2geo 2.0.0 → 2.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/README.md +2 -2
- package/app/lib/ip2geo.js +2 -2
- package/package.json +1 -1
- package/tests/helpers/path.js +7 -0
- package/tests/opts-assigned-subfolder.js +0 -47
- package/tests/opts-assigned.js +0 -50
- package/tests/opts-default.js +0 -77
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm i @k03mad/ip2geo --save-exact
|
|
11
|
-
echo
|
|
11
|
+
echo .geoip >> .gitignore
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
```js
|
|
@@ -23,7 +23,7 @@ const {
|
|
|
23
23
|
isp,
|
|
24
24
|
} = await ip2geo('1.1.1.1', {
|
|
25
25
|
// defaults
|
|
26
|
-
cacheDir: 'geoip',
|
|
26
|
+
cacheDir: '.geoip',
|
|
27
27
|
cacheFileName: 'ips.log',
|
|
28
28
|
cacheFileSeparator: ';;',
|
|
29
29
|
cacheFileNewline: '\n',
|
package/app/lib/ip2geo.js
CHANGED
|
@@ -21,7 +21,7 @@ const debug = _debug('mad:geoip');
|
|
|
21
21
|
|
|
22
22
|
const API = 'https://ipwho.is/';
|
|
23
23
|
|
|
24
|
-
const DEFAULT_CACHE_FILE_DIR = 'geoip';
|
|
24
|
+
const DEFAULT_CACHE_FILE_DIR = '.geoip';
|
|
25
25
|
const DEFAULT_CACHE_FILE_NAME = 'ips.log';
|
|
26
26
|
const DEFAULT_CACHE_FILE_SEPARATOR = ';;';
|
|
27
27
|
const DEFAULT_CACHE_FILE_NEWLINE = '\n';
|
|
@@ -61,7 +61,7 @@ const collectOutputData = dataArr => {
|
|
|
61
61
|
* @returns {string}
|
|
62
62
|
*/
|
|
63
63
|
const getCacheFileFullPath = (ip, cacheDir, cacheFileName) => {
|
|
64
|
-
const [firstOctet] = ip.split(
|
|
64
|
+
const [firstOctet] = ip.split(/\.|:/);
|
|
65
65
|
return path.join(cacheDir, `${firstOctet}_${cacheFileName}`);
|
|
66
66
|
};
|
|
67
67
|
|
package/package.json
CHANGED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import {ip2geo} from '../app/index.js';
|
|
5
|
-
|
|
6
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
7
|
-
|
|
8
|
-
describe('opts-assigned', () => {
|
|
9
|
-
const CACHE_FILE_DIR = 'geoip-subfolder/hello_there';
|
|
10
|
-
const CACHE_FILE_NAME = 'ips.log';
|
|
11
|
-
const CACHE_FILE_SEPARATOR = ';;';
|
|
12
|
-
const CACHE_FILE_NEWLINE = '\n';
|
|
13
|
-
|
|
14
|
-
const REQUEST_IP = '9.9.9.9';
|
|
15
|
-
|
|
16
|
-
const cacheFile = `${REQUEST_IP.split('.')[0]}_${CACHE_FILE_NAME}`;
|
|
17
|
-
|
|
18
|
-
const response = {
|
|
19
|
-
ip: REQUEST_IP,
|
|
20
|
-
emoji: '🇨ðŸ‡',
|
|
21
|
-
country: 'Switzerland',
|
|
22
|
-
countryA2: 'CH',
|
|
23
|
-
region: 'Zurich',
|
|
24
|
-
city: 'Zürich',
|
|
25
|
-
org: 'Quad9',
|
|
26
|
-
isp: 'Quad9',
|
|
27
|
-
ispDomain: 'quad9.net',
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
31
|
-
|
|
32
|
-
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
33
|
-
const data = await ip2geo(REQUEST_IP, {
|
|
34
|
-
cacheDir: CACHE_FILE_DIR,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
assert.deepEqual(data, response);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
checkCacheFile(
|
|
41
|
-
CACHE_FILE_DIR,
|
|
42
|
-
cacheFile,
|
|
43
|
-
CACHE_FILE_SEPARATOR,
|
|
44
|
-
CACHE_FILE_NEWLINE,
|
|
45
|
-
response,
|
|
46
|
-
);
|
|
47
|
-
});
|
package/tests/opts-assigned.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import {ip2geo} from '../app/index.js';
|
|
5
|
-
|
|
6
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
7
|
-
|
|
8
|
-
describe('opts-assigned', () => {
|
|
9
|
-
const CACHE_FILE_DIR = 'geoip-cache-opts';
|
|
10
|
-
const CACHE_FILE_NAME = 'ips.md';
|
|
11
|
-
const CACHE_FILE_SEPARATOR = '-_-';
|
|
12
|
-
const CACHE_FILE_NEWLINE = '%%%';
|
|
13
|
-
|
|
14
|
-
const REQUEST_IP = '8.8.8.8';
|
|
15
|
-
|
|
16
|
-
const cacheFile = `${REQUEST_IP.split('.')[0]}_${CACHE_FILE_NAME}`;
|
|
17
|
-
|
|
18
|
-
const response = {
|
|
19
|
-
ip: REQUEST_IP,
|
|
20
|
-
emoji: '🇺🇸',
|
|
21
|
-
country: 'United States',
|
|
22
|
-
countryA2: 'US',
|
|
23
|
-
region: 'California',
|
|
24
|
-
city: 'Mountain View',
|
|
25
|
-
org: 'Google LLC',
|
|
26
|
-
isp: 'Google LLC',
|
|
27
|
-
ispDomain: 'google.com',
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
31
|
-
|
|
32
|
-
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
33
|
-
const data = await ip2geo(REQUEST_IP, {
|
|
34
|
-
cacheDir: CACHE_FILE_DIR,
|
|
35
|
-
cacheFileName: CACHE_FILE_NAME,
|
|
36
|
-
cacheFileSeparator: CACHE_FILE_SEPARATOR,
|
|
37
|
-
cacheFileNewline: CACHE_FILE_NEWLINE,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
assert.deepEqual(data, response);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
checkCacheFile(
|
|
44
|
-
CACHE_FILE_DIR,
|
|
45
|
-
cacheFile,
|
|
46
|
-
CACHE_FILE_SEPARATOR,
|
|
47
|
-
CACHE_FILE_NEWLINE,
|
|
48
|
-
response,
|
|
49
|
-
);
|
|
50
|
-
});
|
package/tests/opts-default.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import {ip2geo} from '../app/index.js';
|
|
5
|
-
|
|
6
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
7
|
-
|
|
8
|
-
describe('opts-default', () => {
|
|
9
|
-
const CACHE_FILE_DIR = 'geoip';
|
|
10
|
-
const CACHE_FILE_NAME = 'ips.log';
|
|
11
|
-
const CACHE_FILE_SEPARATOR = ';;';
|
|
12
|
-
const CACHE_FILE_NEWLINE = '\n';
|
|
13
|
-
|
|
14
|
-
const REQUEST_IP = '1.1.1.1';
|
|
15
|
-
|
|
16
|
-
const cacheFile = `${REQUEST_IP.split('.')[0]}_${CACHE_FILE_NAME}`;
|
|
17
|
-
|
|
18
|
-
const response = {
|
|
19
|
-
ip: REQUEST_IP,
|
|
20
|
-
emoji: '🇺🇸',
|
|
21
|
-
country: 'United States',
|
|
22
|
-
countryA2: 'US',
|
|
23
|
-
region: 'District of Columbia',
|
|
24
|
-
city: 'Washington',
|
|
25
|
-
org: 'APNIC and Cloudflare DNS Resolver project',
|
|
26
|
-
isp: 'Cloudflare, Inc.',
|
|
27
|
-
ispDomain: 'cloudflare.com',
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const outputKeys = [
|
|
31
|
-
'ip',
|
|
32
|
-
'emoji',
|
|
33
|
-
'country',
|
|
34
|
-
'countryA2',
|
|
35
|
-
'region',
|
|
36
|
-
'city',
|
|
37
|
-
'org',
|
|
38
|
-
'isp',
|
|
39
|
-
'ispDomain',
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
43
|
-
|
|
44
|
-
describe('with ip arg', () => {
|
|
45
|
-
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
46
|
-
const data = await ip2geo(REQUEST_IP);
|
|
47
|
-
|
|
48
|
-
assert.deepEqual(data, response);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
checkCacheFile(
|
|
52
|
-
CACHE_FILE_DIR,
|
|
53
|
-
cacheFile,
|
|
54
|
-
CACHE_FILE_SEPARATOR,
|
|
55
|
-
CACHE_FILE_NEWLINE,
|
|
56
|
-
response,
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe('without ip arg', () => {
|
|
61
|
-
let data;
|
|
62
|
-
|
|
63
|
-
it('should request geoip without ip arg', async () => {
|
|
64
|
-
data = await ip2geo();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
outputKeys.forEach(key => {
|
|
68
|
-
it(`should have "${key}" in request response`, () => {
|
|
69
|
-
assert.ok(data[key]);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should not have extra keys in request response', () => {
|
|
74
|
-
assert.deepEqual(Object.keys(data), outputKeys);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|