fsevents-handler.js 13KB

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