fsevents-handler.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. 'use strict';
  2. var fs = require('fs');
  3. var sysPath = require('path');
  4. var readdirp = require('readdirp');
  5. var fsevents;
  6. try { fsevents = require('fsevents'); } catch (error) {
  7. if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error)
  8. }
  9. // fsevents instance helper functions
  10. // object to hold per-process fsevents instances
  11. // (may be shared across chokidar FSWatcher instances)
  12. var FSEventsWatchers = Object.create(null);
  13. // Threshold of duplicate path prefixes at which to start
  14. // consolidating going forward
  15. var consolidateThreshhold = 10;
  16. // Private function: Instantiates the fsevents interface
  17. // * path - string, path to be watched
  18. // * callback - function, called when fsevents is bound and ready
  19. // Returns new fsevents instance
  20. function createFSEventsInstance(path, callback) {
  21. return (new fsevents(path)).on('fsevent', callback).start();
  22. }
  23. // Private function: Instantiates the fsevents interface or binds listeners
  24. // to an existing one covering the same file tree
  25. // * path - string, path to be watched
  26. // * realPath - string, real path (in case of symlinks)
  27. // * listener - function, called when fsevents emits events
  28. // * rawEmitter - function, passes data to listeners of the 'raw' event
  29. // Returns close function
  30. function setFSEventsListener(path, realPath, listener, rawEmitter) {
  31. var watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
  32. var watchContainer;
  33. var parentPath = sysPath.dirname(watchPath);
  34. // If we've accumulated a substantial number of paths that
  35. // could have been consolidated by watching one directory
  36. // above the current one, create a watcher on the parent
  37. // path instead, so that we do consolidate going forward.
  38. if (couldConsolidate(parentPath)) {
  39. watchPath = parentPath;
  40. }
  41. var resolvedPath = sysPath.resolve(path);
  42. var hasSymlink = resolvedPath !== realPath;
  43. function filteredListener(fullPath, flags, info) {
  44. if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
  45. if (
  46. fullPath === resolvedPath ||
  47. !fullPath.indexOf(resolvedPath + sysPath.sep)
  48. ) listener(fullPath, flags, info);
  49. }
  50. // check if there is already a watcher on a parent path
  51. // modifies `watchPath` to the parent path when it finds a match
  52. function watchedParent() {
  53. return Object.keys(FSEventsWatchers).some(function(watchedPath) {
  54. // condition is met when indexOf returns 0
  55. if (!realPath.indexOf(sysPath.resolve(watchedPath) + sysPath.sep)) {
  56. watchPath = watchedPath;
  57. return true;
  58. }
  59. });
  60. }
  61. if (watchPath in FSEventsWatchers || watchedParent()) {
  62. watchContainer = FSEventsWatchers[watchPath];
  63. watchContainer.listeners.push(filteredListener);
  64. } else {
  65. watchContainer = FSEventsWatchers[watchPath] = {
  66. listeners: [filteredListener],
  67. rawEmitters: [rawEmitter],
  68. watcher: createFSEventsInstance(watchPath, function(fullPath, flags) {
  69. var info = fsevents.getInfo(fullPath, flags);
  70. watchContainer.listeners.forEach(function(listener) {
  71. listener(fullPath, flags, info);
  72. });
  73. watchContainer.rawEmitters.forEach(function(emitter) {
  74. emitter(info.event, fullPath, info);
  75. });
  76. })
  77. };
  78. }
  79. var listenerIndex = watchContainer.listeners.length - 1;
  80. // removes this instance's listeners and closes the underlying fsevents
  81. // instance if there are no more listeners left
  82. return function close() {
  83. delete watchContainer.listeners[listenerIndex];
  84. delete watchContainer.rawEmitters[listenerIndex];
  85. if (!Object.keys(watchContainer.listeners).length) {
  86. watchContainer.watcher.stop();
  87. delete FSEventsWatchers[watchPath];
  88. }
  89. };
  90. }
  91. // Decide whether or not we should start a new higher-level
  92. // parent watcher
  93. function couldConsolidate(path) {
  94. var keys = Object.keys(FSEventsWatchers);
  95. var count = 0;
  96. for (var i = 0, len = keys.length; i < len; ++i) {
  97. var watchPath = keys[i];
  98. if (watchPath.indexOf(path) === 0) {
  99. count++;
  100. if (count >= consolidateThreshhold) {
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. // returns boolean indicating whether fsevents can be used
  108. function canUse() {
  109. return fsevents && Object.keys(FSEventsWatchers).length < 128;
  110. }
  111. // determines subdirectory traversal levels from root to path
  112. function depth(path, root) {
  113. var i = 0;
  114. while (!path.indexOf(root) && (path = sysPath.dirname(path)) !== root) i++;
  115. return i;
  116. }
  117. // fake constructor for attaching fsevents-specific prototype methods that
  118. // will be copied to FSWatcher's prototype
  119. function FsEventsHandler() {}
  120. // Private method: Handle symlinks encountered during directory scan
  121. // * watchPath - string, file/dir path to be watched with fsevents
  122. // * realPath - string, real path (in case of symlinks)
  123. // * transform - function, path transformer
  124. // * globFilter - function, path filter in case a glob pattern was provided
  125. // Returns close function for the watcher instance
  126. FsEventsHandler.prototype._watchWithFsEvents =
  127. function(watchPath, realPath, transform, globFilter) {
  128. if (this._isIgnored(watchPath)) return;
  129. var watchCallback = function(fullPath, flags, info) {
  130. if (
  131. this.options.depth !== undefined &&
  132. depth(fullPath, realPath) > this.options.depth
  133. ) return;
  134. var path = transform(sysPath.join(
  135. watchPath, sysPath.relative(watchPath, fullPath)
  136. ));
  137. if (globFilter && !globFilter(path)) return;
  138. // ensure directories are tracked
  139. var parent = sysPath.dirname(path);
  140. var item = sysPath.basename(path);
  141. var watchedDir = this._getWatchedDir(
  142. info.type === 'directory' ? path : parent
  143. );
  144. var checkIgnored = function(stats) {
  145. if (this._isIgnored(path, stats)) {
  146. this._ignoredPaths[path] = true;
  147. if (stats && stats.isDirectory()) {
  148. this._ignoredPaths[path + '/**/*'] = true;
  149. }
  150. return true;
  151. } else {
  152. delete this._ignoredPaths[path];
  153. delete this._ignoredPaths[path + '/**/*'];
  154. }
  155. }.bind(this);
  156. var handleEvent = function(event) {
  157. if (checkIgnored()) return;
  158. if (event === 'unlink') {
  159. // suppress unlink events on never before seen files
  160. if (info.type === 'directory' || watchedDir.has(item)) {
  161. this._remove(parent, item);
  162. }
  163. } else {
  164. if (event === 'add') {
  165. // track new directories
  166. if (info.type === 'directory') this._getWatchedDir(path);
  167. if (info.type === 'symlink' && this.options.followSymlinks) {
  168. // push symlinks back to the top of the stack to get handled
  169. var curDepth = this.options.depth === undefined ?
  170. undefined : depth(fullPath, realPath) + 1;
  171. return this._addToFsEvents(path, false, true, curDepth);
  172. } else {
  173. // track new paths
  174. // (other than symlinks being followed, which will be tracked soon)
  175. this._getWatchedDir(parent).add(item);
  176. }
  177. }
  178. var eventName = info.type === 'directory' ? event + 'Dir' : event;
  179. this._emit(eventName, path);
  180. if (eventName === 'addDir') this._addToFsEvents(path, false, true);
  181. }
  182. }.bind(this);
  183. function addOrChange() {
  184. handleEvent(watchedDir.has(item) ? 'change' : 'add');
  185. }
  186. function checkFd() {
  187. fs.open(path, 'r', function(error, fd) {
  188. if (error) {
  189. error.code !== 'EACCES' ?
  190. handleEvent('unlink') : addOrChange();
  191. } else {
  192. fs.close(fd, function(err) {
  193. err && err.code !== 'EACCES' ?
  194. handleEvent('unlink') : addOrChange();
  195. });
  196. }
  197. });
  198. }
  199. // correct for wrong events emitted
  200. var wrongEventFlags = [
  201. 69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912
  202. ];
  203. if (wrongEventFlags.indexOf(flags) !== -1 || info.event === 'unknown') {
  204. if (typeof this.options.ignored === 'function') {
  205. fs.stat(path, function(error, stats) {
  206. if (checkIgnored(stats)) return;
  207. stats ? addOrChange() : handleEvent('unlink');
  208. });
  209. } else {
  210. checkFd();
  211. }
  212. } else {
  213. switch (info.event) {
  214. case 'created':
  215. case 'modified':
  216. return addOrChange();
  217. case 'deleted':
  218. case 'moved':
  219. return checkFd();
  220. }
  221. }
  222. }.bind(this);
  223. var closer = setFSEventsListener(
  224. watchPath,
  225. realPath,
  226. watchCallback,
  227. this.emit.bind(this, 'raw')
  228. );
  229. this._emitReady();
  230. return closer;
  231. };
  232. // Private method: Handle symlinks encountered during directory scan
  233. // * linkPath - string, path to symlink
  234. // * fullPath - string, absolute path to the symlink
  235. // * transform - function, pre-existing path transformer
  236. // * curDepth - int, level of subdirectories traversed to where symlink is
  237. // Returns nothing
  238. FsEventsHandler.prototype._handleFsEventsSymlink =
  239. function(linkPath, fullPath, transform, curDepth) {
  240. // don't follow the same symlink more than once
  241. if (this._symlinkPaths[fullPath]) return;
  242. else this._symlinkPaths[fullPath] = true;
  243. this._readyCount++;
  244. fs.realpath(linkPath, function(error, linkTarget) {
  245. if (this._handleError(error) || this._isIgnored(linkTarget)) {
  246. return this._emitReady();
  247. }
  248. this._readyCount++;
  249. // add the linkTarget for watching with a wrapper for transform
  250. // that causes emitted paths to incorporate the link's path
  251. this._addToFsEvents(linkTarget || linkPath, function(path) {
  252. var dotSlash = '.' + sysPath.sep;
  253. var aliasedPath = linkPath;
  254. if (linkTarget && linkTarget !== dotSlash) {
  255. aliasedPath = path.replace(linkTarget, linkPath);
  256. } else if (path !== dotSlash) {
  257. aliasedPath = sysPath.join(linkPath, path);
  258. }
  259. return transform(aliasedPath);
  260. }, false, curDepth);
  261. }.bind(this));
  262. };
  263. // Private method: Handle added path with fsevents
  264. // * path - string, file/directory path or glob pattern
  265. // * transform - function, converts working path to what the user expects
  266. // * forceAdd - boolean, ensure add is emitted
  267. // * priorDepth - int, level of subdirectories already traversed
  268. // Returns nothing
  269. FsEventsHandler.prototype._addToFsEvents =
  270. function(path, transform, forceAdd, priorDepth) {
  271. // applies transform if provided, otherwise returns same value
  272. var processPath = typeof transform === 'function' ?
  273. transform : function(val) { return val; };
  274. var emitAdd = function(newPath, stats) {
  275. var pp = processPath(newPath);
  276. var isDir = stats.isDirectory();
  277. var dirObj = this._getWatchedDir(sysPath.dirname(pp));
  278. var base = sysPath.basename(pp);
  279. // ensure empty dirs get tracked
  280. if (isDir) this._getWatchedDir(pp);
  281. if (dirObj.has(base)) return;
  282. dirObj.add(base);
  283. if (!this.options.ignoreInitial || forceAdd === true) {
  284. this._emit(isDir ? 'addDir' : 'add', pp, stats);
  285. }
  286. }.bind(this);
  287. var wh = this._getWatchHelpers(path);
  288. // evaluate what is at the path we're being asked to watch
  289. fs[wh.statMethod](wh.watchPath, function(error, stats) {
  290. if (this._handleError(error) || this._isIgnored(wh.watchPath, stats)) {
  291. this._emitReady();
  292. return this._emitReady();
  293. }
  294. if (stats.isDirectory()) {
  295. // emit addDir unless this is a glob parent
  296. if (!wh.globFilter) emitAdd(processPath(path), stats);
  297. // don't recurse further if it would exceed depth setting
  298. if (priorDepth && priorDepth > this.options.depth) return;
  299. // scan the contents of the dir
  300. readdirp({
  301. root: wh.watchPath,
  302. entryType: 'all',
  303. fileFilter: wh.filterPath,
  304. directoryFilter: wh.filterDir,
  305. lstat: true,
  306. depth: this.options.depth - (priorDepth || 0)
  307. }).on('data', function(entry) {
  308. // need to check filterPath on dirs b/c filterDir is less restrictive
  309. if (entry.stat.isDirectory() && !wh.filterPath(entry)) return;
  310. var joinedPath = sysPath.join(wh.watchPath, entry.path);
  311. var fullPath = entry.fullPath;
  312. if (wh.followSymlinks && entry.stat.isSymbolicLink()) {
  313. // preserve the current depth here since it can't be derived from
  314. // real paths past the symlink
  315. var curDepth = this.options.depth === undefined ?
  316. undefined : depth(joinedPath, sysPath.resolve(wh.watchPath)) + 1;
  317. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  318. } else {
  319. emitAdd(joinedPath, entry.stat);
  320. }
  321. }.bind(this)).on('error', function() {
  322. // Ignore readdirp errors
  323. }).on('end', this._emitReady);
  324. } else {
  325. emitAdd(wh.watchPath, stats);
  326. this._emitReady();
  327. }
  328. }.bind(this));
  329. if (this.options.persistent && forceAdd !== true) {
  330. var initWatch = function(error, realPath) {
  331. if (this.closed) return;
  332. var closer = this._watchWithFsEvents(
  333. wh.watchPath,
  334. sysPath.resolve(realPath || wh.watchPath),
  335. processPath,
  336. wh.globFilter
  337. );
  338. if (closer) this._closers[path] = closer;
  339. }.bind(this);
  340. if (typeof transform === 'function') {
  341. // realpath has already been resolved
  342. initWatch();
  343. } else {
  344. fs.realpath(wh.watchPath, initWatch);
  345. }
  346. }
  347. };
  348. module.exports = FsEventsHandler;
  349. module.exports.canUse = canUse;