From c9070c7a7792805b5b7250359112304add547e2c Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Sat, 22 Mar 2025 20:37:03 +0100 Subject: [PATCH] [mirotalksfu] - add chokidar --- app/src/HtmlInjector.js | 40 ++++++++++++++++++++++++++++------------ app/src/Server.js | 15 +++++++++++++-- package.json | 3 ++- public/js/Brand.js | 2 +- public/js/Room.js | 4 ++-- public/js/RoomClient.js | 2 +- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/app/src/HtmlInjector.js b/app/src/HtmlInjector.js index 1fab646f..4982092c 100644 --- a/app/src/HtmlInjector.js +++ b/app/src/HtmlInjector.js @@ -1,5 +1,7 @@ const fs = require('fs'); +const chokidar = require('chokidar'); + const Logger = require('./Logger'); const log = new Logger('HtmlInjector'); @@ -10,6 +12,7 @@ class HtmlInjector { this.cache = {}; // Object to store cached files this.config = config; // Configuration containing metadata (OG, title, etc.) this.injectData = this.getInjectData(); // Initialize dynamic injection data + this.watcher = null; // File watcher instance this.preloadPages(filesPath); // Preload pages at startup this.watchFiles(filesPath); // Watch files for changes log.info('filesPath cached', this.filesPath); @@ -45,20 +48,26 @@ class HtmlInjector { filePaths.forEach((filePath) => this.loadFileToCache(filePath)); } - // Function to watch a file for changes and reload the cache - watchFileForChanges(filePath) { - fs.watch(filePath, (eventType) => { - if (eventType === 'change') { + // Function to watch files for changes using chokidar + watchFiles(filePaths) { + if (this.watcher) { + this.watcher.close(); // Close existing watcher if any + } + + this.watcher = chokidar.watch(filePaths, { + persistent: true, + ignoreInitial: true, // Ignore initial 'add' events + }); + + this.watcher + .on('change', (filePath) => { log.debug(`File changed: ${filePath}`); this.loadFileToCache(filePath); - log.debug(`Reload the file ${filePath} into cache`); - } - }); - } - - // Function to watch all files for changes - watchFiles(filePaths) { - filePaths.forEach((filePath) => this.watchFileForChanges(filePath)); + log.debug(`Reloaded file ${filePath} into cache`); + }) + .on('error', (error) => { + log.error(`Watcher error: ${error.message}`); + }); } // Function to inject dynamic data (e.g., OG, TITLE, etc.) into a given file @@ -90,6 +99,13 @@ class HtmlInjector { } } } + + // Cleanup watcher when the instance is no longer needed + cleanup() { + if (this.watcher) { + this.watcher.close(); + } + } } module.exports = HtmlInjector; diff --git a/app/src/Server.js b/app/src/Server.js index c57f6350..cf1424c8 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -11,6 +11,7 @@ prod dependencies: { @mattermost/client : https://www.npmjs.com/package/@mattermost/client @sentry/node : https://www.npmjs.com/package/@sentry/node axios : https://www.npmjs.com/package/axios + chokidar : https://www.npmjs.com/package/chokidar colors : https://www.npmjs.com/package/colors compression : https://www.npmjs.com/package/compression cors : https://www.npmjs.com/package/cors @@ -58,7 +59,7 @@ dev dependencies: { * @license For commercial or closed source, contact us at license.mirotalk@gmail.com or purchase directly via CodeCanyon * @license CodeCanyon: https://codecanyon.net/item/mirotalk-sfu-webrtc-realtime-video-conferences/40769970 * @author Miroslav Pejic - miroslav.pejic.85@gmail.com - * @version 1.7.87 + * @version 1.7.88 * */ @@ -1315,7 +1316,7 @@ function startServer() { const tunnel = list.tunnels[0].public_url; log.info('Server config', getServerConfig(tunnel)); } catch (err) { - log.error('Ngrok Start error: ', err.body); + log.error('Ngrok Start error: ', err); await ngrok.kill(); process.exit(1); } @@ -3555,3 +3556,13 @@ function startServer() { } } } + +process.on('SIGINT', () => { + htmlInjector.cleanup(); + process.exit(); +}); + +process.on('SIGTERM', () => { + htmlInjector.cleanup(); + process.exit(); +}); diff --git a/package.json b/package.json index 5d3d9474..e6d0df06 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mirotalksfu", - "version": "1.7.87", + "version": "1.7.88", "description": "WebRTC SFU browser-based video calls", "main": "Server.js", "scripts": { @@ -60,6 +60,7 @@ "@mattermost/client": "10.6.0", "@sentry/node": "^9.8.0", "axios": "^1.8.4", + "chokidar": "^4.0.3", "colors": "1.4.0", "compression": "1.8.0", "cors": "2.8.5", diff --git a/public/js/Brand.js b/public/js/Brand.js index 07f56e38..dd28a383 100644 --- a/public/js/Brand.js +++ b/public/js/Brand.js @@ -64,7 +64,7 @@ let BRAND = { }, about: { imageUrl: '../images/mirotalk-logo.gif', - title: 'WebRTC SFU v1.7.87', + title: 'WebRTC SFU v1.7.88', html: `