/
linux_watcher.js
193 lines (152 loc) Β· 4.31 KB
/
linux_watcher.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
const { EventEmitter } = require('events');
const path = require('path');
const { SafeMap, Symbol, StringPrototypeStartsWith } = primordials;
const { validateObject } = require('internal/validators');
const { kEmptyObject } = require('internal/util');
const { ERR_FEATURE_UNAVAILABLE_ON_PLATFORM } = require('internal/errors');
const { Dirent, UV_DIRENT_DIR } = require('internal/fs/utils');
const kFSWatchStart = Symbol('kFSWatchStart');
let internalSync;
let internalPromises;
function lazyLoadFsPromises() {
internalPromises ??= require('fs/promises');
return internalPromises;
}
function lazyLoadFsSync() {
internalSync ??= require('fs');
return internalSync;
}
function traverse(dir, files = new SafeMap()) {
const { readdirSync } = lazyLoadFsSync()
const filenames = readdirSync(dir, { withFileTypes: true });
files.set(dir, new Dirent(dir, UV_DIRENT_DIR));
for (let file of filenames) {
const f = path.join(dir, file.name);
files.set(f, file);
if (file.isDirectory()) {
traverse(f, files);
}
}
return files;
}
class FSWatcher extends EventEmitter {
#options = null;
#closed = false;
#files = new SafeMap();
#rootPath = path.resolve();
/**
* @param {{
* persistent?: boolean;
* recursive?: boolean;
* encoding?: string;
* signal?: AbortSignal;
* }} [options]
*/
constructor(options = kEmptyObject) {
super();
validateObject(options, 'options');
this.#options = options;
}
close() {
const { unwatchFile } = lazyLoadFsSync();
this.#closed = true;
for (const file of this.#files.keys()) {
unwatchFile(file);
}
this.emit('close');
}
#getPath(file) {
if (file === this.#rootPath) {
return this.#rootPath;
}
return path.relative(this.#rootPath, file);
}
#unwatchFolder(file) {
const { unwatchFile } = lazyLoadFsSync();
for (const filename of this.#files.keys()) {
if (StringPrototypeStartsWith(filename, file)) {
unwatchFile(filename);
}
}
}
async #watchFolder(folder) {
const { opendir, stat } = lazyLoadFsPromises();
try {
const files = await opendir(folder);
for await (const file of files) {
const f = path.join(folder, file.name);
if (this.#closed) {
break;
}
if (!this.#files.has(f)) {
const fileStats = await stat(f);
this.#files.set(f, fileStats);
this.emit('change', 'rename', this.#getPath(f));
if (fileStats.isDirectory()) {
await this.#watchFolder(f);
} else {
this.#watchFile(f);
}
}
}
} catch (error) {
this.emit('error', error);
}
}
/**
* @param {string} file
*/
#watchFile(file) {
const { watchFile } = lazyLoadFsSync();
if (this.#closed) {
return;
}
const existingStat = this.#files.get(file);
watchFile(file, {
persistent: this.#options.persistent,
}, (statWatcher, previousStatWatcher) => {
if (existingStat && !existingStat.isDirectory() &&
statWatcher.nlink !== 0 && existingStat.mtime?.getTime() === statWatcher.mtime?.getTime()) {
return;
}
this.#files.set(file, statWatcher);
if (statWatcher.birthtimeMs === 0 && previousStatWatcher.birthtimeMs !== 0) {
// The file is now deleted
this.#files.delete(file);
this.emit('change', 'rename', this.#getPath(file));
if (statWatcher.isDirectory()) {
this.#unwatchFolder(file);
}
} else if (statWatcher.isDirectory()) {
this.#watchFolder(file);
this.emit('change', 'change', this.#getPath(file));
} else {
this.emit('change', 'change', this.#getPath(file));
}
});
}
/**
* @param {string | Buffer | URL} filename
*/
[kFSWatchStart](filename) {
this.#rootPath = filename;
this.#closed = false;
this.#files = traverse(filename);
for (const f of this.#files.keys()) {
this.#watchFile(f);
}
}
ref() {
// This is kept to have the same API with FSWatcher
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('ref');
}
unref() {
// This is kept to have the same API with FSWatcher
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('unref');
}
}
module.exports = {
FSWatcher,
kFSWatchStart,
};