diff --git a/server/api/index.js b/server/api/index.js index 0e22584..7f8a547 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -20,10 +20,46 @@ function parseRq (bytes) { class APIMethods { constructor (parentObj) { this.parentObj = parentObj; + this.info.cache = new Object(); + + // Clear method cache on update config + global.server.on("config.update", () => { + this.info.cache = new Object(); + }); } info (con, req, cb) { - const serverInfo = require("./server-info"); + if (!req.fields) req.fields = "*"; + if (!req.fieldsExtSource) req.fieldsExtSource = "*"; + const cache = this.info.cache[ + JSON.stringify({ + f: req.fields, e: req.fieldsExtSource + }) + ]; + + if (cache) { + console.debug("CACHE!"); + return cb({ result: cache, trace_id: req.trace_id, ended: true }); + } + + const serverInfo = require("./server-info")(); + if (req.fields !== "*") { + const fields = Object.fromEntries(req.fields.split(/\s*,\s*/g).map(x => [x.trim(), true])); + Object.keys(serverInfo).forEach(k => { + if (!fields[k]) serverInfo[k] = undefined; + }); + } + if (req.fieldsExtSource !== "*" && req.fields.extSource !== undefined) { + const fields = Object.fromEntries(req.fieldsExtSource.split(/\s*,\s*/g).map(x => [x.trim(), true])); + Object.keys(serverInfo).forEach(k => { + if (!fields[k]) serverInfo.extSource[k] = undefined; + }); + } + this.info.cache[ + JSON.stringify({ + f: req.fields, e: req.fieldsExtSource + }) + ] = serverInfo; cb({ result: serverInfo, trace_id: req.trace_id, ended: true }); }