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

fs: migrate fs_event_wrap to internal/errors #17851

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,8 @@ fs.appendFileSync = function(path, data, options) {
function FSWatcher() {
EventEmitter.call(this);

this._initialized = false;
Copy link
Member

@joyeecheung joyeecheung Feb 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here mentioning that this should be kept in sync with the initialized field of the C++ wrap? (Arguably better if this is an accessor property on the prototype of FSWatcher, that way there is no need to set this field in start and close, although that brings a few more calls into C++ so I am fine with this as well)


var self = this;
this._handle = new FSEvent();
this._handle.owner = this;
Expand All @@ -1689,6 +1691,13 @@ FSWatcher.prototype.start = function(filename,
encoding) {
handleError((filename = getPathFromURL(filename)));
nullCheck(filename);

if (this._initialized) {
return;
}

validatePath(filename, 'filename');

var err = this._handle.start(pathModule.toNamespacedPath(filename),
persistent,
recursive,
Expand All @@ -1698,10 +1707,13 @@ FSWatcher.prototype.start = function(filename,
const error = errnoException(err, `watch ${filename}`);
error.filename = filename;
throw error;
} else {
this._initialized = true;
}
};

FSWatcher.prototype.close = function() {
this._initialized = false;
this._handle.close();
};

Expand Down
7 changes: 2 additions & 5 deletions src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,10 @@ void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
if (wrap->initialized_)
return args.GetReturnValue().Set(0);

static const char kErrMsg[] = "filename must be a string or Buffer";
if (args.Length() < 1)
return env->ThrowTypeError(kErrMsg);
CHECK_GE(args.Length(), 4);

BufferValue path(env->isolate(), args[0]);
if (*path == nullptr)
return env->ThrowTypeError(kErrMsg);
CHECK_NE(*path, nullptr);

unsigned int flags = 0;
if (args[2]->IsTrue())
Expand Down
16 changes: 16 additions & 0 deletions test/sequential/test-fs-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ tmpdir.refresh();

}

{
const msg = 'The "filename" argument must be one of type' +
' string, Buffer, or URL';

[false, 1, [], {}, null, undefined].forEach((i) => {
common.expectsError(
() => fs.watch(i, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: msg
}
);
});
}

// https://github.com/joyent/node/issues/2293 - non-persistent watcher should
// not block the event loop
{
Expand Down