Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ext/node/polyfills/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ import { core, primordials } from "ext:core/mod.js";

const {
ArrayBufferIsView,
ArrayIsArray,
BigInt,
DatePrototypeGetTime,
DateUTC,
Expand All @@ -175,6 +176,7 @@ const {
Promise,
PromisePrototypeThen,
PromiseResolve,
RegExpPrototype,
SafeMap,
StringPrototypeToString,
SymbolAsyncIterator,
Expand Down Expand Up @@ -3133,10 +3135,47 @@ type watchOptions = {
persistent?: boolean;
recursive?: boolean;
encoding?: string;
// deno-lint-ignore no-explicit-any
ignore?: string | RegExp | ((path: string) => boolean) | any[];
};

type watchListener = (eventType: string, filename: string) => void;

// Mirrors Node's `validateIgnoreOption` /
// `validateIgnoreOptionElement` in lib/internal/validators.js.
// deno-lint-ignore no-explicit-any
function validateIgnoreOptionElement(value: any, name: string) {
if (typeof value === "string") {
if (value.length === 0) {
throw new ERR_INVALID_ARG_VALUE(
name,
value,
"must be a non-empty string",
);
}
return;
}
if (ObjectPrototypeIsPrototypeOf(RegExpPrototype, value)) return;
if (typeof value === "function") return;
throw new ERR_INVALID_ARG_TYPE(
name,
["string", "RegExp", "Function"],
value,
);
}

// deno-lint-ignore no-explicit-any
function validateIgnoreOption(value: any, name: string) {
if (value == null) return;
if (ArrayIsArray(value)) {
for (let i = 0; i < value.length; i++) {
validateIgnoreOptionElement(value[i], `${name}[${i}]`);
}
return;
}
validateIgnoreOptionElement(value, name);
}

function watch(
filename: string | URL,
options: watchOptions,
Expand Down Expand Up @@ -3167,6 +3206,8 @@ function watch(
? optionsOrListener2
: undefined;

validateIgnoreOption(options?.ignore, "options.ignore");

// deno-lint-ignore prefer-primordials
const watchPath = getValidatedPath(filename).toString();

Expand Down
1 change: 1 addition & 0 deletions tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@
"parallel/test-fs-utimes-y2K38.js": {},
"parallel/test-fs-utimes.js": {},
"parallel/test-fs-watch-file-enoent-after-deletion.js": {},
"parallel/test-fs-watch-ignore-invalid.js": {},
"parallel/test-fs-watch-recursive-add-file-to-existing-subfolder.js": {},
"parallel/test-fs-watch-recursive-add-folder.js": {},
"parallel/test-fs-watch-recursive-delete.js": {},
Expand Down
Loading