|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { EventEmitter } = require('events'); |
| 4 | +const path = require('path'); |
| 5 | +const { SafeMap, Symbol, StringPrototypeStartsWith } = primordials; |
| 6 | +const { validateObject } = require('internal/validators'); |
| 7 | +const { kEmptyObject } = require('internal/util'); |
| 8 | +const { ERR_FEATURE_UNAVAILABLE_ON_PLATFORM } = require('internal/errors'); |
| 9 | + |
| 10 | +const kFSWatchStart = Symbol('kFSWatchStart'); |
| 11 | + |
| 12 | +let internalSync; |
| 13 | +let internalPromises; |
| 14 | + |
| 15 | +function lazyLoadFsPromises() { |
| 16 | + internalPromises ??= require('fs/promises'); |
| 17 | + return internalPromises; |
| 18 | +} |
| 19 | + |
| 20 | +function lazyLoadFsSync() { |
| 21 | + internalSync ??= require('fs'); |
| 22 | + return internalSync; |
| 23 | +} |
| 24 | + |
| 25 | +async function traverse(dir, files = new SafeMap()) { |
| 26 | + const { stat, opendir } = lazyLoadFsPromises(); |
| 27 | + |
| 28 | + files.set(dir, await stat(dir)); |
| 29 | + |
| 30 | + try { |
| 31 | + const directory = await opendir(dir); |
| 32 | + |
| 33 | + for await (const file of directory) { |
| 34 | + const f = path.join(dir, file.name); |
| 35 | + |
| 36 | + try { |
| 37 | + const stats = await stat(f); |
| 38 | + |
| 39 | + files.set(f, stats); |
| 40 | + |
| 41 | + if (stats.isDirectory()) { |
| 42 | + await traverse(f, files); |
| 43 | + } |
| 44 | + } catch (error) { |
| 45 | + if (error.code !== 'ENOENT' || error.code !== 'EPERM') { |
| 46 | + this.emit('error', error); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + } catch (error) { |
| 52 | + if (error.code !== 'EACCES') { |
| 53 | + this.emit('error', error); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return files; |
| 58 | +} |
| 59 | + |
| 60 | +class FSWatcher extends EventEmitter { |
| 61 | + #options = null; |
| 62 | + #closed = false; |
| 63 | + #files = new SafeMap(); |
| 64 | + |
| 65 | + /** |
| 66 | + * @param {{ |
| 67 | + * persistent?: boolean; |
| 68 | + * recursive?: boolean; |
| 69 | + * encoding?: string; |
| 70 | + * signal?: AbortSignal; |
| 71 | + * }} [options] |
| 72 | + */ |
| 73 | + constructor(options = kEmptyObject) { |
| 74 | + super(); |
| 75 | + |
| 76 | + validateObject(options, 'options'); |
| 77 | + this.#options = options; |
| 78 | + } |
| 79 | + |
| 80 | + close() { |
| 81 | + const { unwatchFile } = lazyLoadFsSync(); |
| 82 | + this.#closed = true; |
| 83 | + |
| 84 | + for (const file of this.#files.keys()) { |
| 85 | + unwatchFile(file); |
| 86 | + } |
| 87 | + |
| 88 | + this.emit('close'); |
| 89 | + } |
| 90 | + |
| 91 | + #unwatchFolder(file) { |
| 92 | + const { unwatchFile } = lazyLoadFsSync(); |
| 93 | + |
| 94 | + for (const filename in this.#files) { |
| 95 | + if (StringPrototypeStartsWith(filename, file)) { |
| 96 | + unwatchFile(filename); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + async #watchFolder(folder) { |
| 102 | + const { opendir, stat } = lazyLoadFsPromises(); |
| 103 | + |
| 104 | + try { |
| 105 | + const files = await opendir(folder); |
| 106 | + |
| 107 | + for await (const file of files) { |
| 108 | + const f = path.join(folder, file.name); |
| 109 | + |
| 110 | + if (this.#closed) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (!this.#files.has(f)) { |
| 115 | + const fileStats = await stat(f); |
| 116 | + this.#files.set(f, fileStats); |
| 117 | + this.emit('change', 'rename', f); |
| 118 | + this.#watchFile(f); |
| 119 | + } |
| 120 | + } |
| 121 | + } catch (error) { |
| 122 | + this.emit('error', error); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param {string} file |
| 128 | + */ |
| 129 | + #watchFile(file) { |
| 130 | + const { watchFile } = lazyLoadFsSync(); |
| 131 | + |
| 132 | + if (this.#closed) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + const existingStat = this.#files.get(file); |
| 137 | + |
| 138 | + watchFile(file, { |
| 139 | + persistent: this.#options.persistent, |
| 140 | + }, (statWatcher, previousStatWatcher) => { |
| 141 | + if (existingStat && !existingStat.isDirectory() && |
| 142 | + statWatcher.nlink !== 0 && existingStat.mtime.getTime() === statWatcher.mtime.getTime()) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + this.#files.set(file, statWatcher); |
| 147 | + |
| 148 | + if (statWatcher.isDirectory()) { |
| 149 | + this.#watchFolder(file); |
| 150 | + } else if (statWatcher.birthtimeMs === 0 && previousStatWatcher.birthtimeMs !== 0) { |
| 151 | + // The file is now deleted |
| 152 | + this.#files.delete(file); |
| 153 | + this.emit('change', 'rename', file); |
| 154 | + |
| 155 | + if (statWatcher.isDirectory()) { |
| 156 | + this.#unwatchFolder(file); |
| 157 | + } |
| 158 | + } else { |
| 159 | + this.emit('change', 'change', file); |
| 160 | + } |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param {string | Buffer | URL} filename |
| 166 | + */ |
| 167 | + async [kFSWatchStart](filename) { |
| 168 | + this.#closed = false; |
| 169 | + this.#files = await traverse(filename); |
| 170 | + |
| 171 | + this.#watchFile(filename); |
| 172 | + |
| 173 | + for (const f in this.#files) { |
| 174 | + this.#watchFile(f); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + ref() { |
| 179 | + // This is kept to have the same API with FSWatcher |
| 180 | + throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('ref'); |
| 181 | + } |
| 182 | + |
| 183 | + unref() { |
| 184 | + // This is kept to have the same API with FSWatcher |
| 185 | + throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('unref'); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +module.exports = { |
| 190 | + FSWatcher, |
| 191 | + kFSWatchStart, |
| 192 | +}; |
0 commit comments