Skip to content

Commit 7451def

Browse files
refactor: use explicit global config
1 parent 00350ee commit 7451def

14 files changed

Lines changed: 63 additions & 59 deletions

File tree

defaultmodules/calendar/calendar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Module.register("calendar", {
105105
}
106106

107107
// Set locale.
108-
moment.updateLocale(config.language, CalendarUtils.getLocaleSpecification(config.timeFormat));
108+
moment.updateLocale(globalThis.config.language, CalendarUtils.getLocaleSpecification(globalThis.config.timeFormat));
109109

110110
// clear data holder before start
111111
this.calendarData = {};

defaultmodules/clock/clock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Module.register("clock", {
55
defaults: {
66
displayType: "digital", // options: digital, analog, both
77

8-
timeFormat: config.timeFormat,
8+
timeFormat: globalThis.config.timeFormat,
99
timezone: null,
1010

1111
displaySeconds: true,
@@ -85,7 +85,7 @@ Module.register("clock", {
8585
setTimeout(notificationTimer, delayCalculator(this.second));
8686

8787
// Set locale.
88-
moment.locale(config.language);
88+
moment.locale(globalThis.config.language);
8989
},
9090
// Override dom generator.
9191
getDom () {

defaultmodules/newsfeed/newsfeed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Module.register("newsfeed", {
3939

4040
getUrlPrefix (item) {
4141
if (item.useCorsProxy) {
42-
return `${location.protocol}//${location.host}${config.basePath}cors?url=`;
42+
return `${location.protocol}//${location.host}${globalThis.config.basePath}cors?url=`;
4343
} else {
4444
return "";
4545
}
@@ -68,7 +68,7 @@ Module.register("newsfeed", {
6868
Log.info(`Starting module: ${this.name}`);
6969

7070
// Set locale.
71-
moment.locale(config.language);
71+
moment.locale(globalThis.config.language);
7272

7373
this.newsItems = [];
7474
this.loaded = false;

defaultmodules/weather/weather.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Module.register("weather", {
66
weatherProvider: "openweathermap",
77
roundTemp: false,
88
type: "current", // current, forecast, daily (equivalent to forecast), hourly
9-
lang: config.language,
10-
units: config.units,
11-
tempUnits: config.units,
12-
windUnits: config.units,
13-
timeFormat: config.timeFormat,
9+
lang: globalThis.config.language,
10+
units: globalThis.config.units,
11+
tempUnits: globalThis.config.units,
12+
windUnits: globalThis.config.units,
13+
timeFormat: globalThis.config.timeFormat,
1414
updateInterval: 10 * 60 * 1000, // every 10 minutes
1515
animationSpeed: 1000,
1616
showFeelsLike: true,

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default defineConfig([
4141
Log: "readonly",
4242
MM: "readonly",
4343
Module: "readonly",
44-
config: "readonly",
4544
moment: "readonly"
4645
}
4746
},

js/loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const moduleObjects = [];
1212
*/
1313
function getEnvVarsFromConfig () {
1414
return {
15-
modulesDir: config.foreignModulesDir || "modules",
16-
defaultModulesDir: config.defaultModulesDir || "defaultmodules",
17-
customCss: config.customCss || "config/custom.css"
15+
modulesDir: globalThis.config.foreignModulesDir || "modules",
16+
defaultModulesDir: globalThis.config.defaultModulesDir || "defaultmodules",
17+
customCss: globalThis.config.customCss || "config/custom.css"
1818
};
1919
}
2020

@@ -30,7 +30,7 @@ async function getEnvVars () {
3030

3131
// In production, fetch env vars from server
3232
try {
33-
const res = await fetch(new URL("env", `${location.origin}${config.basePath}`));
33+
const res = await fetch(new URL("env", `${location.origin}${globalThis.config.basePath}`));
3434
return JSON.parse(await res.text());
3535
} catch (error) {
3636
// Fallback to config values if server fetch fails
@@ -79,7 +79,7 @@ async function startModules () {
7979
* @returns {object[]} module data as configured in config
8080
*/
8181
function getAllModules () {
82-
const AllModules = config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
82+
const AllModules = globalThis.config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
8383
return AllModules;
8484
}
8585

js/main.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -448,27 +448,30 @@ function updateWrapperStates () {
448448

449449
/**
450450
* Loads the core config from the server (already combined with the system defaults).
451+
* @returns {Promise<object>} The loaded config.
451452
*/
452453
async function loadConfig () {
453-
try {
454-
const res = await fetch(new URL("config/", `${location.origin}${config.basePath}`));
454+
const basePath = globalThis.config?.basePath ?? "/";
455+
const res = await fetch(new URL("config/", `${location.origin}${basePath}`));
456+
if (!res.ok) {
457+
throw new Error(`Config request failed with status ${res.status}.`);
458+
}
455459

456-
// The server tags functions as { __mmFunction: "<source>" } because
457-
// JSON.stringify can't serialise live functions. This reviver turns
458-
// those tagged objects back into callable functions.
459-
config = JSON.parse(await res.text(), (key, value) => {
460-
if (value && typeof value === "object" && typeof value.__mmFunction === "string") {
461-
try {
462-
return new Function(`return (${value.__mmFunction})`)();
463-
} catch {
464-
Log.warn(`Failed to revive function for config key "${key}".`);
465-
}
460+
// The server tags functions as { __mmFunction: "<source>" } because
461+
// JSON.stringify can't serialise live functions. This reviver turns
462+
// those tagged objects back into callable functions.
463+
const config = JSON.parse(await res.text(), (key, value) => {
464+
if (value && typeof value === "object" && typeof value.__mmFunction === "string") {
465+
try {
466+
return new Function(`return (${value.__mmFunction})`)();
467+
} catch {
468+
Log.warn(`Failed to revive function for config key "${key}".`);
466469
}
467-
return value;
468-
});
469-
} catch (error) {
470-
Log.error("Unable to retrieve config", error);
471-
}
470+
}
471+
return value;
472+
});
473+
globalThis.config = config;
474+
return config;
472475
}
473476

474477
/**
@@ -570,10 +573,8 @@ export const MM = {
570573
*/
571574
async init () {
572575
Log.info("Initializing MagicMirror².");
573-
await loadConfig();
574-
576+
const config = await loadConfig();
575577
Log.setLogLevel(config.logLevel);
576-
577578
await Translator.loadCoreTranslations(config.language);
578579
await loadModules();
579580
},
@@ -595,20 +596,20 @@ export const MM = {
595596

596597
// Setup global socket listener for RELOAD event (watch mode)
597598
const socket = io("/", {
598-
path: `${config.basePath || "/"}socket.io`
599+
path: `${globalThis.config.basePath || "/"}socket.io`
599600
});
600601

601602
socket.on("RELOAD", () => {
602603
Log.warn("Reload notification received from server");
603604
window.location.reload(true);
604605
});
605606

606-
if (config.reloadAfterServerRestart) {
607+
if (globalThis.config.reloadAfterServerRestart) {
607608
setInterval(async () => {
608609
// if server startup time has changed (which means server was restarted)
609610
// the client reloads the mm page
610611
try {
611-
const res = await fetch(`${location.protocol}//${location.host}${config.basePath}startup`);
612+
const res = await fetch(`${location.protocol}//${location.host}${globalThis.config.basePath}startup`);
612613
const curr = await res.text();
613614
if (startUp === "") startUp = curr;
614615
if (startUp !== curr) {
@@ -619,7 +620,7 @@ export const MM = {
619620
} catch (err) {
620621
Log.error(`MagicMirror not reachable: ${err}`);
621622
}
622-
}, config.checkServerInterval);
623+
}, globalThis.config.checkServerInterval);
623624
}
624625
},
625626

@@ -711,4 +712,8 @@ export const MM = {
711712
// Legacy global bridge for third-party modules that reference window.MM directly.
712713
if (!globalThis.MM) globalThis.MM = MM;
713714

714-
MM.init();
715+
try {
716+
await MM.init();
717+
} catch (error) {
718+
Log.error(error);
719+
}

js/module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class Module {
298298
*/
299299
async loadTranslations () {
300300
const translations = this.getTranslations() || {};
301-
const language = config.language.toLowerCase();
301+
const language = globalThis.config.language.toLowerCase();
302302

303303
const languages = Object.keys(translations);
304304
const fallbackLanguage = languages[0];

js/node_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class NodeHelper {
108108
io.of(this.name).on("connection", (socket) => {
109109
// register catch all.
110110
socket.onAny((notification, payload) => {
111-
if (config?.hideConfigSecrets && payload && typeof payload === "object") {
111+
if (global.config?.hideConfigSecrets && payload && typeof payload === "object") {
112112
try {
113113
// Calculate exactly which secrets this module is allowed to receive
114114
const allowedSecrets = getAllowedSecrets(this.name);

js/server_functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async function cors (req, res) {
7171
} else {
7272
url = match[1];
7373
if (typeof global.config !== "undefined") {
74-
if (config.hideConfigSecrets) {
74+
if (global.config.hideConfigSecrets) {
7575
url = replaceSecretPlaceholder(url);
7676
}
7777
}

0 commit comments

Comments
 (0)