[mirotalksfu] - add chokidar

هذا الالتزام موجود في:
Miroslav Pejic
2025-03-22 20:37:03 +01:00
الأصل e2e8c795be
التزام c9070c7a77
6 ملفات معدلة مع 47 إضافات و19 حذوفات

عرض الملف

@@ -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;

عرض الملف

@@ -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();
});