Skip to content

Commit

Permalink
fs: migrate fs_event_wrap to internal/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maclover7 committed Feb 4, 2018
1 parent 47a984a commit 067a544
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
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;

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

0 comments on commit 067a544

Please sign in to comment.