Files
MagicMirror/js/socketclient.js
Kristjan ESPERANTO 13d51cfce9 CodeQL cleanup for alerts #18, #19, #20 (#4153)
Small cleanup PR to close three CodeQL alerts, without changing app
behavior.

-
[#18](https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning/18)
in `js/loader.js`
-
[#19](https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning/19)
in `js/socketclient.js`
-
[#20](https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning/20)
in `tests/electron/modules/calendar_spec.js`

For `Loader` and `MMSocket`, make the global export explicit via
`globalThis` (instead of suppressing `no-unused-vars`).
For calendar tests, remove the unused debug helper `logAllText`.
Also includes a tiny e2e cleanup: `MM_PORT` is cleared in `afterAll`.

Outcome: Three open CodeQL alerts are addressed with small, low-risk
changes. The global intent for `Loader` and `MMSocket` is explicit, dead
test helper code is removed, and e2e test state is cleaned up more
reliably between runs.

With this, all current [code scanning
issues](https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning)
should be resolved in the develop branch 🎈
2026-05-12 23:32:16 +02:00

49 lines
1.2 KiB
JavaScript

/* global io */
const MMSocket = function (moduleName) {
if (typeof moduleName !== "string") {
throw new Error("Please set the module name for the MMSocket.");
}
this.moduleName = moduleName;
// Private Methods
let base = "/";
if (typeof config !== "undefined" && typeof config.basePath !== "undefined") {
base = config.basePath;
}
this.socket = io(`/${this.moduleName}`, {
path: `${base}socket.io`,
pingInterval: 120000, // send pings every 2 mins
pingTimeout: 120000 // wait up to 2 mins for a pong
});
let notificationCallback = function () {};
const onevent = this.socket.onevent;
this.socket.onevent = (packet) => {
const args = packet.data || [];
onevent.call(this.socket, packet); // original call
packet.data = ["*"].concat(args);
onevent.call(this.socket, packet); // additional call to catch-all
};
// register catch all.
this.socket.on("*", (notification, payload) => {
if (notification !== "*") {
notificationCallback(notification, payload);
}
});
// Public Methods
this.setNotificationCallback = (callback) => {
notificationCallback = callback;
};
this.sendNotification = (notification, payload = {}) => {
this.socket.emit(notification, payload);
};
};
globalThis.MMSocket = MMSocket;