diff --git a/binding/nodejs/index.js b/binding/nodejs/index.js index ee076a9..08a23df 100644 --- a/binding/nodejs/index.js +++ b/binding/nodejs/index.js @@ -14,7 +14,7 @@ const VectorIndexLength = 256 * 256 * (4 + 4) // 二分索引项的字节数 const SegmentIndexSize = 14 // IPv4检查正则 -const IP_REGEX = /((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/ +const IP_REGEX = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/ const getStartEndPtr = Symbol('#getStartEndPtr') const getBuffer = Symbol('#getBuffer') @@ -86,7 +86,7 @@ class Searcher { ioCount: 0 } - if (!IP_REGEX.test(ip)) { + if (!isValidIp(ip)) { throw new Error(`IP: ${ip} is invalid`) } @@ -194,6 +194,10 @@ const _checkFile = dbPath => { } } +const isValidIp = ip => { + return IP_REGEX.test(ip) +} + const newWithFileOnly = dbPath => { _checkFile(dbPath) @@ -238,6 +242,7 @@ const loadContentFromFile = dbPath => { } module.exports = { + isValidIp, loadVectorIndexFromFile, loadContentFromFile, newWithFileOnly, diff --git a/binding/nodejs/tests/bench.app.js b/binding/nodejs/tests/bench.app.js index 0a48374..091e97b 100644 --- a/binding/nodejs/tests/bench.app.js +++ b/binding/nodejs/tests/bench.app.js @@ -86,9 +86,21 @@ const main = async () => { const list = line.split('|') const sip = list[0] const eip = list[1] + + if (!Searcher.isValidIp(sip)) { + throw new Error(`IP: ${sip} is invalid`) + } + if (!Searcher.isValidIp(eip)) { + throw new Error(`IP: ${eip} is invalid`) + } + const sipInt = ipToInt(sip) const eipInt = ipToInt(eip) + if (sipInt > eipInt) { + throw new Error(`start ip(${sip}) should not be greater than end ip(${eip})`) + } + const mipInt = Math.floor((sipInt + eipInt) / 2) const mip = intToIp(mipInt)