Files
OmniRoute/src/lib/plugins/watcher.ts
Diego Rodrigues de Sa e Souza 20c31493af feat(plugins): plugins framework + per-API-key disable-non-public-models (#3041)
Integrates two community contributions into release/v3.8.8 with security hardening and conflict resolution.

- **Plugins framework** (#2913 — thanks @oyi77): hooks + registry unification, plugin SDK (`definePlugin`), worker-thread sandbox, per-plugin hook rate limiting, SHA-256 integrity verification, semver-gated upgrade, and execution analytics. Plugin routes are loopback-only (`isLocalOnlyPath`); `child_process` exec is opt-in via `OMNIROUTE_PLUGINS_ALLOW_EXEC` (default off).
- **API key option: disable non-published models** (#3017 — thanks @androw): a per-key flag restricting the key to discovered public models (combos / `auto/*` / `qtSd/*` routing still allowed).

Hardening applied during integration: migration renumber (089/090/091), `/api/plugins` LOCAL_ONLY route-guard classification (closes the plugin-RCE vector), atomic install/upgrade with path containment, `O_EXCL` tmp-file creation (TOCTOU), rate-limit-map eviction, `validatePluginConfig` on configure, `buildErrorBody` on all plugin error paths. 246/246 tests; typecheck / cycles / docs-sync clean.

Co-authored-by: oyi77 <14921983+oyi77@users.noreply.github.com>
Co-authored-by: Nicolas Lorin <androw95220@gmail.com>
2026-06-01 15:43:55 -03:00

91 lines
2.6 KiB
TypeScript

/**
* Plugin directory watcher — monitors plugin dirs for changes and auto-reloads.
*
* Uses fs.watch with 500ms debounce to avoid rapid reloads.
*
* @module plugins/watcher
*/
import { watch, type FSWatcher } from "fs";
import { logger } from "../../../open-sse/utils/logger.ts";
const log = logger("PLUGIN_WATCHER");
const DEBOUNCE_MS = 500;
interface WatcherEntry {
watcher: FSWatcher;
pluginName: string;
debounceTimer: ReturnType<typeof setTimeout> | null;
}
const watchers = new Map<string, WatcherEntry>();
type ReloadFn = (name: string) => Promise<void>;
/**
* Start watching a plugin directory for changes.
* Calls reload(pluginName) when files change (debounced).
*/
export function startWatching(pluginDir: string, pluginName: string, reload: ReloadFn): void {
if (watchers.has(pluginDir)) return;
const entry: WatcherEntry = { watcher: null as unknown as FSWatcher, pluginName, debounceTimer: null };
try {
entry.watcher = watch(pluginDir, { recursive: false }, (eventType, filename) => {
if (!filename) return;
if (filename === "node_modules" || filename.startsWith(".")) return;
log.info("watcher.change", { pluginName, file: filename, event: eventType });
if (entry.debounceTimer) clearTimeout(entry.debounceTimer);
entry.debounceTimer = setTimeout(async () => {
entry.debounceTimer = null;
try {
await reload(pluginName);
log.info("watcher.reloaded", { pluginName });
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
log.error("watcher.reload_failed", { pluginName, error: msg });
}
}, DEBOUNCE_MS);
});
watchers.set(pluginDir, entry);
log.info("watcher.started", { pluginName, dir: pluginDir });
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
log.error("watcher.start_failed", { pluginName, error: msg });
}
}
/**
* Stop watching a plugin directory.
*/
export function stopWatching(pluginDir: string): void {
const entry = watchers.get(pluginDir);
if (!entry) return;
if (entry.debounceTimer) clearTimeout(entry.debounceTimer);
try { entry.watcher.close(); } catch {}
watchers.delete(pluginDir);
log.info("watcher.stopped", { pluginName: entry.pluginName, dir: pluginDir });
}
/**
* Stop all watchers.
*/
export function stopAllWatchers(): void {
for (const dir of watchers.keys()) {
stopWatching(dir);
}
}
/**
* Get count of active watchers (for diagnostics).
*/
export function getWatcherCount(): number {
return watchers.size;
}