fsevents-handler.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. 'use strict';
  2. const fs = require('fs');
  3. const sysPath = require('path');
  4. const { promisify } = require('util');
  5. let fsevents;
  6. try {
  7. fsevents = require('fsevents');
  8. } catch (error) {
  9. if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error);
  10. }
  11. if (fsevents) {
  12. // TODO: real check
  13. const mtch = process.version.match(/v(\d+)\.(\d+)/);
  14. if (mtch && mtch[1] && mtch[2]) {
  15. const maj = Number.parseInt(mtch[1], 10);
  16. const min = Number.parseInt(mtch[2], 10);
  17. if (maj === 8 && min < 16) {
  18. fsevents = undefined;
  19. }
  20. }
  21. }
  22. const {
  23. EV_ADD,
  24. EV_CHANGE,
  25. EV_ADD_DIR,
  26. EV_UNLINK,
  27. EV_ERROR,
  28. STR_DATA,
  29. STR_END,
  30. FSEVENT_CREATED,
  31. FSEVENT_MODIFIED,
  32. FSEVENT_DELETED,
  33. FSEVENT_MOVED,
  34. // FSEVENT_CLONED,
  35. FSEVENT_UNKNOWN,
  36. FSEVENT_TYPE_FILE,
  37. FSEVENT_TYPE_DIRECTORY,
  38. FSEVENT_TYPE_SYMLINK,
  39. ROOT_GLOBSTAR,
  40. DIR_SUFFIX,
  41. DOT_SLASH,
  42. FUNCTION_TYPE,
  43. EMPTY_FN,
  44. IDENTITY_FN
  45. } = require('./constants');
  46. const Depth = (value) => isNaN(value) ? {} : {depth: value};
  47. const stat = promisify(fs.stat);
  48. const lstat = promisify(fs.lstat);
  49. const realpath = promisify(fs.realpath);
  50. const statMethods = { stat, lstat };
  51. /**
  52. * @typedef {String} Path
  53. */
  54. /**
  55. * @typedef {Object} FsEventsWatchContainer
  56. * @property {Set<Function>} listeners
  57. * @property {Function} rawEmitter
  58. * @property {{stop: Function}} watcher
  59. */
  60. // fsevents instance helper functions
  61. /**
  62. * Object to hold per-process fsevents instances (may be shared across chokidar FSWatcher instances)
  63. * @type {Map<Path,FsEventsWatchContainer>}
  64. */
  65. const FSEventsWatchers = new Map();
  66. // Threshold of duplicate path prefixes at which to start
  67. // consolidating going forward
  68. const consolidateThreshhold = 10;
  69. const wrongEventFlags = new Set([
  70. 69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912
  71. ]);
  72. /**
  73. * Instantiates the fsevents interface
  74. * @param {Path} path path to be watched
  75. * @param {Function} callback called when fsevents is bound and ready
  76. * @returns {{stop: Function}} new fsevents instance
  77. */
  78. const createFSEventsInstance = (path, callback) => {
  79. const stop = fsevents.watch(path, callback);
  80. return {stop};
  81. };
  82. /**
  83. * Instantiates the fsevents interface or binds listeners to an existing one covering
  84. * the same file tree.
  85. * @param {Path} path - to be watched
  86. * @param {Path} realPath - real path for symlinks
  87. * @param {Function} listener - called when fsevents emits events
  88. * @param {Function} rawEmitter - passes data to listeners of the 'raw' event
  89. * @returns {Function} closer
  90. */
  91. function setFSEventsListener(path, realPath, listener, rawEmitter) {
  92. let watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
  93. const parentPath = sysPath.dirname(watchPath);
  94. let cont = FSEventsWatchers.get(watchPath);
  95. // If we've accumulated a substantial number of paths that
  96. // could have been consolidated by watching one directory
  97. // above the current one, create a watcher on the parent
  98. // path instead, so that we do consolidate going forward.
  99. if (couldConsolidate(parentPath)) {
  100. watchPath = parentPath;
  101. }
  102. const resolvedPath = sysPath.resolve(path);
  103. const hasSymlink = resolvedPath !== realPath;
  104. const filteredListener = (fullPath, flags, info) => {
  105. if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
  106. if (
  107. fullPath === resolvedPath ||
  108. !fullPath.indexOf(resolvedPath + sysPath.sep)
  109. ) listener(fullPath, flags, info);
  110. };
  111. // check if there is already a watcher on a parent path
  112. // modifies `watchPath` to the parent path when it finds a match
  113. let watchedParent = false;
  114. for (const watchedPath of FSEventsWatchers.keys()) {
  115. if (realPath.indexOf(sysPath.resolve(watchedPath) + sysPath.sep) === 0) {
  116. watchPath = watchedPath;
  117. cont = FSEventsWatchers.get(watchPath);
  118. watchedParent = true;
  119. break;
  120. }
  121. }
  122. if (cont || watchedParent) {
  123. cont.listeners.add(filteredListener);
  124. } else {
  125. cont = {
  126. listeners: new Set([filteredListener]),
  127. rawEmitter,
  128. watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
  129. if (!cont.listeners.size) return;
  130. const info = fsevents.getInfo(fullPath, flags);
  131. cont.listeners.forEach(list => {
  132. list(fullPath, flags, info);
  133. });
  134. cont.rawEmitter(info.event, fullPath, info);
  135. })
  136. };
  137. FSEventsWatchers.set(watchPath, cont);
  138. }
  139. // removes this instance's listeners and closes the underlying fsevents
  140. // instance if there are no more listeners left
  141. return () => {
  142. const lst = cont.listeners;
  143. lst.delete(filteredListener);
  144. if (!lst.size) {
  145. FSEventsWatchers.delete(watchPath);
  146. if (cont.watcher) return cont.watcher.stop().then(() => {
  147. cont.rawEmitter = cont.watcher = undefined;
  148. Object.freeze(cont);
  149. });
  150. }
  151. };
  152. }
  153. // Decide whether or not we should start a new higher-level
  154. // parent watcher
  155. const couldConsolidate = (path) => {
  156. let count = 0;
  157. for (const watchPath of FSEventsWatchers.keys()) {
  158. if (watchPath.indexOf(path) === 0) {
  159. count++;
  160. if (count >= consolidateThreshhold) {
  161. return true;
  162. }
  163. }
  164. }
  165. return false;
  166. };
  167. // returns boolean indicating whether fsevents can be used
  168. const canUse = () => fsevents && FSEventsWatchers.size < 128;
  169. // determines subdirectory traversal levels from root to path
  170. const calcDepth = (path, root) => {
  171. let i = 0;
  172. while (!path.indexOf(root) && (path = sysPath.dirname(path)) !== root) i++;
  173. return i;
  174. };
  175. // returns boolean indicating whether the fsevents' event info has the same type
  176. // as the one returned by fs.stat
  177. const sameTypes = (info, stats) => (
  178. info.type === FSEVENT_TYPE_DIRECTORY && stats.isDirectory() ||
  179. info.type === FSEVENT_TYPE_SYMLINK && stats.isSymbolicLink() ||
  180. info.type === FSEVENT_TYPE_FILE && stats.isFile()
  181. )
  182. /**
  183. * @mixin
  184. */
  185. class FsEventsHandler {
  186. /**
  187. * @param {import('../index').FSWatcher} fsw
  188. */
  189. constructor(fsw) {
  190. this.fsw = fsw;
  191. }
  192. checkIgnored(path, stats) {
  193. const ipaths = this.fsw._ignoredPaths;
  194. if (this.fsw._isIgnored(path, stats)) {
  195. ipaths.add(path);
  196. if (stats && stats.isDirectory()) {
  197. ipaths.add(path + ROOT_GLOBSTAR);
  198. }
  199. return true;
  200. }
  201. ipaths.delete(path);
  202. ipaths.delete(path + ROOT_GLOBSTAR);
  203. }
  204. addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  205. const event = watchedDir.has(item) ? EV_CHANGE : EV_ADD;
  206. this.handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  207. }
  208. async checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  209. try {
  210. const stats = await stat(path)
  211. if (this.fsw.closed) return;
  212. if (sameTypes(info, stats)) {
  213. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  214. } else {
  215. this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  216. }
  217. } catch (error) {
  218. if (error.code === 'EACCES') {
  219. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  220. } else {
  221. this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  222. }
  223. }
  224. }
  225. handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  226. if (this.fsw.closed || this.checkIgnored(path)) return;
  227. if (event === EV_UNLINK) {
  228. const isDirectory = info.type === FSEVENT_TYPE_DIRECTORY
  229. // suppress unlink events on never before seen files
  230. if (isDirectory || watchedDir.has(item)) {
  231. this.fsw._remove(parent, item, isDirectory);
  232. }
  233. } else {
  234. if (event === EV_ADD) {
  235. // track new directories
  236. if (info.type === FSEVENT_TYPE_DIRECTORY) this.fsw._getWatchedDir(path);
  237. if (info.type === FSEVENT_TYPE_SYMLINK && opts.followSymlinks) {
  238. // push symlinks back to the top of the stack to get handled
  239. const curDepth = opts.depth === undefined ?
  240. undefined : calcDepth(fullPath, realPath) + 1;
  241. return this._addToFsEvents(path, false, true, curDepth);
  242. }
  243. // track new paths
  244. // (other than symlinks being followed, which will be tracked soon)
  245. this.fsw._getWatchedDir(parent).add(item);
  246. }
  247. /**
  248. * @type {'add'|'addDir'|'unlink'|'unlinkDir'}
  249. */
  250. const eventName = info.type === FSEVENT_TYPE_DIRECTORY ? event + DIR_SUFFIX : event;
  251. this.fsw._emit(eventName, path);
  252. if (eventName === EV_ADD_DIR) this._addToFsEvents(path, false, true);
  253. }
  254. }
  255. /**
  256. * Handle symlinks encountered during directory scan
  257. * @param {String} watchPath - file/dir path to be watched with fsevents
  258. * @param {String} realPath - real path (in case of symlinks)
  259. * @param {Function} transform - path transformer
  260. * @param {Function} globFilter - path filter in case a glob pattern was provided
  261. * @returns {Function} closer for the watcher instance
  262. */
  263. _watchWithFsEvents(watchPath, realPath, transform, globFilter) {
  264. if (this.fsw.closed) return;
  265. if (this.fsw._isIgnored(watchPath)) return;
  266. const opts = this.fsw.options;
  267. const watchCallback = async (fullPath, flags, info) => {
  268. if (this.fsw.closed) return;
  269. if (
  270. opts.depth !== undefined &&
  271. calcDepth(fullPath, realPath) > opts.depth
  272. ) return;
  273. const path = transform(sysPath.join(
  274. watchPath, sysPath.relative(watchPath, fullPath)
  275. ));
  276. if (globFilter && !globFilter(path)) return;
  277. // ensure directories are tracked
  278. const parent = sysPath.dirname(path);
  279. const item = sysPath.basename(path);
  280. const watchedDir = this.fsw._getWatchedDir(
  281. info.type === FSEVENT_TYPE_DIRECTORY ? path : parent
  282. );
  283. // correct for wrong events emitted
  284. if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) {
  285. if (typeof opts.ignored === FUNCTION_TYPE) {
  286. let stats;
  287. try {
  288. stats = await stat(path);
  289. } catch (error) {}
  290. if (this.fsw.closed) return;
  291. if (this.checkIgnored(path, stats)) return;
  292. if (sameTypes(info, stats)) {
  293. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  294. } else {
  295. this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  296. }
  297. } else {
  298. this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  299. }
  300. } else {
  301. switch (info.event) {
  302. case FSEVENT_CREATED:
  303. case FSEVENT_MODIFIED:
  304. return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  305. case FSEVENT_DELETED:
  306. case FSEVENT_MOVED:
  307. return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  308. }
  309. }
  310. };
  311. const closer = setFSEventsListener(
  312. watchPath,
  313. realPath,
  314. watchCallback,
  315. this.fsw._emitRaw
  316. );
  317. this.fsw._emitReady();
  318. return closer;
  319. }
  320. /**
  321. * Handle symlinks encountered during directory scan
  322. * @param {String} linkPath path to symlink
  323. * @param {String} fullPath absolute path to the symlink
  324. * @param {Function} transform pre-existing path transformer
  325. * @param {Number} curDepth level of subdirectories traversed to where symlink is
  326. * @returns {Promise<void>}
  327. */
  328. async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) {
  329. // don't follow the same symlink more than once
  330. if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return;
  331. this.fsw._symlinkPaths.set(fullPath, true);
  332. this.fsw._incrReadyCount();
  333. try {
  334. const linkTarget = await realpath(linkPath);
  335. if (this.fsw.closed) return;
  336. if (this.fsw._isIgnored(linkTarget)) {
  337. return this.fsw._emitReady();
  338. }
  339. this.fsw._incrReadyCount();
  340. // add the linkTarget for watching with a wrapper for transform
  341. // that causes emitted paths to incorporate the link's path
  342. this._addToFsEvents(linkTarget || linkPath, (path) => {
  343. let aliasedPath = linkPath;
  344. if (linkTarget && linkTarget !== DOT_SLASH) {
  345. aliasedPath = path.replace(linkTarget, linkPath);
  346. } else if (path !== DOT_SLASH) {
  347. aliasedPath = sysPath.join(linkPath, path);
  348. }
  349. return transform(aliasedPath);
  350. }, false, curDepth);
  351. } catch(error) {
  352. if (this.fsw._handleError(error)) {
  353. return this.fsw._emitReady();
  354. }
  355. }
  356. }
  357. /**
  358. *
  359. * @param {Path} newPath
  360. * @param {fs.Stats} stats
  361. */
  362. emitAdd(newPath, stats, processPath, opts, forceAdd) {
  363. const pp = processPath(newPath);
  364. const isDir = stats.isDirectory();
  365. const dirObj = this.fsw._getWatchedDir(sysPath.dirname(pp));
  366. const base = sysPath.basename(pp);
  367. // ensure empty dirs get tracked
  368. if (isDir) this.fsw._getWatchedDir(pp);
  369. if (dirObj.has(base)) return;
  370. dirObj.add(base);
  371. if (!opts.ignoreInitial || forceAdd === true) {
  372. this.fsw._emit(isDir ? EV_ADD_DIR : EV_ADD, pp, stats);
  373. }
  374. }
  375. initWatch(realPath, path, wh, processPath) {
  376. if (this.fsw.closed) return;
  377. const closer = this._watchWithFsEvents(
  378. wh.watchPath,
  379. sysPath.resolve(realPath || wh.watchPath),
  380. processPath,
  381. wh.globFilter
  382. );
  383. this.fsw._addPathCloser(path, closer);
  384. }
  385. /**
  386. * Handle added path with fsevents
  387. * @param {String} path file/dir path or glob pattern
  388. * @param {Function|Boolean=} transform converts working path to what the user expects
  389. * @param {Boolean=} forceAdd ensure add is emitted
  390. * @param {Number=} priorDepth Level of subdirectories already traversed.
  391. * @returns {Promise<void>}
  392. */
  393. async _addToFsEvents(path, transform, forceAdd, priorDepth) {
  394. if (this.fsw.closed) {
  395. return;
  396. }
  397. const opts = this.fsw.options;
  398. const processPath = typeof transform === FUNCTION_TYPE ? transform : IDENTITY_FN;
  399. const wh = this.fsw._getWatchHelpers(path);
  400. // evaluate what is at the path we're being asked to watch
  401. try {
  402. const stats = await statMethods[wh.statMethod](wh.watchPath);
  403. if (this.fsw.closed) return;
  404. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  405. throw null;
  406. }
  407. if (stats.isDirectory()) {
  408. // emit addDir unless this is a glob parent
  409. if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd);
  410. // don't recurse further if it would exceed depth setting
  411. if (priorDepth && priorDepth > opts.depth) return;
  412. // scan the contents of the dir
  413. this.fsw._readdirp(wh.watchPath, {
  414. fileFilter: entry => wh.filterPath(entry),
  415. directoryFilter: entry => wh.filterDir(entry),
  416. ...Depth(opts.depth - (priorDepth || 0))
  417. }).on(STR_DATA, (entry) => {
  418. // need to check filterPath on dirs b/c filterDir is less restrictive
  419. if (this.fsw.closed) {
  420. return;
  421. }
  422. if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
  423. const joinedPath = sysPath.join(wh.watchPath, entry.path);
  424. const {fullPath} = entry;
  425. if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
  426. // preserve the current depth here since it can't be derived from
  427. // real paths past the symlink
  428. const curDepth = opts.depth === undefined ?
  429. undefined : calcDepth(joinedPath, sysPath.resolve(wh.watchPath)) + 1;
  430. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  431. } else {
  432. this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
  433. }
  434. }).on(EV_ERROR, EMPTY_FN).on(STR_END, () => {
  435. this.fsw._emitReady();
  436. });
  437. } else {
  438. this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd);
  439. this.fsw._emitReady();
  440. }
  441. } catch (error) {
  442. if (!error || this.fsw._handleError(error)) {
  443. // TODO: Strange thing: "should not choke on an ignored watch path" will be failed without 2 ready calls -__-
  444. this.fsw._emitReady();
  445. this.fsw._emitReady();
  446. }
  447. }
  448. if (opts.persistent && forceAdd !== true) {
  449. if (typeof transform === FUNCTION_TYPE) {
  450. // realpath has already been resolved
  451. this.initWatch(undefined, path, wh, processPath);
  452. } else {
  453. let realPath;
  454. try {
  455. realPath = await realpath(wh.watchPath);
  456. } catch (e) {}
  457. this.initWatch(realPath, path, wh, processPath);
  458. }
  459. }
  460. }
  461. }
  462. module.exports = FsEventsHandler;
  463. module.exports.canUse = canUse;