From deb995feb0ae33241b8710fc1d5b8a9d7710449f Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 22 Jul 2022 14:05:04 +0800 Subject: [PATCH] =?UTF-8?q?bench=E7=A8=8B=E5=BA=8F=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9sip=E3=80=81eip=E5=90=88=E6=B3=95=E6=80=A7=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/nodejs/index.js | 9 +++++++-- binding/nodejs/tests/bench.app.js | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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)