Skip to content

fs: improve errors thrown from fs.watch()

  • Add an accessor property initialized to FSEventWrap to check the state of the handle from the JS land
  • Introduce ERR_FS_WATCHER_ALREADY_STARTED so calling start() on a watcher that is already started will throw instead of doing nothing silently.
  • Introduce ERR_FS_WATCHER_NOT_STARTED so calling close() on a watcher that is already closed will throw instead of doing nothing silently.
  • Validate the filename passed to fs.watch()
  • Assert that the handle in the watcher are instances of FSEvent instead of relying on the illegal invocation error from the VM.
  • Add more assertions in FSEventWrap methods now that we check initialized and the filename in JS land before invoking the binding.
  • Use uvException instead of errornoException to create the errors with the error numbers from libuv to make them consistent with other errors in fs.

TODO:

  • Improve fs.watchFile() the same way this patch improves fs.watch()
  • It seems possible to fire both rename and change event from libuv together now that we can check if the handle is closed via initialized in JS land.

This is a retake of https://github.com/nodejs/node/pull/17851

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

fs

Merge request reports

Loading