Skip to content

Commit aa85e01

Browse files
Refactors excluded path handling and validation
Improves excluded path validation by normalizing paths before comparison, ensuring accurate subdirectory checks. Updates debounceMS validation to include an upper bound, preventing excessively long debounce intervals. Provides a more robust and consistent way of retrieving excluded paths, handling cases where the underlying implementation might not support it. Fixes a bug where file modification detection was not working correctly due to incorrect time comparison.
1 parent 6d28968 commit aa85e01

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ declare module 'nsfw' {
2121
start: () => Promise<void>;
2222
/** Returns a Promise that resolves when NSFW object has halted. */
2323
stop: () => Promise<void>;
24-
/** Returns a Promise that resolves when NSFW object has updated the excluded ppaths. */
25-
updateExcludedPaths: (excludedPaths: [string]) => Promise<void>;
24+
/** Returns a Promise that resolves when NSFW object has updated the excluded paths. */
25+
updateExcludedPaths: (excludedPaths: string[]) => Promise<void>;
26+
/** Returns a Promise that resolves with the current excluded paths. */
27+
getExcludedPaths: () => Promise<string[]>;
2628
}
2729

2830
export const enum ActionType {
@@ -65,7 +67,7 @@ declare module 'nsfw' {
6567
/** callback to fire in the case of errors */
6668
errorCallback?: (err: any) => void;
6769
/** paths to be excluded */
68-
excludedPaths?: [string];
70+
excludedPaths?: string[];
6971
}
7072
}
7173

js/src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function NSFWFilePoller(watchPath, eventCallback, debounceMS) {
1818
fileStatus = status;
1919
eventCallback([{ action: CREATED, directory, file }]);
2020
} else if (
21-
status.mtime - fileStatus.mtime !== 0 ||
22-
status.ctime - fileStatus.ctime !== 0
21+
status.mtime.getTime() !== fileStatus.mtime.getTime() ||
22+
status.ctime.getTime() !== fileStatus.ctime.getTime()
2323
) {
2424
fileStatus = status;
2525
eventCallback([{ action: MODIFIED, directory, file }]);
@@ -54,8 +54,8 @@ function NSFWFilePoller(watchPath, eventCallback, debounceMS) {
5454
const buildNSFW = async (watchPath, eventCallback,
5555
{ debounceMS = 500, errorCallback: _errorCallback, excludedPaths = [] } = {}) => {
5656
if (Number.isInteger(debounceMS)) {
57-
if (debounceMS < 1) {
58-
throw new Error('Minimum debounce is 1ms.');
57+
if (debounceMS < 1 || debounceMS > 60000) {
58+
throw new Error('debounceMS must be >= 1 and <= 60000.');
5959
}
6060
} else {
6161
throw new Error('debounceMS must be an integer.');
@@ -75,15 +75,16 @@ const buildNSFW = async (watchPath, eventCallback,
7575
}
7676

7777
if (excludedPaths) {
78+
const normalizedWatchPath = watchPath.replace(/[\\/]+$/, '');
7879
for (let i = 0; i < excludedPaths.length; i++) {
79-
const excludedPath = excludedPaths[i];
80+
const normalizedExcludedPath = excludedPaths[i].replace(/[\\/]+$/, '');
8081
if (process.platform === 'win32') {
81-
if (!excludedPath.substring(0, watchPath.length - 1).
82-
localeCompare(watchPath, undefined, { sensitivity: 'accent' })) {
82+
if (normalizedExcludedPath.substring(0, normalizedWatchPath.length).
83+
localeCompare(normalizedWatchPath, undefined, { sensitivity: 'accent' }) !== 0) {
8384
throw new Error('Excluded path must be a valid subdirectory of the watching path.');
8485
}
8586
} else {
86-
if (!excludedPath.startsWith(watchPath)) {
87+
if (!normalizedExcludedPath.startsWith(normalizedWatchPath)) {
8788
throw new Error('Excluded path must be a valid subdirectory of the watching path.');
8889
}
8990
}
@@ -110,8 +111,8 @@ function nsfw(watchPath, eventCallback, options) {
110111
this.stop = () => implementation.stop();
111112
this.pause = () => implementation.pause();
112113
this.resume = () => implementation.resume();
113-
this.getExcludedPaths = () => implementation.getExcludedPaths();
114-
this.updateExcludedPaths = (paths) => implementation.updateExcludedPaths(paths);
114+
this.getExcludedPaths = () => implementation.getExcludedPaths ? implementation.getExcludedPaths() : Promise.resolve([]);
115+
this.updateExcludedPaths = (paths) => implementation.updateExcludedPaths ? implementation.updateExcludedPaths(paths) : Promise.resolve();
115116
}
116117

117118

0 commit comments

Comments
 (0)