You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

654 lines
20 KiB

2 years ago
  1. 'use strict';
  2. const fs = require('fs');
  3. const sysPath = require('path');
  4. const { promisify } = require('util');
  5. const isBinaryPath = require('is-binary-path');
  6. const {
  7. isWindows,
  8. isLinux,
  9. EMPTY_FN,
  10. EMPTY_STR,
  11. KEY_LISTENERS,
  12. KEY_ERR,
  13. KEY_RAW,
  14. HANDLER_KEYS,
  15. EV_CHANGE,
  16. EV_ADD,
  17. EV_ADD_DIR,
  18. EV_ERROR,
  19. STR_DATA,
  20. STR_END,
  21. BRACE_START,
  22. STAR
  23. } = require('./constants');
  24. const THROTTLE_MODE_WATCH = 'watch';
  25. const open = promisify(fs.open);
  26. const stat = promisify(fs.stat);
  27. const lstat = promisify(fs.lstat);
  28. const close = promisify(fs.close);
  29. const fsrealpath = promisify(fs.realpath);
  30. const statMethods = { lstat, stat };
  31. // TODO: emit errors properly. Example: EMFILE on Macos.
  32. const foreach = (val, fn) => {
  33. if (val instanceof Set) {
  34. val.forEach(fn);
  35. } else {
  36. fn(val);
  37. }
  38. };
  39. const addAndConvert = (main, prop, item) => {
  40. let container = main[prop];
  41. if (!(container instanceof Set)) {
  42. main[prop] = container = new Set([container]);
  43. }
  44. container.add(item);
  45. };
  46. const clearItem = cont => key => {
  47. const set = cont[key];
  48. if (set instanceof Set) {
  49. set.clear();
  50. } else {
  51. delete cont[key];
  52. }
  53. };
  54. const delFromSet = (main, prop, item) => {
  55. const container = main[prop];
  56. if (container instanceof Set) {
  57. container.delete(item);
  58. } else if (container === item) {
  59. delete main[prop];
  60. }
  61. };
  62. const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
  63. /**
  64. * @typedef {String} Path
  65. */
  66. // fs_watch helpers
  67. // object to hold per-process fs_watch instances
  68. // (may be shared across chokidar FSWatcher instances)
  69. /**
  70. * @typedef {Object} FsWatchContainer
  71. * @property {Set} listeners
  72. * @property {Set} errHandlers
  73. * @property {Set} rawEmitters
  74. * @property {fs.FSWatcher=} watcher
  75. * @property {Boolean=} watcherUnusable
  76. */
  77. /**
  78. * @type {Map<String,FsWatchContainer>}
  79. */
  80. const FsWatchInstances = new Map();
  81. /**
  82. * Instantiates the fs_watch interface
  83. * @param {String} path to be watched
  84. * @param {Object} options to be passed to fs_watch
  85. * @param {Function} listener main event handler
  86. * @param {Function} errHandler emits info about errors
  87. * @param {Function} emitRaw emits raw event data
  88. * @returns {fs.FSWatcher} new fsevents instance
  89. */
  90. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  91. const handleEvent = (rawEvent, evPath) => {
  92. listener(path);
  93. emitRaw(rawEvent, evPath, {watchedPath: path});
  94. // emit based on events occurring for files from a directory's watcher in
  95. // case the file's watcher misses it (and rely on throttling to de-dupe)
  96. if (evPath && path !== evPath) {
  97. fsWatchBroadcast(
  98. sysPath.resolve(path, evPath), KEY_LISTENERS, sysPath.join(path, evPath)
  99. );
  100. }
  101. };
  102. try {
  103. return fs.watch(path, options, handleEvent);
  104. } catch (error) {
  105. errHandler(error);
  106. }
  107. }
  108. /**
  109. * Helper for passing fs_watch event data to a collection of listeners
  110. * @param {Path} fullPath absolute path bound to fs_watch instance
  111. * @param {String} type listener type
  112. * @param {*=} val1 arguments to be passed to listeners
  113. * @param {*=} val2
  114. * @param {*=} val3
  115. */
  116. const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
  117. const cont = FsWatchInstances.get(fullPath);
  118. if (!cont) return;
  119. foreach(cont[type], (listener) => {
  120. listener(val1, val2, val3);
  121. });
  122. };
  123. /**
  124. * Instantiates the fs_watch interface or binds listeners
  125. * to an existing one covering the same file system entry
  126. * @param {String} path
  127. * @param {String} fullPath absolute path
  128. * @param {Object} options to be passed to fs_watch
  129. * @param {Object} handlers container for event listener functions
  130. */
  131. const setFsWatchListener = (path, fullPath, options, handlers) => {
  132. const {listener, errHandler, rawEmitter} = handlers;
  133. let cont = FsWatchInstances.get(fullPath);
  134. /** @type {fs.FSWatcher=} */
  135. let watcher;
  136. if (!options.persistent) {
  137. watcher = createFsWatchInstance(
  138. path, options, listener, errHandler, rawEmitter
  139. );
  140. return watcher.close.bind(watcher);
  141. }
  142. if (cont) {
  143. addAndConvert(cont, KEY_LISTENERS, listener);
  144. addAndConvert(cont, KEY_ERR, errHandler);
  145. addAndConvert(cont, KEY_RAW, rawEmitter);
  146. } else {
  147. watcher = createFsWatchInstance(
  148. path,
  149. options,
  150. fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
  151. errHandler, // no need to use broadcast here
  152. fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
  153. );
  154. if (!watcher) return;
  155. watcher.on(EV_ERROR, async (error) => {
  156. const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
  157. cont.watcherUnusable = true; // documented since Node 10.4.1
  158. // Workaround for https://github.com/joyent/node/issues/4337
  159. if (isWindows && error.code === 'EPERM') {
  160. try {
  161. const fd = await open(path, 'r');
  162. await close(fd);
  163. broadcastErr(error);
  164. } catch (err) {}
  165. } else {
  166. broadcastErr(error);
  167. }
  168. });
  169. cont = {
  170. listeners: listener,
  171. errHandlers: errHandler,
  172. rawEmitters: rawEmitter,
  173. watcher
  174. };
  175. FsWatchInstances.set(fullPath, cont);
  176. }
  177. // const index = cont.listeners.indexOf(listener);
  178. // removes this instance's listeners and closes the underlying fs_watch
  179. // instance if there are no more listeners left
  180. return () => {
  181. delFromSet(cont, KEY_LISTENERS, listener);
  182. delFromSet(cont, KEY_ERR, errHandler);
  183. delFromSet(cont, KEY_RAW, rawEmitter);
  184. if (isEmptySet(cont.listeners)) {
  185. // Check to protect against issue gh-730.
  186. // if (cont.watcherUnusable) {
  187. cont.watcher.close();
  188. // }
  189. FsWatchInstances.delete(fullPath);
  190. HANDLER_KEYS.forEach(clearItem(cont));
  191. cont.watcher = undefined;
  192. Object.freeze(cont);
  193. }
  194. };
  195. };
  196. // fs_watchFile helpers
  197. // object to hold per-process fs_watchFile instances
  198. // (may be shared across chokidar FSWatcher instances)
  199. const FsWatchFileInstances = new Map();
  200. /**
  201. * Instantiates the fs_watchFile interface or binds listeners
  202. * to an existing one covering the same file system entry
  203. * @param {String} path to be watched
  204. * @param {String} fullPath absolute path
  205. * @param {Object} options options to be passed to fs_watchFile
  206. * @param {Object} handlers container for event listener functions
  207. * @returns {Function} closer
  208. */
  209. const setFsWatchFileListener = (path, fullPath, options, handlers) => {
  210. const {listener, rawEmitter} = handlers;
  211. let cont = FsWatchFileInstances.get(fullPath);
  212. /* eslint-disable no-unused-vars, prefer-destructuring */
  213. let listeners = new Set();
  214. let rawEmitters = new Set();
  215. const copts = cont && cont.options;
  216. if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
  217. // "Upgrade" the watcher to persistence or a quicker interval.
  218. // This creates some unlikely edge case issues if the user mixes
  219. // settings in a very weird way, but solving for those cases
  220. // doesn't seem worthwhile for the added complexity.
  221. listeners = cont.listeners;
  222. rawEmitters = cont.rawEmitters;
  223. fs.unwatchFile(fullPath);
  224. cont = undefined;
  225. }
  226. /* eslint-enable no-unused-vars, prefer-destructuring */
  227. if (cont) {
  228. addAndConvert(cont, KEY_LISTENERS, listener);
  229. addAndConvert(cont, KEY_RAW, rawEmitter);
  230. } else {
  231. // TODO
  232. // listeners.add(listener);
  233. // rawEmitters.add(rawEmitter);
  234. cont = {
  235. listeners: listener,
  236. rawEmitters: rawEmitter,
  237. options,
  238. watcher: fs.watchFile(fullPath, options, (curr, prev) => {
  239. foreach(cont.rawEmitters, (rawEmitter) => {
  240. rawEmitter(EV_CHANGE, fullPath, {curr, prev});
  241. });
  242. const currmtime = curr.mtimeMs;
  243. if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
  244. foreach(cont.listeners, (listener) => listener(path, curr));
  245. }
  246. })
  247. };
  248. FsWatchFileInstances.set(fullPath, cont);
  249. }
  250. // const index = cont.listeners.indexOf(listener);
  251. // Removes this instance's listeners and closes the underlying fs_watchFile
  252. // instance if there are no more listeners left.
  253. return () => {
  254. delFromSet(cont, KEY_LISTENERS, listener);
  255. delFromSet(cont, KEY_RAW, rawEmitter);
  256. if (isEmptySet(cont.listeners)) {
  257. FsWatchFileInstances.delete(fullPath);
  258. fs.unwatchFile(fullPath);
  259. cont.options = cont.watcher = undefined;
  260. Object.freeze(cont);
  261. }
  262. };
  263. };
  264. /**
  265. * @mixin
  266. */
  267. class NodeFsHandler {
  268. /**
  269. * @param {import("../index").FSWatcher} fsW
  270. */
  271. constructor(fsW) {
  272. this.fsw = fsW;
  273. this._boundHandleError = (error) => fsW._handleError(error);
  274. }
  275. /**
  276. * Watch file for changes with fs_watchFile or fs_watch.
  277. * @param {String} path to file or dir
  278. * @param {Function} listener on fs change
  279. * @returns {Function} closer for the watcher instance
  280. */
  281. _watchWithNodeFs(path, listener) {
  282. const opts = this.fsw.options;
  283. const directory = sysPath.dirname(path);
  284. const basename = sysPath.basename(path);
  285. const parent = this.fsw._getWatchedDir(directory);
  286. parent.add(basename);
  287. const absolutePath = sysPath.resolve(path);
  288. const options = {persistent: opts.persistent};
  289. if (!listener) listener = EMPTY_FN;
  290. let closer;
  291. if (opts.usePolling) {
  292. options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
  293. opts.binaryInterval : opts.interval;
  294. closer = setFsWatchFileListener(path, absolutePath, options, {
  295. listener,
  296. rawEmitter: this.fsw._emitRaw
  297. });
  298. } else {
  299. closer = setFsWatchListener(path, absolutePath, options, {
  300. listener,
  301. errHandler: this._boundHandleError,
  302. rawEmitter: this.fsw._emitRaw
  303. });
  304. }
  305. return closer;
  306. }
  307. /**
  308. * Watch a file and emit add event if warranted.
  309. * @param {Path} file Path
  310. * @param {fs.Stats} stats result of fs_stat
  311. * @param {Boolean} initialAdd was the file added at watch instantiation?
  312. * @returns {Function} closer for the watcher instance
  313. */
  314. _handleFile(file, stats, initialAdd) {
  315. if (this.fsw.closed) {
  316. return;
  317. }
  318. const dirname = sysPath.dirname(file);
  319. const basename = sysPath.basename(file);
  320. const parent = this.fsw._getWatchedDir(dirname);
  321. // stats is always present
  322. let prevStats = stats;
  323. // if the file is already being watched, do nothing
  324. if (parent.has(basename)) return;
  325. const listener = async (path, newStats) => {
  326. if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
  327. if (!newStats || newStats.mtimeMs === 0) {
  328. try {
  329. const newStats = await stat(file);
  330. if (this.fsw.closed) return;
  331. // Check that change event was not fired because of changed only accessTime.
  332. const at = newStats.atimeMs;
  333. const mt = newStats.mtimeMs;
  334. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  335. this.fsw._emit(EV_CHANGE, file, newStats);
  336. }
  337. if (isLinux && prevStats.ino !== newStats.ino) {
  338. this.fsw._closeFile(path)
  339. prevStats = newStats;
  340. this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
  341. } else {
  342. prevStats = newStats;
  343. }
  344. } catch (error) {
  345. // Fix issues where mtime is null but file is still present
  346. this.fsw._remove(dirname, basename);
  347. }
  348. // add is about to be emitted if file not already tracked in parent
  349. } else if (parent.has(basename)) {
  350. // Check that change event was not fired because of changed only accessTime.
  351. const at = newStats.atimeMs;
  352. const mt = newStats.mtimeMs;
  353. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  354. this.fsw._emit(EV_CHANGE, file, newStats);
  355. }
  356. prevStats = newStats;
  357. }
  358. }
  359. // kick off the watcher
  360. const closer = this._watchWithNodeFs(file, listener);
  361. // emit an add event if we're supposed to
  362. if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
  363. if (!this.fsw._throttle(EV_ADD, file, 0)) return;
  364. this.fsw._emit(EV_ADD, file, stats);
  365. }
  366. return closer;
  367. }
  368. /**
  369. * Handle symlinks encountered while reading a dir.
  370. * @param {Object} entry returned by readdirp
  371. * @param {String} directory path of dir being read
  372. * @param {String} path of this item
  373. * @param {String} item basename of this item
  374. * @returns {Promise<Boolean>} true if no more processing is needed for this entry.
  375. */
  376. async _handleSymlink(entry, directory, path, item) {
  377. if (this.fsw.closed) {
  378. return;
  379. }
  380. const full = entry.fullPath;
  381. const dir = this.fsw._getWatchedDir(directory);
  382. if (!this.fsw.options.followSymlinks) {
  383. // watch symlink directly (don't follow) and detect changes
  384. this.fsw._incrReadyCount();
  385. let linkPath;
  386. try {
  387. linkPath = await fsrealpath(path);
  388. } catch (e) {
  389. this.fsw._emitReady();
  390. return true;
  391. }
  392. if (this.fsw.closed) return;
  393. if (dir.has(item)) {
  394. if (this.fsw._symlinkPaths.get(full) !== linkPath) {
  395. this.fsw._symlinkPaths.set(full, linkPath);
  396. this.fsw._emit(EV_CHANGE, path, entry.stats);
  397. }
  398. } else {
  399. dir.add(item);
  400. this.fsw._symlinkPaths.set(full, linkPath);
  401. this.fsw._emit(EV_ADD, path, entry.stats);
  402. }
  403. this.fsw._emitReady();
  404. return true;
  405. }
  406. // don't follow the same symlink more than once
  407. if (this.fsw._symlinkPaths.has(full)) {
  408. return true;
  409. }
  410. this.fsw._symlinkPaths.set(full, true);
  411. }
  412. _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
  413. // Normalize the directory name on Windows
  414. directory = sysPath.join(directory, EMPTY_STR);
  415. if (!wh.hasGlob) {
  416. throttler = this.fsw._throttle('readdir', directory, 1000);
  417. if (!throttler) return;
  418. }
  419. const previous = this.fsw._getWatchedDir(wh.path);
  420. const current = new Set();
  421. let stream = this.fsw._readdirp(directory, {
  422. fileFilter: entry => wh.filterPath(entry),
  423. directoryFilter: entry => wh.filterDir(entry),
  424. depth: 0
  425. }).on(STR_DATA, async (entry) => {
  426. if (this.fsw.closed) {
  427. stream = undefined;
  428. return;
  429. }
  430. const item = entry.path;
  431. let path = sysPath.join(directory, item);
  432. current.add(item);
  433. if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
  434. return;
  435. }
  436. if (this.fsw.closed) {
  437. stream = undefined;
  438. return;
  439. }
  440. // Files that present in current directory snapshot
  441. // but absent in previous are added to watch list and
  442. // emit `add` event.
  443. if (item === target || !target && !previous.has(item)) {
  444. this.fsw._incrReadyCount();
  445. // ensure relativeness of path is preserved in case of watcher reuse
  446. path = sysPath.join(dir, sysPath.relative(dir, path));
  447. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  448. }
  449. }).on(EV_ERROR, this._boundHandleError);
  450. return new Promise(resolve =>
  451. stream.once(STR_END, () => {
  452. if (this.fsw.closed) {
  453. stream = undefined;
  454. return;
  455. }
  456. const wasThrottled = throttler ? throttler.clear() : false;
  457. resolve();
  458. // Files that absent in current directory snapshot
  459. // but present in previous emit `remove` event
  460. // and are removed from @watched[directory].
  461. previous.getChildren().filter((item) => {
  462. return item !== directory &&
  463. !current.has(item) &&
  464. // in case of intersecting globs;
  465. // a path may have been filtered out of this readdir, but
  466. // shouldn't be removed because it matches a different glob
  467. (!wh.hasGlob || wh.filterPath({
  468. fullPath: sysPath.resolve(directory, item)
  469. }));
  470. }).forEach((item) => {
  471. this.fsw._remove(directory, item);
  472. });
  473. stream = undefined;
  474. // one more time for any missed in case changes came in extremely quickly
  475. if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
  476. })
  477. );
  478. }
  479. /**
  480. * Read directory to add / remove files from `@watched` list and re-read it on change.
  481. * @param {String} dir fs path
  482. * @param {fs.Stats} stats
  483. * @param {Boolean} initialAdd
  484. * @param {Number} depth relative to user-supplied path
  485. * @param {String} target child path targeted for watch
  486. * @param {Object} wh Common watch helpers for this path
  487. * @param {String} realpath
  488. * @returns {Promise<Function>} closer for the watcher instance.
  489. */
  490. async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
  491. const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
  492. const tracked = parentDir.has(sysPath.basename(dir));
  493. if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
  494. if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR, dir, stats);
  495. }
  496. // ensure dir is tracked (harmless if redundant)
  497. parentDir.add(sysPath.basename(dir));
  498. this.fsw._getWatchedDir(dir);
  499. let throttler;
  500. let closer;
  501. const oDepth = this.fsw.options.depth;
  502. if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
  503. if (!target) {
  504. await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
  505. if (this.fsw.closed) return;
  506. }
  507. closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
  508. // if current directory is removed, do nothing
  509. if (stats && stats.mtimeMs === 0) return;
  510. this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
  511. });
  512. }
  513. return closer;
  514. }
  515. /**
  516. * Handle added file, directory, or glob pattern.
  517. * Delegates call to _handleFile / _handleDir after checks.
  518. * @param {String} path to file or ir
  519. * @param {Boolean} initialAdd was the file added at watch instantiation?
  520. * @param {Object} priorWh depth relative to user-supplied path
  521. * @param {Number} depth Child path actually targeted for watch
  522. * @param {String=} target Child path actually targeted for watch
  523. * @returns {Promise}
  524. */
  525. async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
  526. const ready = this.fsw._emitReady;
  527. if (this.fsw._isIgnored(path) || this.fsw.closed) {
  528. ready();
  529. return false;
  530. }
  531. const wh = this.fsw._getWatchHelpers(path, depth);
  532. if (!wh.hasGlob && priorWh) {
  533. wh.hasGlob = priorWh.hasGlob;
  534. wh.globFilter = priorWh.globFilter;
  535. wh.filterPath = entry => priorWh.filterPath(entry);
  536. wh.filterDir = entry => priorWh.filterDir(entry);
  537. }
  538. // evaluate what is at the path we're being asked to watch
  539. try {
  540. const stats = await statMethods[wh.statMethod](wh.watchPath);
  541. if (this.fsw.closed) return;
  542. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  543. ready();
  544. return false;
  545. }
  546. const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START);
  547. let closer;
  548. if (stats.isDirectory()) {
  549. const absPath = sysPath.resolve(path);
  550. const targetPath = follow ? await fsrealpath(path) : path;
  551. if (this.fsw.closed) return;
  552. closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
  553. if (this.fsw.closed) return;
  554. // preserve this symlink's target path
  555. if (absPath !== targetPath && targetPath !== undefined) {
  556. this.fsw._symlinkPaths.set(absPath, targetPath);
  557. }
  558. } else if (stats.isSymbolicLink()) {
  559. const targetPath = follow ? await fsrealpath(path) : path;
  560. if (this.fsw.closed) return;
  561. const parent = sysPath.dirname(wh.watchPath);
  562. this.fsw._getWatchedDir(parent).add(wh.watchPath);
  563. this.fsw._emit(EV_ADD, wh.watchPath, stats);
  564. closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
  565. if (this.fsw.closed) return;
  566. // preserve this symlink's target path
  567. if (targetPath !== undefined) {
  568. this.fsw._symlinkPaths.set(sysPath.resolve(path), targetPath);
  569. }
  570. } else {
  571. closer = this._handleFile(wh.watchPath, stats, initialAdd);
  572. }
  573. ready();
  574. this.fsw._addPathCloser(path, closer);
  575. return false;
  576. } catch (error) {
  577. if (this.fsw._handleError(error)) {
  578. ready();
  579. return path;
  580. }
  581. }
  582. }
  583. }
  584. module.exports = NodeFsHandler;