fsevents-handler.js 16 KB

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