|
'use strict'; |
|
|
|
const fs = require('fs'); |
|
const sysPath = require('path'); |
|
const { promisify } = require('util'); |
|
|
|
let fsevents; |
|
try { |
|
fsevents = require('fsevents'); |
|
} catch (error) { |
|
if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error); |
|
} |
|
|
|
if (fsevents) { |
|
|
|
const mtch = process.version.match(/v(\d+)\.(\d+)/); |
|
if (mtch && mtch[1] && mtch[2]) { |
|
const maj = Number.parseInt(mtch[1], 10); |
|
const min = Number.parseInt(mtch[2], 10); |
|
if (maj === 8 && min < 16) { |
|
fsevents = undefined; |
|
} |
|
} |
|
} |
|
|
|
const { |
|
EV_ADD, |
|
EV_CHANGE, |
|
EV_ADD_DIR, |
|
EV_UNLINK, |
|
EV_ERROR, |
|
STR_DATA, |
|
STR_END, |
|
FSEVENT_CREATED, |
|
FSEVENT_MODIFIED, |
|
FSEVENT_DELETED, |
|
FSEVENT_MOVED, |
|
|
|
FSEVENT_UNKNOWN, |
|
FSEVENT_FLAG_MUST_SCAN_SUBDIRS, |
|
FSEVENT_TYPE_FILE, |
|
FSEVENT_TYPE_DIRECTORY, |
|
FSEVENT_TYPE_SYMLINK, |
|
|
|
ROOT_GLOBSTAR, |
|
DIR_SUFFIX, |
|
DOT_SLASH, |
|
FUNCTION_TYPE, |
|
EMPTY_FN, |
|
IDENTITY_FN |
|
} = require('./constants'); |
|
|
|
const Depth = (value) => isNaN(value) ? {} : {depth: value}; |
|
|
|
const stat = promisify(fs.stat); |
|
const lstat = promisify(fs.lstat); |
|
const realpath = promisify(fs.realpath); |
|
|
|
const statMethods = { stat, lstat }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const FSEventsWatchers = new Map(); |
|
|
|
|
|
|
|
const consolidateThreshhold = 10; |
|
|
|
const wrongEventFlags = new Set([ |
|
69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912 |
|
]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const createFSEventsInstance = (path, callback) => { |
|
const stop = fsevents.watch(path, callback); |
|
return {stop}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setFSEventsListener(path, realPath, listener, rawEmitter) { |
|
let watchPath = sysPath.extname(realPath) ? sysPath.dirname(realPath) : realPath; |
|
|
|
const parentPath = sysPath.dirname(watchPath); |
|
let cont = FSEventsWatchers.get(watchPath); |
|
|
|
|
|
|
|
|
|
|
|
if (couldConsolidate(parentPath)) { |
|
watchPath = parentPath; |
|
} |
|
|
|
const resolvedPath = sysPath.resolve(path); |
|
const hasSymlink = resolvedPath !== realPath; |
|
|
|
const filteredListener = (fullPath, flags, info) => { |
|
if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath); |
|
if ( |
|
fullPath === resolvedPath || |
|
!fullPath.indexOf(resolvedPath + sysPath.sep) |
|
) listener(fullPath, flags, info); |
|
}; |
|
|
|
|
|
|
|
let watchedParent = false; |
|
for (const watchedPath of FSEventsWatchers.keys()) { |
|
if (realPath.indexOf(sysPath.resolve(watchedPath) + sysPath.sep) === 0) { |
|
watchPath = watchedPath; |
|
cont = FSEventsWatchers.get(watchPath); |
|
watchedParent = true; |
|
break; |
|
} |
|
} |
|
|
|
if (cont || watchedParent) { |
|
cont.listeners.add(filteredListener); |
|
} else { |
|
cont = { |
|
listeners: new Set([filteredListener]), |
|
rawEmitter, |
|
watcher: createFSEventsInstance(watchPath, (fullPath, flags) => { |
|
if (!cont.listeners.size) return; |
|
if (flags & FSEVENT_FLAG_MUST_SCAN_SUBDIRS) return; |
|
const info = fsevents.getInfo(fullPath, flags); |
|
cont.listeners.forEach(list => { |
|
list(fullPath, flags, info); |
|
}); |
|
|
|
cont.rawEmitter(info.event, fullPath, info); |
|
}) |
|
}; |
|
FSEventsWatchers.set(watchPath, cont); |
|
} |
|
|
|
|
|
|
|
return () => { |
|
const lst = cont.listeners; |
|
|
|
lst.delete(filteredListener); |
|
if (!lst.size) { |
|
FSEventsWatchers.delete(watchPath); |
|
if (cont.watcher) return cont.watcher.stop().then(() => { |
|
cont.rawEmitter = cont.watcher = undefined; |
|
Object.freeze(cont); |
|
}); |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
const couldConsolidate = (path) => { |
|
let count = 0; |
|
for (const watchPath of FSEventsWatchers.keys()) { |
|
if (watchPath.indexOf(path) === 0) { |
|
count++; |
|
if (count >= consolidateThreshhold) { |
|
return true; |
|
} |
|
} |
|
} |
|
|
|
return false; |
|
}; |
|
|
|
|
|
const canUse = () => fsevents && FSEventsWatchers.size < 128; |
|
|
|
|
|
const calcDepth = (path, root) => { |
|
let i = 0; |
|
while (!path.indexOf(root) && (path = sysPath.dirname(path)) !== root) i++; |
|
return i; |
|
}; |
|
|
|
|
|
|
|
const sameTypes = (info, stats) => ( |
|
info.type === FSEVENT_TYPE_DIRECTORY && stats.isDirectory() || |
|
info.type === FSEVENT_TYPE_SYMLINK && stats.isSymbolicLink() || |
|
info.type === FSEVENT_TYPE_FILE && stats.isFile() |
|
) |
|
|
|
|
|
|
|
|
|
class FsEventsHandler { |
|
|
|
|
|
|
|
|
|
constructor(fsw) { |
|
this.fsw = fsw; |
|
} |
|
checkIgnored(path, stats) { |
|
const ipaths = this.fsw._ignoredPaths; |
|
if (this.fsw._isIgnored(path, stats)) { |
|
ipaths.add(path); |
|
if (stats && stats.isDirectory()) { |
|
ipaths.add(path + ROOT_GLOBSTAR); |
|
} |
|
return true; |
|
} |
|
|
|
ipaths.delete(path); |
|
ipaths.delete(path + ROOT_GLOBSTAR); |
|
} |
|
|
|
addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts) { |
|
const event = watchedDir.has(item) ? EV_CHANGE : EV_ADD; |
|
this.handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
|
|
async checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts) { |
|
try { |
|
const stats = await stat(path) |
|
if (this.fsw.closed) return; |
|
if (sameTypes(info, stats)) { |
|
this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} else { |
|
this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
} catch (error) { |
|
if (error.code === 'EACCES') { |
|
this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} else { |
|
this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
} |
|
} |
|
|
|
handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts) { |
|
if (this.fsw.closed || this.checkIgnored(path)) return; |
|
|
|
if (event === EV_UNLINK) { |
|
const isDirectory = info.type === FSEVENT_TYPE_DIRECTORY |
|
|
|
if (isDirectory || watchedDir.has(item)) { |
|
this.fsw._remove(parent, item, isDirectory); |
|
} |
|
} else { |
|
if (event === EV_ADD) { |
|
|
|
if (info.type === FSEVENT_TYPE_DIRECTORY) this.fsw._getWatchedDir(path); |
|
|
|
if (info.type === FSEVENT_TYPE_SYMLINK && opts.followSymlinks) { |
|
|
|
const curDepth = opts.depth === undefined ? |
|
undefined : calcDepth(fullPath, realPath) + 1; |
|
return this._addToFsEvents(path, false, true, curDepth); |
|
} |
|
|
|
|
|
|
|
this.fsw._getWatchedDir(parent).add(item); |
|
} |
|
|
|
|
|
|
|
const eventName = info.type === FSEVENT_TYPE_DIRECTORY ? event + DIR_SUFFIX : event; |
|
this.fsw._emit(eventName, path); |
|
if (eventName === EV_ADD_DIR) this._addToFsEvents(path, false, true); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_watchWithFsEvents(watchPath, realPath, transform, globFilter) { |
|
if (this.fsw.closed || this.fsw._isIgnored(watchPath)) return; |
|
const opts = this.fsw.options; |
|
const watchCallback = async (fullPath, flags, info) => { |
|
if (this.fsw.closed) return; |
|
if ( |
|
opts.depth !== undefined && |
|
calcDepth(fullPath, realPath) > opts.depth |
|
) return; |
|
const path = transform(sysPath.join( |
|
watchPath, sysPath.relative(watchPath, fullPath) |
|
)); |
|
if (globFilter && !globFilter(path)) return; |
|
|
|
const parent = sysPath.dirname(path); |
|
const item = sysPath.basename(path); |
|
const watchedDir = this.fsw._getWatchedDir( |
|
info.type === FSEVENT_TYPE_DIRECTORY ? path : parent |
|
); |
|
|
|
|
|
if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) { |
|
if (typeof opts.ignored === FUNCTION_TYPE) { |
|
let stats; |
|
try { |
|
stats = await stat(path); |
|
} catch (error) {} |
|
if (this.fsw.closed) return; |
|
if (this.checkIgnored(path, stats)) return; |
|
if (sameTypes(info, stats)) { |
|
this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} else { |
|
this.handleEvent(EV_UNLINK, path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
} else { |
|
this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
} else { |
|
switch (info.event) { |
|
case FSEVENT_CREATED: |
|
case FSEVENT_MODIFIED: |
|
return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
case FSEVENT_DELETED: |
|
case FSEVENT_MOVED: |
|
return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts); |
|
} |
|
} |
|
}; |
|
|
|
const closer = setFSEventsListener( |
|
watchPath, |
|
realPath, |
|
watchCallback, |
|
this.fsw._emitRaw |
|
); |
|
|
|
this.fsw._emitReady(); |
|
return closer; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) { |
|
|
|
if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return; |
|
|
|
this.fsw._symlinkPaths.set(fullPath, true); |
|
this.fsw._incrReadyCount(); |
|
|
|
try { |
|
const linkTarget = await realpath(linkPath); |
|
if (this.fsw.closed) return; |
|
if (this.fsw._isIgnored(linkTarget)) { |
|
return this.fsw._emitReady(); |
|
} |
|
|
|
this.fsw._incrReadyCount(); |
|
|
|
|
|
|
|
this._addToFsEvents(linkTarget || linkPath, (path) => { |
|
let aliasedPath = linkPath; |
|
if (linkTarget && linkTarget !== DOT_SLASH) { |
|
aliasedPath = path.replace(linkTarget, linkPath); |
|
} else if (path !== DOT_SLASH) { |
|
aliasedPath = sysPath.join(linkPath, path); |
|
} |
|
return transform(aliasedPath); |
|
}, false, curDepth); |
|
} catch(error) { |
|
if (this.fsw._handleError(error)) { |
|
return this.fsw._emitReady(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
emitAdd(newPath, stats, processPath, opts, forceAdd) { |
|
const pp = processPath(newPath); |
|
const isDir = stats.isDirectory(); |
|
const dirObj = this.fsw._getWatchedDir(sysPath.dirname(pp)); |
|
const base = sysPath.basename(pp); |
|
|
|
|
|
if (isDir) this.fsw._getWatchedDir(pp); |
|
if (dirObj.has(base)) return; |
|
dirObj.add(base); |
|
|
|
if (!opts.ignoreInitial || forceAdd === true) { |
|
this.fsw._emit(isDir ? EV_ADD_DIR : EV_ADD, pp, stats); |
|
} |
|
} |
|
|
|
initWatch(realPath, path, wh, processPath) { |
|
if (this.fsw.closed) return; |
|
const closer = this._watchWithFsEvents( |
|
wh.watchPath, |
|
sysPath.resolve(realPath || wh.watchPath), |
|
processPath, |
|
wh.globFilter |
|
); |
|
this.fsw._addPathCloser(path, closer); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _addToFsEvents(path, transform, forceAdd, priorDepth) { |
|
if (this.fsw.closed) { |
|
return; |
|
} |
|
const opts = this.fsw.options; |
|
const processPath = typeof transform === FUNCTION_TYPE ? transform : IDENTITY_FN; |
|
|
|
const wh = this.fsw._getWatchHelpers(path); |
|
|
|
|
|
try { |
|
const stats = await statMethods[wh.statMethod](wh.watchPath); |
|
if (this.fsw.closed) return; |
|
if (this.fsw._isIgnored(wh.watchPath, stats)) { |
|
throw null; |
|
} |
|
if (stats.isDirectory()) { |
|
|
|
if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd); |
|
|
|
|
|
if (priorDepth && priorDepth > opts.depth) return; |
|
|
|
|
|
this.fsw._readdirp(wh.watchPath, { |
|
fileFilter: entry => wh.filterPath(entry), |
|
directoryFilter: entry => wh.filterDir(entry), |
|
...Depth(opts.depth - (priorDepth || 0)) |
|
}).on(STR_DATA, (entry) => { |
|
|
|
if (this.fsw.closed) { |
|
return; |
|
} |
|
if (entry.stats.isDirectory() && !wh.filterPath(entry)) return; |
|
|
|
const joinedPath = sysPath.join(wh.watchPath, entry.path); |
|
const {fullPath} = entry; |
|
|
|
if (wh.followSymlinks && entry.stats.isSymbolicLink()) { |
|
|
|
|
|
const curDepth = opts.depth === undefined ? |
|
undefined : calcDepth(joinedPath, sysPath.resolve(wh.watchPath)) + 1; |
|
|
|
this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth); |
|
} else { |
|
this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd); |
|
} |
|
}).on(EV_ERROR, EMPTY_FN).on(STR_END, () => { |
|
this.fsw._emitReady(); |
|
}); |
|
} else { |
|
this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd); |
|
this.fsw._emitReady(); |
|
} |
|
} catch (error) { |
|
if (!error || this.fsw._handleError(error)) { |
|
|
|
this.fsw._emitReady(); |
|
this.fsw._emitReady(); |
|
} |
|
} |
|
|
|
if (opts.persistent && forceAdd !== true) { |
|
if (typeof transform === FUNCTION_TYPE) { |
|
|
|
this.initWatch(undefined, path, wh, processPath); |
|
} else { |
|
let realPath; |
|
try { |
|
realPath = await realpath(wh.watchPath); |
|
} catch (e) {} |
|
this.initWatch(realPath, path, wh, processPath); |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
module.exports = FsEventsHandler; |
|
module.exports.canUse = canUse; |
|
|