@k03mad/ip2geo 1.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 CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  ```bash
10
10
  npm i @k03mad/ip2geo --save-exact
11
- echo "/geoip" >> .gitignore
11
+ echo .geoip >> .gitignore
12
12
  ```
13
13
 
14
14
  ```js
@@ -23,8 +23,8 @@ const {
23
23
  isp,
24
24
  } = await ip2geo('1.1.1.1', {
25
25
  // defaults
26
- cacheDir: 'geoip',
27
- cacheFileName: 'ip.log',
26
+ cacheDir: '.geoip',
27
+ cacheFileName: 'ips.log',
28
28
  cacheFileSeparator: ';;',
29
29
  cacheFileNewline: '\n',
30
30
  });
package/app/lib/ip2geo.js CHANGED
@@ -13,15 +13,18 @@ const debug = _debug('mad:geoip');
13
13
  * @property {string} [country]
14
14
  * @property {string} [countryA2]
15
15
  * @property {string} [city]
16
+ * @property {string} [region]
17
+ * @property {string} [org]
16
18
  * @property {string} [isp]
19
+ * @property {string} [ispDomain]
17
20
  */
18
21
 
19
22
  const API = 'https://ipwho.is/';
20
23
 
21
- const CACHE_FILE_DIR = 'geoip';
22
- const CACHE_FILE_NAME = 'ip.log';
23
- const CACHE_FILE_SEPARATOR = ';;';
24
- const CACHE_FILE_NEWLINE = '\n';
24
+ const DEFAULT_CACHE_FILE_DIR = '.geoip';
25
+ const DEFAULT_CACHE_FILE_NAME = 'ips.log';
26
+ const DEFAULT_CACHE_FILE_SEPARATOR = ';;';
27
+ const DEFAULT_CACHE_FILE_NEWLINE = '\n';
25
28
 
26
29
  const cacheMap = new Map();
27
30
 
@@ -30,8 +33,11 @@ const outputKeys = [
30
33
  'emoji',
31
34
  'country',
32
35
  'countryA2',
36
+ 'region',
33
37
  'city',
38
+ 'org',
34
39
  'isp',
40
+ 'ispDomain',
35
41
  ];
36
42
 
37
43
  /**
@@ -55,7 +61,7 @@ const collectOutputData = dataArr => {
55
61
  * @returns {string}
56
62
  */
57
63
  const getCacheFileFullPath = (ip, cacheDir, cacheFileName) => {
58
- const [firstOctet] = ip.split('.');
64
+ const [firstOctet] = ip.split(/\.|:/);
59
65
  return path.join(cacheDir, `${firstOctet}_${cacheFileName}`);
60
66
  };
61
67
 
@@ -106,10 +112,10 @@ const writeToFsCache = async (ip, data, cacheDir, cacheFileName, cacheFileSepara
106
112
  * @returns {Promise<GeoIpOutput>}
107
113
  */
108
114
  export default async (ip = '', {
109
- cacheDir = CACHE_FILE_DIR,
110
- cacheFileName = CACHE_FILE_NAME,
111
- cacheFileSeparator = CACHE_FILE_SEPARATOR,
112
- cacheFileNewline = CACHE_FILE_NEWLINE,
115
+ cacheDir = DEFAULT_CACHE_FILE_DIR,
116
+ cacheFileName = DEFAULT_CACHE_FILE_NAME,
117
+ cacheFileSeparator = DEFAULT_CACHE_FILE_SEPARATOR,
118
+ cacheFileNewline = DEFAULT_CACHE_FILE_NEWLINE,
113
119
  } = {}) => {
114
120
  if (ip) {
115
121
  const ipData = cacheMap.get(ip);
@@ -145,8 +151,11 @@ export default async (ip = '', {
145
151
  body?.flag?.emoji,
146
152
  body?.country,
147
153
  body?.country_code,
154
+ body?.region,
148
155
  body?.city,
156
+ body?.connection?.org,
149
157
  body?.connection?.isp,
158
+ body?.connection?.domain,
150
159
  ];
151
160
 
152
161
  const outputData = collectOutputData(usedData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
@@ -0,0 +1,7 @@
1
+ import path from 'node:path';
2
+
3
+ /**
4
+ * @param {string} file import.meta.url
5
+ * @returns {string}
6
+ */
7
+ export const getCurrentFilename = file => path.basename(file, '.js');
@@ -1,44 +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 = 'ip.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
- city: 'Zürich',
24
- isp: 'Quad9',
25
- };
26
-
27
- removeCacheFolder(CACHE_FILE_DIR);
28
-
29
- it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
30
- const data = await ip2geo(REQUEST_IP, {
31
- cacheDir: CACHE_FILE_DIR,
32
- });
33
-
34
- assert.deepEqual(data, response);
35
- });
36
-
37
- checkCacheFile(
38
- CACHE_FILE_DIR,
39
- cacheFile,
40
- CACHE_FILE_SEPARATOR,
41
- CACHE_FILE_NEWLINE,
42
- response,
43
- );
44
- });
@@ -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-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
- city: 'Mountain View',
24
- isp: 'Google LLC',
25
- };
26
-
27
- removeCacheFolder(CACHE_FILE_DIR);
28
-
29
- it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
30
- const data = await ip2geo(REQUEST_IP, {
31
- cacheDir: CACHE_FILE_DIR,
32
- cacheFileName: CACHE_FILE_NAME,
33
- cacheFileSeparator: CACHE_FILE_SEPARATOR,
34
- cacheFileNewline: CACHE_FILE_NEWLINE,
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
- });
@@ -1,67 +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 = 'ip.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
- city: 'Washington',
24
- isp: 'Cloudflare, Inc.',
25
- };
26
-
27
- const outputKeys = [
28
- 'ip',
29
- 'emoji',
30
- 'country',
31
- 'countryA2',
32
- 'city',
33
- 'isp',
34
- ];
35
-
36
- removeCacheFolder(CACHE_FILE_DIR);
37
-
38
- describe('with ip arg', () => {
39
- it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
40
- const data = await ip2geo(REQUEST_IP);
41
-
42
- assert.deepEqual(data, response);
43
- });
44
-
45
- checkCacheFile(
46
- CACHE_FILE_DIR,
47
- cacheFile,
48
- CACHE_FILE_SEPARATOR,
49
- CACHE_FILE_NEWLINE,
50
- response,
51
- );
52
- });
53
-
54
- describe('without ip arg', () => {
55
- let data;
56
-
57
- it('should request geoip without ip arg', async () => {
58
- data = await ip2geo();
59
- });
60
-
61
- outputKeys.forEach(key => {
62
- it(`should have "${key}" in request response`, () => {
63
- assert.ok(data[key]);
64
- });
65
- });
66
- });
67
- });