Skip to content

Commit

Permalink
Merge pull request #116 from RationAI/dev/js-server
Browse files Browse the repository at this point in the history
feat: more stable js server behavior
  • Loading branch information
Aiosa authored Jan 26, 2025
2 parents daff9b3 + 1b48f01 commit 0614ff2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion server/node/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function throwFatalErrorIfFallback(res, condition, title, description, details="
res.end();
return;
}
//try to add additional info to the file
console.error(`${title}: ${description}`);

const replacer = function(match, p1) {
switch (p1) {
Expand Down Expand Up @@ -64,6 +64,7 @@ function showError(res, errTitle, errDesc, errDetails, locale='en') {
return throwFatalErrorIfFallback(res, true, errTitle, errDesc, errDetails);
}

console.error(`${title}: ${description}`);

const replacer = function(match, p1) {
switch (p1) {
Expand Down
13 changes: 8 additions & 5 deletions server/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const initViewerCoreAndPlugins = (req, res) => {
path => fs.readFileSync(path, { encoding: 'utf8', flag: 'r' }),
key => process.env[key]);

if (throwFatalErrorIf(res, core.exception, "Failed to parse the CORE initialization!")) return null;
if (throwFatalErrorIf(res, core.exception, "Failed to parse the CORE initialization!", core.exception)) return null;
core.CORE.serverStatus.name = "node";
core.CORE.serverStatus.supportsPost = true;

Expand All @@ -50,7 +50,7 @@ const initViewerCoreAndPlugins = (req, res) => {
path => fs.readFileSync(path, { encoding: 'utf8', flag: 'r' }),
dirName => fs.readdirSync(dirName).filter(f => fs.statSync(dirName + '/' + f).isDirectory()),
{ t: function () { return "Unknown Error (e-translate)."; } });
if (throwFatalErrorIf(res, core.exception, "Failed to parse the MODULES or PLUGINS initialization!")) return null;
if (throwFatalErrorIf(res, core.exception, "Failed to parse the MODULES or PLUGINS initialization!", core.exception)) return null;
return core;
}

Expand Down Expand Up @@ -144,7 +144,6 @@ async function responseViewer(req, res) {
const core = initViewerCoreAndPlugins(req, res);
if (!core) return;


const replacer = function(match, p1) {
try {
switch (p1) {
Expand Down Expand Up @@ -208,7 +207,11 @@ async function responseDeveloperSetup(req, res) {
const core = initViewerCoreAndPlugins(req, res);
if (!core) return;

core.MODULES["webgl"].loaded = true;
if (core.MODULES["webgl"]) {
core.MODULES["webgl"].loaded = true;
} else {
console.warn("Could not find webgl module: visualizations will not work!");
}
const replacer = function (match, p1) {
try {
switch (p1) {
Expand Down Expand Up @@ -281,7 +284,7 @@ server.listen(process.env.XOPAT_NODE_PORT || 9000, '0.0.0.0', () => {
console.log("Using env/env.json..");
} else if (ENV) {
if (fs.existsSync(ENV)) console.log("Using static ENV from ", ENV);
else console.log("Using static ENV directly from the variable data: ", ENV.substring(0, 31) + "...");
else console.log("Using configuration from XOPAT_ENV: ", ENV.substring(0, 31) + "...");
} else {
console.log("Using default ENV (no overrides).");
}
Expand Down
5 changes: 2 additions & 3 deletions server/templates/javascript/modules.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const {parse} = require("comment-json");
const {safeScanDir} = require("./utils");

module.exports.loadModules = function(core, fileExists, readFile, scanDir, i18n) {

const isType = core.isType;
const MODULES = core.MODULES,
ENV = core.ENV;

let modulePaths = scanDir(core.ABS_MODULES);
let modulePaths = safeScanDir(core.ABS_MODULES);

for (let dir of modulePaths) {
if (dir == "." || dir == "..") continue;
Expand Down Expand Up @@ -55,10 +56,8 @@ module.exports.loadModules = function(core, fileExists, readFile, scanDir, i18n)
console.error(core.exception, e);
}
}

}


let order = 0;

//DFS assigns smaller numbers to children -> loaded earlier
Expand Down
3 changes: 2 additions & 1 deletion server/templates/javascript/plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {parse} = require("comment-json")
const {loadModules} = require("./modules");
const {safeScanDir} = require("./utils");

module.exports.loadPlugins = function(core, fileExists, readFile, scanDir, i18n) {

Expand All @@ -19,7 +20,7 @@ module.exports.loadPlugins = function(core, fileExists, readFile, scanDir, i18n)
MODULES = core.MODULES,
ENV = core.ENV;

let pluginPaths = scanDir(core.ABS_PLUGINS);
let pluginPaths = safeScanDir(core.ABS_PLUGINS);
for (let dir of pluginPaths) {
if (dir == "." || dir == "..") continue;

Expand Down
24 changes: 24 additions & 0 deletions server/templates/javascript/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require("fs");
const path = require("path");

module.exports.safeScanDir = function (directory) {
let resolvedPaths = [];
try {
const entries = fs.readdirSync(directory);

resolvedPaths = entries.map((entry) => {
const fullPath = path.join(directory, entry);
try {
const realPath = fs.realpathSync(fullPath);
fs.statSync(realPath);
return entry;
} catch (err) {
console.error(`Failed to resolve or stat: ${fullPath}`, err.message);
return null;
}
});
} catch (err) {
console.error(`Error scanning directory: ${directory}`, err.message);
}
return resolvedPaths.filter(Boolean);
}

0 comments on commit 0614ff2

Please sign in to comment.