|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { EventEmitter } = require('events'); |
| 4 | +const path = require('path'); |
| 5 | +const { SafeMap, Symbol, ObjectKeys } = primordials; |
| 6 | +const { validateObject } = require('internal/validators'); |
| 7 | + |
| 8 | +const kFSWatchStart = Symbol('kFSWatchStart'); |
| 9 | + |
| 10 | +let internalSync; |
| 11 | +let internalPromises; |
| 12 | + |
| 13 | +function lazyLoadFsPromises() { |
| 14 | + internalPromises ??= require('fs/promises'); |
| 15 | + return internalPromises; |
| 16 | +} |
| 17 | + |
| 18 | +function lazyLoadFsSync() { |
| 19 | + internalSync ??= require('fs'); |
| 20 | + return internalSync; |
| 21 | +} |
| 22 | + |
| 23 | +async function traverse(dir, files = new SafeMap()) { |
| 24 | + const { stat, opendir } = lazyLoadFsPromises(); |
| 25 | + |
| 26 | + files.set(dir, await stat(dir)); |
| 27 | + |
| 28 | + try { |
| 29 | + const directory = await opendir(dir); |
| 30 | + |
| 31 | + for await (const file of directory) { |
| 32 | + const f = path.join(dir, file); |
| 33 | + |
| 34 | + try { |
| 35 | + const stats = await stat(f); |
| 36 | + |
| 37 | + files.set(f, stats); |
| 38 | + |
| 39 | + if (stats.isDirectory()) { |
| 40 | + await traverse(f, files); |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + if (error.code !== 'ENOENT' || error.code !== 'EPERM') { |
| 44 | + throw error; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + } catch (error) { |
| 50 | + if (error.code !== 'EACCES') { |
| 51 | + throw error; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return files; |
| 56 | +} |
| 57 | + |
| 58 | +class FSWatcher extends EventEmitter { |
| 59 | + #options = null; |
| 60 | + #closed = false; |
| 61 | + #files = new SafeMap(); |
| 62 | + |
| 63 | + /** |
| 64 | + * @param {{ |
| 65 | + * persistent?: boolean; |
| 66 | + * recursive?: boolean; |
| 67 | + * encoding?: string; |
| 68 | + * signal?: AbortSignal; |
| 69 | + * }} [options] |
| 70 | + */ |
| 71 | + constructor(options) { |
| 72 | + super(); |
| 73 | + |
| 74 | + validateObject(options, 'options'); |
| 75 | + this.#options = options; |
| 76 | + } |
| 77 | + |
| 78 | + async close() { |
| 79 | + const { unwatchFile } = lazyLoadFsPromises(); |
| 80 | + this.#closed = true; |
| 81 | + |
| 82 | + for (const file of ObjectKeys(this.#files)) { |
| 83 | + await unwatchFile(file); |
| 84 | + } |
| 85 | + |
| 86 | + this.emit('close'); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param {string} file |
| 91 | + */ |
| 92 | + #watchFile(file) { |
| 93 | + const { opendir } = lazyLoadFsPromises(); |
| 94 | + const { stat, watchFile } = lazyLoadFsSync(); |
| 95 | + |
| 96 | + watchFile(file, this.#options, (event, payload) => { |
| 97 | + const existingStat = this.#files.get(file); |
| 98 | + |
| 99 | + if (existingStat && !existingStat.isDirectory() && |
| 100 | + event.nlink !== 0 && existingStat.mtime.getTime() === event.mtime.getTime()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + this.#files.set(file, event); |
| 105 | + |
| 106 | + if (!event.isDirectory()) { |
| 107 | + this.emit(event, payload); |
| 108 | + } else { |
| 109 | + opendir(file) |
| 110 | + .then(async (files) => { |
| 111 | + for await (const subfile of files) { |
| 112 | + const f = path.join(file, subfile); |
| 113 | + |
| 114 | + if (!this.#files.has(f)) { |
| 115 | + stat(f, (error, stat) => { |
| 116 | + if (error) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + this.#files.set(f, stat); |
| 121 | + this.#watchFile(f); |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param {string | Buffer | URL} filename |
| 132 | + */ |
| 133 | + async [kFSWatchStart](filename) { |
| 134 | + this.#closed = false; |
| 135 | + this.#files = await traverse(filename); |
| 136 | + |
| 137 | + this.#watchFile(filename); |
| 138 | + |
| 139 | + for (const f in this.#files) { |
| 140 | + this.#watchFile(f); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +module.exports = { |
| 146 | + FSWatcher, |
| 147 | + kFSWatchStart, |
| 148 | +}; |
0 commit comments