Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.

fsevents-handler.js 15KB

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