1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const sysPath = require('path');
|
5 | const { promisify } = require('util');
|
6 | const isBinaryPath = require('is-binary-path');
|
7 | const {
|
8 | isWindows,
|
9 | isLinux,
|
10 | EMPTY_FN,
|
11 | EMPTY_STR,
|
12 | KEY_LISTENERS,
|
13 | KEY_ERR,
|
14 | KEY_RAW,
|
15 | HANDLER_KEYS,
|
16 | EV_CHANGE,
|
17 | EV_ADD,
|
18 | EV_ADD_DIR,
|
19 | EV_ERROR,
|
20 | STR_DATA,
|
21 | STR_END,
|
22 | BRACE_START,
|
23 | STAR
|
24 | } = require('./constants');
|
25 |
|
26 | const THROTTLE_MODE_WATCH = 'watch';
|
27 |
|
28 | const open = promisify(fs.open);
|
29 | const stat = promisify(fs.stat);
|
30 | const lstat = promisify(fs.lstat);
|
31 | const close = promisify(fs.close);
|
32 | const fsrealpath = promisify(fs.realpath);
|
33 |
|
34 | const statMethods = { lstat, stat };
|
35 |
|
36 |
|
37 | const foreach = (val, fn) => {
|
38 | if (val instanceof Set) {
|
39 | val.forEach(fn);
|
40 | } else {
|
41 | fn(val);
|
42 | }
|
43 | };
|
44 |
|
45 | const addAndConvert = (main, prop, item) => {
|
46 | let container = main[prop];
|
47 | if (!(container instanceof Set)) {
|
48 | main[prop] = container = new Set([container]);
|
49 | }
|
50 | container.add(item);
|
51 | };
|
52 |
|
53 | const clearItem = cont => key => {
|
54 | const set = cont[key];
|
55 | if (set instanceof Set) {
|
56 | set.clear();
|
57 | } else {
|
58 | delete cont[key];
|
59 | }
|
60 | };
|
61 |
|
62 | const delFromSet = (main, prop, item) => {
|
63 | const container = main[prop];
|
64 | if (container instanceof Set) {
|
65 | container.delete(item);
|
66 | } else if (container === item) {
|
67 | delete main[prop];
|
68 | }
|
69 | };
|
70 |
|
71 | const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | const FsWatchInstances = new Map();
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
|
106 | const handleEvent = (rawEvent, evPath) => {
|
107 | listener(path);
|
108 | emitRaw(rawEvent, evPath, {watchedPath: path});
|
109 |
|
110 |
|
111 |
|
112 | if (evPath && path !== evPath) {
|
113 | fsWatchBroadcast(
|
114 | sysPath.resolve(path, evPath), KEY_LISTENERS, sysPath.join(path, evPath)
|
115 | );
|
116 | }
|
117 | };
|
118 | try {
|
119 | return fs.watch(path, options, handleEvent);
|
120 | } catch (error) {
|
121 | errHandler(error);
|
122 | }
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
|
134 | const cont = FsWatchInstances.get(fullPath);
|
135 | if (!cont) return;
|
136 | foreach(cont[type], (listener) => {
|
137 | listener(val1, val2, val3);
|
138 | });
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | const setFsWatchListener = (path, fullPath, options, handlers) => {
|
150 | const {listener, errHandler, rawEmitter} = handlers;
|
151 | let cont = FsWatchInstances.get(fullPath);
|
152 |
|
153 |
|
154 | let watcher;
|
155 | if (!options.persistent) {
|
156 | watcher = createFsWatchInstance(
|
157 | path, options, listener, errHandler, rawEmitter
|
158 | );
|
159 | return watcher.close.bind(watcher);
|
160 | }
|
161 | if (cont) {
|
162 | addAndConvert(cont, KEY_LISTENERS, listener);
|
163 | addAndConvert(cont, KEY_ERR, errHandler);
|
164 | addAndConvert(cont, KEY_RAW, rawEmitter);
|
165 | } else {
|
166 | watcher = createFsWatchInstance(
|
167 | path,
|
168 | options,
|
169 | fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
|
170 | errHandler,
|
171 | fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
|
172 | );
|
173 | if (!watcher) return;
|
174 | watcher.on(EV_ERROR, async (error) => {
|
175 | const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
|
176 | cont.watcherUnusable = true;
|
177 |
|
178 | if (isWindows && error.code === 'EPERM') {
|
179 | try {
|
180 | const fd = await open(path, 'r');
|
181 | await close(fd);
|
182 | broadcastErr(error);
|
183 | } catch (err) {}
|
184 | } else {
|
185 | broadcastErr(error);
|
186 | }
|
187 | });
|
188 | cont = {
|
189 | listeners: listener,
|
190 | errHandlers: errHandler,
|
191 | rawEmitters: rawEmitter,
|
192 | watcher
|
193 | };
|
194 | FsWatchInstances.set(fullPath, cont);
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | return () => {
|
201 | delFromSet(cont, KEY_LISTENERS, listener);
|
202 | delFromSet(cont, KEY_ERR, errHandler);
|
203 | delFromSet(cont, KEY_RAW, rawEmitter);
|
204 | if (isEmptySet(cont.listeners)) {
|
205 |
|
206 |
|
207 | cont.watcher.close();
|
208 |
|
209 | FsWatchInstances.delete(fullPath);
|
210 | HANDLER_KEYS.forEach(clearItem(cont));
|
211 | cont.watcher = undefined;
|
212 | Object.freeze(cont);
|
213 | }
|
214 | };
|
215 | };
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | const FsWatchFileInstances = new Map();
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | const setFsWatchFileListener = (path, fullPath, options, handlers) => {
|
233 | const {listener, rawEmitter} = handlers;
|
234 | let cont = FsWatchFileInstances.get(fullPath);
|
235 |
|
236 |
|
237 | let listeners = new Set();
|
238 | let rawEmitters = new Set();
|
239 |
|
240 | const copts = cont && cont.options;
|
241 | if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | listeners = cont.listeners;
|
247 | rawEmitters = cont.rawEmitters;
|
248 | fs.unwatchFile(fullPath);
|
249 | cont = undefined;
|
250 | }
|
251 |
|
252 |
|
253 |
|
254 | if (cont) {
|
255 | addAndConvert(cont, KEY_LISTENERS, listener);
|
256 | addAndConvert(cont, KEY_RAW, rawEmitter);
|
257 | } else {
|
258 |
|
259 |
|
260 |
|
261 | cont = {
|
262 | listeners: listener,
|
263 | rawEmitters: rawEmitter,
|
264 | options,
|
265 | watcher: fs.watchFile(fullPath, options, (curr, prev) => {
|
266 | foreach(cont.rawEmitters, (rawEmitter) => {
|
267 | rawEmitter(EV_CHANGE, fullPath, {curr, prev});
|
268 | });
|
269 | const currmtime = curr.mtimeMs;
|
270 | if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
|
271 | foreach(cont.listeners, (listener) => listener(path, curr));
|
272 | }
|
273 | })
|
274 | };
|
275 | FsWatchFileInstances.set(fullPath, cont);
|
276 | }
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | return () => {
|
282 | delFromSet(cont, KEY_LISTENERS, listener);
|
283 | delFromSet(cont, KEY_RAW, rawEmitter);
|
284 | if (isEmptySet(cont.listeners)) {
|
285 | FsWatchFileInstances.delete(fullPath);
|
286 | fs.unwatchFile(fullPath);
|
287 | cont.options = cont.watcher = undefined;
|
288 | Object.freeze(cont);
|
289 | }
|
290 | };
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | class NodeFsHandler {
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | constructor(fsW) {
|
302 | this.fsw = fsW;
|
303 | this._boundHandleError = (error) => fsW._handleError(error);
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | _watchWithNodeFs(path, listener) {
|
313 | const opts = this.fsw.options;
|
314 | const directory = sysPath.dirname(path);
|
315 | const basename = sysPath.basename(path);
|
316 | const parent = this.fsw._getWatchedDir(directory);
|
317 | parent.add(basename);
|
318 | const absolutePath = sysPath.resolve(path);
|
319 | const options = {persistent: opts.persistent};
|
320 | if (!listener) listener = EMPTY_FN;
|
321 |
|
322 | let closer;
|
323 | if (opts.usePolling) {
|
324 | options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
|
325 | opts.binaryInterval : opts.interval;
|
326 | closer = setFsWatchFileListener(path, absolutePath, options, {
|
327 | listener,
|
328 | rawEmitter: this.fsw._emitRaw
|
329 | });
|
330 | } else {
|
331 | closer = setFsWatchListener(path, absolutePath, options, {
|
332 | listener,
|
333 | errHandler: this._boundHandleError,
|
334 | rawEmitter: this.fsw._emitRaw
|
335 | });
|
336 | }
|
337 | return closer;
|
338 | }
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | _handleFile(file, stats, initialAdd) {
|
348 | if (this.fsw.closed) {
|
349 | return;
|
350 | }
|
351 | const dirname = sysPath.dirname(file);
|
352 | const basename = sysPath.basename(file);
|
353 | const parent = this.fsw._getWatchedDir(dirname);
|
354 |
|
355 | let prevStats = stats;
|
356 |
|
357 |
|
358 | if (parent.has(basename)) return;
|
359 |
|
360 | const listener = async (path, newStats) => {
|
361 | if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
|
362 | if (!newStats || newStats.mtimeMs === 0) {
|
363 | try {
|
364 | const newStats = await stat(file);
|
365 | if (this.fsw.closed) return;
|
366 |
|
367 | const at = newStats.atimeMs;
|
368 | const mt = newStats.mtimeMs;
|
369 | if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
370 | this.fsw._emit(EV_CHANGE, file, newStats);
|
371 | }
|
372 | if (isLinux && prevStats.ino !== newStats.ino) {
|
373 | this.fsw._closeFile(path)
|
374 | prevStats = newStats;
|
375 | this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
|
376 | } else {
|
377 | prevStats = newStats;
|
378 | }
|
379 | } catch (error) {
|
380 |
|
381 | this.fsw._remove(dirname, basename);
|
382 | }
|
383 |
|
384 | } else if (parent.has(basename)) {
|
385 |
|
386 | const at = newStats.atimeMs;
|
387 | const mt = newStats.mtimeMs;
|
388 | if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
389 | this.fsw._emit(EV_CHANGE, file, newStats);
|
390 | }
|
391 | prevStats = newStats;
|
392 | }
|
393 | }
|
394 |
|
395 | const closer = this._watchWithNodeFs(file, listener);
|
396 |
|
397 |
|
398 | if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
|
399 | if (!this.fsw._throttle(EV_ADD, file, 0)) return;
|
400 | this.fsw._emit(EV_ADD, file, stats);
|
401 | }
|
402 |
|
403 | return closer;
|
404 | }
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | async _handleSymlink(entry, directory, path, item) {
|
415 | if (this.fsw.closed) {
|
416 | return;
|
417 | }
|
418 | const full = entry.fullPath;
|
419 | const dir = this.fsw._getWatchedDir(directory);
|
420 |
|
421 | if (!this.fsw.options.followSymlinks) {
|
422 |
|
423 | this.fsw._incrReadyCount();
|
424 | const linkPath = await fsrealpath(path);
|
425 | if (this.fsw.closed) return;
|
426 | if (dir.has(item)) {
|
427 | if (this.fsw._symlinkPaths.get(full) !== linkPath) {
|
428 | this.fsw._symlinkPaths.set(full, linkPath);
|
429 | this.fsw._emit(EV_CHANGE, path, entry.stats);
|
430 | }
|
431 | } else {
|
432 | dir.add(item);
|
433 | this.fsw._symlinkPaths.set(full, linkPath);
|
434 | this.fsw._emit(EV_ADD, path, entry.stats);
|
435 | }
|
436 | this.fsw._emitReady();
|
437 | return true;
|
438 | }
|
439 |
|
440 |
|
441 | if (this.fsw._symlinkPaths.has(full)) {
|
442 | return true;
|
443 | }
|
444 |
|
445 | this.fsw._symlinkPaths.set(full, true);
|
446 | }
|
447 |
|
448 | _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
|
449 |
|
450 | directory = sysPath.join(directory, EMPTY_STR);
|
451 |
|
452 | if (!wh.hasGlob) {
|
453 | throttler = this.fsw._throttle('readdir', directory, 1000);
|
454 | if (!throttler) return;
|
455 | }
|
456 |
|
457 | const previous = this.fsw._getWatchedDir(wh.path);
|
458 | const current = new Set();
|
459 |
|
460 | let stream = this.fsw._readdirp(directory, {
|
461 | fileFilter: entry => wh.filterPath(entry),
|
462 | directoryFilter: entry => wh.filterDir(entry),
|
463 | depth: 0
|
464 | }).on(STR_DATA, async (entry) => {
|
465 | if (this.fsw.closed) {
|
466 | stream = undefined;
|
467 | return;
|
468 | }
|
469 | const item = entry.path;
|
470 | let path = sysPath.join(directory, item);
|
471 | current.add(item);
|
472 |
|
473 | if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
|
474 | return;
|
475 | }
|
476 |
|
477 | if (this.fsw.closed) {
|
478 | stream = undefined;
|
479 | return;
|
480 | }
|
481 |
|
482 |
|
483 |
|
484 | if (item === target || !target && !previous.has(item)) {
|
485 | this.fsw._incrReadyCount();
|
486 |
|
487 |
|
488 | path = sysPath.join(dir, sysPath.relative(dir, path));
|
489 |
|
490 | this._addToNodeFs(path, initialAdd, wh, depth + 1);
|
491 | }
|
492 | }).on(EV_ERROR, this._boundHandleError);
|
493 |
|
494 | return new Promise(resolve =>
|
495 | stream.once(STR_END, () => {
|
496 | if (this.fsw.closed) {
|
497 | stream = undefined;
|
498 | return;
|
499 | }
|
500 | const wasThrottled = throttler ? throttler.clear() : false;
|
501 |
|
502 | resolve();
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | previous.getChildren().filter((item) => {
|
508 | return item !== directory &&
|
509 | !current.has(item) &&
|
510 |
|
511 |
|
512 |
|
513 | (!wh.hasGlob || wh.filterPath({
|
514 | fullPath: sysPath.resolve(directory, item)
|
515 | }));
|
516 | }).forEach((item) => {
|
517 | this.fsw._remove(directory, item);
|
518 | });
|
519 |
|
520 | stream = undefined;
|
521 |
|
522 |
|
523 | if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
|
524 | })
|
525 | );
|
526 | }
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
|
540 | const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
|
541 | const tracked = parentDir.has(sysPath.basename(dir));
|
542 | if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
|
543 | if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR, dir, stats);
|
544 | }
|
545 |
|
546 |
|
547 | parentDir.add(sysPath.basename(dir));
|
548 | this.fsw._getWatchedDir(dir);
|
549 | let throttler;
|
550 | let closer;
|
551 |
|
552 | const oDepth = this.fsw.options.depth;
|
553 | if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
|
554 | if (!target) {
|
555 | await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
|
556 | if (this.fsw.closed) return;
|
557 | }
|
558 |
|
559 | closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
|
560 |
|
561 | if (stats && stats.mtimeMs === 0) return;
|
562 |
|
563 | this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
|
564 | });
|
565 | }
|
566 | return closer;
|
567 | }
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 | async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
|
580 | const ready = this.fsw._emitReady;
|
581 | if (this.fsw._isIgnored(path) || this.fsw.closed) {
|
582 | ready();
|
583 | return false;
|
584 | }
|
585 |
|
586 | const wh = this.fsw._getWatchHelpers(path, depth);
|
587 | if (!wh.hasGlob && priorWh) {
|
588 | wh.hasGlob = priorWh.hasGlob;
|
589 | wh.globFilter = priorWh.globFilter;
|
590 | wh.filterPath = entry => priorWh.filterPath(entry);
|
591 | wh.filterDir = entry => priorWh.filterDir(entry);
|
592 | }
|
593 |
|
594 |
|
595 | try {
|
596 | const stats = await statMethods[wh.statMethod](wh.watchPath);
|
597 | if (this.fsw.closed) return;
|
598 | if (this.fsw._isIgnored(wh.watchPath, stats)) {
|
599 | ready();
|
600 | return false;
|
601 | }
|
602 |
|
603 | const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START);
|
604 | let closer;
|
605 | if (stats.isDirectory()) {
|
606 | const absPath = sysPath.resolve(path);
|
607 | const targetPath = follow ? await fsrealpath(path) : path;
|
608 | if (this.fsw.closed) return;
|
609 | closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
|
610 | if (this.fsw.closed) return;
|
611 |
|
612 | if (absPath !== targetPath && targetPath !== undefined) {
|
613 | this.fsw._symlinkPaths.set(absPath, targetPath);
|
614 | }
|
615 | } else if (stats.isSymbolicLink()) {
|
616 | const targetPath = follow ? await fsrealpath(path) : path;
|
617 | if (this.fsw.closed) return;
|
618 | const parent = sysPath.dirname(wh.watchPath);
|
619 | this.fsw._getWatchedDir(parent).add(wh.watchPath);
|
620 | this.fsw._emit(EV_ADD, wh.watchPath, stats);
|
621 | closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
|
622 | if (this.fsw.closed) return;
|
623 |
|
624 |
|
625 | if (targetPath !== undefined) {
|
626 | this.fsw._symlinkPaths.set(sysPath.resolve(path), targetPath);
|
627 | }
|
628 | } else {
|
629 | closer = this._handleFile(wh.watchPath, stats, initialAdd);
|
630 | }
|
631 | ready();
|
632 |
|
633 | this.fsw._addPathCloser(path, closer);
|
634 | return false;
|
635 |
|
636 | } catch (error) {
|
637 | if (this.fsw._handleError(error)) {
|
638 | ready();
|
639 | return path;
|
640 | }
|
641 | }
|
642 | }
|
643 |
|
644 | }
|
645 |
|
646 | module.exports = NodeFsHandler;
|