From 067a5448c3d70a68bcfb2e5ff317c2f1d4d7df35 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sat, 20 Jan 2018 13:34:58 -0500 Subject: [PATCH] fs: migrate fs_event_wrap to internal/errors --- lib/fs.js | 12 ++++++++++++ src/fs_event_wrap.cc | 7 ++----- test/sequential/test-fs-watch.js | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 0b9cf0cc9b8190..f39f86195539a5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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; @@ -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, @@ -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(); }; diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 85a09060a11edc..5a61e1d3e7a23b 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -114,13 +114,10 @@ void FSEventWrap::Start(const FunctionCallbackInfo& 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()) diff --git a/test/sequential/test-fs-watch.js b/test/sequential/test-fs-watch.js index 31708ee6144c7d..25890cd5ce0853 100644 --- a/test/sequential/test-fs-watch.js +++ b/test/sequential/test-fs-watch.js @@ -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 {