Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix handling of non-object options parameter in fs/watchFile #54157

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2512,9 +2512,15 @@ function watchFile(filename, options, listener) {
filename = pathModule.resolve(filename);
let stat;

if (options === null || typeof options !== 'object') {
if (typeof options === 'function') {
listener = options;
options = null;
options = kEmptyObject;
}

if (listener === undefined) {
listener = options;
} else {
validateObject(options, 'options');
}

options = {
Expand All @@ -2532,8 +2538,11 @@ function watchFile(filename, options, listener) {
const watchers = require('internal/fs/watchers');
if (stat === undefined) {
stat = new watchers.StatWatcher(options.bigint);
stat[watchers.kFSStatWatcherStart](filename,
options.persistent, options.interval);
stat[watchers.kFSStatWatcherStart](
filename,
options.persistent,
options.interval,
);
statWatchers.set(filename, stat);
} else {
stat[watchers.kFSStatWatcherAddOrCleanRef]('add');
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ assert.throws(
name: 'TypeError'
});

assert.throws(
() => {
fs.watchFile('./another-file', 'bad listener');
},
{
code: 'ERR_INVALID_ARG_TYPE',
message: `The "listener" argument must be of type function. Received type string ('bad listener')`,
name: 'TypeError'
});

assert.throws(
() => {
fs.watchFile('./another-file', 'bad options', () => {});
},
{
code: 'ERR_INVALID_ARG_TYPE',
message: `The "options" argument must be of type object. Received type string ('bad options')`,
name: 'TypeError'
});

assert.throws(() => {
fs.watchFile(new Object(), common.mustNotCall());
}, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
Expand Down
Loading