WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
"use strict";
 
const { EventEmitter } = require("events");
const path = require("path");
const fs = require("graceful-fs");
 
const watchEventSource = require("./watchEventSource");
 
/** @typedef {import("./index").IgnoredFunction} IgnoredFunction */
/** @typedef {import("./index").EventType} EventType */
/** @typedef {import("./index").TimeInfoEntries} TimeInfoEntries */
/** @typedef {import("./index").Entry} Entry */
/** @typedef {import("./index").ExistenceOnlyTimeEntry} ExistenceOnlyTimeEntry */
/** @typedef {import("./index").OnlySafeTimeEntry} OnlySafeTimeEntry */
/** @typedef {import("./index").EventMap} EventMap */
/** @typedef {import("./getWatcherManager").WatcherManager} WatcherManager */
/** @typedef {import("./watchEventSource").Watcher} EventSourceWatcher */
 
/** @type {ExistenceOnlyTimeEntry} */
const EXISTANCE_ONLY_TIME_ENTRY = Object.freeze({});
 
let FS_ACCURACY = 2000;
 
const IS_OSX = require("os").platform() === "darwin";
const IS_WIN = require("os").platform() === "win32";
 
const { WATCHPACK_POLLING } = process.env;
const FORCE_POLLING =
    // @ts-expect-error avoid additional checks
    `${+WATCHPACK_POLLING}` === WATCHPACK_POLLING
        ? +WATCHPACK_POLLING
        : Boolean(WATCHPACK_POLLING) && WATCHPACK_POLLING !== "false";
 
/**
 * @param {string} str string
 * @returns {string} lower cased string
 */
function withoutCase(str) {
    return str.toLowerCase();
}
 
/**
 * @param {number} times times
 * @param {() => void} callback callback
 * @returns {() => void} result
 */
function needCalls(times, callback) {
    return function needCallsCallback() {
        if (--times === 0) {
            return callback();
        }
    };
}
 
/**
 * @param {Entry} entry entry
 */
function fixupEntryAccuracy(entry) {
    if (entry.accuracy > FS_ACCURACY) {
        entry.safeTime = entry.safeTime - entry.accuracy + FS_ACCURACY;
        entry.accuracy = FS_ACCURACY;
    }
}
 
/**
 * @param {number=} mtime mtime
 */
function ensureFsAccuracy(mtime) {
    if (!mtime) return;
    if (FS_ACCURACY > 1 && mtime % 1 !== 0) FS_ACCURACY = 1;
    else if (FS_ACCURACY > 10 && mtime % 10 !== 0) FS_ACCURACY = 10;
    else if (FS_ACCURACY > 100 && mtime % 100 !== 0) FS_ACCURACY = 100;
    else if (FS_ACCURACY > 1000 && mtime % 1000 !== 0) FS_ACCURACY = 1000;
}
 
/**
 * @typedef {object} FileWatcherEvents
 * @property {(type: EventType) => void} initial-missing initial missing event
 * @property {(mtime: number, type: EventType, initial: boolean) => void} change change event
 * @property {(type: EventType) => void} remove remove event
 * @property {() => void} closed closed event
 */
 
/**
 * @typedef {object} DirectoryWatcherEvents
 * @property {(type: EventType) => void} initial-missing initial missing event
 * @property {((file: string, mtime: number, type: EventType, initial: boolean) => void)} change change event
 * @property {(type: EventType) => void} remove remove event
 * @property {() => void} closed closed event
 */
 
/**
 * @template {EventMap} T
 * @extends {EventEmitter<{ [K in keyof T]: Parameters<T[K]> }>}
 */
class Watcher extends EventEmitter {
    /**
     * @param {DirectoryWatcher} directoryWatcher a directory watcher
     * @param {string} target a target to watch
     * @param {number=} startTime start time
     */
    constructor(directoryWatcher, target, startTime) {
        super();
        this.directoryWatcher = directoryWatcher;
        this.path = target;
        this.startTime = startTime && +startTime;
    }
 
    /**
     * @param {number} mtime mtime
     * @param {boolean} initial true when initial, otherwise false
     * @returns {boolean} true of start time less than mtile, otherwise false
     */
    checkStartTime(mtime, initial) {
        const { startTime } = this;
        if (typeof startTime !== "number") return !initial;
        return startTime <= mtime;
    }
 
    close() {
        // @ts-expect-error bad typing in EventEmitter
        this.emit("closed");
    }
}
 
/** @typedef {Set<string>} InitialScanRemoved */
 
/**
 * @typedef {object} WatchpackEvents
 * @property {(target: string, mtime: string, type: EventType, initial: boolean) => void} change change event
 * @property {() => void} closed closed event
 */
 
/**
 * @typedef {object} DirectoryWatcherOptions
 * @property {boolean=} followSymlinks true when need to resolve symlinks and watch symlink and real file, otherwise false
 * @property {IgnoredFunction=} ignored ignore some files from watching (glob pattern or regexp)
 * @property {number | boolean=} poll true when need to enable polling mode for watching, otherwise false
 */
 
/**
 * @extends {EventEmitter<{ [K in keyof WatchpackEvents]: Parameters<WatchpackEvents[K]> }>}
 */
class DirectoryWatcher extends EventEmitter {
    /**
     * @param {WatcherManager} watcherManager a watcher manager
     * @param {string} directoryPath directory path
     * @param {DirectoryWatcherOptions=} options options
     */
    constructor(watcherManager, directoryPath, options = {}) {
        super();
        if (FORCE_POLLING) {
            options.poll = FORCE_POLLING;
        }
        this.watcherManager = watcherManager;
        this.options = options;
        this.path = directoryPath;
        // safeTime is the point in time after which reading is safe to be unchanged
        // timestamp is a value that should be compared with another timestamp (mtime)
        /** @type {Map<string, Entry>} */
        this.files = new Map();
        /** @type {Map<string, number>} */
        this.filesWithoutCase = new Map();
        /** @type {Map<string, Watcher<DirectoryWatcherEvents> | boolean>} */
        this.directories = new Map();
        this.lastWatchEvent = 0;
        this.initialScan = true;
        this.ignored = options.ignored || (() => false);
        this.nestedWatching = false;
        /** @type {number | false} */
        this.polledWatching =
            typeof options.poll === "number"
                ? options.poll
                : options.poll
                    ? 5007
                    : false;
        /** @type {undefined | NodeJS.Timeout} */
        this.timeout = undefined;
        /** @type {null | InitialScanRemoved} */
        this.initialScanRemoved = new Set();
        /** @type {undefined | number} */
        this.initialScanFinished = undefined;
        /** @type {Map<string, Set<Watcher<DirectoryWatcherEvents> | Watcher<FileWatcherEvents>>>} */
        this.watchers = new Map();
        /** @type {Watcher<FileWatcherEvents> | null} */
        this.parentWatcher = null;
        this.refs = 0;
        /** @type {Map<string, boolean>} */
        this._activeEvents = new Map();
        this.closed = false;
        this.scanning = false;
        this.scanAgain = false;
        this.scanAgainInitial = false;
 
        this.createWatcher();
        this.doScan(true);
    }
 
    createWatcher() {
        try {
            if (this.polledWatching) {
                /** @type {EventSourceWatcher} */
                (this.watcher) = /** @type {EventSourceWatcher} */ ({
                    close: () => {
                        if (this.timeout) {
                            clearTimeout(this.timeout);
                            this.timeout = undefined;
                        }
                    },
                });
            } else {
                if (IS_OSX) {
                    this.watchInParentDirectory();
                }
                this.watcher =
                    /** @type {EventSourceWatcher} */
                    (watchEventSource.watch(this.path));
                this.watcher.on("change", this.onWatchEvent.bind(this));
                this.watcher.on("error", this.onWatcherError.bind(this));
            }
        } catch (err) {
            this.onWatcherError(err);
        }
    }
 
    /**
     * @template {(watcher: Watcher<EventMap>) => void} T
     * @param {string} path path
     * @param {T} fn function
     */
    forEachWatcher(path, fn) {
        const watchers = this.watchers.get(withoutCase(path));
        if (watchers !== undefined) {
            for (const w of watchers) {
                fn(w);
            }
        }
    }
 
    /**
     * @param {string} itemPath an item path
     * @param {boolean} initial true when initial, otherwise false
     * @param {EventType} type even type
     */
    setMissing(itemPath, initial, type) {
        if (this.initialScan) {
            /** @type {InitialScanRemoved} */
            (this.initialScanRemoved).add(itemPath);
        }
 
        const oldDirectory = this.directories.get(itemPath);
        if (oldDirectory) {
            if (this.nestedWatching) {
                /** @type {Watcher<DirectoryWatcherEvents>} */
                (oldDirectory).close();
            }
            this.directories.delete(itemPath);
            this.forEachWatcher(itemPath, (w) => w.emit("remove", type));
            if (!initial) {
                this.forEachWatcher(this.path, (w) =>
                    w.emit("change", itemPath, null, type, initial),
                );
            }
        }
 
        const oldFile = this.files.get(itemPath);
        if (oldFile) {
            this.files.delete(itemPath);
            const key = withoutCase(itemPath);
            const count = /** @type {number} */ (this.filesWithoutCase.get(key)) - 1;
            if (count <= 0) {
                this.filesWithoutCase.delete(key);
                this.forEachWatcher(itemPath, (w) => w.emit("remove", type));
            } else {
                this.filesWithoutCase.set(key, count);
            }
 
            if (!initial) {
                this.forEachWatcher(this.path, (w) =>
                    w.emit("change", itemPath, null, type, initial),
                );
            }
        }
    }
 
    /**
     * @param {string} target a target to set file time
     * @param {number} mtime mtime
     * @param {boolean} initial true when initial, otherwise false
     * @param {boolean} ignoreWhenEqual true to ignore when equal, otherwise false
     * @param {EventType} type type
     */
    setFileTime(target, mtime, initial, ignoreWhenEqual, type) {
        const now = Date.now();
 
        if (this.ignored(target)) return;
 
        const old = this.files.get(target);
 
        let safeTime;
        let accuracy;
        if (initial) {
            safeTime = Math.min(now, mtime) + FS_ACCURACY;
            accuracy = FS_ACCURACY;
        } else {
            safeTime = now;
            accuracy = 0;
 
            if (old && old.timestamp === mtime && mtime + FS_ACCURACY < now) {
                // We are sure that mtime is untouched
                // This can be caused by some file attribute change
                // e. g. when access time has been changed
                // but the file content is untouched
                return;
            }
        }
 
        if (ignoreWhenEqual && old && old.timestamp === mtime) return;
 
        this.files.set(target, {
            safeTime,
            accuracy,
            timestamp: mtime,
        });
 
        if (!old) {
            const key = withoutCase(target);
            const count = this.filesWithoutCase.get(key);
            this.filesWithoutCase.set(key, (count || 0) + 1);
            if (count !== undefined) {
                // There is already a file with case-insensitive-equal name
                // On a case-insensitive filesystem we may miss the renaming
                // when only casing is changed.
                // To be sure that our information is correct
                // we trigger a rescan here
                this.doScan(false);
            }
 
            this.forEachWatcher(target, (w) => {
                if (!initial || w.checkStartTime(safeTime, initial)) {
                    w.emit("change", mtime, type);
                }
            });
        } else if (!initial) {
            this.forEachWatcher(target, (w) => w.emit("change", mtime, type));
        }
        this.forEachWatcher(this.path, (w) => {
            if (!initial || w.checkStartTime(safeTime, initial)) {
                w.emit("change", target, safeTime, type, initial);
            }
        });
    }
 
    /**
     * @param {string} directoryPath directory path
     * @param {number} birthtime birthtime
     * @param {boolean} initial true when initial, otherwise false
     * @param {EventType} type even type
     */
    setDirectory(directoryPath, birthtime, initial, type) {
        if (this.ignored(directoryPath)) return;
        if (directoryPath === this.path) {
            if (!initial) {
                this.forEachWatcher(this.path, (w) =>
                    w.emit("change", directoryPath, birthtime, type, initial),
                );
            }
        } else {
            const old = this.directories.get(directoryPath);
            if (!old) {
                const now = Date.now();
 
                if (this.nestedWatching) {
                    this.createNestedWatcher(directoryPath);
                } else {
                    this.directories.set(directoryPath, true);
                }
 
                const safeTime = initial ? Math.min(now, birthtime) + FS_ACCURACY : now;
 
                this.forEachWatcher(directoryPath, (w) => {
                    if (!initial || w.checkStartTime(safeTime, false)) {
                        w.emit("change", birthtime, type);
                    }
                });
                this.forEachWatcher(this.path, (w) => {
                    if (!initial || w.checkStartTime(safeTime, initial)) {
                        w.emit("change", directoryPath, safeTime, type, initial);
                    }
                });
            }
        }
    }
 
    /**
     * @param {string} directoryPath directory path
     */
    createNestedWatcher(directoryPath) {
        const watcher = this.watcherManager.watchDirectory(directoryPath, 1);
        watcher.on("change", (target, mtime, type, initial) => {
            this.forEachWatcher(this.path, (w) => {
                if (!initial || w.checkStartTime(mtime, initial)) {
                    w.emit("change", target, mtime, type, initial);
                }
            });
        });
        this.directories.set(directoryPath, watcher);
    }
 
    /**
     * @param {boolean} flag true when nested, otherwise false
     */
    setNestedWatching(flag) {
        if (this.nestedWatching !== Boolean(flag)) {
            this.nestedWatching = Boolean(flag);
            if (this.nestedWatching) {
                for (const directory of this.directories.keys()) {
                    this.createNestedWatcher(directory);
                }
            } else {
                for (const [directory, watcher] of this.directories) {
                    /** @type {Watcher<DirectoryWatcherEvents>} */
                    (watcher).close();
                    this.directories.set(directory, true);
                }
            }
        }
    }
 
    /**
     * @param {string} target a target to watch
     * @param {number=} startTime start time
     * @returns {Watcher<DirectoryWatcherEvents> | Watcher<FileWatcherEvents>} watcher
     */
    watch(target, startTime) {
        const key = withoutCase(target);
        let watchers = this.watchers.get(key);
        if (watchers === undefined) {
            watchers = new Set();
            this.watchers.set(key, watchers);
        }
        this.refs++;
        const watcher =
            /** @type {Watcher<DirectoryWatcherEvents> | Watcher<FileWatcherEvents>} */
            (new Watcher(this, target, startTime));
        watcher.on("closed", () => {
            if (--this.refs <= 0) {
                this.close();
                return;
            }
            watchers.delete(watcher);
            if (watchers.size === 0) {
                this.watchers.delete(key);
                if (this.path === target) this.setNestedWatching(false);
            }
        });
        watchers.add(watcher);
        let safeTime;
        if (target === this.path) {
            this.setNestedWatching(true);
            safeTime = this.lastWatchEvent;
            for (const entry of this.files.values()) {
                fixupEntryAccuracy(entry);
                safeTime = Math.max(safeTime, entry.safeTime);
            }
        } else {
            const entry = this.files.get(target);
            if (entry) {
                fixupEntryAccuracy(entry);
                safeTime = entry.safeTime;
            } else {
                safeTime = 0;
            }
        }
        if (safeTime) {
            if (startTime && safeTime >= startTime) {
                process.nextTick(() => {
                    if (this.closed) return;
                    if (target === this.path) {
                        /** @type {Watcher<DirectoryWatcherEvents>} */
                        (watcher).emit(
                            "change",
                            target,
                            safeTime,
                            "watch (outdated on attach)",
                            true,
                        );
                    } else {
                        /** @type {Watcher<FileWatcherEvents>} */
                        (watcher).emit(
                            "change",
                            safeTime,
                            "watch (outdated on attach)",
                            true,
                        );
                    }
                });
            }
        } else if (this.initialScan) {
            if (
                /** @type {InitialScanRemoved} */
                (this.initialScanRemoved).has(target)
            ) {
                process.nextTick(() => {
                    if (this.closed) return;
                    watcher.emit("remove");
                });
            }
        } else if (
            target !== this.path &&
            !this.directories.has(target) &&
            watcher.checkStartTime(
                /** @type {number} */
                (this.initialScanFinished),
                false,
            )
        ) {
            process.nextTick(() => {
                if (this.closed) return;
                watcher.emit("initial-missing", "watch (missing on attach)");
            });
        }
        return watcher;
    }
 
    /**
     * @param {EventType} eventType event type
     * @param {string=} filename filename
     */
    onWatchEvent(eventType, filename) {
        if (this.closed) return;
        if (!filename) {
            // In some cases no filename is provided
            // This seem to happen on windows
            // So some event happened but we don't know which file is affected
            // We have to do a full scan of the directory
            this.doScan(false);
            return;
        }
 
        const target = path.join(this.path, filename);
        if (this.ignored(target)) return;
 
        if (this._activeEvents.get(filename) === undefined) {
            this._activeEvents.set(filename, false);
            const checkStats = () => {
                if (this.closed) return;
                this._activeEvents.set(filename, false);
                fs.lstat(target, (err, stats) => {
                    if (this.closed) return;
                    if (this._activeEvents.get(filename) === true) {
                        process.nextTick(checkStats);
                        return;
                    }
                    this._activeEvents.delete(filename);
                    // ENOENT happens when the file/directory doesn't exist
                    // EPERM happens when the containing directory doesn't exist
                    if (err) {
                        if (
                            err.code !== "ENOENT" &&
                            err.code !== "EPERM" &&
                            err.code !== "EBUSY"
                        ) {
                            this.onStatsError(err);
                        } else if (
                            filename === path.basename(this.path) && // This may indicate that the directory itself was removed
                            !fs.existsSync(this.path)
                        ) {
                            this.onDirectoryRemoved("stat failed");
                        }
                    }
                    this.lastWatchEvent = Date.now();
                    if (!stats) {
                        this.setMissing(target, false, eventType);
                    } else if (stats.isDirectory()) {
                        this.setDirectory(target, +stats.birthtime || 1, false, eventType);
                    } else if (stats.isFile() || stats.isSymbolicLink()) {
                        if (stats.mtime) {
                            ensureFsAccuracy(+stats.mtime);
                        }
                        this.setFileTime(
                            target,
                            +stats.mtime || +stats.ctime || 1,
                            false,
                            false,
                            eventType,
                        );
                    }
                });
            };
            process.nextTick(checkStats);
        } else {
            this._activeEvents.set(filename, true);
        }
    }
 
    /**
     * @param {unknown=} err error
     */
    onWatcherError(err) {
        if (this.closed) return;
        if (err) {
            if (
                /** @type {NodeJS.ErrnoException} */
                (err).code !== "EPERM" &&
                /** @type {NodeJS.ErrnoException} */
                (err).code !== "ENOENT"
            ) {
                // eslint-disable-next-line no-console
                console.error(`Watchpack Error (watcher): ${err}`);
            }
            this.onDirectoryRemoved("watch error");
        }
    }
 
    /**
     * @param {Error | NodeJS.ErrnoException=} err error
     */
    onStatsError(err) {
        if (err) {
            // eslint-disable-next-line no-console
            console.error(`Watchpack Error (stats): ${err}`);
        }
    }
 
    /**
     * @param {Error | NodeJS.ErrnoException=} err error
     */
    onScanError(err) {
        if (err) {
            // eslint-disable-next-line no-console
            console.error(`Watchpack Error (initial scan): ${err}`);
        }
        this.onScanFinished();
    }
 
    onScanFinished() {
        if (this.polledWatching) {
            this.timeout = setTimeout(() => {
                if (this.closed) return;
                this.doScan(false);
            }, this.polledWatching);
        }
    }
 
    /**
     * @param {string} reason a reason
     */
    onDirectoryRemoved(reason) {
        if (this.watcher) {
            this.watcher.close();
            this.watcher = null;
        }
        this.watchInParentDirectory();
        const type = /** @type {EventType} */ (`directory-removed (${reason})`);
        for (const directory of this.directories.keys()) {
            this.setMissing(directory, false, type);
        }
        for (const file of this.files.keys()) {
            this.setMissing(file, false, type);
        }
    }
 
    watchInParentDirectory() {
        if (!this.parentWatcher) {
            const parentDir = path.dirname(this.path);
            // avoid watching in the root directory
            // removing directories in the root directory is not supported
            if (path.dirname(parentDir) === parentDir) return;
 
            this.parentWatcher = this.watcherManager.watchFile(this.path, 1);
            /** @type {Watcher<FileWatcherEvents>} */
            (this.parentWatcher).on("change", (mtime, type) => {
                if (this.closed) return;
 
                // On non-osx platforms we don't need this watcher to detect
                // directory removal, as an EPERM error indicates that
                if ((!IS_OSX || this.polledWatching) && this.parentWatcher) {
                    this.parentWatcher.close();
                    this.parentWatcher = null;
                }
                // Try to create the watcher when parent directory is found
                if (!this.watcher) {
                    this.createWatcher();
                    this.doScan(false);
 
                    // directory was created so we emit an event
                    this.forEachWatcher(this.path, (w) =>
                        w.emit("change", this.path, mtime, type, false),
                    );
                }
            });
            /** @type {Watcher<FileWatcherEvents>} */
            (this.parentWatcher).on("remove", () => {
                this.onDirectoryRemoved("parent directory removed");
            });
        }
    }
 
    /**
     * @param {boolean} initial true when initial, otherwise false
     */
    doScan(initial) {
        if (this.scanning) {
            if (this.scanAgain) {
                if (!initial) this.scanAgainInitial = false;
            } else {
                this.scanAgain = true;
                this.scanAgainInitial = initial;
            }
            return;
        }
        this.scanning = true;
        if (this.timeout) {
            clearTimeout(this.timeout);
            this.timeout = undefined;
        }
        process.nextTick(() => {
            if (this.closed) return;
            fs.readdir(this.path, (err, items) => {
                if (this.closed) return;
                if (err) {
                    if (err.code === "ENOENT" || err.code === "EPERM") {
                        this.onDirectoryRemoved("scan readdir failed");
                    } else {
                        this.onScanError(err);
                    }
                    this.initialScan = false;
                    this.initialScanFinished = Date.now();
                    if (initial) {
                        for (const watchers of this.watchers.values()) {
                            for (const watcher of watchers) {
                                if (watcher.checkStartTime(this.initialScanFinished, false)) {
                                    watcher.emit(
                                        "initial-missing",
                                        "scan (parent directory missing in initial scan)",
                                    );
                                }
                            }
                        }
                    }
                    if (this.scanAgain) {
                        this.scanAgain = false;
                        this.doScan(this.scanAgainInitial);
                    } else {
                        this.scanning = false;
                    }
                    return;
                }
                const itemPaths = new Set(
                    items.map((item) => path.join(this.path, item.normalize("NFC"))),
                );
                for (const file of this.files.keys()) {
                    if (!itemPaths.has(file)) {
                        this.setMissing(file, initial, "scan (missing)");
                    }
                }
                for (const directory of this.directories.keys()) {
                    if (!itemPaths.has(directory)) {
                        this.setMissing(directory, initial, "scan (missing)");
                    }
                }
                if (this.scanAgain) {
                    // Early repeat of scan
                    this.scanAgain = false;
                    this.doScan(initial);
                    return;
                }
                const itemFinished = needCalls(itemPaths.size + 1, () => {
                    if (this.closed) return;
                    this.initialScan = false;
                    this.initialScanRemoved = null;
                    this.initialScanFinished = Date.now();
                    if (initial) {
                        const missingWatchers = new Map(this.watchers);
                        missingWatchers.delete(withoutCase(this.path));
                        for (const item of itemPaths) {
                            missingWatchers.delete(withoutCase(item));
                        }
                        for (const watchers of missingWatchers.values()) {
                            for (const watcher of watchers) {
                                if (watcher.checkStartTime(this.initialScanFinished, false)) {
                                    watcher.emit(
                                        "initial-missing",
                                        "scan (missing in initial scan)",
                                    );
                                }
                            }
                        }
                    }
                    if (this.scanAgain) {
                        this.scanAgain = false;
                        this.doScan(this.scanAgainInitial);
                    } else {
                        this.scanning = false;
                        this.onScanFinished();
                    }
                });
                for (const itemPath of itemPaths) {
                    fs.lstat(itemPath, (err2, stats) => {
                        if (this.closed) return;
                        if (err2) {
                            if (
                                err2.code === "ENOENT" ||
                                err2.code === "EPERM" ||
                                err2.code === "EACCES" ||
                                err2.code === "EBUSY" ||
                                // TODO https://github.com/libuv/libuv/pull/4566
                                (err2.code === "EINVAL" && IS_WIN)
                            ) {
                                this.setMissing(itemPath, initial, `scan (${err2.code})`);
                            } else {
                                this.onScanError(err2);
                            }
                            itemFinished();
                            return;
                        }
                        if (stats.isFile() || stats.isSymbolicLink()) {
                            if (stats.mtime) {
                                ensureFsAccuracy(+stats.mtime);
                            }
                            this.setFileTime(
                                itemPath,
                                +stats.mtime || +stats.ctime || 1,
                                initial,
                                true,
                                "scan (file)",
                            );
                        } else if (
                            stats.isDirectory() &&
                            (!initial || !this.directories.has(itemPath))
                        ) {
                            this.setDirectory(
                                itemPath,
                                +stats.birthtime || 1,
                                initial,
                                "scan (dir)",
                            );
                        }
                        itemFinished();
                    });
                }
                itemFinished();
            });
        });
    }
 
    /**
     * @returns {Record<string, number>} times
     */
    getTimes() {
        const obj = Object.create(null);
        let safeTime = this.lastWatchEvent;
        for (const [file, entry] of this.files) {
            fixupEntryAccuracy(entry);
            safeTime = Math.max(safeTime, entry.safeTime);
            obj[file] = Math.max(entry.safeTime, entry.timestamp);
        }
        if (this.nestedWatching) {
            for (const w of this.directories.values()) {
                const times =
                    /** @type {Watcher<DirectoryWatcherEvents>} */
                    (w).directoryWatcher.getTimes();
                for (const file of Object.keys(times)) {
                    const time = times[file];
                    safeTime = Math.max(safeTime, time);
                    obj[file] = time;
                }
            }
            obj[this.path] = safeTime;
        }
        if (!this.initialScan) {
            for (const watchers of this.watchers.values()) {
                for (const watcher of watchers) {
                    const { path } = watcher;
                    if (!Object.prototype.hasOwnProperty.call(obj, path)) {
                        obj[path] = null;
                    }
                }
            }
        }
        return obj;
    }
 
    /**
     * @param {TimeInfoEntries} fileTimestamps file timestamps
     * @param {TimeInfoEntries} directoryTimestamps directory timestamps
     * @returns {number} safe time
     */
    collectTimeInfoEntries(fileTimestamps, directoryTimestamps) {
        let safeTime = this.lastWatchEvent;
        for (const [file, entry] of this.files) {
            fixupEntryAccuracy(entry);
            safeTime = Math.max(safeTime, entry.safeTime);
            fileTimestamps.set(file, entry);
        }
        if (this.nestedWatching) {
            for (const w of this.directories.values()) {
                safeTime = Math.max(
                    safeTime,
                    /** @type {Watcher<DirectoryWatcherEvents>} */
                    (w).directoryWatcher.collectTimeInfoEntries(
                        fileTimestamps,
                        directoryTimestamps,
                    ),
                );
            }
            fileTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
            directoryTimestamps.set(this.path, {
                safeTime,
            });
        } else {
            for (const dir of this.directories.keys()) {
                // No additional info about this directory
                // but maybe another DirectoryWatcher has info
                fileTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
                if (!directoryTimestamps.has(dir)) {
                    directoryTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
                }
            }
            fileTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
            directoryTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
        }
        if (!this.initialScan) {
            for (const watchers of this.watchers.values()) {
                for (const watcher of watchers) {
                    const { path } = watcher;
                    if (!fileTimestamps.has(path)) {
                        fileTimestamps.set(path, null);
                    }
                }
            }
        }
        return safeTime;
    }
 
    close() {
        this.closed = true;
        this.initialScan = false;
        if (this.watcher) {
            this.watcher.close();
            this.watcher = null;
        }
        if (this.nestedWatching) {
            for (const w of this.directories.values()) {
                /** @type {Watcher<DirectoryWatcherEvents>} */
                (w).close();
            }
            this.directories.clear();
        }
        if (this.parentWatcher) {
            this.parentWatcher.close();
            this.parentWatcher = null;
        }
        this.emit("closed");
    }
}
 
module.exports = DirectoryWatcher;
module.exports.EXISTANCE_ONLY_TIME_ENTRY = EXISTANCE_ONLY_TIME_ENTRY;
module.exports.Watcher = Watcher;