Release 2.29.0 (#3568)

## [2.29.0] - 2024-10-01

Thanks to: @bugsounet, @dkallen78, @jargordon, @khassel,
@KristjanESPERANTO, @MarcLandis, @rejas, @ryan-d-williams, @sdetweil,
@skpanagiotis.

> ⚠️ This release needs nodejs version `v20` or `v22`, minimum version
is `v20.9.0`

### Added

- [compliments] Added support for cron type date/time format entries mm
hh DD MM dow (minutes/hours/days/months and day of week) see
https://crontab.cronhub.io for construction (#3481)
- [core] Check config at every start of MagicMirror² (#3450)
- [core] Add spelling check (cspell): `npm run test:spelling` and handle
spelling issues (#3544)
- [core] removed `config.paths.vendor` (could not work because `vendor`
is hardcoded in `index.html`), renamed `config.paths.modules` to
`config.foreignModulesDir`, added variable `MM_CUSTOMCSS_FILE` which -
if set - overrides `config.customCss`, added variable `MM_MODULES_DIR`
which - if set - overrides `config.foreignModulesDir`, added test for
`MM_MODULES_DIR` (#3530)
- [core] elements are now removed from `index.html` when loading script
or stylesheet files fails
- [core] Added `MODULE_DOM_UPDATED` notification each time the DOM is
re-rendered via `updateDom` (#3534)
- [tests] added minimal needed node version to tests (currently v20.9.0)
to avoid releases with wrong node version info
- [tests] Added `node-libgpiod` library to electron-rebuild tests
(#3563)

### Removed

- [core] removed installer only files (#3492)
- [core] removed raspberry object from systeminformation (#3505)
- [linter] removed `eslint-plugin-import`, because it doesn't support
ESLint v9. We will reenter it later when it does.
- [tests] removed `onoff` library from electron-rebuild tests (#3563)

### Updated

- [weather] Updated `apiVersion` default from 2.5 to 3.0 (#3424)
- [core] Updated dependencies including stylistic-eslint
- [core] nail down `node-ical` version to `0.18.0` with exception
`allow-ghsas: GHSA-8hc4-vh64-cxmj` in `dep-review.yaml` (which should
removed after next `node-ical` update)
- [core] Updated SocketIO catch all to new API
- [core] Allow custom modules positions by scanning index.html for the
defined regions, instead of hard coded (PR #3518 fixes issue #3504)
- [core] Detail optimizations in `config_check.js`
- [core] Updated minimal needed node version in `package.json`
(currently v20.9.0) (#3559) and except for v21 (no security updates)
(#3561)
- [linter] Switch to ESLint v9 and flat config and replace
`eslint-plugin-unicorn` by `@eslint/js`
- [core] fix discovering module positions twice after #3450

### Fixed

- Fixed `checks` badge in README.md
- [weather] Fixed issue with the UK Met Office provider following a
change in their API paths and header info.
- [core] add check for node_helper loading for multiple instances of
same module (#3502)
- [weather] Fixed issue for respecting unit config on broadcasted
notifications
- [tests] Fixes calendar test by moving it from e2e to electron with
fixed date (#3532)
- [calendar] fixed sliceMultiDayEvents getting wrong count and
displaying incorrect entries, Europe/Berlin (#3542)
- [tests] ignore `js/positions.js` when linting (this file is created at
runtime)
- [calendar] fixed sliceMultiDayEvents showing previous day without
config enabled

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Michael Teeuw <michael@xonaymedia.nl>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ross Younger <crazyscot@gmail.com>
Co-authored-by: Veeck <github@veeck.de>
Co-authored-by: Bugsounet - Cédric <github@bugsounet.fr>
Co-authored-by: jkriegshauser <joshuakr@nvidia.com>
Co-authored-by: illimarkangur <116028111+illimarkangur@users.noreply.github.com>
Co-authored-by: sam detweiler <sdetweil@gmail.com>
Co-authored-by: vppencilsharpener <tim.pray@gmail.com>
Co-authored-by: veeck <michael.veeck@nebenan.de>
Co-authored-by: Paranoid93 <6515818+Paranoid93@users.noreply.github.com>
Co-authored-by: Brian O'Connor <btoconnor@users.noreply.github.com>
Co-authored-by: WallysWellies <59727507+WallysWellies@users.noreply.github.com>
Co-authored-by: Jason Stieber <jrstieber@gmail.com>
Co-authored-by: jargordon <50050429+jargordon@users.noreply.github.com>
Co-authored-by: Daniel <32464403+dkallen78@users.noreply.github.com>
Co-authored-by: Ryan Williams <65094007+ryan-d-williams@users.noreply.github.com>
Co-authored-by: Panagiotis Skias <panagiotis.skias@gmail.com>
Co-authored-by: Marc Landis <dirk.rettschlag@gmail.com>
This commit is contained in:
Karsten Hassel 2024-10-01 00:02:17 +02:00 committed by GitHub
parent 53fc814ff8
commit 94c3c699e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 3899 additions and 2830 deletions

View file

@ -9,6 +9,7 @@ const Log = require("logger");
const Server = require(`${__dirname}/server`);
const Utils = require(`${__dirname}/utils`);
const defaultModules = require(`${__dirname}/../modules/default/defaultmodules`);
const { getEnvVarsAsObj } = require(`${__dirname}/server_functions`);
// Get version number.
global.version = require(`${__dirname}/../package.json`).version;
@ -116,6 +117,8 @@ function App () {
}
}
require(`${global.root_path}/js/check_config.js`);
try {
fs.accessSync(configFilename, fs.F_OK);
const c = require(configFilename);
@ -159,10 +162,19 @@ function App () {
function loadModule (module) {
const elements = module.split("/");
const moduleName = elements[elements.length - 1];
let moduleFolder = `${__dirname}/../modules/${module}`;
const env = getEnvVarsAsObj();
let moduleFolder = `${__dirname}/../${env.modulesDir}/${module}`;
if (defaultModules.includes(moduleName)) {
moduleFolder = `${__dirname}/../modules/default/${module}`;
const defaultModuleFolder = `${__dirname}/../modules/default/${module}`;
if (process.env.JEST_WORKER_ID === undefined) {
moduleFolder = defaultModuleFolder;
} else {
// running in Jest, allow defaultModules placed under moduleDir for testing
if (env.modulesDir === "modules") {
moduleFolder = defaultModuleFolder;
}
}
}
const moduleFile = `${moduleFolder}/${module}.js`;
@ -183,6 +195,7 @@ function App () {
Log.log(`No helper found for module: ${moduleName}.`);
}
// if the helper was found
if (loadHelper) {
const Module = require(helperPath);
let m = new Module();
@ -255,17 +268,23 @@ function App () {
Log.setLogLevel(config.logLevel);
// get the used module positions
Utils.getModulePositions();
let modules = [];
for (const module of config.modules) {
if (module.disabled) continue;
if (module.module) {
if (Utils.moduleHasValidPosition(module.position) || typeof (module.position) === "undefined") {
modules.push(module.module);
// Only add this module to be loaded if it is not a duplicate (repeated instance of the same module)
if (!modules.includes(module.module)) {
modules.push(module.module);
}
} else {
Log.warn("Invalid module position found for this configuration:", module);
Log.warn("Invalid module position found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
}
} else {
Log.warn("No module name found for this configuration:", module);
Log.warn("No module name found for this configuration:" + `\n${JSON.stringify(module, null, 2)}`);
}
}

View file

@ -1,16 +1,16 @@
const path = require("node:path");
const fs = require("node:fs");
const colors = require("ansis");
const { Linter } = require("eslint");
const linter = new Linter();
const Ajv = require("ajv");
const ajv = new Ajv();
const colors = require("ansis");
const globals = require("globals");
const { Linter } = require("eslint");
const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`);
const Utils = require(`${rootPath}/js/utils.js`);
const linter = new Linter({ configType: "flat" });
const ajv = new Ajv();
/**
* Returns a string with path of configuration file.
@ -30,46 +30,55 @@ function checkConfigFile () {
// Check if file is present
if (fs.existsSync(configFileName) === false) {
Log.error(`File not found: ${configFileName}`);
throw new Error("No config file present!");
throw new Error(`File not found: ${configFileName}\nNo config file present!`);
}
// Check permission
try {
fs.accessSync(configFileName, fs.F_OK);
} catch (e) {
Log.error(e);
throw new Error("No permission to access config file!");
} catch (error) {
throw new Error(`${error}\nNo permission to access config file!`);
}
// Validate syntax of the configuration file.
Log.info("Checking file... ", configFileName);
Log.info(`Checking config file ${configFileName} ...`);
// I'm not sure if all ever is utf-8
const configFile = fs.readFileSync(configFileName, "utf-8");
// Explicitly tell linter that he might encounter es6 syntax ("let config = {...}")
const errors = linter.verify(configFile, {
env: {
es6: true
}
});
const errors = linter.verify(
configFile,
{
languageOptions: {
ecmaVersion: "latest",
globals: {
...globals.node
}
}
},
configFileName
);
if (errors.length === 0) {
Log.info(colors.green("Your configuration file doesn't contain syntax errors :)"));
validateModulePositions(configFileName);
} else {
Log.error(colors.red("Your configuration file contains syntax errors :("));
let errorMessage = "Your configuration file contains syntax errors :(";
for (const error of errors) {
Log.error(`Line ${error.line} column ${error.column}: ${error.message}`);
errorMessage += `\nLine ${error.line} column ${error.column}: ${error.message}`;
}
return;
throw new Error(errorMessage);
}
}
Log.info("Checking modules structure configuration... ");
function validateModulePositions (configFileName) {
Log.info("Checking modules structure configuration ...");
// Make Ajv schema confguration of modules config
// only scan "module" and "position"
const positionList = Utils.getModulePositions();
// Make Ajv schema configuration of modules config
// Only scan "module" and "position"
const schema = {
type: "object",
properties: {
@ -83,21 +92,7 @@ function checkConfigFile () {
},
position: {
type: "string",
enum: [
"top_bar",
"top_left",
"top_center",
"top_right",
"upper_third",
"middle_center",
"lower_third",
"bottom_left",
"bottom_center",
"bottom_right",
"bottom_bar",
"fullscreen_above",
"fullscreen_below"
]
enum: positionList
}
},
required: ["module"]
@ -106,26 +101,31 @@ function checkConfigFile () {
}
};
// scan all modules
// Scan all modules
const validate = ajv.compile(schema);
const data = require(configFileName);
const valid = validate(data);
if (!valid) {
let module = validate.errors[0].instancePath.split("/")[2];
let position = validate.errors[0].instancePath.split("/")[3];
Log.error(colors.red("This module configuration contains errors:"));
Log.error(data.modules[module]);
if (position) {
Log.error(colors.red(`${position}: ${validate.errors[0].message}`));
Log.error(validate.errors[0].params.allowedValues);
} else {
Log.error(colors.red(validate.errors[0].message));
}
} else {
if (valid) {
Log.info(colors.green("Your modules structure configuration doesn't contain errors :)"));
} else {
const module = validate.errors[0].instancePath.split("/")[2];
const position = validate.errors[0].instancePath.split("/")[3];
let errorMessage = "This module configuration contains errors:";
errorMessage += `\n${JSON.stringify(data.modules[module], null, 2)}`;
if (position) {
errorMessage += `\n${position}: ${validate.errors[0].message}`;
errorMessage += `\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`;
} else {
errorMessage += validate.errors[0].message;
}
Log.error(errorMessage);
}
}
checkConfigFile();
try {
checkConfigFile();
} catch (error) {
Log.error(error.message);
process.exit(1);
}

View file

@ -1,6 +1,7 @@
/* global Class, xyz */
/* Simple JavaScript Inheritance
/*
* Simple JavaScript Inheritance
* By John Resig https://johnresig.com/
*
* Inspired by base2 and Prototype
@ -22,8 +23,10 @@
Class.extend = function (prop) {
let _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
/*
* Instantiate a base class (but only create the instance,
* don't run the init constructor)
*/
initializing = true;
const prototype = new this();
initializing = false;
@ -42,12 +45,16 @@
return function () {
const tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
/*
* Add a new ._super() method that is the same method
* but on the super-class
*/
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
/*
* The method only need to be bound temporarily, so we
* remove it when we're done executing
*/
const ret = fn.apply(this, arguments);
this._super = tmp;

View file

@ -19,6 +19,7 @@ const defaults = {
units: "metric",
zoom: 1,
customCss: "css/custom.css",
foreignModulesDir: "modules",
// httpHeaders used by helmet, see https://helmetjs.github.io/. You can add other/more object values by overriding this in config.js,
// e.g. you need to add `frameguard: false` for embedding MagicMirror in another website, see https://github.com/MagicMirrorOrg/MagicMirror/issues/2847
httpHeaders: { contentSecurityPolicy: false, crossOriginOpenerPolicy: false, crossOriginEmbedderPolicy: false, crossOriginResourcePolicy: false, originAgentCluster: false },
@ -72,12 +73,7 @@ const defaults = {
text: "www.michaelteeuw.nl"
}
}
],
paths: {
modules: "modules",
vendor: "vendor"
}
]
};
/*************** DO NOT EDIT THE LINE BELOW ***************/

View file

@ -8,9 +8,12 @@ const Log = require("./logger");
let config = process.env.config ? JSON.parse(process.env.config) : {};
// Module to control application life.
const app = electron.app;
// Per default electron is started with --disable-gpu flag, if you want the gpu enabled,
// you must set the env var ELECTRON_ENABLE_GPU=1 on startup.
// See https://www.electronjs.org/docs/latest/tutorial/offscreen-rendering for more info.
/*
* Per default electron is started with --disable-gpu flag, if you want the gpu enabled,
* you must set the env var ELECTRON_ENABLE_GPU=1 on startup.
* See https://www.electronjs.org/docs/latest/tutorial/offscreen-rendering for more info.
*/
if (process.env.ELECTRON_ENABLE_GPU !== "1") {
app.disableHardwareAcceleration();
}
@ -18,16 +21,21 @@ if (process.env.ELECTRON_ENABLE_GPU !== "1") {
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
/*
* Keep a global reference of the window object, if you don't, the window will
* be closed automatically when the JavaScript object is garbage collected.
*/
let mainWindow;
/**
*
*/
function createWindow () {
// see https://www.electronjs.org/docs/latest/api/screen
// Create a window that fills the screen's available work area.
/*
* see https://www.electronjs.org/docs/latest/api/screen
* Create a window that fills the screen's available work area.
*/
let electronSize = (800, 600);
try {
electronSize = electron.screen.getPrimaryDisplay().workAreaSize;
@ -52,8 +60,10 @@ function createWindow () {
backgroundColor: "#000000"
};
// DEPRECATED: "kioskmode" backwards compatibility, to be removed
// settings these options directly instead provides cleaner interface
/*
* DEPRECATED: "kioskmode" backwards compatibility, to be removed
* settings these options directly instead provides cleaner interface
*/
if (config.kioskmode) {
electronOptionsDefaults.kiosk = true;
} else {
@ -69,8 +79,10 @@ function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow(electronOptions);
// and load the index.html of the app.
// If config.address is not defined or is an empty string (listening on all interfaces), connect to localhost
/*
* and load the index.html of the app.
* If config.address is not defined or is an empty string (listening on all interfaces), connect to localhost
*/
let prefix;
if ((config["tls"] !== null && config["tls"]) || config.useHttps) {
@ -149,14 +161,18 @@ app.on("window-all-closed", function () {
});
app.on("activate", function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
/*
* On OS X it's common to re-create a window in the app when the
* dock icon is clicked and there are no other windows open.
*/
if (mainWindow === null) {
createWindow();
}
});
/* This method will be called when SIGINT is received and will call
/*
* This method will be called when SIGINT is received and will call
* each node_helper's stop function if it exists. Added to fix #1056
*
* Note: this is only used if running Electron. Otherwise
@ -187,8 +203,10 @@ if (process.env.clientonly) {
});
}
// Start the core application if server is run on localhost
// This starts all node helpers and starts the webserver.
/*
* Start the core application if server is run on localhost
* This starts all node helpers and starts the webserver.
*/
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].includes(config.address)) {
core.start().then((c) => {
config = c;

View file

@ -10,6 +10,15 @@ const Loader = (function () {
/* Private Methods */
/**
* Retrieve object of env variables.
* @returns {object} with key: values as assembled in js/server_functions.js
*/
const getEnvVars = async function () {
const res = await fetch(`${location.protocol}//${location.host}/env`);
return JSON.parse(await res.text());
};
/**
* Loops through all modules and requests start for every module.
*/
@ -58,19 +67,28 @@ const Loader = (function () {
* Generate array with module information including module paths.
* @returns {object[]} Module information.
*/
const getModuleData = function () {
const getModuleData = async function () {
const modules = getAllModules();
const moduleFiles = [];
const envVars = await getEnvVars();
modules.forEach(function (moduleData, index) {
const module = moduleData.module;
const elements = module.split("/");
const moduleName = elements[elements.length - 1];
let moduleFolder = `${config.paths.modules}/${module}`;
let moduleFolder = `${envVars.modulesDir}/${module}`;
if (defaultModules.indexOf(moduleName) !== -1) {
moduleFolder = `${config.paths.modules}/default/${module}`;
const defaultModuleFolder = `modules/default/${module}`;
if (window.name !== "jsdom") {
moduleFolder = defaultModuleFolder;
} else {
// running in Jest, allow defaultModules placed under moduleDir for testing
if (envVars.modulesDir === "modules") {
moduleFolder = defaultModuleFolder;
}
}
}
if (moduleData.disabled === true) {
@ -166,6 +184,7 @@ const Loader = (function () {
};
script.onerror = function () {
Log.error("Error on loading script:", fileName);
script.remove();
resolve();
};
document.getElementsByTagName("body")[0].appendChild(script);
@ -183,6 +202,7 @@ const Loader = (function () {
};
stylesheet.onerror = function () {
Log.error("Error on loading stylesheet:", fileName);
stylesheet.remove();
resolve();
};
document.getElementsByTagName("head")[0].appendChild(stylesheet);
@ -197,7 +217,9 @@ const Loader = (function () {
* Load all modules as defined in the config.
*/
async loadModules () {
let moduleData = getModuleData();
let moduleData = await getModuleData();
const envVars = await getEnvVars();
const customCss = envVars.customCss;
/**
* @returns {Promise<void>} when all modules are loaded
@ -212,7 +234,7 @@ const Loader = (function () {
// All modules loaded. Load custom.css
// This is done after all the modules so we can
// overwrite all the defined styles.
await loadFile(config.customCss);
await loadFile(customCss);
// custom.css loaded. Start all modules.
await startModules();
}
@ -244,7 +266,7 @@ const Loader = (function () {
// This file is available in the vendor folder.
// Load it from this vendor folder.
loadedFiles.push(fileName.toLowerCase());
return loadFile(`${config.paths.vendor}/${vendor[fileName]}`);
return loadFile(`vendor/${vendor[fileName]}`);
}
// File not loaded yet.

View file

@ -1,4 +1,4 @@
/* global Loader, defaults, Translator, addAnimateCSS, removeAnimateCSS, AnimateCSSIn, AnimateCSSOut */
/* global Loader, defaults, Translator, addAnimateCSS, removeAnimateCSS, AnimateCSSIn, AnimateCSSOut, modulePositions */
const MM = (function () {
let modules = [];
@ -286,9 +286,9 @@ const MM = (function () {
Log.debug(`${module.identifier} Force remove animateIn (in hide): ${module.hasAnimateIn}`);
module.hasAnimateIn = false;
}
// haveAnimateName for verify if we are using AninateCSS library
// haveAnimateName for verify if we are using AnimateCSS library
// we check AnimateCSSOut Array for validate it
// and finaly return the animate name or `null` (for default MM² animation)
// and finally return the animate name or `null` (for default MM² animation)
let haveAnimateName = null;
// check if have valid animateOut in module definition (module.data.animateOut)
if (module.data.animateOut && AnimateCSSOut.indexOf(module.data.animateOut) !== -1) haveAnimateName = module.data.animateOut;
@ -357,7 +357,7 @@ const MM = (function () {
}
}
// Check if there are no more lockstrings set, or the force option is set.
// Check if there are no more lockStrings set, or the force option is set.
// Otherwise cancel show action.
if (module.lockStrings.length !== 0 && options.force !== true) {
Log.log(`Will not show ${module.name}. LockStrings active: ${module.lockStrings.join(",")}`);
@ -380,7 +380,7 @@ const MM = (function () {
module.hidden = false;
// If forced show, clean current lockstrings.
// If forced show, clean current lockStrings.
if (module.lockStrings.length !== 0 && options.force === true) {
Log.log(`Force show of module: ${module.name}`);
module.lockStrings = [];
@ -390,9 +390,9 @@ const MM = (function () {
if (moduleWrapper !== null) {
clearTimeout(module.showHideTimer);
// haveAnimateName for verify if we are using AninateCSS library
// haveAnimateName for verify if we are using AnimateCSS library
// we check AnimateCSSIn Array for validate it
// and finaly return the animate name or `null` (for default MM² animation)
// and finally return the animate name or `null` (for default MM² animation)
let haveAnimateName = null;
// check if have valid animateOut in module definition (module.data.animateIn)
if (module.data.animateIn && AnimateCSSIn.indexOf(module.data.animateIn) !== -1) haveAnimateName = module.data.animateIn;
@ -450,7 +450,6 @@ const MM = (function () {
* an ugly top margin. By using this function, the top bar will be hidden if the
* update notification is not visible.
*/
const modulePositions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];
const updateWrapperStates = function () {
modulePositions.forEach(function (position) {
@ -667,7 +666,10 @@ const MM = (function () {
}
// Further implementation is done in the private method.
updateDom(module, updateOptions);
updateDom(module, updateOptions).then(function () {
// Once the update is complete and rendered, send a notification to the module that the DOM has been updated
sendNotification("MODULE_DOM_UPDATED", null, null, module);
});
},
/**
@ -703,7 +705,7 @@ const MM = (function () {
showModule(module, speed, callback, options);
},
// return all available module postions.
// Return all available module positions.
getAvailableModulePositions: modulePositions
};
}());

View file

@ -1,13 +1,16 @@
/* global Class, cloneObject, Loader, MMSocket, nunjucks, Translator */
/* Module Blueprint.
/*
* Module Blueprint.
* @typedef {Object} Module
*/
const Module = Class.extend({
/*********************************************************
/**
********************************************************
* All methods (and properties) below can be subclassed. *
*********************************************************/
********************************************************
*/
// Set the minimum MagicMirror² module version for this module.
requiresVersion: "2.0.0",
@ -18,13 +21,17 @@ const Module = Class.extend({
// Timer reference used for showHide animation callbacks.
showHideTimer: null,
// Array to store lockStrings. These strings are used to lock
// visibility when hiding and showing module.
/*
* Array to store lockStrings. These strings are used to lock
* visibility when hiding and showing module.
*/
lockStrings: [],
// Storage of the nunjucks Environment,
// This should not be referenced directly.
// Use the nunjucksEnvironment() to get it.
/*
* Storage of the nunjucks Environment,
* This should not be referenced directly.
* Use the nunjucksEnvironment() to get it.
*/
_nunjucksEnvironment: null,
/**
@ -189,9 +196,11 @@ const Module = Class.extend({
Log.log(`${this.name} is resumed.`);
},
/*********************************************
/**
********************************************
* The methods below don't need subclassing. *
*********************************************/
********************************************
*/
/**
* Set the module data.

View file

@ -49,7 +49,8 @@ const NodeHelper = Class.extend({
this.path = path;
},
/* sendSocketNotification(notification, payload)
/*
* sendSocketNotification(notification, payload)
* Send a socket notification to the node helper.
*
* argument notification string - The identifier of the notification.
@ -59,7 +60,8 @@ const NodeHelper = Class.extend({
this.io.of(this.name).emit(notification, payload);
},
/* setExpressApp(app)
/*
* setExpressApp(app)
* Sets the express app object for this module.
* This allows you to host files from the created webserver.
*
@ -71,7 +73,8 @@ const NodeHelper = Class.extend({
app.use(`/${this.name}`, express.static(`${this.path}/public`));
},
/* setSocketIO(io)
/*
* setSocketIO(io)
* Sets the socket io object for this module.
* Binds message receiver.
*
@ -83,20 +86,9 @@ const NodeHelper = Class.extend({
Log.log(`Connecting socket for: ${this.name}`);
io.of(this.name).on("connection", (socket) => {
// add a catch all event.
const onevent = socket.onevent;
socket.onevent = function (packet) {
const args = packet.data || [];
onevent.call(this, packet); // original call
packet.data = ["*"].concat(args);
onevent.call(this, packet); // additional call to catch-all
};
// register catch all.
socket.on("*", (notification, payload) => {
if (notification !== "*") {
this.socketNotificationReceived(notification, payload);
}
socket.onAny((notification, payload) => {
this.socketNotificationReceived(notification, payload);
});
});
}

View file

@ -8,8 +8,7 @@ const helmet = require("helmet");
const socketio = require("socket.io");
const Log = require("logger");
const Utils = require("./utils");
const { cors, getConfig, getHtml, getVersion, getStartup } = require("./server_functions");
const { cors, getConfig, getHtml, getVersion, getStartup, getEnvVars } = require("./server_functions");
/**
* Server
@ -73,8 +72,11 @@ function Server (config) {
app.use(helmet(config.httpHeaders));
app.use("/js", express.static(__dirname));
// TODO add tests directory only when running tests?
const directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs", "/tests/mocks"];
let directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations"];
if (process.env.JEST_WORKER_ID !== undefined) {
// add tests directories only when running tests
directories.push("/tests/configs", "/tests/mocks");
}
for (const directory of directories) {
app.use(directory, express.static(path.resolve(global.root_path + directory)));
}
@ -87,6 +89,8 @@ function Server (config) {
app.get("/startup", (req, res) => getStartup(req, res));
app.get("/env", (req, res) => getEnvVars(req, res));
app.get("/", (req, res) => getHtml(req, res));
server.on("listening", () => {

View file

@ -45,12 +45,12 @@ async function cors (req, res) {
url = match[1];
const headersToSend = getHeadersToSend(req.url);
const expectedRecievedHeaders = geExpectedRecievedHeaders(req.url);
const expectedReceivedHeaders = geExpectedReceivedHeaders(req.url);
Log.log(`cors url: ${url}`);
const response = await fetch(url, { headers: headersToSend });
for (const header of expectedRecievedHeaders) {
for (const header of expectedReceivedHeaders) {
const headerValue = response.headers.get(header);
if (header) res.set(header, headerValue);
}
@ -89,16 +89,16 @@ function getHeadersToSend (url) {
* @param {string} url - The url containing the expected headers from the response.
* @returns {string[]} headers - The name of the expected headers.
*/
function geExpectedRecievedHeaders (url) {
const expectedRecievedHeaders = ["Content-Type"];
const expectedRecievedHeadersMatch = new RegExp("expectedheaders=(.+?)(&|$)", "g").exec(url);
if (expectedRecievedHeadersMatch) {
const headers = expectedRecievedHeadersMatch[1].split(",");
function geExpectedReceivedHeaders (url) {
const expectedReceivedHeaders = ["Content-Type"];
const expectedReceivedHeadersMatch = new RegExp("expectedheaders=(.+?)(&|$)", "g").exec(url);
if (expectedReceivedHeadersMatch) {
const headers = expectedReceivedHeadersMatch[1].split(",");
for (const header of headers) {
expectedRecievedHeaders.push(header);
expectedReceivedHeaders.push(header);
}
}
return expectedRecievedHeaders;
return expectedReceivedHeaders;
}
/**
@ -128,4 +128,30 @@ function getVersion (req, res) {
res.send(global.version);
}
module.exports = { cors, getConfig, getHtml, getVersion, getStartup };
/**
* Gets environment variables needed in the browser.
* @returns {object} environment variables key: values
*/
function getEnvVarsAsObj () {
const obj = { modulesDir: `${config.foreignModulesDir}`, customCss: `${config.customCss}` };
if (process.env.MM_MODULES_DIR) {
obj.modulesDir = process.env.MM_MODULES_DIR.replace(`${global.root_path}/`, "");
}
if (process.env.MM_CUSTOMCSS_FILE) {
obj.customCss = process.env.MM_CUSTOMCSS_FILE.replace(`${global.root_path}/`, "");
}
return obj;
}
/**
* Gets environment variables needed in the browser.
* @param {Request} req - the request
* @param {Response} res - the result
*/
function getEnvVars (req, res) {
const obj = getEnvVarsAsObj();
res.send(obj);
}
module.exports = { cors, getConfig, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj };

View file

@ -15,14 +15,14 @@ const Translator = (function () {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// needs error handler try/catch at least
let fileinfo = null;
let fileInfo = null;
try {
fileinfo = JSON.parse(xhr.responseText);
fileInfo = JSON.parse(xhr.responseText);
} catch (exception) {
// nothing here, but don't die
Log.error(` loading json file =${file} failed`);
}
resolve(fileinfo);
resolve(fileInfo);
}
};
xhr.send(null);

View file

@ -1,7 +1,17 @@
const execSync = require("node:child_process").execSync;
const Log = require("logger");
const path = require("node:path");
const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`);
const os = require("node:os");
const fs = require("node:fs");
const si = require("systeminformation");
const modulePositions = []; // will get list from index.html
const regionRegEx = /"region ([^"]*)/i;
const indexFileName = "index.html";
const discoveredPositionsJSFilename = "js/positions.js";
module.exports = {
async logSystemInformation () {
@ -14,7 +24,7 @@ module.exports = {
versions: "kernel, node, npm, pm2"
});
let systemDataString = "System information:";
systemDataString += `\n### SYSTEM: manufacturer: ${staticData["system"]["manufacturer"]}; model: ${staticData["system"]["model"]}; raspberry: ${staticData["system"]["raspberry"]}; virtual: ${staticData["system"]["virtual"]}`;
systemDataString += `\n### SYSTEM: manufacturer: ${staticData["system"]["manufacturer"]}; model: ${staticData["system"]["model"]}; virtual: ${staticData["system"]["virtual"]}`;
systemDataString += `\n### OS: platform: ${staticData["osInfo"]["platform"]}; distro: ${staticData["osInfo"]["distro"]}; release: ${staticData["osInfo"]["release"]}; arch: ${staticData["osInfo"]["arch"]}; kernel: ${staticData["versions"]["kernel"]}`;
systemDataString += `\n### VERSIONS: electron: ${process.versions.electron}; used node: ${staticData["versions"]["node"]}; installed node: ${installedNodeVersion}; npm: ${staticData["versions"]["npm"]}; pm2: ${staticData["versions"]["pm2"]}`;
systemDataString += `\n### OTHER: timeZone: ${Intl.DateTimeFormat().resolvedOptions().timeZone}; ELECTRON_ENABLE_GPU: ${process.env.ELECTRON_ENABLE_GPU}`;
@ -29,12 +39,35 @@ module.exports = {
// return all available module positions
getAvailableModulePositions () {
return ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"];
return modulePositions;
},
// return if postion is on modulePositions Array (true/false)
// return if position is on modulePositions Array (true/false)
moduleHasValidPosition (position) {
if (this.getAvailableModulePositions().indexOf(position) === -1) return false;
return true;
},
getModulePositions () {
// if not already discovered
if (modulePositions.length === 0) {
// get the lines of the index.html
const lines = fs.readFileSync(indexFileName).toString().split(os.EOL);
// loop thru the lines
lines.forEach((line) => {
// run the regex on each line
const results = regionRegEx.exec(line);
// if the regex returned something
if (results && results.length > 0) {
// get the position parts and replace space with underscore
const positionName = results[1].replace(" ", "_");
// add it to the list
modulePositions.push(positionName);
}
});
fs.writeFileSync(discoveredPositionsJSFilename, `const modulePositions=${JSON.stringify(modulePositions)}`);
}
// return the list to the caller
return modulePositions;
}
};