@@ -6,6 +6,7 @@ const { SafeMap, Symbol, StringPrototypeStartsWith } = primordials;
6
6
const { validateObject } = require ( 'internal/validators' ) ;
7
7
const { kEmptyObject } = require ( 'internal/util' ) ;
8
8
const { ERR_FEATURE_UNAVAILABLE_ON_PLATFORM } = require ( 'internal/errors' ) ;
9
+ const { Dirent, UV_DIRENT_DIR } = require ( 'internal/fs/utils' ) ;
9
10
10
11
const kFSWatchStart = Symbol ( 'kFSWatchStart' ) ;
11
12
@@ -22,35 +23,20 @@ function lazyLoadFsSync() {
22
23
return internalSync ;
23
24
}
24
25
25
- async function traverse ( dir , files = new SafeMap ( ) ) {
26
- const { stat , opendir } = lazyLoadFsPromises ( ) ;
26
+ function traverse ( dir , files = new SafeMap ( ) ) {
27
+ const { readdirSync } = lazyLoadFsSync ( )
27
28
28
- files . set ( dir , await stat ( dir ) ) ;
29
+ const filenames = readdirSync ( dir , { withFileTypes : true } ) ;
29
30
30
- try {
31
- const directory = await opendir ( dir ) ;
31
+ files . set ( dir , new Dirent ( dir , UV_DIRENT_DIR ) ) ;
32
32
33
- for await ( const file of directory ) {
34
- const f = path . join ( dir , file . name ) ;
33
+ for ( let file of filenames ) {
34
+ const f = path . join ( dir , file . name ) ;
35
35
36
- try {
37
- const stats = await stat ( f ) ;
36
+ files . set ( f , file ) ;
38
37
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 ) ;
38
+ if ( file . isDirectory ( ) ) {
39
+ traverse ( f , files ) ;
54
40
}
55
41
}
56
42
@@ -154,7 +140,7 @@ class FSWatcher extends EventEmitter {
154
140
persistent : this . #options. persistent ,
155
141
} , ( statWatcher , previousStatWatcher ) => {
156
142
if ( existingStat && ! existingStat . isDirectory ( ) &&
157
- statWatcher . nlink !== 0 && existingStat . mtime . getTime ( ) === statWatcher . mtime . getTime ( ) ) {
143
+ statWatcher . nlink !== 0 && existingStat . mtime ? .getTime ( ) === statWatcher . mtime ? .getTime ( ) ) {
158
144
return ;
159
145
}
160
146
@@ -180,10 +166,10 @@ class FSWatcher extends EventEmitter {
180
166
/**
181
167
* @param {string | Buffer | URL } filename
182
168
*/
183
- async [ kFSWatchStart ] ( filename ) {
169
+ [ kFSWatchStart ] ( filename ) {
184
170
this . #rootPath = filename ;
185
171
this . #closed = false ;
186
- this . #files = await traverse ( filename ) ;
172
+ this . #files = traverse ( filename ) ;
187
173
188
174
for ( const f of this . #files. keys ( ) ) {
189
175
this . #watchFile( f ) ;
0 commit comments