@k03mad/ip2geo 2.1.0 → 2.2.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
@@ -4,6 +4,28 @@
4
4
  — Runtime cache \
5
5
  — Filesystem infinity cache
6
6
 
7
+ ## Global
8
+
9
+ ```bash
10
+ npm i @k03mad/ip2geo -g
11
+
12
+ ip2geo 1.1.1.1
13
+ # {
14
+ # ip: '1.1.1.1',
15
+ # emoji: '🇺🇸',
16
+ # country: 'United States',
17
+ # countryA2: 'US',
18
+ # region: 'District of Columbia',
19
+ # city: 'Washington',
20
+ # org: 'APNIC and Cloudflare DNS Resolver project',
21
+ # isp: 'Cloudflare, Inc.',
22
+ # ispDomain: 'cloudflare.com'
23
+ # }
24
+
25
+ ip2geo 1.1.1.1 --json
26
+ # {"ip":"1.1.1.1","emoji":"🇺🇸","country":"United States","countryA2":"US","region":"District of Columbia","city":"Washington","org":"APNIC and Cloudflare DNS Resolver project","isp":"Cloudflare, Inc.","ispDomain":"cloudflare.com"}
27
+ ```
28
+
7
29
  ## API
8
30
 
9
31
  ```bash
@@ -14,14 +36,7 @@ echo .geoip >> .gitignore
14
36
  ```js
15
37
  import {ip2geo} from '@k03mad/ip2geo';
16
38
 
17
- const {
18
- ip,
19
- emoji,
20
- country,
21
- countryA2,
22
- city,
23
- isp,
24
- } = await ip2geo('1.1.1.1', {
39
+ const info = await ip2geo('1.1.1.1', {
25
40
  // defaults
26
41
  cacheDir: '.geoip',
27
42
  cacheFileName: 'ips.log',
@@ -29,10 +44,15 @@ const {
29
44
  cacheFileNewline: '\n',
30
45
  });
31
46
 
32
- // ip: "1.1.1.1"
33
- // emoji: "🇺🇸"
34
- // country: "United States"
35
- // countryA2: "US"
36
- // city: "Washington"
37
- // isp: "Cloudflare, Inc."
47
+ // info {
48
+ // ip: '1.1.1.1',
49
+ // emoji: '🇺🇸',
50
+ // country: 'United States',
51
+ // countryA2: 'US',
52
+ // region: 'District of Columbia',
53
+ // city: 'Washington',
54
+ // org: 'APNIC and Cloudflare DNS Resolver project',
55
+ // isp: 'Cloudflare, Inc.',
56
+ // ispDomain: 'cloudflare.com'
57
+ // }
38
58
  ```
package/app/cli.js ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env node
2
+
3
+ import {codeText, errorText, nameText} from './helpers/colors.js';
4
+ import {log, throwError} from './helpers/logging.js';
5
+
6
+ import {ip2geo} from './index.js';
7
+
8
+ const jsonParam = '--json';
9
+
10
+ let args = process.argv.slice(2);
11
+
12
+ if (args.length === 0) {
13
+ const prefix = codeText('$');
14
+ const name = nameText('ip2geo');
15
+
16
+ throwError([
17
+ errorText('IP(s) should be passed as args'),
18
+ '',
19
+ `${prefix} ${name} 1.1.1.1`,
20
+ `${prefix} ${name} 1.1.1.1 8.8.8.8`,
21
+ ]);
22
+ }
23
+
24
+ let json;
25
+
26
+ if (args.includes(jsonParam)) {
27
+ json = true;
28
+ args = args.filter(elem => elem !== jsonParam);
29
+ }
30
+
31
+ await Promise.all(args.map(async arg => {
32
+ const output = await ip2geo(arg);
33
+
34
+ if (json) {
35
+ return log(JSON.stringify(output));
36
+ }
37
+
38
+ log(output);
39
+ }));
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @param {any|any[]} elem
3
+ * @returns {any[]}
4
+ */
5
+ export const convertToArray = elem => Array.isArray(elem) ? elem : [elem];
@@ -0,0 +1,6 @@
1
+ import chalk from 'chalk';
2
+
3
+ export const errorText = chalk.red.bold.bgGray;
4
+ export const codeText = chalk.gray;
5
+ export const numText = chalk.blue;
6
+ export const nameText = chalk.magenta;
@@ -0,0 +1,26 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import {convertToArray} from './array.js';
4
+
5
+ /**
6
+ * @param {any|any[]} msg
7
+ * @returns {void}
8
+ */
9
+ export const log = msg => convertToArray(msg)
10
+ .forEach(elem => console.log(elem));
11
+
12
+ /**
13
+ * @param {any|any[]} msg
14
+ * @returns {void}
15
+ */
16
+ export const logError = msg => convertToArray(msg)
17
+ .forEach(elem => console.error(elem));
18
+
19
+ /**
20
+ * @param {any|any[]} msg
21
+ * @returns {void}
22
+ */
23
+ export const throwError = msg => {
24
+ logError(msg);
25
+ process.exit(1);
26
+ };
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"
7
7
  ],
8
+ "bin": {
9
+ "ip2geo": "app/cli.js"
10
+ },
8
11
  "main": "app/index.js",
9
12
  "repository": {
10
13
  "type": "git",
@@ -17,6 +20,7 @@
17
20
  },
18
21
  "dependencies": {
19
22
  "@k03mad/request": "5.4.1",
23
+ "chalk": "5.3.0",
20
24
  "debug": "4.3.4"
21
25
  },
22
26
  "devDependencies": {
@@ -0,0 +1,50 @@
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 {getCurrentFilename} from './helpers/path.js';
7
+ import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
8
+
9
+ const testName = getCurrentFilename(import.meta.url);
10
+
11
+ describe(testName, () => {
12
+ const CACHE_FILE_DIR = testName;
13
+ const CACHE_FILE_NAME = 'ips.log';
14
+ const CACHE_FILE_SEPARATOR = ';;';
15
+ const CACHE_FILE_NEWLINE = '\n';
16
+
17
+ const REQUEST_IP = '2a00:dd80:40:100::';
18
+
19
+ const cacheFile = `${REQUEST_IP.split(/\.|:/)[0]}_${CACHE_FILE_NAME}`;
20
+
21
+ const response = {
22
+ ip: REQUEST_IP,
23
+ emoji: '🇳🇱',
24
+ country: 'Netherlands',
25
+ countryA2: 'NL',
26
+ region: 'North Holland',
27
+ city: 'Amsterdam',
28
+ org: '',
29
+ isp: 'NetActuate Inc',
30
+ ispDomain: '',
31
+ };
32
+
33
+ removeCacheFolder(CACHE_FILE_DIR);
34
+
35
+ it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
36
+ const data = await ip2geo(REQUEST_IP, {
37
+ cacheDir: CACHE_FILE_DIR,
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
+ });
@@ -0,0 +1,55 @@
1
+ import assert from 'node:assert/strict';
2
+ import path from 'node:path';
3
+ import {describe, it} from 'node:test';
4
+
5
+ import {ip2geo} from '../app/index.js';
6
+
7
+ import {getCurrentFilename} from './helpers/path.js';
8
+ import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
9
+
10
+ const testName = getCurrentFilename(import.meta.url);
11
+ const SUBFOLDERS = 5;
12
+
13
+ describe(testName, () => {
14
+ const CACHE_FILE_DIR = path.join(
15
+ ...Array.from({length: SUBFOLDERS}, () => testName),
16
+ );
17
+
18
+ const CACHE_FILE_NAME = 'ips.log';
19
+ const CACHE_FILE_SEPARATOR = ';;';
20
+ const CACHE_FILE_NEWLINE = '\n';
21
+
22
+ const REQUEST_IP = '9.9.9.9';
23
+
24
+ const cacheFile = `${REQUEST_IP.split(/\.|:/)[0]}_${CACHE_FILE_NAME}`;
25
+
26
+ const response = {
27
+ ip: REQUEST_IP,
28
+ emoji: '🇨🇭',
29
+ country: 'Switzerland',
30
+ countryA2: 'CH',
31
+ region: 'Zurich',
32
+ city: 'Zürich',
33
+ org: 'Quad9',
34
+ isp: 'Quad9',
35
+ ispDomain: 'quad9.net',
36
+ };
37
+
38
+ removeCacheFolder(CACHE_FILE_DIR);
39
+
40
+ it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
41
+ const data = await ip2geo(REQUEST_IP, {
42
+ cacheDir: CACHE_FILE_DIR,
43
+ });
44
+
45
+ assert.deepEqual(data, response);
46
+ });
47
+
48
+ checkCacheFile(
49
+ CACHE_FILE_DIR,
50
+ cacheFile,
51
+ CACHE_FILE_SEPARATOR,
52
+ CACHE_FILE_NEWLINE,
53
+ response,
54
+ );
55
+ });
@@ -0,0 +1,53 @@
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 {getCurrentFilename} from './helpers/path.js';
7
+ import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
8
+
9
+ const testName = getCurrentFilename(import.meta.url);
10
+
11
+ describe(testName, () => {
12
+ const CACHE_FILE_DIR = testName;
13
+ const CACHE_FILE_NAME = 'ips.md';
14
+ const CACHE_FILE_SEPARATOR = '-_-';
15
+ const CACHE_FILE_NEWLINE = '%%%';
16
+
17
+ const REQUEST_IP = '8.8.8.8';
18
+
19
+ const cacheFile = `${REQUEST_IP.split(/\.|:/)[0]}_${CACHE_FILE_NAME}`;
20
+
21
+ const response = {
22
+ ip: REQUEST_IP,
23
+ emoji: '🇺🇸',
24
+ country: 'United States',
25
+ countryA2: 'US',
26
+ region: 'California',
27
+ city: 'Mountain View',
28
+ org: 'Google LLC',
29
+ isp: 'Google LLC',
30
+ ispDomain: 'google.com',
31
+ };
32
+
33
+ removeCacheFolder(CACHE_FILE_DIR);
34
+
35
+ it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
36
+ const data = await ip2geo(REQUEST_IP, {
37
+ cacheDir: CACHE_FILE_DIR,
38
+ cacheFileName: CACHE_FILE_NAME,
39
+ cacheFileSeparator: CACHE_FILE_SEPARATOR,
40
+ cacheFileNewline: CACHE_FILE_NEWLINE,
41
+ });
42
+
43
+ assert.deepEqual(data, response);
44
+ });
45
+
46
+ checkCacheFile(
47
+ CACHE_FILE_DIR,
48
+ cacheFile,
49
+ CACHE_FILE_SEPARATOR,
50
+ CACHE_FILE_NEWLINE,
51
+ response,
52
+ );
53
+ });
@@ -0,0 +1,77 @@
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
+ });