ip2region/binding/javascript/tests/searcher.test.js
2025-10-12 13:48:26 +08:00

35 lines
1.1 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 tester
// @Author Lion <chenxin619315@gmail.com>
const {IPv4, parseIP, ipToString} = require('../util');
const {newWithFileOnly} = require('../searcher');
const path = require('path');
const dbPath = {
v4: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v4.xdb'),
v6: path.join(__dirname, '..', '..', '..', 'data', 'ip2region_v6.xdb')
}
test('ipv4 searcher test', async () => {
try {
let searcher = newWithFileOnly(IPv4, dbPath.v4);
let ip_list = [
'1.0.0.0',
parseIP('113.118.112.93'),
'240e:3b7::'
];
for (var i = 0; i < ip_list.length; i++) {
let ip = ip_list[i];
let region = await searcher.search(ip);
let ipStr = Buffer.isBuffer(ip) ? ipToString(ip) : ip;
console.log(`search(${ipStr}): {region: ${region}, ioCount: ${searcher.getIOCount()}}`);
}
} catch (e) {
console.log(`${e.message}`);
}
});