All files / lib/internal/watch_mode files_watcher.js

89.47% Statements 119/133
83.78% Branches 31/37
91.66% Functions 11/12
89.47% Lines 119/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 13413x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 39x 20x 20x 19x 39x 5x     5x 19x 19x 39x 21x 21x               21x 21x 19x 19x 19x 21x 21x 19x 3x 3x 19x 1x 1x 15x 15x 15x 19x 21x 21x 1x 1x 21x 21x 39x 20x 20x 19x 19x 19x 19x 39x     39x 21x 21x 39x   39x 39x 39x 39x 39x 39x 39x 21x 23x     23x 43x 43x 26x 26x 43x 11x 11x 43x 1x 1x 23x 23x 21x 12x 12x 21x 20x 20x 20x 20x 21x 13x 13x  
'use strict';
 
const {
  SafeMap,
  SafeSet,
  StringPrototypeStartsWith,
} = primordials;
 
const { validateNumber, validateOneOf } = require('internal/validators');
const { kEmptyObject } = require('internal/util');
const { TIMEOUT_MAX } = require('internal/timers');
 
const EventEmitter = require('events');
const { watch } = require('fs');
const { fileURLToPath } = require('url');
const { resolve, dirname } = require('path');
const { setTimeout } = require('timers');
 
 
const supportsRecursiveWatching = process.platform === 'win32' ||
  process.platform === 'darwin';
 
class FilesWatcher extends EventEmitter {
  #watchers = new SafeMap();
  #filteredFiles = new SafeSet();
  #throttling = new SafeSet();
  #throttle;
  #mode;
 
  constructor({ throttle = 500, mode = 'filter' } = kEmptyObject) {
    super();
 
    validateNumber(throttle, 'options.throttle', 0, TIMEOUT_MAX);
    validateOneOf(mode, 'options.mode', ['filter', 'all']);
    this.#throttle = throttle;
    this.#mode = mode;
  }
 
  #isPathWatched(path) {
    if (this.#watchers.has(path)) {
      return true;
    }
 
    for (const { 0: watchedPath, 1: watcher } of this.#watchers.entries()) {
      if (watcher.recursive && StringPrototypeStartsWith(path, watchedPath)) {
        return true;
      }
    }
 
    return false;
  }
 
  #removeWatchedChildren(path) {
    for (const { 0: watchedPath, 1: watcher } of this.#watchers.entries()) {
      if (path !== watchedPath && StringPrototypeStartsWith(watchedPath, path)) {
        this.#unwatch(watcher);
        this.#watchers.delete(watchedPath);
      }
    }
  }
 
  #unwatch(watcher) {
    watcher.handle.removeAllListeners();
    watcher.handle.close();
  }
 
  #onChange(trigger) {
    if (this.#throttling.has(trigger)) {
      return;
    }
    if (this.#mode === 'filter' && !this.#filteredFiles.has(trigger)) {
      return;
    }
    this.#throttling.add(trigger);
    this.emit('changed');
    setTimeout(() => this.#throttling.delete(trigger), this.#throttle).unref();
  }
 
  get watchedPaths() {
    return [...this.#watchers.keys()];
  }
 
  watchPath(path, recursive = true) {
    if (this.#isPathWatched(path)) {
      return;
    }
    const watcher = watch(path, { recursive });
    watcher.on('change', (eventType, fileName) => this
      .#onChange(recursive ? resolve(path, fileName) : path));
    this.#watchers.set(path, { handle: watcher, recursive });
    if (recursive) {
      this.#removeWatchedChildren(path);
    }
  }
 
  filterFile(file) {
    if (supportsRecursiveWatching) {
      this.watchPath(dirname(file));
    } else {
      // Having multiple FSWatcher's seems to be slower
      // than a single recursive FSWatcher
      this.watchPath(file, false);
    }
    this.#filteredFiles.add(file);
  }
  watchChildProcessModules(child) {
    if (this.#mode !== 'filter') {
      return;
    }
    child.on('message', (message) => {
      try {
        if (message['watch:require']) {
          this.filterFile(message['watch:require']);
        }
        if (message['watch:import']) {
          this.filterFile(fileURLToPath(message['watch:import']));
        }
      } catch {
        // Failed watching file. ignore
      }
    });
  }
  clearFileFilters() {
    this.#filteredFiles.clear();
  }
  clear() {
    this.#watchers.forEach(this.#unwatch);
    this.#watchers.clear();
    this.#filteredFiles.clear();
  }
}
 
module.exports = { FilesWatcher };