123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- 'use strict';
- const fs = require('fs');
- const sysPath = require('path');
- const { promisify } = require('util');
- const isBinaryPath = require('is-binary-path');
- const {
- isWindows,
- isLinux,
- EMPTY_FN,
- EMPTY_STR,
- KEY_LISTENERS,
- KEY_ERR,
- KEY_RAW,
- HANDLER_KEYS,
- EV_CHANGE,
- EV_ADD,
- EV_ADD_DIR,
- EV_ERROR,
- STR_DATA,
- STR_END,
- BRACE_START,
- STAR
- } = require('./constants');
- const THROTTLE_MODE_WATCH = 'watch';
- const open = promisify(fs.open);
- const stat = promisify(fs.stat);
- const lstat = promisify(fs.lstat);
- const close = promisify(fs.close);
- const fsrealpath = promisify(fs.realpath);
- const statMethods = { lstat, stat };
- const foreach = (val, fn) => {
- if (val instanceof Set) {
- val.forEach(fn);
- } else {
- fn(val);
- }
- };
- const addAndConvert = (main, prop, item) => {
- let container = main[prop];
- if (!(container instanceof Set)) {
- main[prop] = container = new Set([container]);
- }
- container.add(item);
- };
- const clearItem = cont => key => {
- const set = cont[key];
- if (set instanceof Set) {
- set.clear();
- } else {
- delete cont[key];
- }
- };
- const delFromSet = (main, prop, item) => {
- const container = main[prop];
- if (container instanceof Set) {
- container.delete(item);
- } else if (container === item) {
- delete main[prop];
- }
- };
- const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
- const FsWatchInstances = new Map();
- function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
- const handleEvent = (rawEvent, evPath) => {
- listener(path);
- emitRaw(rawEvent, evPath, {watchedPath: path});
-
-
- if (evPath && path !== evPath) {
- fsWatchBroadcast(
- sysPath.resolve(path, evPath), KEY_LISTENERS, sysPath.join(path, evPath)
- );
- }
- };
- try {
- return fs.watch(path, options, handleEvent);
- } catch (error) {
- errHandler(error);
- }
- }
- const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
- const cont = FsWatchInstances.get(fullPath);
- if (!cont) return;
- foreach(cont[type], (listener) => {
- listener(val1, val2, val3);
- });
- };
- const setFsWatchListener = (path, fullPath, options, handlers) => {
- const {listener, errHandler, rawEmitter} = handlers;
- let cont = FsWatchInstances.get(fullPath);
-
- let watcher;
- if (!options.persistent) {
- watcher = createFsWatchInstance(
- path, options, listener, errHandler, rawEmitter
- );
- return watcher.close.bind(watcher);
- }
- if (cont) {
- addAndConvert(cont, KEY_LISTENERS, listener);
- addAndConvert(cont, KEY_ERR, errHandler);
- addAndConvert(cont, KEY_RAW, rawEmitter);
- } else {
- watcher = createFsWatchInstance(
- path,
- options,
- fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
- errHandler,
- fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
- );
- if (!watcher) return;
- watcher.on(EV_ERROR, async (error) => {
- const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
- cont.watcherUnusable = true;
-
- if (isWindows && error.code === 'EPERM') {
- try {
- const fd = await open(path, 'r');
- await close(fd);
- broadcastErr(error);
- } catch (err) {}
- } else {
- broadcastErr(error);
- }
- });
- cont = {
- listeners: listener,
- errHandlers: errHandler,
- rawEmitters: rawEmitter,
- watcher
- };
- FsWatchInstances.set(fullPath, cont);
- }
-
-
-
- return () => {
- delFromSet(cont, KEY_LISTENERS, listener);
- delFromSet(cont, KEY_ERR, errHandler);
- delFromSet(cont, KEY_RAW, rawEmitter);
- if (isEmptySet(cont.listeners)) {
-
-
- cont.watcher.close();
-
- FsWatchInstances.delete(fullPath);
- HANDLER_KEYS.forEach(clearItem(cont));
- cont.watcher = undefined;
- Object.freeze(cont);
- }
- };
- };
- const FsWatchFileInstances = new Map();
- const setFsWatchFileListener = (path, fullPath, options, handlers) => {
- const {listener, rawEmitter} = handlers;
- let cont = FsWatchFileInstances.get(fullPath);
-
- let listeners = new Set();
- let rawEmitters = new Set();
- const copts = cont && cont.options;
- if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
-
-
-
-
- listeners = cont.listeners;
- rawEmitters = cont.rawEmitters;
- fs.unwatchFile(fullPath);
- cont = undefined;
- }
-
- if (cont) {
- addAndConvert(cont, KEY_LISTENERS, listener);
- addAndConvert(cont, KEY_RAW, rawEmitter);
- } else {
-
-
-
- cont = {
- listeners: listener,
- rawEmitters: rawEmitter,
- options,
- watcher: fs.watchFile(fullPath, options, (curr, prev) => {
- foreach(cont.rawEmitters, (rawEmitter) => {
- rawEmitter(EV_CHANGE, fullPath, {curr, prev});
- });
- const currmtime = curr.mtimeMs;
- if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
- foreach(cont.listeners, (listener) => listener(path, curr));
- }
- })
- };
- FsWatchFileInstances.set(fullPath, cont);
- }
-
-
-
- return () => {
- delFromSet(cont, KEY_LISTENERS, listener);
- delFromSet(cont, KEY_RAW, rawEmitter);
- if (isEmptySet(cont.listeners)) {
- FsWatchFileInstances.delete(fullPath);
- fs.unwatchFile(fullPath);
- cont.options = cont.watcher = undefined;
- Object.freeze(cont);
- }
- };
- };
- class NodeFsHandler {
- constructor(fsW) {
- this.fsw = fsW;
- this._boundHandleError = (error) => fsW._handleError(error);
- }
- _watchWithNodeFs(path, listener) {
- const opts = this.fsw.options;
- const directory = sysPath.dirname(path);
- const basename = sysPath.basename(path);
- const parent = this.fsw._getWatchedDir(directory);
- parent.add(basename);
- const absolutePath = sysPath.resolve(path);
- const options = {persistent: opts.persistent};
- if (!listener) listener = EMPTY_FN;
- let closer;
- if (opts.usePolling) {
- options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
- opts.binaryInterval : opts.interval;
- closer = setFsWatchFileListener(path, absolutePath, options, {
- listener,
- rawEmitter: this.fsw._emitRaw
- });
- } else {
- closer = setFsWatchListener(path, absolutePath, options, {
- listener,
- errHandler: this._boundHandleError,
- rawEmitter: this.fsw._emitRaw
- });
- }
- return closer;
- }
- _handleFile(file, stats, initialAdd) {
- if (this.fsw.closed) {
- return;
- }
- const dirname = sysPath.dirname(file);
- const basename = sysPath.basename(file);
- const parent = this.fsw._getWatchedDir(dirname);
-
- let prevStats = stats;
-
- if (parent.has(basename)) return;
- const listener = async (path, newStats) => {
- if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
- if (!newStats || newStats.mtimeMs === 0) {
- try {
- const newStats = await stat(file);
- if (this.fsw.closed) return;
-
- const at = newStats.atimeMs;
- const mt = newStats.mtimeMs;
- if (!at || at <= mt || mt !== prevStats.mtimeMs) {
- this.fsw._emit(EV_CHANGE, file, newStats);
- }
- if (isLinux && prevStats.ino !== newStats.ino) {
- this.fsw._closeFile(path)
- prevStats = newStats;
- this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
- } else {
- prevStats = newStats;
- }
- } catch (error) {
-
- this.fsw._remove(dirname, basename);
- }
-
- } else if (parent.has(basename)) {
-
- const at = newStats.atimeMs;
- const mt = newStats.mtimeMs;
- if (!at || at <= mt || mt !== prevStats.mtimeMs) {
- this.fsw._emit(EV_CHANGE, file, newStats);
- }
- prevStats = newStats;
- }
- }
-
- const closer = this._watchWithNodeFs(file, listener);
-
- if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
- if (!this.fsw._throttle(EV_ADD, file, 0)) return;
- this.fsw._emit(EV_ADD, file, stats);
- }
- return closer;
- }
- async _handleSymlink(entry, directory, path, item) {
- if (this.fsw.closed) {
- return;
- }
- const full = entry.fullPath;
- const dir = this.fsw._getWatchedDir(directory);
- if (!this.fsw.options.followSymlinks) {
-
- this.fsw._incrReadyCount();
- const linkPath = await fsrealpath(path);
- if (this.fsw.closed) return;
- if (dir.has(item)) {
- if (this.fsw._symlinkPaths.get(full) !== linkPath) {
- this.fsw._symlinkPaths.set(full, linkPath);
- this.fsw._emit(EV_CHANGE, path, entry.stats);
- }
- } else {
- dir.add(item);
- this.fsw._symlinkPaths.set(full, linkPath);
- this.fsw._emit(EV_ADD, path, entry.stats);
- }
- this.fsw._emitReady();
- return true;
- }
-
- if (this.fsw._symlinkPaths.has(full)) {
- return true;
- }
- this.fsw._symlinkPaths.set(full, true);
- }
- _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
-
- directory = sysPath.join(directory, EMPTY_STR);
- if (!wh.hasGlob) {
- throttler = this.fsw._throttle('readdir', directory, 1000);
- if (!throttler) return;
- }
- const previous = this.fsw._getWatchedDir(wh.path);
- const current = new Set();
- let stream = this.fsw._readdirp(directory, {
- fileFilter: entry => wh.filterPath(entry),
- directoryFilter: entry => wh.filterDir(entry),
- depth: 0
- }).on(STR_DATA, async (entry) => {
- if (this.fsw.closed) {
- stream = undefined;
- return;
- }
- const item = entry.path;
- let path = sysPath.join(directory, item);
- current.add(item);
- if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
- return;
- }
- if (this.fsw.closed) {
- stream = undefined;
- return;
- }
-
-
-
- if (item === target || !target && !previous.has(item)) {
- this.fsw._incrReadyCount();
-
- path = sysPath.join(dir, sysPath.relative(dir, path));
- this._addToNodeFs(path, initialAdd, wh, depth + 1);
- }
- }).on(EV_ERROR, this._boundHandleError);
- return new Promise(resolve =>
- stream.once(STR_END, () => {
- if (this.fsw.closed) {
- stream = undefined;
- return;
- }
- const wasThrottled = throttler ? throttler.clear() : false;
- resolve();
-
-
-
- previous.getChildren().filter((item) => {
- return item !== directory &&
- !current.has(item) &&
-
-
-
- (!wh.hasGlob || wh.filterPath({
- fullPath: sysPath.resolve(directory, item)
- }));
- }).forEach((item) => {
- this.fsw._remove(directory, item);
- });
- stream = undefined;
-
- if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
- })
- );
- }
- async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
- const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
- const tracked = parentDir.has(sysPath.basename(dir));
- if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
- if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR, dir, stats);
- }
-
- parentDir.add(sysPath.basename(dir));
- this.fsw._getWatchedDir(dir);
- let throttler;
- let closer;
- const oDepth = this.fsw.options.depth;
- if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
- if (!target) {
- await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
- if (this.fsw.closed) return;
- }
- closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
-
- if (stats && stats.mtimeMs === 0) return;
- this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
- });
- }
- return closer;
- }
- async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
- const ready = this.fsw._emitReady;
- if (this.fsw._isIgnored(path) || this.fsw.closed) {
- ready();
- return false;
- }
- const wh = this.fsw._getWatchHelpers(path, depth);
- if (!wh.hasGlob && priorWh) {
- wh.hasGlob = priorWh.hasGlob;
- wh.globFilter = priorWh.globFilter;
- wh.filterPath = entry => priorWh.filterPath(entry);
- wh.filterDir = entry => priorWh.filterDir(entry);
- }
-
- try {
- const stats = await statMethods[wh.statMethod](wh.watchPath);
- if (this.fsw.closed) return;
- if (this.fsw._isIgnored(wh.watchPath, stats)) {
- ready();
- return false;
- }
- const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START);
- let closer;
- if (stats.isDirectory()) {
- const targetPath = follow ? await fsrealpath(path) : path;
- if (this.fsw.closed) return;
- closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
- if (this.fsw.closed) return;
-
- if (path !== targetPath && targetPath !== undefined) {
- this.fsw._symlinkPaths.set(targetPath, true);
- }
- } else if (stats.isSymbolicLink()) {
- const targetPath = follow ? await fsrealpath(path) : path;
- if (this.fsw.closed) return;
- const parent = sysPath.dirname(wh.watchPath);
- this.fsw._getWatchedDir(parent).add(wh.watchPath);
- this.fsw._emit(EV_ADD, wh.watchPath, stats);
- closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
- if (this.fsw.closed) return;
-
- if (targetPath !== undefined) {
- this.fsw._symlinkPaths.set(sysPath.resolve(path), targetPath);
- }
- } else {
- closer = this._handleFile(wh.watchPath, stats, initialAdd);
- }
- ready();
- this.fsw._addPathCloser(path, closer);
- return false;
- } catch (error) {
- if (this.fsw._handleError(error)) {
- ready();
- return path;
- }
- }
- }
- }
- module.exports = NodeFsHandler;
|