Layout von Websiten mit Bootstrap und Foundation
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.

nodefs-handler.js 19KB

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