ip2region/binding/javascript/tests/searcher.test.js
2025-10-13 15:28:30 +08:00

71 lines
2.3 KiB
JavaScript

// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
// searcher new tester
// @Author Lion <chenxin619315@gmail.com>
import path from 'path';
import { fileURLToPath } from 'url';
import {
IPv4, IPv6, XdbIPv4Id,
loadVectorIndexFromFile, loadContentFromFile,
newWithFileOnly, newWithVectorIndex, newWithBuffer
} from '../index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const dbPath = {
v4: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb'),
v6: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v6.xdb')
}
function _get_creater_list(version) {
let dbFile = version.id == XdbIPv4Id ? dbPath.v4 : dbPath.v6;
return [function(){
return ["newWithFileOnly", newWithFileOnly(version, dbFile)];
}, function(){
const vIndex = loadVectorIndexFromFile(dbFile);
return ["newWithVectorIndex", newWithVectorIndex(version, dbFile, vIndex)];
}, function(){
const cBuffer = loadContentFromFile(dbFile);
return ["newWithBuffer", newWithBuffer(version, cBuffer)];
}];
}
test('ipv4 searcher test', async () => {
const ip_Str = '120.229.45.92';
const c_list = _get_creater_list(IPv4);
try {
let bRegion = null;
for (var i = 0; i < c_list.length; i++) {
const meta = c_list[i]();
const region = await meta[1].search(ip_Str);
if (bRegion != null) {
expect(region).toBe(region);
}
bRegion = region;
console.log(`${meta[0]}.search(${ip_Str}): ${region}`);
}
} catch (e) {
console.error(`${e.message}`);
}
});
test('ipv6 searcher test', async () => {
const ip_Str = '240e:57f:32ff:ffff:ffff:ffff:ffff:ffff';
const c_list = _get_creater_list(IPv6);
try {
let bRegion = null;
for (var i = 0; i < c_list.length; i++) {
const meta = c_list[i]();
const region = await meta[1].search(ip_Str);
if (bRegion != null) {
expect(region).toBe(region);
}
bRegion = region;
console.log(`${meta[0]}.search(${ip_Str}): ${region}`);
}
} catch (e) {
console.error(`${e.message}`);
}
});