@k03mad/ip2geo 2.2.0 → 2.3.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/app/cli.js +6 -1
- package/package.json +4 -3
- package/tests/helpers/path.js +6 -0
- package/tests/opts-assigned-ipv6.js +7 -6
- package/tests/opts-assigned-subfolder.js +10 -7
- package/tests/opts-assigned.js +7 -6
- package/tests/opts-default.js +4 -3
- package/tests/shared/fs.js +10 -15
package/app/cli.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
3
6
|
import {codeText, errorText, nameText} from './helpers/colors.js';
|
|
4
7
|
import {log, throwError} from './helpers/logging.js';
|
|
5
8
|
|
|
@@ -29,7 +32,9 @@ if (args.includes(jsonParam)) {
|
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
await Promise.all(args.map(async arg => {
|
|
32
|
-
const output = await ip2geo(arg
|
|
35
|
+
const output = await ip2geo(arg, {
|
|
36
|
+
cacheDir: path.join(os.tmpdir(), '.ip2geo'),
|
|
37
|
+
});
|
|
33
38
|
|
|
34
39
|
if (json) {
|
|
35
40
|
return log(JSON.stringify(output));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/ip2geo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "GeoIP library",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Kirill Molchanov <k03.mad@gmail.com"
|
|
@@ -26,12 +26,13 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@k03mad/eslint-config": "19.2.0",
|
|
28
28
|
"eslint": "8.56.0",
|
|
29
|
-
"husky": "8.0.3"
|
|
29
|
+
"husky": "8.0.3",
|
|
30
|
+
"mocha": "10.2.0"
|
|
30
31
|
},
|
|
31
32
|
"scripts": {
|
|
32
33
|
"lint": "npm run lint:eslint",
|
|
33
34
|
"lint:eslint": "eslint ./ --cache",
|
|
34
|
-
"test": "
|
|
35
|
+
"test": "mocha tests",
|
|
35
36
|
"clean": "npm run clean:modules && npm run clean:eslint:cache",
|
|
36
37
|
"clean:modules": "rm -rf ./node_modules || true",
|
|
37
38
|
"clean:eslint:cache": "rm -rf .eslintcache || true",
|
package/tests/helpers/path.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import {describe, it} from 'mocha';
|
|
3
4
|
|
|
4
5
|
import {ip2geo} from '../app/index.js';
|
|
5
6
|
|
|
6
|
-
import {getCurrentFilename} from './helpers/path.js';
|
|
7
|
+
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
7
8
|
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
8
9
|
|
|
9
10
|
const testName = getCurrentFilename(import.meta.url);
|
|
10
11
|
|
|
11
12
|
describe(testName, () => {
|
|
12
|
-
const CACHE_FILE_DIR = testName;
|
|
13
|
+
const CACHE_FILE_DIR = getTestFolder(testName);
|
|
13
14
|
const CACHE_FILE_NAME = 'ips.log';
|
|
14
15
|
const CACHE_FILE_SEPARATOR = ';;';
|
|
15
16
|
const CACHE_FILE_NEWLINE = '\n';
|
|
@@ -30,7 +31,7 @@ describe(testName, () => {
|
|
|
30
31
|
ispDomain: '',
|
|
31
32
|
};
|
|
32
33
|
|
|
33
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
34
|
+
it('should remove fs cache dir if exist', () => removeCacheFolder(CACHE_FILE_DIR));
|
|
34
35
|
|
|
35
36
|
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
36
37
|
const data = await ip2geo(REQUEST_IP, {
|
|
@@ -40,11 +41,11 @@ describe(testName, () => {
|
|
|
40
41
|
assert.deepEqual(data, response);
|
|
41
42
|
});
|
|
42
43
|
|
|
43
|
-
checkCacheFile(
|
|
44
|
+
it('should have cache file', () => checkCacheFile(
|
|
44
45
|
CACHE_FILE_DIR,
|
|
45
46
|
cacheFile,
|
|
46
47
|
CACHE_FILE_SEPARATOR,
|
|
47
48
|
CACHE_FILE_NEWLINE,
|
|
48
49
|
response,
|
|
49
|
-
);
|
|
50
|
+
));
|
|
50
51
|
});
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import {describe, it} from 'mocha';
|
|
4
5
|
|
|
5
6
|
import {ip2geo} from '../app/index.js';
|
|
6
7
|
|
|
7
|
-
import {getCurrentFilename} from './helpers/path.js';
|
|
8
|
+
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
8
9
|
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
9
10
|
|
|
10
11
|
const testName = getCurrentFilename(import.meta.url);
|
|
11
12
|
const SUBFOLDERS = 5;
|
|
12
13
|
|
|
13
14
|
describe(testName, () => {
|
|
14
|
-
const CACHE_FILE_DIR =
|
|
15
|
-
|
|
15
|
+
const CACHE_FILE_DIR = getTestFolder(
|
|
16
|
+
path.join(
|
|
17
|
+
...Array.from({length: SUBFOLDERS}, () => testName),
|
|
18
|
+
),
|
|
16
19
|
);
|
|
17
20
|
|
|
18
21
|
const CACHE_FILE_NAME = 'ips.log';
|
|
@@ -35,7 +38,7 @@ describe(testName, () => {
|
|
|
35
38
|
ispDomain: 'quad9.net',
|
|
36
39
|
};
|
|
37
40
|
|
|
38
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
41
|
+
it('should remove fs cache dir if exist', () => removeCacheFolder(CACHE_FILE_DIR));
|
|
39
42
|
|
|
40
43
|
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
41
44
|
const data = await ip2geo(REQUEST_IP, {
|
|
@@ -45,11 +48,11 @@ describe(testName, () => {
|
|
|
45
48
|
assert.deepEqual(data, response);
|
|
46
49
|
});
|
|
47
50
|
|
|
48
|
-
checkCacheFile(
|
|
51
|
+
it('should have cache file', () => checkCacheFile(
|
|
49
52
|
CACHE_FILE_DIR,
|
|
50
53
|
cacheFile,
|
|
51
54
|
CACHE_FILE_SEPARATOR,
|
|
52
55
|
CACHE_FILE_NEWLINE,
|
|
53
56
|
response,
|
|
54
|
-
);
|
|
57
|
+
));
|
|
55
58
|
});
|
package/tests/opts-assigned.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import {describe, it} from 'mocha';
|
|
3
4
|
|
|
4
5
|
import {ip2geo} from '../app/index.js';
|
|
5
6
|
|
|
6
|
-
import {getCurrentFilename} from './helpers/path.js';
|
|
7
|
+
import {getCurrentFilename, getTestFolder} from './helpers/path.js';
|
|
7
8
|
import {checkCacheFile, removeCacheFolder} from './shared/fs.js';
|
|
8
9
|
|
|
9
10
|
const testName = getCurrentFilename(import.meta.url);
|
|
10
11
|
|
|
11
12
|
describe(testName, () => {
|
|
12
|
-
const CACHE_FILE_DIR = testName;
|
|
13
|
+
const CACHE_FILE_DIR = getTestFolder(testName);
|
|
13
14
|
const CACHE_FILE_NAME = 'ips.md';
|
|
14
15
|
const CACHE_FILE_SEPARATOR = '-_-';
|
|
15
16
|
const CACHE_FILE_NEWLINE = '%%%';
|
|
@@ -30,7 +31,7 @@ describe(testName, () => {
|
|
|
30
31
|
ispDomain: 'google.com',
|
|
31
32
|
};
|
|
32
33
|
|
|
33
|
-
removeCacheFolder(CACHE_FILE_DIR);
|
|
34
|
+
it('should remove fs cache dir if exist', () => removeCacheFolder(CACHE_FILE_DIR));
|
|
34
35
|
|
|
35
36
|
it(`should return correct response for IP: "${REQUEST_IP}"`, async () => {
|
|
36
37
|
const data = await ip2geo(REQUEST_IP, {
|
|
@@ -43,11 +44,11 @@ describe(testName, () => {
|
|
|
43
44
|
assert.deepEqual(data, response);
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
checkCacheFile(
|
|
47
|
+
it('should have cache file', () => checkCacheFile(
|
|
47
48
|
CACHE_FILE_DIR,
|
|
48
49
|
cacheFile,
|
|
49
50
|
CACHE_FILE_SEPARATOR,
|
|
50
51
|
CACHE_FILE_NEWLINE,
|
|
51
52
|
response,
|
|
52
|
-
);
|
|
53
|
+
));
|
|
53
54
|
});
|
package/tests/opts-default.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import {describe, it} from 'mocha';
|
|
3
4
|
|
|
4
5
|
import {ip2geo} from '../app/index.js';
|
|
5
6
|
|
|
@@ -48,13 +49,13 @@ describe('opts-default', () => {
|
|
|
48
49
|
assert.deepEqual(data, response);
|
|
49
50
|
});
|
|
50
51
|
|
|
51
|
-
checkCacheFile(
|
|
52
|
+
it('should have cache file', () => checkCacheFile(
|
|
52
53
|
CACHE_FILE_DIR,
|
|
53
54
|
cacheFile,
|
|
54
55
|
CACHE_FILE_SEPARATOR,
|
|
55
56
|
CACHE_FILE_NEWLINE,
|
|
56
57
|
response,
|
|
57
|
-
);
|
|
58
|
+
));
|
|
58
59
|
});
|
|
59
60
|
|
|
60
61
|
describe('without ip arg', () => {
|
package/tests/shared/fs.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
-
import {it} from 'node:test';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* @param {string} cacheDir
|
|
8
7
|
*/
|
|
9
|
-
export const removeCacheFolder = cacheDir => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
throw err;
|
|
16
|
-
}
|
|
8
|
+
export const removeCacheFolder = async cacheDir => {
|
|
9
|
+
try {
|
|
10
|
+
await fs.rm(cacheDir, {recursive: true, force: true});
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (err.code !== 'ENOENT') {
|
|
13
|
+
throw err;
|
|
17
14
|
}
|
|
18
|
-
}
|
|
15
|
+
}
|
|
19
16
|
};
|
|
20
17
|
|
|
21
18
|
/**
|
|
@@ -25,10 +22,8 @@ export const removeCacheFolder = cacheDir => {
|
|
|
25
22
|
* @param {string} cacheFileNewline
|
|
26
23
|
* @param {object} response
|
|
27
24
|
*/
|
|
28
|
-
export const checkCacheFile = (cacheDir, cacheFileName, cacheFileSeparator, cacheFileNewline, response) => {
|
|
29
|
-
|
|
30
|
-
const data = await fs.readFile(path.join(cacheDir, cacheFileName), {encoding: 'utf8'});
|
|
25
|
+
export const checkCacheFile = async (cacheDir, cacheFileName, cacheFileSeparator, cacheFileNewline, response) => {
|
|
26
|
+
const data = await fs.readFile(path.join(cacheDir, cacheFileName), {encoding: 'utf8'});
|
|
31
27
|
|
|
32
|
-
|
|
33
|
-
});
|
|
28
|
+
assert.equal(data, Object.values(response).join(cacheFileSeparator) + cacheFileNewline);
|
|
34
29
|
};
|