Improved state-less server code (#4645)

Ylian Saint-Hilaire committed Oct 24, 2022 at 16:58 UTC cc7670cc312d521bd4006064826b01c23cdbd1a5
2 files changed +17 -86
db.js
+14 -85
@@ -1542,20 +1542,6 @@ module.exports.CreateDB = function (parent, func) {
1542 // List all configuration files
1543 obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
1544
1545 - // Get all configuration files (TODO: This is not SQL)
1546 - obj.getAllConfigFiles = function (password, func) {
1547 - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
1548 - if (err != null) { func(null); return; }
1549 - var r = null;
1550 - for (var i = 0; i < docs.length; i++) {
1551 - var name = docs[i]._id.split('/')[1];
1552 - var data = obj.decryptData(password, docs[i].data);
1553 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
1554 - }
1555 - func(r);
1556 - });
1557 - }
1558 -
1545 // Get database information (TODO: Complete this)
1546 obj.getDbStats = function (func) {
1547 obj.stats = { c: 4 };
@@ -1796,21 +1782,6 @@ module.exports.CreateDB = function (parent, func) {
1782 });
1783 }
1784
1799 - // Get all configuration files
1800 - obj.getAllConfigFiles = function (password, func) {
1801 - obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
1802 - const docs = [];
1803 - for (var i in snapshots) { docs.push(snapshots[i].val()); }
1804 - var r = null;
1805 - for (var i = 0; i < docs.length; i++) {
1806 - var name = docs[i]._id.split('/')[1];
1807 - var data = obj.decryptData(password, docs[i].data);
1808 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
1809 - }
1810 - func(r);
1811 - });
1812 - }
1813 -
1785 // Get database information
1786 obj.getDbStats = function (func) {
1787 obj.stats = { c: 5 };
@@ -1983,20 +1954,6 @@ module.exports.CreateDB = function (parent, func) {
1954 // List all configuration files
1955 obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
1956
1986 - // Get all configuration files
1987 - obj.getAllConfigFiles = function (password, func) {
1988 - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
1989 - if (err != null) { func(null); return; }
1990 - var r = null;
1991 - for (var i = 0; i < docs.length; i++) {
1992 - var name = docs[i]._id.split('/')[1];
1993 - var data = obj.decryptData(password, docs[i].data);
1994 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
1995 - }
1996 - func(r);
1997 - });
1998 - }
1999 -
1957 // Get database information (TODO: Complete this)
1958 obj.getDbStats = function (func) {
1959 obj.stats = { c: 4 };
@@ -2166,20 +2123,6 @@ module.exports.CreateDB = function (parent, func) {
2123
2124 // List all configuration files
2125 obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
2169 -
2170 - // Get all configuration files
2171 - obj.getAllConfigFiles = function (password, func) {
2172 - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
2173 - if (err != null) { func(null); return; }
2174 - var r = null;
2175 - for (var i = 0; i < docs.length; i++) {
2176 - var name = docs[i]._id.split('/')[1];
2177 - var data = obj.decryptData(password, docs[i].data);
2178 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
2179 - }
2180 - func(r);
2181 - });
2182 - }
2126
2127 // Get database information (TODO: Complete this)
2128 obj.getDbStats = function (func) {
@@ -2451,20 +2394,6 @@ module.exports.CreateDB = function (parent, func) {
2394 // List all configuration files
2395 obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); }
2396
2454 - // Get all configuration files
2455 - obj.getAllConfigFiles = function (password, func) {
2456 - obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
2457 - if (err != null) { func(null); return; }
2458 - var r = null;
2459 - for (var i = 0; i < docs.length; i++) {
2460 - var name = docs[i]._id.split('/')[1];
2461 - var data = obj.decryptData(password, docs[i].data);
2462 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
2463 - }
2464 - func(r);
2465 - });
2466 - }
2467 -
2397 // Get database information
2398 obj.getDbStats = function (func) {
2399 obj.stats = { c: 6 };
@@ -2635,20 +2564,6 @@ module.exports.CreateDB = function (parent, func) {
2564 // List all configuration files
2565 obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); }
2566
2638 - // Get all configuration files
2639 - obj.getAllConfigFiles = function (password, func) {
2640 - obj.file.find({ type: 'cfile' }, function (err, docs) {
2641 - if (err != null) { func(null); return; }
2642 - var r = null;
2643 - for (var i = 0; i < docs.length; i++) {
2644 - var name = docs[i]._id.split('/')[1];
2645 - var data = obj.decryptData(password, docs[i].data);
2646 - if (data != null) { if (r == null) { r = {}; } r[name] = data; }
2647 - }
2648 - func(r);
2649 - });
2650 - }
2651 -
2567 // Get database information
2568 obj.getDbStats = function (func) {
2569 obj.stats = { c: 5 };
@@ -2682,6 +2597,20 @@ module.exports.CreateDB = function (parent, func) {
2597
2598 }
2599
2600 + // Get all configuration files
2601 + obj.getAllConfigFiles = function (password, func) {
2602 + obj.GetAllType('cfile', function (err, docs) {
2603 + if (err != null) { func(null); return; }
2604 + var r = null;
2605 + for (var i = 0; i < docs.length; i++) {
2606 + var name = docs[i]._id.split('/')[1];
2607 + var data = obj.decryptData(password, docs[i].data);
2608 + if (data != null) { if (r == null) { r = {}; } r[name] = data; }
2609 + }
2610 + func(r);
2611 + });
2612 + }
2613 +
2614 func(obj); // Completed function setup
2615 }
2616
meshcentral.js
+3 -1
@@ -1189,11 +1189,13 @@ function CreateMeshCentralServer(config, args) {
1189 obj.db.getAllConfigFiles(key, function (configFiles) {
1190 if (configFiles == null) { console.log("Error, no configuration files found or invalid configkey."); process.exit(); return; }
1191 if (!configFiles['config.json']) { console.log("Error, could not file config.json from database."); process.exit(); return; }
1192 + if (typeof configFiles['config.json'] == 'object') { configFiles['config.json'] = configFiles['config.json'].toString(); }
1193 + if (configFiles['config.json'].charCodeAt(0) == 65279) { configFiles['config.json'] = configFiles['config.json'].substring(1); }
1194 obj.configurationFiles = configFiles;
1195
1196 // Parse the new configuration file
1197 var config2 = null;
1196 - try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.'); process.exit(); return; }
1198 + try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.', ex); process.exit(); return; }
1199
1200 // Set the command line arguments to the config file if they are not present
1201 if (!config2.settings) { config2.settings = {}; }