From 8aafd4a69f324fa87268a5b4368067ecfc87b49c Mon Sep 17 00:00:00 2001 From: ArjhanToteck <38510221+ArjhanToteck@users.noreply.github.com> Date: Tue, 12 Mar 2024 10:42:42 -0600 Subject: [PATCH] Fixed polling --- lib/DirectoryWatcher.js | 50 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index 54788ea..091f198 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -111,8 +111,8 @@ class DirectoryWatcher extends EventEmitter { this.watchInParentDirectory(); } this.watcher = watchEventSource.watch(this.path); - this.watcher.on("change", this.onWatchEvent.bind(this)); this.watcher.on("error", this.onWatcherError.bind(this)); + this.watcher.on("change", this.onWatchEvent.bind(this)); } } catch (err) { this.onWatcherError(err); @@ -627,21 +627,35 @@ class DirectoryWatcher extends EventEmitter { } }); for (const itemPath of itemPaths) { + const handleStatsError = err2 => { + if ( + err2.code === "ENOENT" || + err2.code === "EPERM" || + err2.code === "EACCES" || + err2.code === "EBUSY" + ) { + this.setMissing(itemPath, initial, "scan (" + err2.code + ")"); + } else { + this.onScanError(err2); + } + itemFinished(); + return; + }; const handleStats = (err2, stats) => { if (this.closed) return; if (err2) { - if ( - err2.code === "ENOENT" || - err2.code === "EPERM" || - err2.code === "EACCES" || - err2.code === "EBUSY" - ) { - this.setMissing(itemPath, initial, "scan (" + err2.code + ")"); - } else { - this.onScanError(err2); + handleStatsError(err2); + } + let symlinkStats; + if ( + stats.isSymbolicLink() && + this.watcherManager.options.followSymlinks + ) { + try { + symlinkStats = fs.statSync(itemPath); + } catch (err3) { + handleStatsError(err3); } - itemFinished(); - return; } if (stats.isFile() || stats.isSymbolicLink()) { if (stats.mtime) { @@ -654,7 +668,11 @@ class DirectoryWatcher extends EventEmitter { true, "scan (file)" ); - } else if (stats.isDirectory()) { + } + if ( + stats.isDirectory() || + (symlinkStats && symlinkStats.isDirectory()) + ) { if (!initial || !this.directories.has(itemPath)) this.setDirectory( itemPath, @@ -665,11 +683,7 @@ class DirectoryWatcher extends EventEmitter { } itemFinished(); }; - if (this.watcherManager.options.followSymlinks) { - fs.stat(itemPath, handleStats); - } else { - fs.lstat(itemPath, handleStats); - } + fs.lstat(itemPath, handleStats); } itemFinished(); });