nodefs-handler.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. 'use strict';
  2. var fs = require('fs');
  3. var sysPath = require('path');
  4. var readdirp = require('readdirp');
  5. var isBinaryPath = require('is-binary-path');
  6. // fs.watch helpers
  7. // object to hold per-process fs.watch instances
  8. // (may be shared across chokidar FSWatcher instances)
  9. var FsWatchInstances = Object.create(null);
  10. // Private function: Instantiates the fs.watch interface
  11. // * path - string, path to be watched
  12. // * options - object, options to be passed to fs.watch
  13. // * listener - function, main event handler
  14. // * errHandler - function, handler which emits info about errors
  15. // * emitRaw - function, handler which emits raw event data
  16. // Returns new fsevents instance
  17. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  18. var handleEvent = function(rawEvent, evPath) {
  19. listener(path);
  20. emitRaw(rawEvent, evPath, {watchedPath: path});
  21. // emit based on events occuring for files from a directory's watcher in
  22. // case the file's watcher misses it (and rely on throttling to de-dupe)
  23. if (evPath && path !== evPath) {
  24. fsWatchBroadcast(
  25. sysPath.resolve(path, evPath), 'listeners', sysPath.join(path, evPath)
  26. );
  27. }
  28. };
  29. try {
  30. return fs.watch(path, options, handleEvent);
  31. } catch (error) {
  32. errHandler(error);
  33. }
  34. }
  35. // Private function: Helper for passing fs.watch event data to a
  36. // collection of listeners
  37. // * fullPath - string, absolute path bound to the fs.watch instance
  38. // * type - string, listener type
  39. // * val[1..3] - arguments to be passed to listeners
  40. // Returns nothing
  41. function fsWatchBroadcast(fullPath, type, val1, val2, val3) {
  42. if (!FsWatchInstances[fullPath]) return;
  43. FsWatchInstances[fullPath][type].forEach(function(listener) {
  44. listener(val1, val2, val3);
  45. });
  46. }
  47. // Private function: Instantiates the fs.watch interface or binds listeners
  48. // to an existing one covering the same file system entry
  49. // * path - string, path to be watched
  50. // * fullPath - string, absolute path
  51. // * options - object, options to be passed to fs.watch
  52. // * handlers - object, container for event listener functions
  53. // Returns close function
  54. function setFsWatchListener(path, fullPath, options, handlers) {
  55. var listener = handlers.listener;
  56. var errHandler = handlers.errHandler;
  57. var rawEmitter = handlers.rawEmitter;
  58. var container = FsWatchInstances[fullPath];
  59. var watcher;
  60. if (!options.persistent) {
  61. watcher = createFsWatchInstance(
  62. path, options, listener, errHandler, rawEmitter
  63. );
  64. return watcher.close.bind(watcher);
  65. }
  66. if (!container) {
  67. watcher = createFsWatchInstance(
  68. path,
  69. options,
  70. fsWatchBroadcast.bind(null, fullPath, 'listeners'),
  71. errHandler, // no need to use broadcast here
  72. fsWatchBroadcast.bind(null, fullPath, 'rawEmitters')
  73. );
  74. if (!watcher) return;
  75. var broadcastErr = fsWatchBroadcast.bind(null, fullPath, 'errHandlers');
  76. watcher.on('error', function(error) {
  77. container.watcherUnusable = true; // documented since Node 10.4.1
  78. // Workaround for https://github.com/joyent/node/issues/4337
  79. if (process.platform === 'win32' && error.code === 'EPERM') {
  80. fs.open(path, 'r', function(err, fd) {
  81. if (!err) fs.close(fd, function(err) {
  82. if (!err) broadcastErr(error);
  83. });
  84. });
  85. } else {
  86. broadcastErr(error);
  87. }
  88. });
  89. container = FsWatchInstances[fullPath] = {
  90. listeners: [listener],
  91. errHandlers: [errHandler],
  92. rawEmitters: [rawEmitter],
  93. watcher: watcher
  94. };
  95. } else {
  96. container.listeners.push(listener);
  97. container.errHandlers.push(errHandler);
  98. container.rawEmitters.push(rawEmitter);
  99. }
  100. var listenerIndex = container.listeners.length - 1;
  101. // removes this instance's listeners and closes the underlying fs.watch
  102. // instance if there are no more listeners left
  103. return function close() {
  104. delete container.listeners[listenerIndex];
  105. delete container.errHandlers[listenerIndex];
  106. delete container.rawEmitters[listenerIndex];
  107. if (!Object.keys(container.listeners).length) {
  108. if (!container.watcherUnusable) { // check to protect against issue #730
  109. container.watcher.close();
  110. }
  111. delete FsWatchInstances[fullPath];
  112. }
  113. };
  114. }
  115. // fs.watchFile helpers
  116. // object to hold per-process fs.watchFile instances
  117. // (may be shared across chokidar FSWatcher instances)
  118. var FsWatchFileInstances = Object.create(null);
  119. // Private function: Instantiates the fs.watchFile interface or binds listeners
  120. // to an existing one covering the same file system entry
  121. // * path - string, path to be watched
  122. // * fullPath - string, absolute path
  123. // * options - object, options to be passed to fs.watchFile
  124. // * handlers - object, container for event listener functions
  125. // Returns close function
  126. function setFsWatchFileListener(path, fullPath, options, handlers) {
  127. var listener = handlers.listener;
  128. var rawEmitter = handlers.rawEmitter;
  129. var container = FsWatchFileInstances[fullPath];
  130. var listeners = [];
  131. var rawEmitters = [];
  132. if (
  133. container && (
  134. container.options.persistent < options.persistent ||
  135. container.options.interval > options.interval
  136. )
  137. ) {
  138. // "Upgrade" the watcher to persistence or a quicker interval.
  139. // This creates some unlikely edge case issues if the user mixes
  140. // settings in a very weird way, but solving for those cases
  141. // doesn't seem worthwhile for the added complexity.
  142. listeners = container.listeners;
  143. rawEmitters = container.rawEmitters;
  144. fs.unwatchFile(fullPath);
  145. container = false;
  146. }
  147. if (!container) {
  148. listeners.push(listener);
  149. rawEmitters.push(rawEmitter);
  150. container = FsWatchFileInstances[fullPath] = {
  151. listeners: listeners,
  152. rawEmitters: rawEmitters,
  153. options: options,
  154. watcher: fs.watchFile(fullPath, options, function(curr, prev) {
  155. container.rawEmitters.forEach(function(rawEmitter) {
  156. rawEmitter('change', fullPath, {curr: curr, prev: prev});
  157. });
  158. var currmtime = curr.mtime.getTime();
  159. if (curr.size !== prev.size || currmtime > prev.mtime.getTime() || currmtime === 0) {
  160. container.listeners.forEach(function(listener) {
  161. listener(path, curr);
  162. });
  163. }
  164. })
  165. };
  166. } else {
  167. container.listeners.push(listener);
  168. container.rawEmitters.push(rawEmitter);
  169. }
  170. var listenerIndex = container.listeners.length - 1;
  171. // removes this instance's listeners and closes the underlying fs.watchFile
  172. // instance if there are no more listeners left
  173. return function close() {
  174. delete container.listeners[listenerIndex];
  175. delete container.rawEmitters[listenerIndex];
  176. if (!Object.keys(container.listeners).length) {
  177. fs.unwatchFile(fullPath);
  178. delete FsWatchFileInstances[fullPath];
  179. }
  180. };
  181. }
  182. // fake constructor for attaching nodefs-specific prototype methods that
  183. // will be copied to FSWatcher's prototype
  184. function NodeFsHandler() {}
  185. // Private method: Watch file for changes with fs.watchFile or fs.watch.
  186. // * path - string, path to file or directory.
  187. // * listener - function, to be executed on fs change.
  188. // Returns close function for the watcher instance
  189. NodeFsHandler.prototype._watchWithNodeFs =
  190. function(path, listener) {
  191. var directory = sysPath.dirname(path);
  192. var basename = sysPath.basename(path);
  193. var parent = this._getWatchedDir(directory);
  194. parent.add(basename);
  195. var absolutePath = sysPath.resolve(path);
  196. var options = {persistent: this.options.persistent};
  197. if (!listener) listener = Function.prototype; // empty function
  198. var closer;
  199. if (this.options.usePolling) {
  200. options.interval = this.enableBinaryInterval && isBinaryPath(basename) ?
  201. this.options.binaryInterval : this.options.interval;
  202. closer = setFsWatchFileListener(path, absolutePath, options, {
  203. listener: listener,
  204. rawEmitter: this.emit.bind(this, 'raw')
  205. });
  206. } else {
  207. closer = setFsWatchListener(path, absolutePath, options, {
  208. listener: listener,
  209. errHandler: this._handleError.bind(this),
  210. rawEmitter: this.emit.bind(this, 'raw')
  211. });
  212. }
  213. return closer;
  214. };
  215. // Private method: Watch a file and emit add event if warranted
  216. // * file - string, the file's path
  217. // * stats - object, result of fs.stat
  218. // * initialAdd - boolean, was the file added at watch instantiation?
  219. // * callback - function, called when done processing as a newly seen file
  220. // Returns close function for the watcher instance
  221. NodeFsHandler.prototype._handleFile =
  222. function(file, stats, initialAdd, callback) {
  223. var dirname = sysPath.dirname(file);
  224. var basename = sysPath.basename(file);
  225. var parent = this._getWatchedDir(dirname);
  226. // if the file is already being watched, do nothing
  227. if (parent.has(basename)) return callback();
  228. // kick off the watcher
  229. var closer = this._watchWithNodeFs(file, function(path, newStats) {
  230. if (!this._throttle('watch', file, 5)) return;
  231. if (!newStats || newStats && newStats.mtime.getTime() === 0) {
  232. fs.stat(file, function(error, newStats) {
  233. // Fix issues where mtime is null but file is still present
  234. if (error) {
  235. this._remove(dirname, basename);
  236. } else {
  237. this._emit('change', file, newStats);
  238. }
  239. }.bind(this));
  240. // add is about to be emitted if file not already tracked in parent
  241. } else if (parent.has(basename)) {
  242. this._emit('change', file, newStats);
  243. }
  244. }.bind(this));
  245. // emit an add event if we're supposed to
  246. if (!(initialAdd && this.options.ignoreInitial)) {
  247. if (!this._throttle('add', file, 0)) return;
  248. this._emit('add', file, stats);
  249. }
  250. if (callback) callback();
  251. return closer;
  252. };
  253. // Private method: Handle symlinks encountered while reading a dir
  254. // * entry - object, entry object returned by readdirp
  255. // * directory - string, path of the directory being read
  256. // * path - string, path of this item
  257. // * item - string, basename of this item
  258. // Returns true if no more processing is needed for this entry.
  259. NodeFsHandler.prototype._handleSymlink =
  260. function(entry, directory, path, item) {
  261. var full = entry.fullPath;
  262. var dir = this._getWatchedDir(directory);
  263. if (!this.options.followSymlinks) {
  264. // watch symlink directly (don't follow) and detect changes
  265. this._readyCount++;
  266. fs.realpath(path, function(error, linkPath) {
  267. if (dir.has(item)) {
  268. if (this._symlinkPaths[full] !== linkPath) {
  269. this._symlinkPaths[full] = linkPath;
  270. this._emit('change', path, entry.stat);
  271. }
  272. } else {
  273. dir.add(item);
  274. this._symlinkPaths[full] = linkPath;
  275. this._emit('add', path, entry.stat);
  276. }
  277. this._emitReady();
  278. }.bind(this));
  279. return true;
  280. }
  281. // don't follow the same symlink more than once
  282. if (this._symlinkPaths[full]) return true;
  283. else this._symlinkPaths[full] = true;
  284. };
  285. // Private method: Read directory to add / remove files from `@watched` list
  286. // and re-read it on change.
  287. // * dir - string, fs path.
  288. // * stats - object, result of fs.stat
  289. // * initialAdd - boolean, was the file added at watch instantiation?
  290. // * depth - int, depth relative to user-supplied path
  291. // * target - string, child path actually targeted for watch
  292. // * wh - object, common watch helpers for this path
  293. // * callback - function, called when dir scan is complete
  294. // Returns close function for the watcher instance
  295. NodeFsHandler.prototype._handleDir =
  296. function(dir, stats, initialAdd, depth, target, wh, callback) {
  297. var parentDir = this._getWatchedDir(sysPath.dirname(dir));
  298. var tracked = parentDir.has(sysPath.basename(dir));
  299. if (!(initialAdd && this.options.ignoreInitial) && !target && !tracked) {
  300. if (!wh.hasGlob || wh.globFilter(dir)) this._emit('addDir', dir, stats);
  301. }
  302. // ensure dir is tracked (harmless if redundant)
  303. parentDir.add(sysPath.basename(dir));
  304. this._getWatchedDir(dir);
  305. var read = function(directory, initialAdd, done) {
  306. // Normalize the directory name on Windows
  307. directory = sysPath.join(directory, '');
  308. if (!wh.hasGlob) {
  309. var throttler = this._throttle('readdir', directory, 1000);
  310. if (!throttler) return;
  311. }
  312. var previous = this._getWatchedDir(wh.path);
  313. var current = [];
  314. readdirp({
  315. root: directory,
  316. entryType: 'all',
  317. fileFilter: wh.filterPath,
  318. directoryFilter: wh.filterDir,
  319. depth: 0,
  320. lstat: true
  321. }).on('data', function(entry) {
  322. var item = entry.path;
  323. var path = sysPath.join(directory, item);
  324. current.push(item);
  325. if (entry.stat.isSymbolicLink() &&
  326. this._handleSymlink(entry, directory, path, item)) return;
  327. // Files that present in current directory snapshot
  328. // but absent in previous are added to watch list and
  329. // emit `add` event.
  330. if (item === target || !target && !previous.has(item)) {
  331. this._readyCount++;
  332. // ensure relativeness of path is preserved in case of watcher reuse
  333. path = sysPath.join(dir, sysPath.relative(dir, path));
  334. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  335. }
  336. }.bind(this)).on('end', function() {
  337. var wasThrottled = throttler ? throttler.clear() : false;
  338. if (done) done();
  339. // Files that absent in current directory snapshot
  340. // but present in previous emit `remove` event
  341. // and are removed from @watched[directory].
  342. previous.children().filter(function(item) {
  343. return item !== directory &&
  344. current.indexOf(item) === -1 &&
  345. // in case of intersecting globs;
  346. // a path may have been filtered out of this readdir, but
  347. // shouldn't be removed because it matches a different glob
  348. (!wh.hasGlob || wh.filterPath({
  349. fullPath: sysPath.resolve(directory, item)
  350. }));
  351. }).forEach(function(item) {
  352. this._remove(directory, item);
  353. }, this);
  354. // one more time for any missed in case changes came in extremely quickly
  355. if (wasThrottled) read(directory, false);
  356. }.bind(this)).on('error', this._handleError.bind(this));
  357. }.bind(this);
  358. var closer;
  359. if (this.options.depth == null || depth <= this.options.depth) {
  360. if (!target) read(dir, initialAdd, callback);
  361. closer = this._watchWithNodeFs(dir, function(dirPath, stats) {
  362. // if current directory is removed, do nothing
  363. if (stats && stats.mtime.getTime() === 0) return;
  364. read(dirPath, false);
  365. });
  366. } else {
  367. callback();
  368. }
  369. return closer;
  370. };
  371. // Private method: Handle added file, directory, or glob pattern.
  372. // Delegates call to _handleFile / _handleDir after checks.
  373. // * path - string, path to file or directory.
  374. // * initialAdd - boolean, was the file added at watch instantiation?
  375. // * depth - int, depth relative to user-supplied path
  376. // * target - string, child path actually targeted for watch
  377. // * callback - function, indicates whether the path was found or not
  378. // Returns nothing
  379. NodeFsHandler.prototype._addToNodeFs =
  380. function(path, initialAdd, priorWh, depth, target, callback) {
  381. if (!callback) callback = Function.prototype;
  382. var ready = this._emitReady;
  383. if (this._isIgnored(path) || this.closed) {
  384. ready();
  385. return callback(null, false);
  386. }
  387. var wh = this._getWatchHelpers(path, depth);
  388. if (!wh.hasGlob && priorWh) {
  389. wh.hasGlob = priorWh.hasGlob;
  390. wh.globFilter = priorWh.globFilter;
  391. wh.filterPath = priorWh.filterPath;
  392. wh.filterDir = priorWh.filterDir;
  393. }
  394. // evaluate what is at the path we're being asked to watch
  395. fs[wh.statMethod](wh.watchPath, function(error, stats) {
  396. if (this._handleError(error)) return callback(null, path);
  397. if (this._isIgnored(wh.watchPath, stats)) {
  398. ready();
  399. return callback(null, false);
  400. }
  401. var initDir = function(dir, target) {
  402. return this._handleDir(dir, stats, initialAdd, depth, target, wh, ready);
  403. }.bind(this);
  404. var closer;
  405. if (stats.isDirectory()) {
  406. closer = initDir(wh.watchPath, target);
  407. } else if (stats.isSymbolicLink()) {
  408. var parent = sysPath.dirname(wh.watchPath);
  409. this._getWatchedDir(parent).add(wh.watchPath);
  410. this._emit('add', wh.watchPath, stats);
  411. closer = initDir(parent, path);
  412. // preserve this symlink's target path
  413. fs.realpath(path, function(error, targetPath) {
  414. this._symlinkPaths[sysPath.resolve(path)] = targetPath;
  415. ready();
  416. }.bind(this));
  417. } else {
  418. closer = this._handleFile(wh.watchPath, stats, initialAdd, ready);
  419. }
  420. if (closer) this._closers[path] = closer;
  421. callback(null, false);
  422. }.bind(this));
  423. };
  424. module.exports = NodeFsHandler;