@k03mad/ip2geo 21.0.2 β 21.0.4
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/package.json +10 -2
- package/.github/dependabot.yml +0 -14
- package/.github/workflows/lint.yml +0 -20
- package/.github/workflows/publish.yml +0 -35
- package/.github/workflows/test.yml +0 -20
- package/.husky/pre-commit +0 -1
- package/.nvmrc +0 -1
- package/.oxlintrc.json +0 -7
- package/.vscode/settings.json +0 -28
- package/tests/cache-file-entries.ts +0 -37
- package/tests/cache-file-modify.ts +0 -34
- package/tests/cache-file-subfolder.ts +0 -34
- package/tests/cache-map-entries.ts +0 -41
- package/tests/cache-map-max-entries.ts +0 -50
- package/tests/cache-map-off.ts +0 -34
- package/tests/default.ts +0 -55
- package/tests/helpers/path.ts +0 -5
- package/tests/ip-multi-requests-cache.ts +0 -39
- package/tests/ip-no-data.ts +0 -33
- package/tests/ip-v6.ts +0 -31
- package/tests/shared/consts.ts +0 -49
- package/tests/shared/fs.ts +0 -44
- package/tsconfig.json +0 -9
package/README.md
CHANGED
|
@@ -45,8 +45,8 @@ import {ip2geo} from '@k03mad/ip2geo';
|
|
|
45
45
|
const info = await ip2geo({
|
|
46
46
|
ip: '1.1.1.1', // make key falsy to use current external IP
|
|
47
47
|
// defaults
|
|
48
|
-
cacheDir: path.join(os.tmpdir(), '.ip2geo'),
|
|
49
|
-
cacheFileName: '
|
|
48
|
+
cacheDir: path.join(os.tmpdir(), '.ip2geo-cache'),
|
|
49
|
+
cacheFileName: 'ip.log',
|
|
50
50
|
cacheFileSeparator: ';;',
|
|
51
51
|
cacheFileNewline: '\n',
|
|
52
52
|
cacheMap: new Map(),
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k03mad/ip2geo",
|
|
3
|
-
"version": "21.0.
|
|
3
|
+
"version": "21.0.4",
|
|
4
4
|
"description": "GeoIP library",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"maintainers": [
|
|
7
|
-
"Kirill Molchanov <k03.mad@gmail.com"
|
|
7
|
+
"Kirill Molchanov <k03.mad@gmail.com>"
|
|
8
8
|
],
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -13,6 +13,14 @@
|
|
|
13
13
|
"bin": {
|
|
14
14
|
"ip2geo": "cli.js"
|
|
15
15
|
},
|
|
16
|
+
"files": [
|
|
17
|
+
"api.d.ts",
|
|
18
|
+
"api.js",
|
|
19
|
+
"app",
|
|
20
|
+
"cli.js",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
16
24
|
"type": "module",
|
|
17
25
|
"main": "api.js",
|
|
18
26
|
"types": "api.d.ts",
|
package/.github/dependabot.yml
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
name: Lint
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
pull_request:
|
|
8
|
-
branches:
|
|
9
|
-
- master
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
lint:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v6
|
|
16
|
-
- uses: actions/setup-node@v6
|
|
17
|
-
with:
|
|
18
|
-
node-version-file: '.nvmrc'
|
|
19
|
-
- run: npm i
|
|
20
|
-
- run: npm run lint
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
name: Publish
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
|
|
8
|
-
permissions:
|
|
9
|
-
id-token: write
|
|
10
|
-
contents: read
|
|
11
|
-
|
|
12
|
-
jobs:
|
|
13
|
-
publish:
|
|
14
|
-
runs-on: ubuntu-latest
|
|
15
|
-
steps:
|
|
16
|
-
- uses: actions/checkout@v6
|
|
17
|
-
with:
|
|
18
|
-
fetch-depth: 0
|
|
19
|
-
|
|
20
|
-
- uses: EndBug/version-check@v3
|
|
21
|
-
id: check_pkg_version
|
|
22
|
-
with:
|
|
23
|
-
diff-search: true
|
|
24
|
-
|
|
25
|
-
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
26
|
-
uses: actions/setup-node@v6
|
|
27
|
-
with:
|
|
28
|
-
node-version-file: '.nvmrc'
|
|
29
|
-
registry-url: 'https://registry.npmjs.org'
|
|
30
|
-
|
|
31
|
-
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
32
|
-
run: npm i
|
|
33
|
-
|
|
34
|
-
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
35
|
-
run: npm publish
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
name: Test
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
pull_request:
|
|
8
|
-
branches:
|
|
9
|
-
- master
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
test:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v6
|
|
16
|
-
- uses: actions/setup-node@v6
|
|
17
|
-
with:
|
|
18
|
-
node-version-file: '.nvmrc'
|
|
19
|
-
- run: npm i
|
|
20
|
-
- run: npm run test
|
package/.husky/pre-commit
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
npm run lint && npm run test
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
24
|
package/.oxlintrc.json
DELETED
package/.vscode/settings.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"oxc.fmt.configPath": "node_modules/@k03mad/oxlint-config/.oxfmtrc.json",
|
|
3
|
-
"oxc.fixKind": "safe_fix",
|
|
4
|
-
"editor.formatOnSave": true,
|
|
5
|
-
"editor.defaultFormatter": "oxc.oxc-vscode",
|
|
6
|
-
"editor.codeActionsOnSave": {
|
|
7
|
-
"source.fixAll.oxc": "always"
|
|
8
|
-
},
|
|
9
|
-
"[javascript]": {
|
|
10
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
11
|
-
},
|
|
12
|
-
"[json]": {
|
|
13
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
14
|
-
},
|
|
15
|
-
"[jsonc]": {
|
|
16
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
17
|
-
},
|
|
18
|
-
"[yaml]": {
|
|
19
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
20
|
-
},
|
|
21
|
-
"[markdown]": {
|
|
22
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
23
|
-
},
|
|
24
|
-
"[typescript]": {
|
|
25
|
-
"editor.defaultFormatter": "oxc.oxc-vscode"
|
|
26
|
-
},
|
|
27
|
-
"cSpell.words": ["geoip", "oxfmt", "oxfmtrc", "oxlint"]
|
|
28
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
9
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const opts = {
|
|
15
|
-
cacheDir: getTestFolder(testName),
|
|
16
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
17
|
-
cacheMapMaxEntries: 0,
|
|
18
|
-
tries: 5,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
22
|
-
|
|
23
|
-
Array.from({length: opts.tries}, (_, i) => i + 1).forEach(num => {
|
|
24
|
-
describe(`Try: ${num}`, () => {
|
|
25
|
-
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
26
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? '', ...opts});
|
|
27
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should have cache file', () =>
|
|
31
|
-
checkCacheFile({
|
|
32
|
-
...opts,
|
|
33
|
-
response: REQUEST_IPV4,
|
|
34
|
-
}));
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
9
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const opts = {
|
|
15
|
-
cacheDir: getTestFolder(testName),
|
|
16
|
-
cacheFileName: 'ips.md',
|
|
17
|
-
cacheFileSeparator: '-_-',
|
|
18
|
-
cacheFileNewline: '%%%',
|
|
19
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
23
|
-
|
|
24
|
-
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
25
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? '', ...opts});
|
|
26
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should have cache file', () =>
|
|
30
|
-
checkCacheFile({
|
|
31
|
-
...opts,
|
|
32
|
-
response: REQUEST_IPV4,
|
|
33
|
-
}));
|
|
34
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import {describe, it} from 'node:test';
|
|
4
|
-
|
|
5
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
6
|
-
import {ip2geo} from '../app/api.ts';
|
|
7
|
-
|
|
8
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
9
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
10
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
11
|
-
|
|
12
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
13
|
-
|
|
14
|
-
describe(testName, () => {
|
|
15
|
-
const SUBFOLDERS = 5;
|
|
16
|
-
|
|
17
|
-
const opts = {
|
|
18
|
-
cacheDir: getTestFolder(path.join(...Array.from({length: SUBFOLDERS}, () => testName))),
|
|
19
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
23
|
-
|
|
24
|
-
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
25
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? '', ...opts});
|
|
26
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should have cache file', () =>
|
|
30
|
-
checkCacheFile({
|
|
31
|
-
...opts,
|
|
32
|
-
response: REQUEST_IPV4,
|
|
33
|
-
}));
|
|
34
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4, REQUEST_IPV6} from './shared/consts.ts';
|
|
9
|
-
import {removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const opts = {
|
|
15
|
-
cacheDir: getTestFolder(testName),
|
|
16
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
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.ip}"`, async () => {
|
|
22
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? '', ...opts});
|
|
23
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should have 1 correct cache entry', () => {
|
|
27
|
-
assert.equal(opts.cacheMap.size, 1);
|
|
28
|
-
assert.deepEqual(opts.cacheMap.get(REQUEST_IPV4.ip ?? ''), REQUEST_IPV4);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it(`should return correct response for IP: "${REQUEST_IPV6.ip}"`, async () => {
|
|
32
|
-
const data = await ip2geo({ip: REQUEST_IPV6.ip ?? '', ...opts});
|
|
33
|
-
assert.deepEqual(data, REQUEST_IPV6);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should have 2 correct cache entries', () => {
|
|
37
|
-
assert.equal(opts.cacheMap.size, 2);
|
|
38
|
-
assert.deepEqual(opts.cacheMap.get(REQUEST_IPV4.ip ?? ''), REQUEST_IPV4);
|
|
39
|
-
assert.deepEqual(opts.cacheMap.get(REQUEST_IPV6.ip ?? ''), REQUEST_IPV6);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {removeCacheFolder} from './shared/fs.ts';
|
|
9
|
-
|
|
10
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
11
|
-
|
|
12
|
-
describe(testName, () => {
|
|
13
|
-
const firstReqIps = ['10.10.10.10', '20.20.20.20', '30.30.30.30', '40.40.40.40', '50.50.50.50'];
|
|
14
|
-
|
|
15
|
-
const secondReqIps = ['60.60.60.60'];
|
|
16
|
-
|
|
17
|
-
const opts = {
|
|
18
|
-
cacheDir: getTestFolder(testName),
|
|
19
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
20
|
-
cacheMapMaxEntries: 2,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
24
|
-
|
|
25
|
-
firstReqIps.forEach(ip => {
|
|
26
|
-
it(`should request geo for IP: "${ip}"`, async () => {
|
|
27
|
-
await ip2geo({ip, ...opts});
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it(`should have ${opts.cacheMapMaxEntries} correct cache entries`, () => {
|
|
32
|
-
assert.equal(opts.cacheMap.size, opts.cacheMapMaxEntries);
|
|
33
|
-
assert.deepEqual([...opts.cacheMap.keys()], firstReqIps.slice(-opts.cacheMapMaxEntries));
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
secondReqIps.forEach(ip => {
|
|
37
|
-
it(`should request geo for IP: "${ip}"`, async () => {
|
|
38
|
-
await ip2geo({ip, ...opts});
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it(`should have ${opts.cacheMapMaxEntries} correct cache entries`, () => {
|
|
43
|
-
assert.equal(opts.cacheMap.size, opts.cacheMapMaxEntries);
|
|
44
|
-
|
|
45
|
-
assert.deepEqual(
|
|
46
|
-
[...opts.cacheMap.keys()],
|
|
47
|
-
[firstReqIps, secondReqIps].flat().slice(-opts.cacheMapMaxEntries),
|
|
48
|
-
);
|
|
49
|
-
});
|
|
50
|
-
});
|
package/tests/cache-map-off.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import {cacheStorage, ip2geo} from '../app/api.ts';
|
|
5
|
-
|
|
6
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
7
|
-
import {REQUEST_IPV4_MAP_OFF_ONLY} from './shared/consts.ts';
|
|
8
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
9
|
-
|
|
10
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
11
|
-
|
|
12
|
-
describe(testName, () => {
|
|
13
|
-
const opts = {
|
|
14
|
-
cacheDir: getTestFolder(testName),
|
|
15
|
-
cacheMapMaxEntries: 0,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
19
|
-
|
|
20
|
-
it(`should return correct response for IP: "${REQUEST_IPV4_MAP_OFF_ONLY.ip}"`, async () => {
|
|
21
|
-
const data = await ip2geo({ip: REQUEST_IPV4_MAP_OFF_ONLY.ip ?? '', ...opts});
|
|
22
|
-
assert.deepEqual(data, REQUEST_IPV4_MAP_OFF_ONLY);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should have cache file', () =>
|
|
26
|
-
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
|
-
});
|
package/tests/default.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {cacheStorage, ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
9
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder());
|
|
15
|
-
|
|
16
|
-
describe('with ip arg', () => {
|
|
17
|
-
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
18
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? ''});
|
|
19
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should have cache file', () =>
|
|
23
|
-
checkCacheFile({
|
|
24
|
-
response: REQUEST_IPV4,
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
it('should have 1 correct cache entry', () => {
|
|
28
|
-
assert.equal(cacheStorage.size, 1);
|
|
29
|
-
assert.deepEqual(cacheStorage.get(REQUEST_IPV4.ip ?? ''), REQUEST_IPV4);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
describe('without ip arg', () => {
|
|
34
|
-
let data: ReqOutput;
|
|
35
|
-
|
|
36
|
-
it('should request geoip without ip arg', async () => {
|
|
37
|
-
data = await ip2geo();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
Object.keys(REQUEST_IPV4).forEach(key => {
|
|
41
|
-
it(`should have "${key}" in request response`, () => {
|
|
42
|
-
assert.ok(Object.hasOwn(data, key));
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should not have extra keys in request response', () => {
|
|
47
|
-
assert.deepEqual(Object.keys(data), Object.keys(REQUEST_IPV4));
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should have 2 cache entries', () => {
|
|
51
|
-
assert.equal(cacheStorage.size, 2);
|
|
52
|
-
assert.deepEqual(cacheStorage.get(REQUEST_IPV4.ip ?? ''), REQUEST_IPV4);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
});
|
package/tests/helpers/path.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
9
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const opts = {
|
|
15
|
-
cacheDir: getTestFolder(testName),
|
|
16
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
17
|
-
requestsCount: 5,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
21
|
-
|
|
22
|
-
Array.from({length: opts.requestsCount}).forEach(() => {
|
|
23
|
-
it(`should return correct response for IP: "${REQUEST_IPV4.ip}"`, async () => {
|
|
24
|
-
const data = await ip2geo({ip: REQUEST_IPV4.ip ?? '', ...opts});
|
|
25
|
-
assert.deepEqual(data, REQUEST_IPV4);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should have cache file', () =>
|
|
30
|
-
checkCacheFile({
|
|
31
|
-
...opts,
|
|
32
|
-
response: REQUEST_IPV4,
|
|
33
|
-
}));
|
|
34
|
-
|
|
35
|
-
it('should have 1 correct cache entry', () => {
|
|
36
|
-
assert.equal(opts.cacheMap.size, 1);
|
|
37
|
-
assert.deepEqual(opts.cacheMap.get(REQUEST_IPV4.ip ?? ''), REQUEST_IPV4);
|
|
38
|
-
});
|
|
39
|
-
});
|
package/tests/ip-no-data.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV4} from './shared/consts.ts';
|
|
9
|
-
import {removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const responses = ['10.10.10.10', '192.168.1.100'];
|
|
15
|
-
|
|
16
|
-
const opts = {
|
|
17
|
-
cacheDir: getTestFolder(testName),
|
|
18
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
22
|
-
|
|
23
|
-
responses.forEach(ip => {
|
|
24
|
-
const expectedResponse = Object.fromEntries(
|
|
25
|
-
Object.keys(REQUEST_IPV4).map(key => [key, key === 'ip' ? ip : '']),
|
|
26
|
-
) as ReqOutput;
|
|
27
|
-
|
|
28
|
-
it(`should return empty response for IP: "${ip}"`, async () => {
|
|
29
|
-
const data = await ip2geo({ip, ...opts});
|
|
30
|
-
assert.deepEqual(data, expectedResponse);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
});
|
package/tests/ip-v6.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import {describe, it} from 'node:test';
|
|
3
|
-
|
|
4
|
-
import type {ReqOutput} from '../api.d.ts';
|
|
5
|
-
import {ip2geo} from '../app/api.ts';
|
|
6
|
-
|
|
7
|
-
import {getCurrentFilename, getTestFolder} from './helpers/path.ts';
|
|
8
|
-
import {REQUEST_IPV6} from './shared/consts.ts';
|
|
9
|
-
import {checkCacheFile, removeCacheFolder} from './shared/fs.ts';
|
|
10
|
-
|
|
11
|
-
const testName = getCurrentFilename(import.meta.url);
|
|
12
|
-
|
|
13
|
-
describe(testName, () => {
|
|
14
|
-
const opts = {
|
|
15
|
-
cacheDir: getTestFolder(testName),
|
|
16
|
-
cacheMap: new Map<string, ReqOutput>(),
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
it('should remove fs cache dir if exist', () => removeCacheFolder(opts.cacheDir));
|
|
20
|
-
|
|
21
|
-
it(`should return correct response for IP: "${REQUEST_IPV6.ip}"`, async () => {
|
|
22
|
-
const data = await ip2geo({ip: REQUEST_IPV6.ip ?? '', ...opts});
|
|
23
|
-
assert.deepEqual(data, REQUEST_IPV6);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should have cache file', () =>
|
|
27
|
-
checkCacheFile({
|
|
28
|
-
...opts,
|
|
29
|
-
response: REQUEST_IPV6,
|
|
30
|
-
}));
|
|
31
|
-
});
|
package/tests/shared/consts.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type {ReqOutput} from '../../api.d.ts';
|
|
2
|
-
|
|
3
|
-
export const REQUEST_IPV4: ReqOutput = {
|
|
4
|
-
ip: '8.8.8.8',
|
|
5
|
-
continent: 'North America',
|
|
6
|
-
continentCode: 'NA',
|
|
7
|
-
country: 'United States',
|
|
8
|
-
countryCode: 'US',
|
|
9
|
-
countryEmoji: 'πΊπΈ',
|
|
10
|
-
region: 'California',
|
|
11
|
-
regionCode: 'CA',
|
|
12
|
-
city: 'San Jose',
|
|
13
|
-
connectionAsn: 15_169,
|
|
14
|
-
connectionOrg: 'Google LLC',
|
|
15
|
-
connectionIsp: 'Google LLC',
|
|
16
|
-
connectionDomain: 'google.com',
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export const REQUEST_IPV4_MAP_OFF_ONLY: ReqOutput = {
|
|
20
|
-
ip: '9.9.9.9',
|
|
21
|
-
continent: 'North America',
|
|
22
|
-
continentCode: 'NA',
|
|
23
|
-
country: 'United States',
|
|
24
|
-
countryCode: 'US',
|
|
25
|
-
countryEmoji: 'πΊπΈ',
|
|
26
|
-
region: 'California',
|
|
27
|
-
regionCode: 'CA',
|
|
28
|
-
city: 'San Francisco',
|
|
29
|
-
connectionAsn: 19_281,
|
|
30
|
-
connectionOrg: 'Quad9',
|
|
31
|
-
connectionIsp: 'Quad9',
|
|
32
|
-
connectionDomain: 'quad9.net',
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export const REQUEST_IPV6: ReqOutput = {
|
|
36
|
-
ip: '2a00:dd80:40:100::',
|
|
37
|
-
continent: 'Europe',
|
|
38
|
-
continentCode: 'EU',
|
|
39
|
-
country: 'Poland',
|
|
40
|
-
countryCode: 'PL',
|
|
41
|
-
countryEmoji: 'π΅π±',
|
|
42
|
-
region: 'Masovian Voivodeship',
|
|
43
|
-
regionCode: '14',
|
|
44
|
-
city: 'Warsaw',
|
|
45
|
-
connectionAsn: 36_236,
|
|
46
|
-
connectionOrg: 'NetActuate Inc',
|
|
47
|
-
connectionIsp: 'NetActuate, Inc',
|
|
48
|
-
connectionDomain: 'netactuate.com',
|
|
49
|
-
};
|
package/tests/shared/fs.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import fs from 'node:fs/promises';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
|
|
5
|
-
import type {ReqOutput} from '../../api.d.ts';
|
|
6
|
-
import {
|
|
7
|
-
DEFAULT_CACHE_FILE_DIR,
|
|
8
|
-
DEFAULT_CACHE_FILE_NAME,
|
|
9
|
-
DEFAULT_CACHE_FILE_NEWLINE,
|
|
10
|
-
DEFAULT_CACHE_FILE_SEPARATOR,
|
|
11
|
-
} from '../../app/api.ts';
|
|
12
|
-
|
|
13
|
-
interface CheckCacheFileOpts {
|
|
14
|
-
cacheDir?: string;
|
|
15
|
-
cacheFileName?: string;
|
|
16
|
-
cacheFileSeparator?: string;
|
|
17
|
-
cacheFileNewline?: string;
|
|
18
|
-
response: ReqOutput;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const removeCacheFolder = async (
|
|
22
|
-
cacheDir: string = DEFAULT_CACHE_FILE_DIR,
|
|
23
|
-
): Promise<void> => {
|
|
24
|
-
try {
|
|
25
|
-
await fs.rm(cacheDir, {recursive: true, force: true});
|
|
26
|
-
} catch (err) {
|
|
27
|
-
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
|
|
28
|
-
throw err;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const checkCacheFile = async ({
|
|
34
|
-
cacheDir = DEFAULT_CACHE_FILE_DIR,
|
|
35
|
-
cacheFileName = DEFAULT_CACHE_FILE_NAME,
|
|
36
|
-
cacheFileSeparator = DEFAULT_CACHE_FILE_SEPARATOR,
|
|
37
|
-
cacheFileNewline = DEFAULT_CACHE_FILE_NEWLINE,
|
|
38
|
-
response,
|
|
39
|
-
}: CheckCacheFileOpts): Promise<void> => {
|
|
40
|
-
const cacheFile = `${response.ip?.split(/[.:]/)[0] ?? ''}_${cacheFileName}`;
|
|
41
|
-
const data = await fs.readFile(path.join(cacheDir, cacheFile), {encoding: 'utf8'});
|
|
42
|
-
|
|
43
|
-
assert.equal(data, cacheFileNewline + Object.values(response).join(cacheFileSeparator));
|
|
44
|
-
};
|