Front end of the Slack clone application.

nodefs-handler.js 16KB

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