MongoDB TextEncoder is not defined fix (#4499)

Ylian Saint-Hilaire committed Sep 5, 2022 at 01:40 UTC 43abf32dba86e7b25d57c163a53d30f6aa8bc837
1 file changed +9 -4
db.js
+9 -4
@@ -307,7 +307,7 @@ module.exports.CreateDB = function (parent, func) {
307 validIdentifiers[docs[i]._id] = 1;
308 }
309 }
310 -
310 +
311 // Fix all of the creating & login to ticks by seconds, not milliseconds.
312 obj.GetAllType('user', function (err, docs) {
313 if (err != null) { parent.debug('db', 'ERROR (GetAll user): ' + err); }
@@ -675,7 +675,7 @@ module.exports.CreateDB = function (parent, func) {
675 obj.file = new sqlite3.Database(parent.path.join(parent.datapath, 'meshcentral.sqlite'), sqlite3.OPEN_READWRITE, function (err) {
676 if (err && (err.code == 'SQLITE_CANTOPEN')) {
677 // Database needs to be created
678 - obj.file = new sqlite3.Database(parent.path.join(parent.datapath, 'meshcentral.sqlite'), function(err) {
678 + obj.file = new sqlite3.Database(parent.path.join(parent.datapath, 'meshcentral.sqlite'), function (err) {
679 if (err) { console.log("SQLite Error: " + err); exit(1); return; }
680 obj.file.exec(`
681 CREATE TABLE main (id VARCHAR(256) PRIMARY KEY NOT NULL, type CHAR(32), domain CHAR(64), extra CHAR(255), extraex CHAR(255), doc JSON);
@@ -724,7 +724,7 @@ module.exports.CreateDB = function (parent, func) {
724 obj.file.indexes.create('meshcenral', 'intelamt.uuid');
725 obj.file.indexes.create('events', 'userid', { include: ['action'] });
726 obj.file.indexes.create('events', 'domain', { include: ['nodeid', 'time'] });
727 - obj.file.indexes.create('events', 'ids', { include: ['time'] });
727 + obj.file.indexes.create('events', 'ids', { include: ['time'] });
728 obj.file.indexes.create('events', 'time');
729 obj.file.indexes.create('power', 'nodeid', { include: ['time'] });
730 obj.file.indexes.create('power', 'time');
@@ -743,7 +743,7 @@ module.exports.CreateDB = function (parent, func) {
743
744 try {
745 if (connectinArgs.ssl) {
746 - if (connectinArgs.ssl.dontcheckserveridentity == true) { connectionObject.ssl.checkServerIdentity = function(name, cert) { return undefined; } };
746 + if (connectinArgs.ssl.dontcheckserveridentity == true) { connectionObject.ssl.checkServerIdentity = function (name, cert) { return undefined; } };
747 if (connectinArgs.ssl.cacertpath) { connectionObject.ssl.ca = [require('fs').readFileSync(connectinArgs.ssl.cacertpath, 'utf8')]; }
748 if (connectinArgs.ssl.clientcertpath) { connectionObject.ssl.cert = [require('fs').readFileSync(connectinArgs.ssl.clientcertpath, 'utf8')]; }
749 if (connectinArgs.ssl.clientkeypath) { connectionObject.ssl.key = [require('fs').readFileSync(connectinArgs.ssl.clientkeypath, 'utf8')]; }
@@ -828,6 +828,11 @@ module.exports.CreateDB = function (parent, func) {
828 } else if (parent.args.mongodb) {
829 // Use MongoDB
830 obj.databaseType = 3;
831 +
832 + // If running an older NodeJS version, TextEncoder/TextDecoder is required
833 + if (global.TextEncoder == null) { global.TextEncoder = require('util').TextEncoder; }
834 + if (global.TextDecoder == null) { global.TextDecoder = require('util').TextDecoder; }
835 +
836 require('mongodb').MongoClient.connect(parent.args.mongodb, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {
837 if (err != null) { console.log("Unable to connect to database: " + err); process.exit(); return; }
838 Datastore = client;