fsevents-handler.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 || this.fsw._isIgnored(watchPath)) return;
  265. const opts = this.fsw.options;
  266. const watchCallback = async (fullPath, flags, info) => {
  267. if (this.fsw.closed) return;
  268. if (
  269. opts.depth !== undefined &&
  270. calcDepth(fullPath, realPath) > opts.depth
  271. ) return;
  272. const path = transform(sysPath.join(
  273. watchPath, sysPath.relative(watchPath, fullPath)
  274. ));
  275. if (globFilter && !globFilter(path)) return;
  276. // ensure directories are tracked
  277. const parent = sysPath.dirname(path);
  278. const item = sysPath.basename(path);
  279. const watchedDir = this.fsw._getWatchedDir(
  280. info.type === FSEVENT_TYPE_DIRECTORY ? path : parent
  281. );
  282. // correct for wrong events emitted
  283. if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) {
  284. if (typeof opts.ignored === FUNCTION_TYPE) {
  285. let stats;
  286. try {
  287. stats = await stat(path);
  288. } catch (error) {}
  289. if (this.fsw.closed) return;
  290. if (this.checkIgnored(path, stats)) return;
  291. if (sameTypes(info, stats)) {
  292. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  293. } else {
  294. this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  295. }
  296. } else {
  297. this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  298. }
  299. } else {
  300. switch (info.event) {
  301. case FSEVENT_CREATED:
  302. case FSEVENT_MODIFIED:
  303. return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  304. case FSEVENT_DELETED:
  305. case FSEVENT_MOVED:
  306. return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  307. }
  308. }
  309. };
  310. const closer = setFSEventsListener(
  311. watchPath,
  312. realPath,
  313. watchCallback,
  314. this.fsw._emitRaw
  315. );
  316. this.fsw._emitReady();
  317. return closer;
  318. }
  319. /**
  320. * Handle symlinks encountered during directory scan
  321. * @param {String} linkPath path to symlink
  322. * @param {String} fullPath absolute path to the symlink
  323. * @param {Function} transform pre-existing path transformer
  324. * @param {Number} curDepth level of subdirectories traversed to where symlink is
  325. * @returns {Promise<void>}
  326. */
  327. async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) {
  328. // don't follow the same symlink more than once
  329. if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return;
  330. this.fsw._symlinkPaths.set(fullPath, true);
  331. this.fsw._incrReadyCount();
  332. try {
  333. const linkTarget = await realpath(linkPath);
  334. if (this.fsw.closed) return;
  335. if (this.fsw._isIgnored(linkTarget)) {
  336. return this.fsw._emitReady();
  337. }
  338. this.fsw._incrReadyCount();
  339. // add the linkTarget for watching with a wrapper for transform
  340. // that causes emitted paths to incorporate the link's path
  341. this._addToFsEvents(linkTarget || linkPath, (path) => {
  342. let aliasedPath = linkPath;
  343. if (linkTarget && linkTarget !== DOT_SLASH) {
  344. aliasedPath = path.replace(linkTarget, linkPath);
  345. } else if (path !== DOT_SLASH) {
  346. aliasedPath = sysPath.join(linkPath, path);
  347. }
  348. return transform(aliasedPath);
  349. }, false, curDepth);
  350. } catch(error) {
  351. if (this.fsw._handleError(error)) {
  352. return this.fsw._emitReady();
  353. }
  354. }
  355. }
  356. /**
  357. *
  358. * @param {Path} newPath
  359. * @param {fs.Stats} stats
  360. */
  361. emitAdd(newPath, stats, processPath, opts, forceAdd) {
  362. const pp = processPath(newPath);
  363. const isDir = stats.isDirectory();
  364. const dirObj = this.fsw._getWatchedDir(sysPath.dirname(pp));
  365. const base = sysPath.basename(pp);
  366. // ensure empty dirs get tracked
  367. if (isDir) this.fsw._getWatchedDir(pp);
  368. if (dirObj.has(base)) return;
  369. dirObj.add(base);
  370. if (!opts.ignoreInitial || forceAdd === true) {
  371. this.fsw._emit(isDir ? EV_ADD_DIR : EV_ADD, pp, stats);
  372. }
  373. }
  374. initWatch(realPath, path, wh, processPath) {
  375. if (this.fsw.closed) return;
  376. const closer = this._watchWithFsEvents(
  377. wh.watchPath,
  378. sysPath.resolve(realPath || wh.watchPath),
  379. processPath,
  380. wh.globFilter
  381. );
  382. this.fsw._addPathCloser(path, closer);
  383. }
  384. /**
  385. * Handle added path with fsevents
  386. * @param {String} path file/dir path or glob pattern
  387. * @param {Function|Boolean=} transform converts working path to what the user expects
  388. * @param {Boolean=} forceAdd ensure add is emitted
  389. * @param {Number=} priorDepth Level of subdirectories already traversed.
  390. * @returns {Promise<void>}
  391. */
  392. async _addToFsEvents(path, transform, forceAdd, priorDepth) {
  393. if (this.fsw.closed) {
  394. return;
  395. }
  396. const opts = this.fsw.options;
  397. const processPath = typeof transform === FUNCTION_TYPE ? transform : IDENTITY_FN;
  398. const wh = this.fsw._getWatchHelpers(path);
  399. // evaluate what is at the path we're being asked to watch
  400. try {
  401. const stats = await statMethods[wh.statMethod](wh.watchPath);
  402. if (this.fsw.closed) return;
  403. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  404. throw null;
  405. }
  406. if (stats.isDirectory()) {
  407. // emit addDir unless this is a glob parent
  408. if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd);
  409. // don't recurse further if it would exceed depth setting
  410. if (priorDepth && priorDepth > opts.depth) return;
  411. // scan the contents of the dir
  412. this.fsw._readdirp(wh.watchPath, {
  413. fileFilter: entry => wh.filterPath(entry),
  414. directoryFilter: entry => wh.filterDir(entry),
  415. ...Depth(opts.depth - (priorDepth || 0))
  416. }).on(STR_DATA, (entry) => {
  417. // need to check filterPath on dirs b/c filterDir is less restrictive
  418. if (this.fsw.closed) {
  419. return;
  420. }
  421. if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
  422. const joinedPath = sysPath.join(wh.watchPath, entry.path);
  423. const {fullPath} = entry;
  424. if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
  425. // preserve the current depth here since it can't be derived from
  426. // real paths past the symlink
  427. const curDepth = opts.depth === undefined ?
  428. undefined : calcDepth(joinedPath, sysPath.resolve(wh.watchPath)) + 1;
  429. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  430. } else {
  431. this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
  432. }
  433. }).on(EV_ERROR, EMPTY_FN).on(STR_END, () => {
  434. this.fsw._emitReady();
  435. });
  436. } else {
  437. this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd);
  438. this.fsw._emitReady();
  439. }
  440. } catch (error) {
  441. if (!error || this.fsw._handleError(error)) {
  442. // TODO: Strange thing: "should not choke on an ignored watch path" will be failed without 2 ready calls -__-
  443. this.fsw._emitReady();
  444. this.fsw._emitReady();
  445. }
  446. }
  447. if (opts.persistent && forceAdd !== true) {
  448. if (typeof transform === FUNCTION_TYPE) {
  449. // realpath has already been resolved
  450. this.initWatch(undefined, path, wh, processPath);
  451. } else {
  452. let realPath;
  453. try {
  454. realPath = await realpath(wh.watchPath);
  455. } catch (e) {}
  456. this.initWatch(realPath, path, wh, processPath);
  457. }
  458. }
  459. }
  460. }
  461. module.exports = FsEventsHandler;
  462. module.exports.canUse = canUse;