1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

feat: parse info data as numbers if possible and improve parsing

This commit is contained in:
Ruben Bridgewater
2017-05-28 00:12:36 +02:00
parent 8da9e98fe6
commit 8c63233968
14 changed files with 91 additions and 65 deletions

View File

@@ -116,36 +116,54 @@ Multi.prototype.quit = function quit () {
function infoCallback (client) {
return function (err, res) {
if (err) {
client.serverInfo = {}
return err
}
const obj = {}
const lines = res.toString().split('\r\n')
var line, parts, subParts
if (typeof res !== 'string') {
res = res.toString()
}
for (var i = 0; i < lines.length; i++) {
parts = lines[i].split(':')
if (parts[1]) {
if (parts[0].indexOf('db') === 0) {
subParts = parts[1].split(',')
obj[parts[0]] = {}
for (line = subParts.pop(); line !== undefined; line = subParts.pop()) {
line = line.split('=')
obj[parts[0]][line[0]] = +line[1]
const obj = {}
const lines = res.split('\r\n')
var topic = ''
while (lines.length) {
const parts = lines.shift().split(':')
const key = parts[0]
if (parts.length === 1) {
if (key !== '') {
topic = key[2].toLowerCase() + key.substr(3)
obj[topic] = {}
}
} else {
const value = parts[1]
const part = obj[topic]
if (value === '') {
part[key] = ''
} else if (topic === 'keyspace' || topic === 'commandstats') {
const subParts = value.split(',')
part[key] = {}
while (subParts.length) {
const line = subParts.shift().split('=')
part[key][line[0]] = +line[1]
}
} else {
obj[parts[0]] = parts[1]
const num = +value
// A fast Number.isNaN check
// eslint-disable-next-line no-self-compare
if (num === num) {
part[key] = num
} else {
part[key] = value
}
}
}
}
obj.versions = []
if (obj.redis_version) {
obj.redis_version.split('.').forEach((num) => {
obj.versions.push(+num)
})
if (obj.server && obj.server.redis_version) {
obj.server.version = obj.server.redis_version.split('.').map(Number)
}
// Expose info key/values to users
client.serverInfo = obj
return res
}