search api desc & return empty string for empty match

This commit is contained in:
lion 2025-10-21 10:48:25 +08:00
parent ca6c0506e6
commit bb3c845c81
3 changed files with 11 additions and 4 deletions

View File

@ -7,6 +7,13 @@
npm install ip2region.js --save
```
### 关于查询 API
查询 API 的原型为:
```javascript
search(ip: string | Buffer): string;
```
如果查询出错会抛异常,查询成功则会返回字符的 `region` 信息,如果指定的 IP 查询不到则会返回空字符串 `""`
### 关于 IPv4 和 IPv6
该 xdb 查询客户端实现同时支持对 IPv4 和 IPv6 的查询,使用方式如下:
```javascript

View File

@ -1,6 +1,6 @@
{
"name": "ip2region.js",
"version": "3.1.5",
"version": "3.1.6",
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
"type": "module",
"main": "index.js",

View File

@ -70,7 +70,7 @@ export class Searcher {
const bytes = ipBytes.length, dBytes = ipBytes.length << 1;
const indexSize = this.version.indexSize;
const buff = Buffer.alloc(indexSize);
let dLen = -1, dPtr = -1, l = 0, h = (ePtr - sPtr) / indexSize;
let dLen = 0, dPtr = 0, l = 0, h = (ePtr - sPtr) / indexSize;
while (l <= h) {
const m = (l + h) >> 1;
const p = sPtr + m * indexSize;
@ -90,8 +90,8 @@ export class Searcher {
// empty match interception.
// and this could be a case.
if (dPtr == -1) {
return null;
if (dLen == 0) {
return "";
}
// console.log(`dLen: ${dLen}, dPtr: ${dPtr}`);