rollup 5.0.0-0 → 5.0.0-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +0 -17
- package/dist/bin/rollup +119 -108
- package/dist/es/getLogFilter.js +2 -2
- package/dist/es/parseAst.js +2 -2
- package/dist/es/rollup.js +2 -2
- package/dist/es/shared/node-entry.js +1637 -1092
- package/dist/es/shared/parseAst.js +123 -31
- package/dist/es/shared/watch.js +3204 -1008
- package/dist/getLogFilter.js +2 -2
- package/dist/loadConfigFile.js +2 -2
- package/dist/native.js +6 -1
- package/dist/parseAst.js +2 -2
- package/dist/rollup.android-arm-eabi.node +0 -0
- package/dist/rollup.android-arm64.node +0 -0
- package/dist/rollup.android-x64.node +0 -0
- package/dist/rollup.d.ts +23 -9
- package/dist/rollup.darwin-arm64.node +0 -0
- package/dist/rollup.darwin-x64.node +0 -0
- package/dist/rollup.freebsd-arm64.node +0 -0
- package/dist/rollup.freebsd-x64.node +0 -0
- package/dist/rollup.js +2 -2
- package/dist/rollup.linux-arm-gnueabihf.node +0 -0
- package/dist/rollup.linux-arm-musleabihf.node +0 -0
- package/dist/rollup.linux-arm64-gnu.node +0 -0
- package/dist/rollup.linux-arm64-musl.node +0 -0
- package/dist/rollup.linux-loong64-gnu.node +0 -0
- package/dist/rollup.linux-loong64-musl.node +0 -0
- package/dist/rollup.linux-ppc64-gnu.node +0 -0
- package/dist/rollup.linux-ppc64-musl.node +0 -0
- package/dist/rollup.linux-riscv64-gnu.node +0 -0
- package/dist/rollup.linux-riscv64-musl.node +0 -0
- package/dist/rollup.linux-s390x-gnu.node +0 -0
- package/dist/rollup.linux-x64-gnu.node +0 -0
- package/dist/rollup.linux-x64-musl.node +0 -0
- package/dist/rollup.openbsd-x64.node +0 -0
- package/dist/rollup.openharmony-arm64.node +0 -0
- package/dist/rollup.win32-arm64-msvc.node +0 -0
- package/dist/rollup.win32-ia32-msvc.node +0 -0
- package/dist/rollup.win32-x64-gnu.node +0 -0
- package/dist/rollup.win32-x64-msvc.node +0 -0
- package/dist/shared/index.js +3198 -1002
- package/dist/shared/loadConfigFile.js +2 -2
- package/dist/shared/node-entry.js +2 -2
- package/dist/shared/parseAst.js +129 -37
- package/dist/shared/rollup.js +1594 -1049
- package/dist/shared/watch-cli.js +2 -2
- package/dist/shared/watch.js +2 -2
- package/package.json +54 -51
package/dist/shared/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
@license
|
|
3
|
-
Rollup.js v5.0.0-
|
|
4
|
-
|
|
3
|
+
Rollup.js v5.0.0-2
|
|
4
|
+
Sun, 01 Nov 2026 08:56:25 GMT - commit 4009894760dbcbff5ba6cefa494787cc54ffaa62
|
|
5
5
|
|
|
6
6
|
https://github.com/rollup/rollup
|
|
7
7
|
|
|
@@ -345,16 +345,41 @@ function readdirp(root, options = {}) {
|
|
|
345
345
|
return new ReaddirpStream(opts);
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
-
const
|
|
349
|
-
const
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
const pl = process.platform;
|
|
353
|
-
const isWindows = pl === 'win32';
|
|
354
|
-
const isMacos = pl === 'darwin';
|
|
355
|
-
const isLinux = pl === 'linux';
|
|
356
|
-
const isFreeBSD = pl === 'freebsd';
|
|
348
|
+
const platform = process.platform;
|
|
349
|
+
const isWindows = platform === 'win32';
|
|
350
|
+
const isMacos = platform === 'darwin';
|
|
351
|
+
const isLinux = platform === 'linux';
|
|
357
352
|
const isIBMi = node_os.type() === 'OS400';
|
|
353
|
+
function errorCode(error) {
|
|
354
|
+
return error?.code;
|
|
355
|
+
}
|
|
356
|
+
function classifyError(error) {
|
|
357
|
+
const code = errorCode(error);
|
|
358
|
+
if (code === 'ENOENT' || code === 'ENOTDIR')
|
|
359
|
+
return 'missing';
|
|
360
|
+
if (code === 'EPERM' || code === 'EACCES')
|
|
361
|
+
return 'permission';
|
|
362
|
+
if (code === 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM')
|
|
363
|
+
return 'recursive-unsupported';
|
|
364
|
+
return 'other';
|
|
365
|
+
}
|
|
366
|
+
function isMissingError(error) {
|
|
367
|
+
return classifyError(error) === 'missing';
|
|
368
|
+
}
|
|
369
|
+
function isPermissionError(error) {
|
|
370
|
+
return classifyError(error) === 'permission';
|
|
371
|
+
}
|
|
372
|
+
function isRecursiveWatchUnsupported(error) {
|
|
373
|
+
return classifyError(error) === 'recursive-unsupported';
|
|
374
|
+
}
|
|
375
|
+
const systemScheduler = Object.freeze({
|
|
376
|
+
now: () => Date.now(),
|
|
377
|
+
setTimeout: (callback, delay) => setTimeout(callback, delay),
|
|
378
|
+
clearTimeout: (timer) => {
|
|
379
|
+
if (timer)
|
|
380
|
+
clearTimeout(timer);
|
|
381
|
+
},
|
|
382
|
+
});
|
|
358
383
|
const EVENTS = {
|
|
359
384
|
ALL: 'all',
|
|
360
385
|
READY: 'ready',
|
|
@@ -366,85 +391,323 @@ const EVENTS = {
|
|
|
366
391
|
RAW: 'raw',
|
|
367
392
|
ERROR: 'error',
|
|
368
393
|
};
|
|
369
|
-
const
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
const
|
|
375
|
-
const
|
|
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
|
-
|
|
394
|
+
const BACK_SLASH_RE = /\\/g;
|
|
395
|
+
const DOUBLE_SLASH_RE = /\/\//g;
|
|
396
|
+
function normalizePath(path) {
|
|
397
|
+
if (typeof path !== 'string')
|
|
398
|
+
throw new TypeError('string expected');
|
|
399
|
+
const unix = path.replace(BACK_SLASH_RE, '/');
|
|
400
|
+
const unc = unix.startsWith('//');
|
|
401
|
+
let normalized = sp__namespace.normalize(unix).replace(BACK_SLASH_RE, '/').replace(DOUBLE_SLASH_RE, '/');
|
|
402
|
+
if (unc && !normalized.startsWith('//'))
|
|
403
|
+
normalized = `/${normalized}`;
|
|
404
|
+
return normalized;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Plain-number size, mtime, and inode. readdirp returns BigIntStats on Windows,
|
|
408
|
+
* which never compare equal to the number Stats of `lstat`.
|
|
409
|
+
*/
|
|
410
|
+
function statIdentity(stats) {
|
|
411
|
+
if (typeof stats.mtimeMs !== 'bigint')
|
|
412
|
+
return stats;
|
|
413
|
+
const big = stats;
|
|
414
|
+
const sec = big.mtimeNs / 1000000000n;
|
|
415
|
+
const nsec = big.mtimeNs % 1000000000n;
|
|
416
|
+
// Node's own number formula, so both kinds of Stats agree exactly.
|
|
417
|
+
const mtimeMs = Number(sec) * 1e3 + Number(nsec) / 1e6;
|
|
418
|
+
return { size: Number(big.size), mtimeMs, ino: Number(big.ino) };
|
|
419
|
+
}
|
|
420
|
+
function logicalPathKey(path) {
|
|
421
|
+
const resolved = sp__namespace.resolve(path);
|
|
422
|
+
return (isWindows || !resolved.includes('\\')
|
|
423
|
+
? resolved.replace(BACK_SLASH_RE, '/')
|
|
424
|
+
: normalizePath(resolved));
|
|
425
|
+
}
|
|
426
|
+
function isInsideRelativePath(relative) {
|
|
427
|
+
return relative !== '..' && !relative.startsWith(`..${sp__namespace.sep}`) && !sp__namespace.isAbsolute(relative);
|
|
428
|
+
}
|
|
429
|
+
function isSameOrInside(root, candidate) {
|
|
430
|
+
const relative = sp__namespace.relative(root, candidate);
|
|
431
|
+
return relative === '' || isInsideRelativePath(relative);
|
|
432
|
+
}
|
|
433
|
+
function isStrictlyInside(root, candidate) {
|
|
434
|
+
const relative = sp__namespace.relative(root, candidate);
|
|
435
|
+
return relative !== '' && isInsideRelativePath(relative);
|
|
436
|
+
}
|
|
437
|
+
function isMatcherObject(matcher) {
|
|
438
|
+
return typeof matcher === 'object' && matcher !== null && !(matcher instanceof RegExp);
|
|
439
|
+
}
|
|
440
|
+
function cloneOwnedMatcher(matcher) {
|
|
441
|
+
if (matcher instanceof RegExp)
|
|
442
|
+
return new RegExp(matcher.source, matcher.flags);
|
|
443
|
+
if (isMatcherObject(matcher))
|
|
444
|
+
return Object.freeze({ ...matcher });
|
|
445
|
+
return matcher;
|
|
446
|
+
}
|
|
447
|
+
function compileMatcher(matcher) {
|
|
448
|
+
if (typeof matcher === 'function')
|
|
449
|
+
return matcher;
|
|
450
|
+
if (typeof matcher === 'string')
|
|
451
|
+
return (candidate) => matcher === candidate;
|
|
452
|
+
if (matcher instanceof RegExp) {
|
|
453
|
+
return (candidate) => {
|
|
454
|
+
matcher.lastIndex = 0;
|
|
455
|
+
return matcher.test(candidate);
|
|
456
|
+
};
|
|
416
457
|
}
|
|
417
|
-
|
|
418
|
-
|
|
458
|
+
if (isMatcherObject(matcher)) {
|
|
459
|
+
return (candidate) => matcher.path === candidate ||
|
|
460
|
+
Boolean(matcher.recursive && isStrictlyInside(matcher.path, candidate));
|
|
419
461
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
462
|
+
return () => false;
|
|
463
|
+
}
|
|
464
|
+
function compileMatchers(matchers) {
|
|
465
|
+
const patterns = matchers.map(compileMatcher);
|
|
466
|
+
return (candidate, stats) => {
|
|
467
|
+
if (patterns.length === 0)
|
|
468
|
+
return false;
|
|
469
|
+
const normalized = normalizePath(candidate);
|
|
470
|
+
return patterns.some((pattern) => pattern(normalized, stats));
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function normalizeMatcher(matcher, cwd = process.cwd()) {
|
|
474
|
+
if (typeof matcher === 'string') {
|
|
475
|
+
return normalizePath(sp__namespace.isAbsolute(matcher) ? matcher : sp__namespace.join(cwd, matcher));
|
|
476
|
+
}
|
|
477
|
+
if (isMatcherObject(matcher)) {
|
|
478
|
+
return {
|
|
479
|
+
path: normalizePath(sp__namespace.isAbsolute(matcher.path) ? matcher.path : sp__namespace.join(cwd, matcher.path)),
|
|
480
|
+
recursive: matcher.recursive,
|
|
481
|
+
};
|
|
425
482
|
}
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
483
|
+
return matcher;
|
|
484
|
+
}
|
|
485
|
+
const REPLACER_RE = /^\.[/\\]/;
|
|
486
|
+
/** Temporary traversal scope; replaced by the observation engine's root scope. */
|
|
487
|
+
class WatchHelper {
|
|
488
|
+
watchPath;
|
|
489
|
+
followSymlinks;
|
|
490
|
+
recursiveRoot;
|
|
491
|
+
recursiveDisabled;
|
|
492
|
+
observationTrigger;
|
|
493
|
+
realpathAncestry;
|
|
494
|
+
pathGeneration;
|
|
495
|
+
context;
|
|
496
|
+
constructor(path, follow, context) {
|
|
497
|
+
this.context = context;
|
|
498
|
+
this.watchPath = path.replace(REPLACER_RE, '');
|
|
499
|
+
this.followSymlinks = follow;
|
|
500
|
+
this.realpathAncestry = new Set();
|
|
501
|
+
this.pathGeneration = context.capturePathGeneration();
|
|
502
|
+
}
|
|
503
|
+
fork(path) {
|
|
504
|
+
// Filters must resolve scan entries against the fork's own watchPath, not
|
|
505
|
+
// the parent's, or nested entries are matched as if they were siblings.
|
|
506
|
+
const helper = new WatchHelper(path, this.followSymlinks, this.context);
|
|
507
|
+
helper.recursiveRoot = this.recursiveRoot;
|
|
508
|
+
helper.recursiveDisabled = this.recursiveDisabled;
|
|
509
|
+
helper.observationTrigger = this.observationTrigger;
|
|
510
|
+
helper.realpathAncestry = new Set(this.realpathAncestry);
|
|
511
|
+
helper.pathGeneration = this.pathGeneration;
|
|
512
|
+
return helper;
|
|
513
|
+
}
|
|
514
|
+
withObservation(trigger) {
|
|
515
|
+
const helper = Object.create(this);
|
|
516
|
+
helper.observationTrigger = trigger;
|
|
517
|
+
return helper;
|
|
432
518
|
}
|
|
433
|
-
|
|
434
|
-
|
|
519
|
+
filterPath(entry) {
|
|
520
|
+
return entry.stats?.isSymbolicLink()
|
|
521
|
+
? this.filterDir(entry)
|
|
522
|
+
: this.context.isntIgnored(sp__namespace.join(this.watchPath, entry.path), entry.stats);
|
|
435
523
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
524
|
+
filterDir(entry) {
|
|
525
|
+
return this.context.isntIgnored(sp__namespace.join(this.watchPath, entry.path), entry.stats);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
let nextGeneration = 0;
|
|
530
|
+
function allocateSharedResourceGeneration() {
|
|
531
|
+
return ++nextGeneration;
|
|
532
|
+
}
|
|
533
|
+
function attachSharedSubscriber(resource, subscriber) {
|
|
534
|
+
if (resource.closed)
|
|
535
|
+
return false;
|
|
536
|
+
resource.subscribers.add(subscriber);
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
function detachSharedSubscriber(resource, subscriber) {
|
|
540
|
+
if (resource.closed || !resource.subscribers.delete(subscriber))
|
|
541
|
+
return;
|
|
542
|
+
return resource.subscribers.size > 0 ? 'remaining' : 'last';
|
|
543
|
+
}
|
|
544
|
+
/** Invalidates exactly this resource generation and returns its subscribers. */
|
|
545
|
+
function invalidateSharedResource(resource) {
|
|
546
|
+
if (resource.closed)
|
|
547
|
+
return;
|
|
548
|
+
resource.closed = true;
|
|
549
|
+
const subscribers = [...resource.subscribers];
|
|
550
|
+
resource.subscribers.clear();
|
|
551
|
+
return subscribers;
|
|
552
|
+
}
|
|
553
|
+
function reconfigureWatcherPersistence(resource) {
|
|
554
|
+
const persistent = [...resource.subscribers].some((subscriber) => subscriber.persistent);
|
|
555
|
+
if (persistent)
|
|
556
|
+
resource.watcher.ref();
|
|
557
|
+
else
|
|
558
|
+
resource.watcher.unref();
|
|
559
|
+
}
|
|
560
|
+
function invalidateWatcherResource(resource, registry) {
|
|
561
|
+
const subscribers = invalidateSharedResource(resource);
|
|
562
|
+
if (!subscribers)
|
|
563
|
+
return;
|
|
564
|
+
if (registry.get(resource.resource) === resource)
|
|
565
|
+
registry.delete(resource.resource);
|
|
566
|
+
resource.watcher.close();
|
|
567
|
+
return subscribers;
|
|
568
|
+
}
|
|
569
|
+
function attachWatcherSubscriber(resource, subscriber) {
|
|
570
|
+
attachSharedSubscriber(resource, subscriber);
|
|
571
|
+
reconfigureWatcherPersistence(resource);
|
|
572
|
+
}
|
|
573
|
+
function createWatcherSubscription(resource, subscriber, registry) {
|
|
574
|
+
let closed = false;
|
|
575
|
+
return {
|
|
576
|
+
resource: resource.resource,
|
|
577
|
+
close: () => {
|
|
578
|
+
if (closed)
|
|
579
|
+
return;
|
|
580
|
+
closed = true;
|
|
581
|
+
const remaining = detachSharedSubscriber(resource, subscriber);
|
|
582
|
+
if (remaining === 'remaining')
|
|
583
|
+
reconfigureWatcherPersistence(resource);
|
|
584
|
+
else if (remaining === 'last' && registry.get(resource.resource) === resource) {
|
|
585
|
+
invalidateWatcherResource(resource, registry);
|
|
586
|
+
}
|
|
587
|
+
},
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
function selectBackend(options) {
|
|
591
|
+
const preferRecursive = options.backend === 'native-recursive' ||
|
|
592
|
+
(options.backend === 'auto' && (isMacos || isWindows));
|
|
593
|
+
const kind = options.backend === 'polling'
|
|
594
|
+
? 'polling'
|
|
595
|
+
: preferRecursive && options.depth === undefined
|
|
596
|
+
? 'native-recursive-preferred'
|
|
597
|
+
: 'native-per-directory';
|
|
598
|
+
return Object.freeze({
|
|
599
|
+
kind,
|
|
600
|
+
polling: kind === 'polling',
|
|
601
|
+
recursive: kind === 'native-recursive-preferred',
|
|
602
|
+
perDirectory: kind === 'native-per-directory',
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
const EV$1 = EVENTS;
|
|
606
|
+
const POLL_DIRECTORY_RECHECK_DELAY = 1000;
|
|
607
|
+
/** Native resources use one process-global monotonic time domain. */
|
|
608
|
+
function backendNow() {
|
|
609
|
+
return performance.now();
|
|
610
|
+
}
|
|
611
|
+
function toNativeWatchPath(path) {
|
|
612
|
+
const nativePath = sp__namespace.normalize(path);
|
|
613
|
+
if (!isWindows)
|
|
614
|
+
return nativePath;
|
|
615
|
+
try {
|
|
616
|
+
// Node 24's Windows fs-event implementation compares long callback paths
|
|
617
|
+
// with the spelling passed here. Logical/resource keys remain lexical.
|
|
618
|
+
return node_fs.realpathSync.native(nativePath);
|
|
441
619
|
}
|
|
442
|
-
|
|
443
|
-
|
|
620
|
+
catch {
|
|
621
|
+
return nativePath;
|
|
444
622
|
}
|
|
445
|
-
}
|
|
446
|
-
|
|
623
|
+
}
|
|
624
|
+
function projectNativeRelativePath(nativeRoot, relativePath) {
|
|
625
|
+
if (!isWindows || relativePath === null || !sp__namespace.isAbsolute(relativePath))
|
|
626
|
+
return relativePath;
|
|
627
|
+
const root = sp__namespace.normalize(sp__namespace.toNamespacedPath(nativeRoot));
|
|
628
|
+
const candidate = sp__namespace.normalize(sp__namespace.toNamespacedPath(relativePath));
|
|
629
|
+
const projected = sp__namespace.relative(root, candidate);
|
|
630
|
+
if (!isSameOrInside(root, candidate))
|
|
631
|
+
return relativePath;
|
|
632
|
+
return projected || null;
|
|
633
|
+
}
|
|
447
634
|
const FsWatchInstances = new Map();
|
|
635
|
+
function systemNativeWatchFactory(path, options, listener) {
|
|
636
|
+
return node_fs.watch(path, options, listener);
|
|
637
|
+
}
|
|
638
|
+
let nativeWatchFactory = systemNativeWatchFactory;
|
|
639
|
+
const RecursiveWatchInstances = new Map();
|
|
640
|
+
let recursiveWatchUnsupported = false;
|
|
641
|
+
function systemRecursiveWatchFactory(path, options, listener) {
|
|
642
|
+
return node_fs.watch(path, options, listener);
|
|
643
|
+
}
|
|
644
|
+
let recursiveWatchFactory = systemRecursiveWatchFactory;
|
|
645
|
+
function failRecursiveResource(resource, error) {
|
|
646
|
+
const subscribers = invalidateWatcherResource(resource, RecursiveWatchInstances);
|
|
647
|
+
if (!subscribers)
|
|
648
|
+
return;
|
|
649
|
+
subscribers.forEach((subscriber) => subscriber.failure(error));
|
|
650
|
+
}
|
|
651
|
+
function subscribeRecursiveNative(path, persistent, handlers, signal) {
|
|
652
|
+
if (recursiveWatchUnsupported)
|
|
653
|
+
return { kind: 'unsupported' };
|
|
654
|
+
const resourceKey = sp__namespace.resolve(path);
|
|
655
|
+
const subscriber = {
|
|
656
|
+
...handlers,
|
|
657
|
+
watchedPath: path,
|
|
658
|
+
persistent,
|
|
659
|
+
};
|
|
660
|
+
let resource = RecursiveWatchInstances.get(resourceKey);
|
|
661
|
+
if (!resource) {
|
|
662
|
+
let watcher;
|
|
663
|
+
let created;
|
|
664
|
+
const nativePath = toNativeWatchPath(path);
|
|
665
|
+
try {
|
|
666
|
+
watcher = recursiveWatchFactory(nativePath, { persistent, recursive: true }, (rawEvent, relativePath) => {
|
|
667
|
+
const active = created;
|
|
668
|
+
if (!active || active.closed)
|
|
669
|
+
return;
|
|
670
|
+
// A late callback from a retired handle must not reach a replacement generation.
|
|
671
|
+
if (RecursiveWatchInstances.get(resourceKey) !== active)
|
|
672
|
+
return;
|
|
673
|
+
const trigger = {
|
|
674
|
+
kind: 'native',
|
|
675
|
+
resource: resourceKey,
|
|
676
|
+
rawEvent,
|
|
677
|
+
relativePath: projectNativeRelativePath(nativePath, relativePath),
|
|
678
|
+
sequence: ++active.sequence,
|
|
679
|
+
observedAt: backendNow(),
|
|
680
|
+
};
|
|
681
|
+
active.subscribers.forEach((current) => {
|
|
682
|
+
current.rawEmitter(rawEvent, relativePath, { watchedPath: current.watchedPath });
|
|
683
|
+
current.publish(trigger);
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
catch (error) {
|
|
688
|
+
if (isRecursiveWatchUnsupported(error)) {
|
|
689
|
+
recursiveWatchUnsupported = true;
|
|
690
|
+
return { kind: 'unsupported' };
|
|
691
|
+
}
|
|
692
|
+
return { kind: 'failed', error };
|
|
693
|
+
}
|
|
694
|
+
created = resource = {
|
|
695
|
+
resource: resourceKey,
|
|
696
|
+
generation: allocateSharedResourceGeneration(),
|
|
697
|
+
subscribers: new Set(),
|
|
698
|
+
closed: false,
|
|
699
|
+
watcher,
|
|
700
|
+
sequence: 0,
|
|
701
|
+
};
|
|
702
|
+
RecursiveWatchInstances.set(resourceKey, resource);
|
|
703
|
+
watcher.on(EV$1.ERROR, (error) => failRecursiveResource(resource, error));
|
|
704
|
+
}
|
|
705
|
+
attachWatcherSubscriber(resource, subscriber);
|
|
706
|
+
const subscription = createWatcherSubscription(resource, subscriber, RecursiveWatchInstances);
|
|
707
|
+
if (signal.aborted)
|
|
708
|
+
void subscription.close();
|
|
709
|
+
return { kind: 'subscribed', subscription };
|
|
710
|
+
}
|
|
448
711
|
/**
|
|
449
712
|
* Instantiates the fs_watch interface
|
|
450
713
|
* @param path to be watched
|
|
@@ -454,20 +717,11 @@ const FsWatchInstances = new Map();
|
|
|
454
717
|
* @param emitRaw emits raw event data
|
|
455
718
|
* @returns {NativeFsWatcher}
|
|
456
719
|
*/
|
|
457
|
-
function createFsWatchInstance(path, options, listener, errHandler
|
|
458
|
-
const handleEvent = (rawEvent, evPath) => {
|
|
459
|
-
listener(path);
|
|
460
|
-
emitRaw(rawEvent, evPath, { watchedPath: path });
|
|
461
|
-
// emit based on events occurring for files from a directory's watcher in
|
|
462
|
-
// case the file's watcher misses it (and rely on throttling to de-dupe)
|
|
463
|
-
if (evPath && path !== evPath) {
|
|
464
|
-
fsWatchBroadcast(sp__namespace.resolve(path, evPath), KEY_LISTENERS, sp__namespace.join(path, evPath));
|
|
465
|
-
}
|
|
466
|
-
};
|
|
720
|
+
function createFsWatchInstance(path, options, listener, errHandler) {
|
|
467
721
|
try {
|
|
468
|
-
return
|
|
722
|
+
return nativeWatchFactory(path, {
|
|
469
723
|
persistent: options.persistent,
|
|
470
|
-
},
|
|
724
|
+
}, listener);
|
|
471
725
|
}
|
|
472
726
|
catch (error) {
|
|
473
727
|
errHandler(error);
|
|
@@ -475,17 +729,112 @@ function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
|
|
|
475
729
|
}
|
|
476
730
|
}
|
|
477
731
|
/**
|
|
478
|
-
*
|
|
479
|
-
* @param fullPath absolute path bound to fs_watch instance
|
|
732
|
+
* Publish one invalidation to subscribers of an exact native resource.
|
|
480
733
|
*/
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
if (!cont)
|
|
734
|
+
function publishNativeTrigger(cont, rawEvent, relativePath, emitRaw, rawRelativePath = relativePath) {
|
|
735
|
+
if (cont.closed || FsWatchInstances.get(cont.resource) !== cont)
|
|
484
736
|
return;
|
|
485
|
-
|
|
486
|
-
|
|
737
|
+
const trigger = {
|
|
738
|
+
kind: 'native',
|
|
739
|
+
resource: cont.resource,
|
|
740
|
+
rawEvent,
|
|
741
|
+
relativePath,
|
|
742
|
+
sequence: ++cont.sequence,
|
|
743
|
+
observedAt: backendNow(),
|
|
744
|
+
};
|
|
745
|
+
cont.subscribers.forEach((subscriber) => {
|
|
746
|
+
if (emitRaw) {
|
|
747
|
+
subscriber.rawEmitter(rawEvent, rawRelativePath, { watchedPath: subscriber.watchedPath });
|
|
748
|
+
}
|
|
749
|
+
subscriber.publish(trigger);
|
|
487
750
|
});
|
|
488
|
-
}
|
|
751
|
+
}
|
|
752
|
+
function broadcastNativeError(cont, error) {
|
|
753
|
+
if (cont.closed || FsWatchInstances.get(cont.resource) !== cont)
|
|
754
|
+
return;
|
|
755
|
+
cont.subscribers.forEach((subscriber) => subscriber.errHandler(error));
|
|
756
|
+
}
|
|
757
|
+
function closeFailedNativeResource(cont, error) {
|
|
758
|
+
// Subscribers that attached while the error was being classified are retired
|
|
759
|
+
// too, so every one of them learns that its subscription is gone.
|
|
760
|
+
const subscribers = invalidateWatcherResource(cont, FsWatchInstances);
|
|
761
|
+
subscribers?.forEach((subscriber) => subscriber.failure?.(error));
|
|
762
|
+
}
|
|
763
|
+
async function handleNativeError(path, cont, error) {
|
|
764
|
+
// Workaround for https://github.com/joyent/node/issues/4337
|
|
765
|
+
if (isWindows && error.code === 'EPERM') {
|
|
766
|
+
try {
|
|
767
|
+
const fd = await promises.open(path, 'r');
|
|
768
|
+
await fd.close();
|
|
769
|
+
broadcastNativeError(cont, error);
|
|
770
|
+
}
|
|
771
|
+
catch {
|
|
772
|
+
// ReadDirectoryChangesW reports a deleted watched directory as EPERM.
|
|
773
|
+
// Turn that terminal backend failure into one final invalidation so
|
|
774
|
+
// each subscriber can reconcile the path before the unusable shared
|
|
775
|
+
// handle is discarded. If the path still exists, reconciliation is a
|
|
776
|
+
// harmless refresh and preserves the historical error suppression.
|
|
777
|
+
publishNativeTrigger(cont, 'rename', null, false);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
else {
|
|
781
|
+
broadcastNativeError(cont, error);
|
|
782
|
+
}
|
|
783
|
+
closeFailedNativeResource(cont, error);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* inotify reports a watched directory's own deletion (IN_DELETE_SELF, IN_IGNORED)
|
|
787
|
+
* as `rename` of its basename, after which the handle is dead. When the directory
|
|
788
|
+
* was recreated before this callback ran, nothing else reveals that: ext4 even
|
|
789
|
+
* reuses the inode number. Retire the handle so subscribers re-subscribe the new
|
|
790
|
+
* directory and reconcile it. A same-named child that still exists is no sign.
|
|
791
|
+
*/
|
|
792
|
+
async function retireIfSelfRemoved(cont, nativePath) {
|
|
793
|
+
try {
|
|
794
|
+
await promises.lstat(sp__namespace.join(nativePath, sp__namespace.basename(nativePath)));
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
catch (error) {
|
|
798
|
+
if (!isMissingError(error))
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
try {
|
|
802
|
+
if (!(await promises.stat(nativePath)).isDirectory())
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
catch {
|
|
806
|
+
// Deleted and not recreated: subscribers reconcile the removal themselves.
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (cont.closed || FsWatchInstances.get(cont.resource) !== cont)
|
|
810
|
+
return;
|
|
811
|
+
closeFailedNativeResource(cont, new Error(`Watched directory was replaced: ${nativePath}`));
|
|
812
|
+
}
|
|
813
|
+
function createNativeContainer(path, nativePath, resourceKey, options, subscriber) {
|
|
814
|
+
let cont;
|
|
815
|
+
const watcher = createFsWatchInstance(nativePath, options, (rawEvent, relativePath) => {
|
|
816
|
+
const projectedPath = projectNativeRelativePath(nativePath, relativePath);
|
|
817
|
+
publishNativeTrigger(cont, rawEvent, projectedPath, true, relativePath);
|
|
818
|
+
if (isLinux && rawEvent === 'rename' && relativePath === sp__namespace.basename(nativePath)) {
|
|
819
|
+
void retireIfSelfRemoved(cont, nativePath);
|
|
820
|
+
}
|
|
821
|
+
}, subscriber.errHandler);
|
|
822
|
+
if (!watcher)
|
|
823
|
+
return;
|
|
824
|
+
cont = {
|
|
825
|
+
resource: resourceKey,
|
|
826
|
+
generation: allocateSharedResourceGeneration(),
|
|
827
|
+
subscribers: new Set([subscriber]),
|
|
828
|
+
closed: false,
|
|
829
|
+
watcher,
|
|
830
|
+
sequence: 0,
|
|
831
|
+
};
|
|
832
|
+
FsWatchInstances.set(resourceKey, cont);
|
|
833
|
+
watcher.on(EV$1.ERROR, (error) => {
|
|
834
|
+
void handleNativeError(path, cont, error);
|
|
835
|
+
});
|
|
836
|
+
return cont;
|
|
837
|
+
}
|
|
489
838
|
/**
|
|
490
839
|
* Instantiates the fs_watch interface or binds listeners
|
|
491
840
|
* to an existing one covering the same file system entry
|
|
@@ -494,231 +843,1285 @@ const fsWatchBroadcast = (fullPath, listenerType, val1, val2, val3) => {
|
|
|
494
843
|
* @param options to be passed to fs_watch
|
|
495
844
|
* @param handlers container for event listener functions
|
|
496
845
|
*/
|
|
497
|
-
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
return watcher.close.bind(watcher);
|
|
506
|
-
}
|
|
846
|
+
function setFsWatchListener(path, fullPath, options, handlers, signal) {
|
|
847
|
+
const resourceKey = fullPath;
|
|
848
|
+
const subscriber = {
|
|
849
|
+
...handlers,
|
|
850
|
+
watchedPath: path,
|
|
851
|
+
persistent: options.persistent ?? true,
|
|
852
|
+
};
|
|
853
|
+
let cont = FsWatchInstances.get(resourceKey);
|
|
507
854
|
if (cont) {
|
|
508
|
-
|
|
509
|
-
addAndConvert(cont, KEY_ERR, errHandler);
|
|
510
|
-
addAndConvert(cont, KEY_RAW, rawEmitter);
|
|
855
|
+
attachWatcherSubscriber(cont, subscriber);
|
|
511
856
|
}
|
|
512
857
|
else {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
if (!watcher)
|
|
858
|
+
cont = createNativeContainer(path, toNativeWatchPath(path), resourceKey, options, subscriber);
|
|
859
|
+
if (!cont)
|
|
516
860
|
return;
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
861
|
+
}
|
|
862
|
+
const subscription = createWatcherSubscription(cont, subscriber, FsWatchInstances);
|
|
863
|
+
if (signal.aborted) {
|
|
864
|
+
void subscription.close();
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
return subscription;
|
|
868
|
+
}
|
|
869
|
+
const MISSING_POLL_OBSERVATION = Object.freeze({ missing: true });
|
|
870
|
+
const PollingInstances = new Map();
|
|
871
|
+
function isMissingObservation(observation) {
|
|
872
|
+
return 'missing' in observation;
|
|
873
|
+
}
|
|
874
|
+
function observationsDiffer(previous, current) {
|
|
875
|
+
const previousMissing = isMissingObservation(previous);
|
|
876
|
+
const currentMissing = isMissingObservation(current);
|
|
877
|
+
if (previousMissing || currentMissing)
|
|
878
|
+
return previousMissing !== currentMissing;
|
|
879
|
+
const reliableInodeChanged = !isWindows && previous.ino !== 0 && current.ino !== 0 && previous.ino !== current.ino;
|
|
880
|
+
return (previous.size !== current.size || previous.mtimeMs !== current.mtimeMs || reliableInodeChanged);
|
|
881
|
+
}
|
|
882
|
+
function setTimerPersistence(resource) {
|
|
883
|
+
if (!resource.timer)
|
|
884
|
+
return;
|
|
885
|
+
if (resource.persistent)
|
|
886
|
+
resource.timer.ref();
|
|
887
|
+
else
|
|
888
|
+
resource.timer.unref();
|
|
889
|
+
}
|
|
890
|
+
function schedulePoll(resource, delay = resource.interval) {
|
|
891
|
+
if (resource.closed || resource.subscribers.size === 0)
|
|
892
|
+
return;
|
|
893
|
+
resource.timer = resource.scheduler.setTimeout(() => {
|
|
894
|
+
resource.timer = undefined;
|
|
895
|
+
const currentPoll = pollResource(resource);
|
|
896
|
+
resource.currentPoll = currentPoll;
|
|
897
|
+
void currentPoll.then(() => {
|
|
898
|
+
if (resource.currentPoll === currentPoll)
|
|
899
|
+
resource.currentPoll = undefined;
|
|
900
|
+
}, () => {
|
|
901
|
+
if (resource.currentPoll === currentPoll)
|
|
902
|
+
resource.currentPoll = undefined;
|
|
903
|
+
});
|
|
904
|
+
}, delay);
|
|
905
|
+
setTimerPersistence(resource);
|
|
906
|
+
}
|
|
907
|
+
async function pollResource(resource) {
|
|
908
|
+
if (resource.closed || resource.running || resource.subscribers.size === 0)
|
|
909
|
+
return;
|
|
910
|
+
resource.running = true;
|
|
911
|
+
const startedAt = resource.scheduler.now();
|
|
912
|
+
let current;
|
|
913
|
+
try {
|
|
914
|
+
current = await promises.stat(resource.resource);
|
|
915
|
+
}
|
|
916
|
+
catch (error) {
|
|
917
|
+
if (isMissingError(error)) {
|
|
918
|
+
current = MISSING_POLL_OBSERVATION;
|
|
919
|
+
}
|
|
920
|
+
else {
|
|
921
|
+
resource.subscribers.forEach((subscriber) => subscriber.errHandler(error));
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
if (!resource.closed && current) {
|
|
925
|
+
const previous = resource.previous;
|
|
926
|
+
resource.previous = current;
|
|
927
|
+
const now = resource.scheduler.now();
|
|
928
|
+
const changed = previous && observationsDiffer(previous, current);
|
|
929
|
+
const directoryReplayDue = resource.directoryReplayAt !== undefined && now >= resource.directoryReplayAt;
|
|
930
|
+
if (!isMissingObservation(current) && current.isDirectory()) {
|
|
931
|
+
if (changed)
|
|
932
|
+
resource.directoryReplayAt = now + POLL_DIRECTORY_RECHECK_DELAY;
|
|
933
|
+
else if (directoryReplayDue)
|
|
934
|
+
resource.directoryReplayAt = undefined;
|
|
935
|
+
}
|
|
936
|
+
else {
|
|
937
|
+
resource.directoryReplayAt = undefined;
|
|
938
|
+
}
|
|
939
|
+
if (previous && (changed || directoryReplayDue)) {
|
|
940
|
+
const trigger = {
|
|
941
|
+
kind: 'poll',
|
|
942
|
+
resource: resource.resource,
|
|
943
|
+
current,
|
|
944
|
+
previous,
|
|
945
|
+
sequence: ++resource.sequence,
|
|
946
|
+
observedAt: resource.scheduler.now(),
|
|
947
|
+
};
|
|
948
|
+
resource.subscribers.forEach((subscriber) => {
|
|
949
|
+
subscriber.rawEmitter(EV$1.CHANGE, resource.resource, { current, previous });
|
|
950
|
+
subscriber.publish(trigger);
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
resource.running = false;
|
|
955
|
+
if (!resource.closed && resource.subscribers.size > 0) {
|
|
956
|
+
const elapsed = resource.scheduler.now() - startedAt;
|
|
957
|
+
schedulePoll(resource, Math.max(0, resource.interval - elapsed));
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
function reconfigurePollResource(resource) {
|
|
961
|
+
if (resource.closed || resource.subscribers.size === 0)
|
|
962
|
+
return;
|
|
963
|
+
const interval = Math.min(...[...resource.subscribers].map((subscriber) => subscriber.interval));
|
|
964
|
+
const persistent = [...resource.subscribers].some((subscriber) => subscriber.persistent);
|
|
965
|
+
const intervalChanged = resource.interval !== interval;
|
|
966
|
+
resource.interval = interval;
|
|
967
|
+
resource.persistent = persistent;
|
|
968
|
+
if (resource.timer && intervalChanged) {
|
|
969
|
+
resource.scheduler.clearTimeout(resource.timer);
|
|
970
|
+
resource.timer = undefined;
|
|
971
|
+
schedulePoll(resource);
|
|
972
|
+
}
|
|
973
|
+
else if (resource.timer) {
|
|
974
|
+
setTimerPersistence(resource);
|
|
975
|
+
}
|
|
976
|
+
else if (!resource.running) {
|
|
977
|
+
schedulePoll(resource);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Adds a subscriber to a Chokidar-owned polling resource.
|
|
982
|
+
*/
|
|
983
|
+
function setPollingListener(path, fullPath, options, scheduler, handlers, initialStats, signal) {
|
|
984
|
+
const resourceKey = fullPath;
|
|
985
|
+
const subscriber = { watchedPath: path, ...options, ...handlers };
|
|
986
|
+
let resources = PollingInstances.get(resourceKey);
|
|
987
|
+
if (!resources) {
|
|
988
|
+
resources = new Map();
|
|
989
|
+
PollingInstances.set(resourceKey, resources);
|
|
990
|
+
}
|
|
991
|
+
let resource = resources.get(scheduler);
|
|
992
|
+
if (!resource) {
|
|
993
|
+
resource = {
|
|
994
|
+
resource: resourceKey,
|
|
995
|
+
generation: allocateSharedResourceGeneration(),
|
|
996
|
+
scheduler,
|
|
997
|
+
subscribers: new Set(),
|
|
998
|
+
interval: options.interval,
|
|
999
|
+
persistent: options.persistent,
|
|
1000
|
+
previous: initialStats,
|
|
1001
|
+
running: false,
|
|
1002
|
+
closed: false,
|
|
1003
|
+
sequence: 0,
|
|
1004
|
+
};
|
|
1005
|
+
resources.set(scheduler, resource);
|
|
1006
|
+
}
|
|
1007
|
+
else if (!resource.previous && initialStats) {
|
|
1008
|
+
resource.previous = initialStats;
|
|
1009
|
+
}
|
|
1010
|
+
resource.subscribers.add(subscriber);
|
|
1011
|
+
reconfigurePollResource(resource);
|
|
1012
|
+
let closed = false;
|
|
1013
|
+
let closePromise;
|
|
1014
|
+
const subscription = {
|
|
1015
|
+
resource: resourceKey,
|
|
1016
|
+
close: () => {
|
|
1017
|
+
if (closePromise)
|
|
1018
|
+
return closePromise;
|
|
1019
|
+
closePromise = (async () => {
|
|
1020
|
+
if (closed)
|
|
1021
|
+
return;
|
|
1022
|
+
closed = true;
|
|
1023
|
+
resource.subscribers.delete(subscriber);
|
|
1024
|
+
if (resource.subscribers.size > 0) {
|
|
1025
|
+
reconfigurePollResource(resource);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (resource.closed)
|
|
1029
|
+
return;
|
|
1030
|
+
resource.closed = true;
|
|
1031
|
+
if (resource.timer)
|
|
1032
|
+
resource.scheduler.clearTimeout(resource.timer);
|
|
1033
|
+
resource.timer = undefined;
|
|
1034
|
+
const currentResources = PollingInstances.get(resourceKey);
|
|
1035
|
+
if (currentResources?.get(scheduler) === resource) {
|
|
1036
|
+
currentResources.delete(scheduler);
|
|
1037
|
+
if (currentResources.size === 0)
|
|
1038
|
+
PollingInstances.delete(resourceKey);
|
|
1039
|
+
}
|
|
1040
|
+
if (resource.currentPoll) {
|
|
1041
|
+
await Promise.allSettled([resource.currentPoll]);
|
|
1042
|
+
}
|
|
1043
|
+
})();
|
|
1044
|
+
return closePromise;
|
|
1045
|
+
},
|
|
1046
|
+
};
|
|
1047
|
+
if (signal.aborted) {
|
|
1048
|
+
void subscription.close();
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
return subscription;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* Owns every asynchronous task and backend subscription created by one public
|
|
1056
|
+
* watcher. It deliberately knows nothing about paths, events, or backends.
|
|
1057
|
+
*/
|
|
1058
|
+
class LifecycleScope {
|
|
1059
|
+
state = 'OPEN';
|
|
1060
|
+
generation = 0;
|
|
1061
|
+
abortController = new AbortController();
|
|
1062
|
+
tasks = new Set();
|
|
1063
|
+
closers = new Map();
|
|
1064
|
+
onTaskSettled;
|
|
1065
|
+
onTaskError;
|
|
1066
|
+
constructor(onTaskSettled, onTaskError) {
|
|
1067
|
+
this.onTaskSettled = onTaskSettled;
|
|
1068
|
+
this.onTaskError = onTaskError;
|
|
1069
|
+
}
|
|
1070
|
+
isActive(generation) {
|
|
1071
|
+
return this.state === 'OPEN' && this.generation === generation;
|
|
1072
|
+
}
|
|
1073
|
+
track(task) {
|
|
1074
|
+
const tracked = Promise.resolve(task).finally(() => {
|
|
1075
|
+
this.tasks.delete(tracked);
|
|
1076
|
+
this.onTaskSettled();
|
|
1077
|
+
});
|
|
1078
|
+
this.tasks.add(tracked);
|
|
1079
|
+
void tracked.catch(this.onTaskError);
|
|
1080
|
+
return tracked;
|
|
1081
|
+
}
|
|
1082
|
+
addCloser(key, closer) {
|
|
1083
|
+
const existing = this.closers.get(key);
|
|
1084
|
+
if (existing)
|
|
1085
|
+
existing.push(closer);
|
|
1086
|
+
else
|
|
1087
|
+
this.closers.set(key, [closer]);
|
|
1088
|
+
}
|
|
1089
|
+
takeClosers(key) {
|
|
1090
|
+
const closers = this.closers.get(key) ?? [];
|
|
1091
|
+
this.closers.delete(key);
|
|
1092
|
+
return closers;
|
|
1093
|
+
}
|
|
1094
|
+
beginClose() {
|
|
1095
|
+
if (this.state !== 'OPEN')
|
|
1096
|
+
return [];
|
|
1097
|
+
this.state = 'CLOSING';
|
|
1098
|
+
this.generation += 1;
|
|
1099
|
+
this.abortController.abort();
|
|
1100
|
+
const closing = [];
|
|
1101
|
+
this.closers.forEach((closers) => {
|
|
1102
|
+
closers.forEach((closer) => {
|
|
523
1103
|
try {
|
|
524
|
-
|
|
525
|
-
await fd.close();
|
|
526
|
-
broadcastErr(error);
|
|
1104
|
+
closing.push(Promise.resolve(closer()));
|
|
527
1105
|
}
|
|
528
|
-
catch (
|
|
529
|
-
|
|
1106
|
+
catch (error) {
|
|
1107
|
+
closing.push(Promise.reject(error));
|
|
530
1108
|
}
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
broadcastErr(error);
|
|
534
|
-
}
|
|
1109
|
+
});
|
|
535
1110
|
});
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
1111
|
+
this.closers.clear();
|
|
1112
|
+
return closing;
|
|
1113
|
+
}
|
|
1114
|
+
async drain() {
|
|
1115
|
+
while (this.tasks.size > 0) {
|
|
1116
|
+
await Promise.allSettled([...this.tasks]);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
finishClose() {
|
|
1120
|
+
this.state = 'CLOSED';
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
function IGNORE_RECONCILIATION_ERROR() { }
|
|
1124
|
+
function resolveRecursiveCandidate(root, relativePath) {
|
|
1125
|
+
if (sp__namespace.isAbsolute(relativePath))
|
|
1126
|
+
return;
|
|
1127
|
+
const absoluteRoot = sp__namespace.resolve(root);
|
|
1128
|
+
const absoluteCandidate = sp__namespace.resolve(absoluteRoot, relativePath);
|
|
1129
|
+
if (!isSameOrInside(absoluteRoot, absoluteCandidate))
|
|
1130
|
+
return;
|
|
1131
|
+
return sp__namespace.join(root, relativePath);
|
|
1132
|
+
}
|
|
1133
|
+
/** Serializes commits per root and coalesces redundant candidate invalidations. */
|
|
1134
|
+
class ReconciliationQueue {
|
|
1135
|
+
queues = new Map();
|
|
1136
|
+
pending = new Map();
|
|
1137
|
+
lifecycle;
|
|
1138
|
+
constructor(lifecycle) {
|
|
1139
|
+
this.lifecycle = lifecycle;
|
|
1140
|
+
}
|
|
1141
|
+
enqueue(scope, work, coalescePath = scope) {
|
|
1142
|
+
const key = logicalPathKey(scope);
|
|
1143
|
+
const pendingKey = coalescePath === false ? undefined : `${key}\0${logicalPathKey(coalescePath)}`;
|
|
1144
|
+
const existing = pendingKey ? this.pending.get(pendingKey) : undefined;
|
|
1145
|
+
if (existing) {
|
|
1146
|
+
existing.work = work;
|
|
1147
|
+
existing.replay = true;
|
|
1148
|
+
return existing.promise;
|
|
1149
|
+
}
|
|
1150
|
+
const generation = this.lifecycle.generation;
|
|
1151
|
+
const reconciliation = {
|
|
1152
|
+
promise: Promise.resolve(),
|
|
1153
|
+
work,
|
|
1154
|
+
replay: false,
|
|
541
1155
|
};
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
1156
|
+
const previous = this.queues.get(key) ?? Promise.resolve();
|
|
1157
|
+
const task = previous
|
|
1158
|
+
.catch(IGNORE_RECONCILIATION_ERROR)
|
|
1159
|
+
.then(async () => {
|
|
1160
|
+
do {
|
|
1161
|
+
const currentWork = reconciliation.work;
|
|
1162
|
+
reconciliation.replay = false;
|
|
1163
|
+
if (!this.lifecycle.isActive(generation))
|
|
1164
|
+
return;
|
|
1165
|
+
await currentWork();
|
|
1166
|
+
} while (reconciliation.replay);
|
|
1167
|
+
})
|
|
1168
|
+
.finally(() => {
|
|
1169
|
+
if (this.queues.get(key) === task)
|
|
1170
|
+
this.queues.delete(key);
|
|
1171
|
+
if (pendingKey && this.pending.get(pendingKey) === reconciliation) {
|
|
1172
|
+
this.pending.delete(pendingKey);
|
|
1173
|
+
}
|
|
1174
|
+
});
|
|
1175
|
+
this.queues.set(key, task);
|
|
1176
|
+
const tracked = this.lifecycle.track(task);
|
|
1177
|
+
reconciliation.promise = tracked;
|
|
1178
|
+
if (pendingKey)
|
|
1179
|
+
this.pending.set(pendingKey, reconciliation);
|
|
1180
|
+
return tracked;
|
|
1181
|
+
}
|
|
1182
|
+
forgetPending(predicate) {
|
|
1183
|
+
for (const key of this.pending.keys()) {
|
|
1184
|
+
const [scope, candidate] = key.split('\0', 2);
|
|
1185
|
+
if (predicate(scope, candidate))
|
|
1186
|
+
this.pending.delete(key);
|
|
561
1187
|
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
1188
|
+
}
|
|
1189
|
+
clear() {
|
|
1190
|
+
this.queues.clear();
|
|
1191
|
+
this.pending.clear();
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
const CURRENT = '.';
|
|
1195
|
+
const PARENT = '..';
|
|
1196
|
+
const RECURSIVE_CREATE_BURST_WINDOW = 25;
|
|
1197
|
+
const RECURSIVE_WRITE_BURST_WINDOW = 10;
|
|
1198
|
+
const INITIAL_OBSERVATION_FACT = Object.freeze({
|
|
1199
|
+
size: 0,
|
|
1200
|
+
mtimeMs: 0,
|
|
1201
|
+
ino: 0,
|
|
1202
|
+
kind: 'other',
|
|
1203
|
+
initialRecursive: true,
|
|
1204
|
+
});
|
|
1205
|
+
function statKind(stats) {
|
|
1206
|
+
return stats.isSymbolicLink()
|
|
1207
|
+
? 'symlink'
|
|
1208
|
+
: stats.isDirectory()
|
|
1209
|
+
? 'directory'
|
|
1210
|
+
: stats.isFile()
|
|
1211
|
+
? 'file'
|
|
1212
|
+
: 'other';
|
|
1213
|
+
}
|
|
1214
|
+
function sameStatFact(fact, stats) {
|
|
1215
|
+
const current = statIdentity(stats);
|
|
1216
|
+
return (fact?.size === current.size &&
|
|
1217
|
+
fact.mtimeMs === current.mtimeMs &&
|
|
1218
|
+
fact.ino === current.ino &&
|
|
1219
|
+
fact.kind === statKind(stats));
|
|
1220
|
+
}
|
|
1221
|
+
function withinWindow(fact, trigger, window) {
|
|
1222
|
+
return (fact?.observedAt !== undefined &&
|
|
1223
|
+
trigger.observedAt >= fact.observedAt &&
|
|
1224
|
+
trigger.observedAt - fact.observedAt <= window);
|
|
1225
|
+
}
|
|
1226
|
+
/** A directory membership snapshot owned by one public watcher. */
|
|
1227
|
+
class DirEntry {
|
|
1228
|
+
path;
|
|
1229
|
+
items = new Set();
|
|
1230
|
+
constructor(dir) {
|
|
1231
|
+
this.path = dir;
|
|
1232
|
+
}
|
|
1233
|
+
add(item) {
|
|
1234
|
+
// A filesystem root's basename is '' and its dirname is itself; listing ''
|
|
1235
|
+
// would make the root its own child and unlink everything after a scan.
|
|
1236
|
+
if (item !== CURRENT && item !== PARENT && item !== '')
|
|
1237
|
+
this.items.add(item);
|
|
1238
|
+
}
|
|
1239
|
+
/** Returns true when removing `item` left the directory empty. */
|
|
1240
|
+
remove(item) {
|
|
1241
|
+
return this.items.delete(item) && this.items.size === 0;
|
|
1242
|
+
}
|
|
1243
|
+
has(item) {
|
|
1244
|
+
return this.items.has(item);
|
|
1245
|
+
}
|
|
1246
|
+
getChildren() {
|
|
1247
|
+
return [...this.items];
|
|
1248
|
+
}
|
|
1249
|
+
dispose() {
|
|
1250
|
+
this.items.clear();
|
|
1251
|
+
this.path = '';
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
568
1254
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
* @param path to be watched
|
|
572
|
-
* @param fullPath absolute path
|
|
573
|
-
* @param options options to be passed to fs_watchFile
|
|
574
|
-
* @param handlers container for event listener functions
|
|
575
|
-
* @returns closer
|
|
1255
|
+
* Per-watcher filesystem truth. Backends never mutate this object directly;
|
|
1256
|
+
* scanner and reconciliation stages commit observations through it.
|
|
576
1257
|
*/
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
1258
|
+
class TreeState {
|
|
1259
|
+
watched = new Map();
|
|
1260
|
+
observed = new Map();
|
|
1261
|
+
symlinkPaths = new Map();
|
|
1262
|
+
recursiveRoots = new Set();
|
|
1263
|
+
observesNative;
|
|
1264
|
+
constructor(observesNative) {
|
|
1265
|
+
this.observesNative = observesNative;
|
|
1266
|
+
}
|
|
1267
|
+
getDirectory(directory) {
|
|
1268
|
+
const dir = sp__namespace.resolve(directory);
|
|
1269
|
+
const key = logicalPathKey(dir);
|
|
1270
|
+
let entry = this.watched.get(key);
|
|
1271
|
+
if (!entry) {
|
|
1272
|
+
entry = new DirEntry(dir);
|
|
1273
|
+
this.watched.set(key, entry);
|
|
1274
|
+
}
|
|
1275
|
+
return entry;
|
|
592
1276
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
1277
|
+
/** Looks up a directory entry without creating one. */
|
|
1278
|
+
peekDirectory(directory) {
|
|
1279
|
+
return this.watched.get(logicalPathKey(sp__namespace.resolve(directory)));
|
|
596
1280
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
1281
|
+
recordObserved(path, stats, transition, trigger, initialRecursive = false, retainWithoutTrigger = false) {
|
|
1282
|
+
if (!this.observesNative())
|
|
1283
|
+
return;
|
|
1284
|
+
if (!trigger && !initialRecursive && !retainWithoutTrigger)
|
|
1285
|
+
return;
|
|
1286
|
+
const { size, mtimeMs, ino } = statIdentity(stats);
|
|
1287
|
+
this.observed.set(logicalPathKey(path), {
|
|
1288
|
+
size,
|
|
1289
|
+
mtimeMs,
|
|
1290
|
+
ino,
|
|
1291
|
+
kind: statKind(stats),
|
|
1292
|
+
transition,
|
|
1293
|
+
rawEvent: trigger?.rawEvent,
|
|
1294
|
+
relativePath: trigger?.relativePath,
|
|
1295
|
+
observedAt: trigger?.observedAt,
|
|
1296
|
+
initialRecursive,
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
consumeInitialCreate(path, stats, trigger, ignoreInitial) {
|
|
1300
|
+
const previous = this.observed.get(logicalPathKey(path));
|
|
1301
|
+
if (!previous?.initialRecursive || trigger.rawEvent !== 'rename')
|
|
1302
|
+
return;
|
|
1303
|
+
this.recordObserved(path, stats, 'add', trigger);
|
|
1304
|
+
if (!ignoreInitial)
|
|
1305
|
+
return false;
|
|
1306
|
+
return stats.isDirectory() ? EVENTS.ADD_DIR : EVENTS.ADD;
|
|
1307
|
+
}
|
|
1308
|
+
markInitialCreate(path) {
|
|
1309
|
+
const key = logicalPathKey(path);
|
|
1310
|
+
const fact = this.observed.get(key);
|
|
1311
|
+
if (fact)
|
|
1312
|
+
fact.initialRecursive = true;
|
|
1313
|
+
else
|
|
1314
|
+
this.observed.set(key, { ...INITIAL_OBSERVATION_FACT });
|
|
1315
|
+
}
|
|
1316
|
+
clearInitialCreate(path) {
|
|
1317
|
+
const fact = this.observed.get(logicalPathKey(path));
|
|
1318
|
+
if (fact)
|
|
1319
|
+
fact.initialRecursive = false;
|
|
1320
|
+
}
|
|
1321
|
+
clearInitialCreates(root) {
|
|
1322
|
+
const rootKey = logicalPathKey(root);
|
|
1323
|
+
for (const [path, fact] of this.observed) {
|
|
1324
|
+
if (isSameOrInside(rootKey, path) && fact.initialRecursive)
|
|
1325
|
+
fact.initialRecursive = false;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
/** True when a tracked directory path now names a different directory inode. */
|
|
1329
|
+
replacedSinceObserved(path, stats) {
|
|
1330
|
+
const previous = this.observed.get(logicalPathKey(path));
|
|
1331
|
+
const { ino } = statIdentity(stats);
|
|
1332
|
+
return (previous?.kind === 'directory' &&
|
|
1333
|
+
stats.isDirectory() &&
|
|
1334
|
+
previous.ino !== 0 &&
|
|
1335
|
+
ino !== 0 &&
|
|
1336
|
+
previous.ino !== ino);
|
|
1337
|
+
}
|
|
1338
|
+
/** True when `stats` no longer matches a recorded fact of the same kind. */
|
|
1339
|
+
changedSinceObserved(path, stats) {
|
|
1340
|
+
const previous = this.observed.get(logicalPathKey(path));
|
|
1341
|
+
return (previous !== undefined && previous.kind === statKind(stats) && !sameStatFact(previous, stats));
|
|
1342
|
+
}
|
|
1343
|
+
isDuplicateObservation(path, stats, trigger) {
|
|
1344
|
+
const previous = this.observed.get(logicalPathKey(path));
|
|
1345
|
+
const sameFact = sameStatFact(previous, stats);
|
|
1346
|
+
// Burst windows only collapse callbacks that confirm the fact already
|
|
1347
|
+
// published. A different fact is a write that landed after that stat.
|
|
1348
|
+
const duplicateCreate = sameFact &&
|
|
1349
|
+
previous?.transition === 'add' &&
|
|
1350
|
+
previous?.relativePath === trigger.relativePath &&
|
|
1351
|
+
previous.rawEvent === 'rename' &&
|
|
1352
|
+
trigger.rawEvent === 'change' &&
|
|
1353
|
+
withinWindow(previous, trigger, RECURSIVE_CREATE_BURST_WINDOW);
|
|
1354
|
+
const duplicateUnchanged = sameFact &&
|
|
1355
|
+
stats.isFile() &&
|
|
1356
|
+
!previous?.initialRecursive &&
|
|
1357
|
+
(previous?.transition === 'add' || trigger.relativePath === null);
|
|
1358
|
+
const duplicateWrite = sameFact &&
|
|
1359
|
+
previous?.transition === 'change' &&
|
|
1360
|
+
previous.relativePath === trigger.relativePath &&
|
|
1361
|
+
withinWindow(previous, trigger, RECURSIVE_WRITE_BURST_WINDOW) &&
|
|
1362
|
+
previous.rawEvent === trigger.rawEvent;
|
|
1363
|
+
const duplicate = duplicateUnchanged || duplicateCreate || duplicateWrite;
|
|
1364
|
+
const transition = duplicateWrite
|
|
1365
|
+
? undefined
|
|
1366
|
+
: duplicateUnchanged
|
|
1367
|
+
? previous?.transition
|
|
1368
|
+
: 'change';
|
|
1369
|
+
this.recordObserved(path, stats, transition, trigger);
|
|
1370
|
+
return duplicate;
|
|
1371
|
+
}
|
|
1372
|
+
dispose() {
|
|
1373
|
+
this.watched.forEach((entry) => entry.dispose());
|
|
1374
|
+
this.watched.clear();
|
|
1375
|
+
this.observed.clear();
|
|
1376
|
+
this.symlinkPaths.clear();
|
|
1377
|
+
this.recursiveRoots.clear();
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* `writeFile` creates or truncates before it writes, and a callback can stat the
|
|
1383
|
+
* file in between (always on Windows with Node 22/24). An `add` or leading
|
|
1384
|
+
* `change` that sees an empty file is held until the data arrives, at most this
|
|
1385
|
+
* long: one event, with the written stats.
|
|
1386
|
+
*/
|
|
1387
|
+
const EMPTY_FILE_HOLD = 10;
|
|
1388
|
+
function isEmptyFile(stats) {
|
|
1389
|
+
return stats !== undefined && stats.isFile() && statIdentity(stats).size === 0;
|
|
1390
|
+
}
|
|
1391
|
+
function sameChangeStats(left, right) {
|
|
1392
|
+
if (left === undefined || right === undefined)
|
|
1393
|
+
return false;
|
|
1394
|
+
const a = statIdentity(left);
|
|
1395
|
+
const b = statIdentity(right);
|
|
1396
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs && a.ino === b.ino;
|
|
1397
|
+
}
|
|
1398
|
+
/** Converts truthful tree transitions into the public, timing-aware event API. */
|
|
1399
|
+
class EventPolicy {
|
|
1400
|
+
pendingWrites = new Map();
|
|
1401
|
+
pendingUnlinks = new Map();
|
|
1402
|
+
pendingChanges = new Map();
|
|
1403
|
+
heldAdds = new Map();
|
|
1404
|
+
throttled = new Map();
|
|
1405
|
+
context;
|
|
1406
|
+
constructor(context) {
|
|
1407
|
+
this.context = context;
|
|
1408
|
+
}
|
|
1409
|
+
presentPath(path) {
|
|
1410
|
+
if (isWindows)
|
|
1411
|
+
path = sp__namespace.normalize(path);
|
|
1412
|
+
return this.context.options.cwd ? sp__namespace.relative(this.context.options.cwd, path) : path;
|
|
1413
|
+
}
|
|
1414
|
+
dropHeldAdd(logicalKey) {
|
|
1415
|
+
const held = this.heldAdds.get(logicalKey);
|
|
1416
|
+
if (held)
|
|
1417
|
+
this.context.scheduler.clearTimeout(held.timer);
|
|
1418
|
+
this.heldAdds.delete(logicalKey);
|
|
1419
|
+
}
|
|
1420
|
+
dropPendingChange(logicalKey) {
|
|
1421
|
+
const pending = this.pendingChanges.get(logicalKey);
|
|
1422
|
+
if (pending?.held)
|
|
1423
|
+
this.context.scheduler.clearTimeout(pending.held.timer);
|
|
1424
|
+
this.pendingChanges.delete(logicalKey);
|
|
1425
|
+
}
|
|
1426
|
+
setTimeout(callback, delay) {
|
|
1427
|
+
const timer = this.context.scheduler.setTimeout(callback, delay);
|
|
1428
|
+
if (!this.context.options.persistent)
|
|
1429
|
+
timer.unref();
|
|
1430
|
+
return timer;
|
|
1431
|
+
}
|
|
1432
|
+
async emit(event, path, stats) {
|
|
1433
|
+
if (this.context.isClosed())
|
|
1434
|
+
return;
|
|
1435
|
+
const { options, scheduler } = this.context;
|
|
1436
|
+
const sourcePath = path;
|
|
1437
|
+
const logicalKey = logicalPathKey(path);
|
|
1438
|
+
const generation = this.context.lifecycle.generation;
|
|
1439
|
+
const pathGeneration = this.context.capturePathGeneration();
|
|
1440
|
+
path = this.presentPath(path);
|
|
1441
|
+
const args = [path];
|
|
1442
|
+
if (stats != null)
|
|
1443
|
+
args.push(stats);
|
|
1444
|
+
const heldAdd = this.heldAdds.get(logicalKey);
|
|
1445
|
+
if (heldAdd) {
|
|
1446
|
+
if (event === EVENTS.CHANGE) {
|
|
1447
|
+
// The data of a held create landed: announce the file with it.
|
|
1448
|
+
heldAdd.path = sourcePath;
|
|
1449
|
+
if (stats)
|
|
1450
|
+
heldAdd.stats = stats;
|
|
1451
|
+
if (!isEmptyFile(stats))
|
|
1452
|
+
heldAdd.release();
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
// Any other event for the path follows the add it held back.
|
|
1456
|
+
heldAdd.release();
|
|
1457
|
+
}
|
|
1458
|
+
if (event === EVENTS.UNLINK || event === EVENTS.UNLINK_DIR) {
|
|
1459
|
+
this.dropPendingChange(logicalKey);
|
|
1460
|
+
this.throttled.get(EVENTS.CHANGE)?.get(logicalKey)?.clear(false);
|
|
1461
|
+
}
|
|
1462
|
+
const awf = options.awaitWriteFinish;
|
|
1463
|
+
const pendingWrite = awf ? this.pendingWrites.get(logicalKey) : undefined;
|
|
1464
|
+
if (pendingWrite) {
|
|
1465
|
+
pendingWrite.lastChange = scheduler.now();
|
|
1466
|
+
return;
|
|
1467
|
+
}
|
|
1468
|
+
if (options.atomic) {
|
|
1469
|
+
if (event === EVENTS.UNLINK) {
|
|
1470
|
+
const existing = this.pendingUnlinks.get(logicalKey);
|
|
1471
|
+
if (existing)
|
|
1472
|
+
scheduler.clearTimeout(existing.timer);
|
|
1473
|
+
const entry = [event, ...args];
|
|
1474
|
+
const timer = this.setTimeout(() => {
|
|
1475
|
+
const pending = this.pendingUnlinks.get(logicalKey);
|
|
1476
|
+
if (!pending || pending.timer !== timer)
|
|
1477
|
+
return;
|
|
1478
|
+
this.pendingUnlinks.delete(logicalKey);
|
|
1479
|
+
if (this.context.isClosed())
|
|
1480
|
+
return;
|
|
1481
|
+
const [pendingEvent, ...pendingArgs] = pending.event;
|
|
1482
|
+
this.context.publish(pendingEvent, pendingArgs);
|
|
1483
|
+
}, typeof options.atomic === 'number' ? options.atomic : 100);
|
|
1484
|
+
this.pendingUnlinks.set(logicalKey, { event: entry, timer });
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
const pendingUnlink = this.pendingUnlinks.get(logicalKey);
|
|
1488
|
+
if (event === EVENTS.ADD && pendingUnlink) {
|
|
1489
|
+
event = EVENTS.CHANGE;
|
|
1490
|
+
scheduler.clearTimeout(pendingUnlink.timer);
|
|
1491
|
+
this.pendingUnlinks.delete(logicalKey);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this.context.isReady()) {
|
|
1495
|
+
const awfEmit = (error, currentStats) => {
|
|
1496
|
+
if (this.context.isClosed())
|
|
1497
|
+
return;
|
|
1498
|
+
if (error) {
|
|
1499
|
+
event = EVENTS.ERROR;
|
|
1500
|
+
args[0] = error;
|
|
1501
|
+
this.context.publish(event, args);
|
|
1502
|
+
}
|
|
1503
|
+
else if (currentStats) {
|
|
1504
|
+
if (args.length > 1)
|
|
1505
|
+
args[1] = currentStats;
|
|
1506
|
+
else
|
|
1507
|
+
args.push(currentStats);
|
|
1508
|
+
this.context.publish(event, args);
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
this.awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit, logicalKey);
|
|
1512
|
+
return;
|
|
1513
|
+
}
|
|
1514
|
+
if (event === EVENTS.ADD && isEmptyFile(stats) && this.context.isReady()) {
|
|
1515
|
+
const held = {
|
|
1516
|
+
path: sourcePath,
|
|
1517
|
+
stats,
|
|
1518
|
+
release: () => {
|
|
1519
|
+
this.context.scheduler.clearTimeout(held.timer);
|
|
1520
|
+
if (this.heldAdds.get(logicalKey) !== held)
|
|
1521
|
+
return;
|
|
1522
|
+
this.heldAdds.delete(logicalKey);
|
|
1523
|
+
if (this.context.isClosed() ||
|
|
1524
|
+
!this.context.lifecycle.isActive(generation) ||
|
|
1525
|
+
!this.context.isPathGenerationActive(logicalKey, pathGeneration)) {
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
const heldArgs = [this.presentPath(held.path)];
|
|
1529
|
+
if (held.stats)
|
|
1530
|
+
heldArgs.push(held.stats);
|
|
1531
|
+
this.context.publish(EVENTS.ADD, heldArgs);
|
|
1532
|
+
},
|
|
1533
|
+
};
|
|
1534
|
+
held.timer = this.setTimeout(held.release, EMPTY_FILE_HOLD);
|
|
1535
|
+
this.heldAdds.set(logicalKey, held);
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1538
|
+
if (event === EVENTS.CHANGE) {
|
|
1539
|
+
const throttler = this.throttle(EVENTS.CHANGE, sourcePath, 50, (suppressedCount) => {
|
|
1540
|
+
const pending = this.pendingChanges.get(logicalKey);
|
|
1541
|
+
this.dropPendingChange(logicalKey);
|
|
1542
|
+
if (suppressedCount > 0 &&
|
|
1543
|
+
pending?.replay &&
|
|
1544
|
+
this.context.lifecycle.isActive(generation) &&
|
|
1545
|
+
this.context.isPathGenerationActive(logicalKey, pathGeneration)) {
|
|
1546
|
+
void this.emit(EVENTS.CHANGE, pending.path, pending.stats);
|
|
612
1547
|
}
|
|
613
|
-
})
|
|
1548
|
+
});
|
|
1549
|
+
if (!throttler) {
|
|
1550
|
+
const pending = this.pendingChanges.get(logicalKey);
|
|
1551
|
+
if (pending && !sameChangeStats(pending.stats, stats)) {
|
|
1552
|
+
pending.path = sourcePath;
|
|
1553
|
+
pending.stats = stats;
|
|
1554
|
+
pending.replay = true;
|
|
1555
|
+
// The data of a held truncate-then-write landed: publish it now.
|
|
1556
|
+
if (pending.held && !isEmptyFile(stats))
|
|
1557
|
+
pending.held.release();
|
|
1558
|
+
}
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
const pending = { path: sourcePath, stats, replay: false };
|
|
1562
|
+
this.pendingChanges.set(logicalKey, pending);
|
|
1563
|
+
if (isEmptyFile(stats)) {
|
|
1564
|
+
// Publish the newest stats seen; later writes replay as usual.
|
|
1565
|
+
const release = () => {
|
|
1566
|
+
if (pending.held)
|
|
1567
|
+
this.context.scheduler.clearTimeout(pending.held.timer);
|
|
1568
|
+
pending.held = undefined;
|
|
1569
|
+
if (this.pendingChanges.get(logicalKey) !== pending ||
|
|
1570
|
+
this.context.isClosed() ||
|
|
1571
|
+
!this.context.lifecycle.isActive(generation) ||
|
|
1572
|
+
!this.context.isPathGenerationActive(logicalKey, pathGeneration)) {
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
pending.replay = false;
|
|
1576
|
+
const heldArgs = [this.presentPath(pending.path)];
|
|
1577
|
+
if (pending.stats)
|
|
1578
|
+
heldArgs.push(pending.stats);
|
|
1579
|
+
this.context.publish(EVENTS.CHANGE, heldArgs);
|
|
1580
|
+
};
|
|
1581
|
+
pending.held = { timer: this.setTimeout(release, EMPTY_FILE_HOLD), release };
|
|
1582
|
+
return;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
if (options.alwaysStat &&
|
|
1586
|
+
stats === undefined &&
|
|
1587
|
+
(event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
|
|
1588
|
+
const fullPath = options.cwd ? sp__namespace.join(options.cwd, path) : path;
|
|
1589
|
+
try {
|
|
1590
|
+
stats = await promises.stat(fullPath);
|
|
1591
|
+
}
|
|
1592
|
+
catch (error) {
|
|
1593
|
+
this.context.handleError(error);
|
|
1594
|
+
}
|
|
1595
|
+
if (!stats ||
|
|
1596
|
+
!this.context.lifecycle.isActive(generation) ||
|
|
1597
|
+
!this.context.isPathGenerationActive(logicalKey, pathGeneration)) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
args.push(stats);
|
|
1601
|
+
}
|
|
1602
|
+
this.context.publish(event, args);
|
|
1603
|
+
}
|
|
1604
|
+
throttle(actionType, path, timeout, onRelease) {
|
|
1605
|
+
let action = this.throttled.get(actionType);
|
|
1606
|
+
if (!action) {
|
|
1607
|
+
action = new Map();
|
|
1608
|
+
this.throttled.set(actionType, action);
|
|
1609
|
+
}
|
|
1610
|
+
const key = actionType === 'readdir' ? path : logicalPathKey(path);
|
|
1611
|
+
const active = action.get(key);
|
|
1612
|
+
if (active) {
|
|
1613
|
+
active.count += 1;
|
|
1614
|
+
return false;
|
|
1615
|
+
}
|
|
1616
|
+
let timeoutObject;
|
|
1617
|
+
const clear = (invokeRelease = true) => {
|
|
1618
|
+
const item = action.get(key);
|
|
1619
|
+
const count = item?.count ?? 0;
|
|
1620
|
+
action.delete(key);
|
|
1621
|
+
this.context.scheduler.clearTimeout(timeoutObject);
|
|
1622
|
+
if (item)
|
|
1623
|
+
this.context.scheduler.clearTimeout(item.timeoutObject);
|
|
1624
|
+
if (invokeRelease && onRelease)
|
|
1625
|
+
onRelease(count);
|
|
1626
|
+
return count;
|
|
614
1627
|
};
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
1628
|
+
timeoutObject = this.setTimeout(clear, timeout);
|
|
1629
|
+
const throttler = { timeoutObject, clear, count: 0 };
|
|
1630
|
+
action.set(key, throttler);
|
|
1631
|
+
return throttler;
|
|
1632
|
+
}
|
|
1633
|
+
awaitWriteFinish(path, threshold, event, awfEmit, logicalKey) {
|
|
1634
|
+
const awf = this.context.options.awaitWriteFinish;
|
|
1635
|
+
if (typeof awf !== 'object')
|
|
1636
|
+
return;
|
|
1637
|
+
const pollInterval = awf.pollInterval;
|
|
1638
|
+
let timeoutHandler;
|
|
1639
|
+
let fullPath = path;
|
|
1640
|
+
if (this.context.options.cwd && !sp__namespace.isAbsolute(path)) {
|
|
1641
|
+
fullPath = sp__namespace.join(this.context.options.cwd, path);
|
|
628
1642
|
}
|
|
1643
|
+
const writeKey = logicalKey ?? logicalPathKey(fullPath);
|
|
1644
|
+
if (this.pendingWrites.has(writeKey))
|
|
1645
|
+
return;
|
|
1646
|
+
const generation = this.context.lifecycle.generation;
|
|
1647
|
+
const pending = {
|
|
1648
|
+
lastChange: this.context.scheduler.now(),
|
|
1649
|
+
cancelWait: () => {
|
|
1650
|
+
if (this.pendingWrites.get(writeKey) === pending)
|
|
1651
|
+
this.pendingWrites.delete(writeKey);
|
|
1652
|
+
this.context.scheduler.clearTimeout(timeoutHandler);
|
|
1653
|
+
return event;
|
|
1654
|
+
},
|
|
1655
|
+
};
|
|
1656
|
+
// A stat in flight when this wait is cancelled must not adopt a newer wait for the path.
|
|
1657
|
+
const isCurrent = () => this.context.lifecycle.isActive(generation) && this.pendingWrites.get(writeKey) === pending;
|
|
1658
|
+
const inspect = (previous) => {
|
|
1659
|
+
const task = (async () => {
|
|
1660
|
+
let current;
|
|
1661
|
+
try {
|
|
1662
|
+
current = await promises.stat(fullPath);
|
|
1663
|
+
}
|
|
1664
|
+
catch (error) {
|
|
1665
|
+
if (!isCurrent())
|
|
1666
|
+
return;
|
|
1667
|
+
if (isMissingError(error)) {
|
|
1668
|
+
this.context.remove(sp__namespace.dirname(fullPath), sp__namespace.basename(fullPath));
|
|
1669
|
+
// Never leave the path suppressed if the removal did not cancel this wait.
|
|
1670
|
+
if (this.pendingWrites.get(writeKey) === pending)
|
|
1671
|
+
pending.cancelWait();
|
|
1672
|
+
}
|
|
1673
|
+
else {
|
|
1674
|
+
this.pendingWrites.delete(writeKey);
|
|
1675
|
+
awfEmit(error);
|
|
1676
|
+
}
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
if (!isCurrent())
|
|
1680
|
+
return;
|
|
1681
|
+
const now = this.context.scheduler.now();
|
|
1682
|
+
if (previous && current.size !== previous.size)
|
|
1683
|
+
pending.lastChange = now;
|
|
1684
|
+
if (now - pending.lastChange >= threshold) {
|
|
1685
|
+
this.pendingWrites.delete(writeKey);
|
|
1686
|
+
awfEmit(undefined, current);
|
|
1687
|
+
}
|
|
1688
|
+
else {
|
|
1689
|
+
timeoutHandler = this.setTimeout(() => inspect(current), pollInterval);
|
|
1690
|
+
}
|
|
1691
|
+
})();
|
|
1692
|
+
this.context.lifecycle.track(task);
|
|
1693
|
+
};
|
|
1694
|
+
this.pendingWrites.set(writeKey, pending);
|
|
1695
|
+
timeoutHandler = this.setTimeout(inspect, pollInterval);
|
|
1696
|
+
}
|
|
1697
|
+
cancelPath(path) {
|
|
1698
|
+
const logicalKey = logicalPathKey(path);
|
|
1699
|
+
const pendingUnlink = this.pendingUnlinks.get(logicalKey);
|
|
1700
|
+
if (pendingUnlink) {
|
|
1701
|
+
this.context.scheduler.clearTimeout(pendingUnlink.timer);
|
|
1702
|
+
this.pendingUnlinks.delete(logicalKey);
|
|
1703
|
+
}
|
|
1704
|
+
this.pendingWrites.get(logicalKey)?.cancelWait();
|
|
1705
|
+
this.dropPendingChange(logicalKey);
|
|
1706
|
+
this.dropHeldAdd(logicalKey);
|
|
1707
|
+
['watch', 'add', 'remove', 'change'].forEach((actionType) => {
|
|
1708
|
+
this.throttled.get(actionType)?.get(logicalKey)?.clear(false);
|
|
1709
|
+
});
|
|
1710
|
+
const readdirEntries = this.throttled.get('readdir');
|
|
1711
|
+
readdirEntries?.get(logicalKey)?.clear(false);
|
|
1712
|
+
readdirEntries
|
|
1713
|
+
?.get(`${logicalPathKey(sp__namespace.dirname(logicalKey))}\0${sp__namespace.basename(logicalKey)}`)
|
|
1714
|
+
?.clear(false);
|
|
1715
|
+
}
|
|
1716
|
+
cancelWhere(contains) {
|
|
1717
|
+
const paths = new Set([
|
|
1718
|
+
...this.pendingUnlinks.keys(),
|
|
1719
|
+
...this.pendingWrites.keys(),
|
|
1720
|
+
...this.pendingChanges.keys(),
|
|
1721
|
+
...this.heldAdds.keys(),
|
|
1722
|
+
]);
|
|
1723
|
+
[...paths].filter(contains).forEach((path) => this.cancelPath(path));
|
|
1724
|
+
this.throttled.forEach((entries, actionType) => {
|
|
1725
|
+
[...entries.keys()]
|
|
1726
|
+
.filter((key) => contains(actionType === 'readdir' ? key.split('\0', 1)[0] : key))
|
|
1727
|
+
.forEach((key) => entries.get(key)?.clear(false));
|
|
1728
|
+
});
|
|
1729
|
+
}
|
|
1730
|
+
close() {
|
|
1731
|
+
this.pendingWrites.forEach((pending) => pending.cancelWait());
|
|
1732
|
+
this.pendingUnlinks.forEach(({ timer }) => this.context.scheduler.clearTimeout(timer));
|
|
1733
|
+
this.pendingUnlinks.clear();
|
|
1734
|
+
this.pendingChanges.forEach(({ held }) => this.context.scheduler.clearTimeout(held?.timer));
|
|
1735
|
+
this.pendingChanges.clear();
|
|
1736
|
+
this.heldAdds.forEach(({ timer }) => this.context.scheduler.clearTimeout(timer));
|
|
1737
|
+
this.heldAdds.clear();
|
|
1738
|
+
this.throttled.forEach((entries) => {
|
|
1739
|
+
entries.forEach((throttler) => throttler.clear(false));
|
|
1740
|
+
});
|
|
1741
|
+
this.throttled.clear();
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
const EV = EVENTS;
|
|
1746
|
+
const RECURSIVE_TRIGGER_BUFFER_LIMIT = 1024;
|
|
1747
|
+
const MAX_NATIVE_RESUBSCRIBES = 3;
|
|
1748
|
+
const FALLBACK_SUBSCRIBE_CONCURRENCY = 16;
|
|
1749
|
+
const EXACT_CANDIDATE = Object.freeze({});
|
|
1750
|
+
/** An invalidation with no name: reconcile the whole subscribed path. */
|
|
1751
|
+
function recoveryTrigger(resource) {
|
|
1752
|
+
return {
|
|
1753
|
+
kind: 'native',
|
|
1754
|
+
resource: resource,
|
|
1755
|
+
rawEvent: EV.CHANGE,
|
|
1756
|
+
relativePath: null,
|
|
1757
|
+
sequence: Number.MAX_SAFE_INTEGER,
|
|
1758
|
+
observedAt: backendNow(),
|
|
629
1759
|
};
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
1760
|
+
}
|
|
1761
|
+
function sameMappedFileSnapshot(left, right) {
|
|
1762
|
+
if (left === false || right === false)
|
|
1763
|
+
return left === right;
|
|
1764
|
+
return (left.size === right.size &&
|
|
1765
|
+
left.mtimeMs === right.mtimeMs &&
|
|
1766
|
+
left.ctimeMs === right.ctimeMs &&
|
|
1767
|
+
left.ino === right.ino &&
|
|
1768
|
+
left.mode === right.mode);
|
|
1769
|
+
}
|
|
1770
|
+
class ReplayBuffer {
|
|
1771
|
+
entries = [];
|
|
1772
|
+
overflow;
|
|
1773
|
+
limit;
|
|
1774
|
+
constructor(limit) {
|
|
1775
|
+
this.limit = limit;
|
|
1776
|
+
}
|
|
1777
|
+
get overflowed() {
|
|
1778
|
+
return this.overflow !== undefined;
|
|
1779
|
+
}
|
|
1780
|
+
push(value) {
|
|
1781
|
+
if (this.entries.length < this.limit && !this.overflowed)
|
|
1782
|
+
this.entries.push(value);
|
|
1783
|
+
else
|
|
1784
|
+
this.overflow = value;
|
|
1785
|
+
}
|
|
1786
|
+
clear() {
|
|
1787
|
+
this.entries.length = 0;
|
|
1788
|
+
this.overflow = undefined;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
function consumeDirectoryStream(stream, onEntry) {
|
|
1792
|
+
const tasks = [];
|
|
1793
|
+
stream.on('data', (entry) => {
|
|
1794
|
+
let result;
|
|
1795
|
+
try {
|
|
1796
|
+
result = onEntry(entry);
|
|
1797
|
+
}
|
|
1798
|
+
catch (error) {
|
|
1799
|
+
result = Promise.reject(error);
|
|
1800
|
+
}
|
|
1801
|
+
if (!result)
|
|
1802
|
+
return;
|
|
1803
|
+
tasks.push(Promise.resolve(result));
|
|
1804
|
+
});
|
|
1805
|
+
return new Promise((resolve) => {
|
|
1806
|
+
stream.once('end', () => resolve({ complete: true }));
|
|
1807
|
+
stream.once('close', () => resolve({ complete: false }));
|
|
1808
|
+
stream.once(EV.ERROR, (error) => resolve({ complete: false, error }));
|
|
1809
|
+
}).then(async ({ complete, error }) => {
|
|
1810
|
+
const settled = await Promise.allSettled(tasks);
|
|
1811
|
+
const failures = settled
|
|
1812
|
+
.filter((result) => result.status === 'rejected')
|
|
1813
|
+
.map((result) => result.reason);
|
|
1814
|
+
return { complete: complete && failures.length === 0, error, failures };
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
const binaryExtensions = new Set(`3dm 3ds 3g2 3gp 7z a aac adp afdesign afphoto afpub ai aif aiff alz ape apk appimage ar arj asf au avi bak baml bh bin bk bmp btif bz2 bzip2 cab caf cgm class cmx cpio cr2 cur dat dcm deb dex djvu dll dmg dng doc docm docx dot dotm dra ds_store dsk dts dtshd dvb dwg dxf ecelp4800 ecelp7470 ecelp9600 egg eol eot epub exe f4v fbs fh fla flac flatpak fli flv fpx fst fvt g3 gh gif graffle gz gzip h261 h263 h264 icns ico ief img ipa iso jar jpeg jpg jpgv jpm jxr key ktx lha lib lvp lz lzh lzma lzo m3u m4a m4v mar mdi mht mid midi mj2 mka mkv mmr mng mobi mov movie mp3 mp4 mp4a mpeg mpg mpga mxu nef npx numbers nupkg o odp ods odt oga ogg ogv otf ott pages pbm pcx pdb pdf pea pgm pic png pnm pot potm potx ppa ppam ppm pps ppsm ppsx ppt pptm pptx psd pya pyc pyo pyv qt rar ras raw resources rgb rip rlc rmf rmvb rpm rtf rz s3m s7z scpt sgi shar snap sil sketch slk smv snk so stl suo sub swf tar tbz tbz2 tga tgz thmx tif tiff tlz ttc ttf txz udf uvh uvi uvm uvp uvs uvu viv vob war wav wax wbmp wdp weba webm webp whl wim wm wma wmv wmx woff woff2 wrm wvx xbm xif xla xlam xls xlsb xlsm xlsx xlt xltm xltx xm xmind xpi xpm xwd xz z zip zipx`.split(' '));
|
|
1818
|
+
function isBinaryPath(filePath) {
|
|
1819
|
+
return binaryExtensions.has(sp__namespace.extname(filePath).slice(1).toLowerCase());
|
|
1820
|
+
}
|
|
1821
|
+
/** The only scanner, subscriber, and filesystem transition engine. */
|
|
1822
|
+
class ObservationEngine {
|
|
635
1823
|
fsw;
|
|
636
|
-
|
|
1824
|
+
reportError;
|
|
637
1825
|
constructor(fsW) {
|
|
638
1826
|
this.fsw = fsW;
|
|
639
|
-
this.
|
|
1827
|
+
this.reportError = (error) => fsW.handleError(error);
|
|
640
1828
|
}
|
|
641
1829
|
/**
|
|
642
|
-
* Watch
|
|
1830
|
+
* Watch a path with the owned polling scheduler or fs.watch.
|
|
643
1831
|
* @param path to file or dir
|
|
644
1832
|
* @param listener on fs change
|
|
645
1833
|
* @returns closer for the watcher instance
|
|
646
1834
|
*/
|
|
647
|
-
|
|
1835
|
+
subscribePath(path, listener, initialStats, mapRelative = false, queueScope = path) {
|
|
648
1836
|
const opts = this.fsw.options;
|
|
649
|
-
const directory = sp__namespace.dirname(path);
|
|
650
1837
|
const basename = sp__namespace.basename(path);
|
|
651
|
-
const parent = this.fsw._getWatchedDir(directory);
|
|
652
|
-
parent.add(basename);
|
|
653
1838
|
const absolutePath = sp__namespace.resolve(path);
|
|
654
1839
|
const options = {
|
|
655
1840
|
persistent: opts.persistent,
|
|
656
1841
|
};
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
1842
|
+
const generation = this.fsw.lifecycle.generation;
|
|
1843
|
+
let active = true;
|
|
1844
|
+
const trackedListener = (watchedPath, stats, trigger) => {
|
|
1845
|
+
if (!active || !this.fsw.lifecycle.isActive(generation))
|
|
1846
|
+
return;
|
|
1847
|
+
return listener(watchedPath, stats, trigger);
|
|
1848
|
+
};
|
|
1849
|
+
const publish = (trigger) => {
|
|
1850
|
+
void this.reconcileBackendTrigger(path, mapRelative, trigger, (candidate) => trackedListener(candidate, trigger.kind === 'poll' && !isMissingObservation(trigger.current)
|
|
1851
|
+
? trigger.current
|
|
1852
|
+
: undefined, trigger), () => active && this.fsw.lifecycle.isActive(generation), true, queueScope);
|
|
1853
|
+
};
|
|
1854
|
+
let subscription;
|
|
1855
|
+
let resubscribes = 0;
|
|
1856
|
+
const subscribeNative = () => setFsWatchListener(path, absolutePath, options, {
|
|
1857
|
+
publish: (trigger) => {
|
|
1858
|
+
resubscribes = 0;
|
|
1859
|
+
publish(trigger);
|
|
1860
|
+
},
|
|
1861
|
+
errHandler: this.reportError,
|
|
1862
|
+
rawEmitter: (...args) => this.fsw.emitRaw(...args),
|
|
1863
|
+
failure: () => {
|
|
1864
|
+
if (!active || !this.fsw.lifecycle.isActive(generation))
|
|
1865
|
+
return;
|
|
1866
|
+
subscription = resubscribes++ < MAX_NATIVE_RESUBSCRIBES ? subscribeNative() : undefined;
|
|
1867
|
+
publish(recoveryTrigger(absolutePath));
|
|
1868
|
+
},
|
|
1869
|
+
}, this.fsw.lifecycle.abortController.signal);
|
|
1870
|
+
if (opts.backendCapabilities.polling) {
|
|
1871
|
+
const enableBin = opts.pollingInterval !== opts.pollingBinaryInterval;
|
|
1872
|
+
const pollingInterval = enableBin && isBinaryPath(basename) ? opts.pollingBinaryInterval : opts.pollingInterval;
|
|
1873
|
+
subscription = setPollingListener(path, absolutePath, { interval: pollingInterval, persistent: opts.persistent }, this.fsw.scheduler, {
|
|
1874
|
+
publish,
|
|
1875
|
+
errHandler: this.reportError,
|
|
1876
|
+
rawEmitter: (...args) => this.fsw.emitRaw(...args),
|
|
1877
|
+
}, initialStats, this.fsw.lifecycle.abortController.signal);
|
|
667
1878
|
}
|
|
668
1879
|
else {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
1880
|
+
subscription = subscribeNative();
|
|
1881
|
+
}
|
|
1882
|
+
if (!subscription)
|
|
1883
|
+
return;
|
|
1884
|
+
return () => {
|
|
1885
|
+
if (!active)
|
|
1886
|
+
return;
|
|
1887
|
+
active = false;
|
|
1888
|
+
return subscription?.close();
|
|
1889
|
+
};
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* Watch a followed file symlink through the target's parent directory.
|
|
1893
|
+
* macOS also starts a one-shot exact-target fallback: FSEvents can omit the
|
|
1894
|
+
* first directory callback immediately after a subscription is established.
|
|
1895
|
+
*/
|
|
1896
|
+
subscribeMappedFile(logicalPath, targetPath, helper, depth, useExactFallback = isMacos) {
|
|
1897
|
+
if (this.fsw.options.backendCapabilities.polling)
|
|
1898
|
+
return;
|
|
1899
|
+
const targetDirectory = sp__namespace.dirname(targetPath);
|
|
1900
|
+
const targetKey = logicalPathKey(targetPath);
|
|
1901
|
+
const logicalParent = sp__namespace.dirname(logicalPath);
|
|
1902
|
+
const generation = this.fsw.lifecycle.generation;
|
|
1903
|
+
let active = true;
|
|
1904
|
+
let exactActive = useExactFallback;
|
|
1905
|
+
let suppressDirectoryEcho = false;
|
|
1906
|
+
let fallbackSnapshot;
|
|
1907
|
+
let directorySubscription;
|
|
1908
|
+
let exactSubscription;
|
|
1909
|
+
const isActive = () => active && this.fsw.lifecycle.isActive(generation) && this.isHelperActive(helper, logicalPath);
|
|
1910
|
+
const matchesTarget = (relativePath) => {
|
|
1911
|
+
if (relativePath === null)
|
|
1912
|
+
return true;
|
|
1913
|
+
const candidate = sp__namespace.isAbsolute(relativePath)
|
|
1914
|
+
? relativePath
|
|
1915
|
+
: sp__namespace.resolve(targetDirectory, relativePath);
|
|
1916
|
+
const candidateKey = logicalPathKey(candidate);
|
|
1917
|
+
return isWindows
|
|
1918
|
+
? candidateKey.toLowerCase() === targetKey.toLowerCase()
|
|
1919
|
+
: candidateKey === targetKey;
|
|
1920
|
+
};
|
|
1921
|
+
const retireExact = () => {
|
|
1922
|
+
if (!exactActive)
|
|
1923
|
+
return;
|
|
1924
|
+
exactActive = false;
|
|
1925
|
+
if (exactSubscription)
|
|
1926
|
+
void exactSubscription.close();
|
|
1927
|
+
};
|
|
1928
|
+
const publish = (source, trigger) => {
|
|
1929
|
+
if (trigger.kind !== 'native' || !matchesTarget(trigger.relativePath) || !isActive())
|
|
1930
|
+
return;
|
|
1931
|
+
const deduplicateAgainstFallback = source === 'directory' && suppressDirectoryEcho;
|
|
1932
|
+
if (source === 'directory') {
|
|
1933
|
+
suppressDirectoryEcho = false;
|
|
1934
|
+
retireExact();
|
|
1935
|
+
}
|
|
1936
|
+
else if (directorySubscription) {
|
|
1937
|
+
suppressDirectoryEcho = true;
|
|
1938
|
+
retireExact();
|
|
1939
|
+
}
|
|
1940
|
+
void this.reconcileBackendTrigger(targetDirectory, true, trigger, async () => {
|
|
1941
|
+
if (deduplicateAgainstFallback && fallbackSnapshot !== undefined) {
|
|
1942
|
+
const current = await this.mappedFileSnapshot(targetPath);
|
|
1943
|
+
if (current !== undefined && sameMappedFileSnapshot(fallbackSnapshot, current))
|
|
1944
|
+
return;
|
|
1945
|
+
}
|
|
1946
|
+
await this.reconcileNativeTrigger(logicalParent, helper, trigger, logicalPath, depth - 1, EXACT_CANDIDATE);
|
|
1947
|
+
if (source === 'exact')
|
|
1948
|
+
fallbackSnapshot = await this.mappedFileSnapshot(targetPath);
|
|
1949
|
+
}, isActive, false, logicalPath);
|
|
1950
|
+
};
|
|
1951
|
+
let resubscribes = 0;
|
|
1952
|
+
const handlers = (source) => ({
|
|
1953
|
+
publish: (trigger) => {
|
|
1954
|
+
if (source === 'directory')
|
|
1955
|
+
resubscribes = 0;
|
|
1956
|
+
publish(source, trigger);
|
|
1957
|
+
},
|
|
1958
|
+
errHandler: this.reportError,
|
|
1959
|
+
rawEmitter: (event, relativePath) => {
|
|
1960
|
+
if ((source === 'directory' || exactActive) && matchesTarget(relativePath) && isActive()) {
|
|
1961
|
+
this.fsw.emitRaw(event, relativePath, { watchedPath: logicalPath });
|
|
1962
|
+
}
|
|
1963
|
+
},
|
|
1964
|
+
failure: () => {
|
|
1965
|
+
if (source === 'exact') {
|
|
1966
|
+
exactSubscription = undefined;
|
|
1967
|
+
exactActive = false;
|
|
1968
|
+
return;
|
|
1969
|
+
}
|
|
1970
|
+
directorySubscription = undefined;
|
|
1971
|
+
if (!isActive())
|
|
1972
|
+
return;
|
|
1973
|
+
// Re-establish the retired handle and reconcile anything it missed.
|
|
1974
|
+
if (resubscribes++ < MAX_NATIVE_RESUBSCRIBES)
|
|
1975
|
+
directorySubscription = subscribe(source);
|
|
1976
|
+
publish(source, recoveryTrigger(sp__namespace.resolve(targetDirectory)));
|
|
1977
|
+
},
|
|
1978
|
+
});
|
|
1979
|
+
const subscribe = (source) => {
|
|
1980
|
+
const path = source === 'directory' ? targetDirectory : targetPath;
|
|
1981
|
+
return setFsWatchListener(path, sp__namespace.resolve(path), { persistent: this.fsw.options.persistent }, handlers(source), this.fsw.lifecycle.abortController.signal);
|
|
1982
|
+
};
|
|
1983
|
+
directorySubscription = subscribe('directory');
|
|
1984
|
+
exactSubscription = useExactFallback ? subscribe('exact') : undefined;
|
|
1985
|
+
if (!exactSubscription)
|
|
1986
|
+
exactActive = false;
|
|
1987
|
+
if (!directorySubscription && !exactSubscription)
|
|
1988
|
+
return;
|
|
1989
|
+
return async () => {
|
|
1990
|
+
if (!active)
|
|
1991
|
+
return;
|
|
1992
|
+
active = false;
|
|
1993
|
+
const subscriptions = [directorySubscription, exactSubscription].filter((subscription) => subscription !== undefined);
|
|
1994
|
+
await Promise.allSettled(subscriptions.map((subscription) => subscription.close()));
|
|
1995
|
+
};
|
|
1996
|
+
}
|
|
1997
|
+
async mappedFileSnapshot(path) {
|
|
1998
|
+
try {
|
|
1999
|
+
return await promises.lstat(path);
|
|
2000
|
+
}
|
|
2001
|
+
catch (error) {
|
|
2002
|
+
if (isMissingError(error))
|
|
2003
|
+
return false;
|
|
2004
|
+
this.reportError(error);
|
|
2005
|
+
return;
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* macOS can follow an exact symlink handle and miss the link's own removal.
|
|
2010
|
+
* Check only link existence so target-directory activity stays invisible when
|
|
2011
|
+
* followSymlinks is disabled.
|
|
2012
|
+
*/
|
|
2013
|
+
watchSymlinkDeletion(path, force = false) {
|
|
2014
|
+
if ((!isMacos && !force) || this.fsw.options.backendCapabilities.polling)
|
|
2015
|
+
return;
|
|
2016
|
+
const fsw = this.fsw;
|
|
2017
|
+
const generation = fsw.lifecycle.generation;
|
|
2018
|
+
const directory = sp__namespace.dirname(path);
|
|
2019
|
+
const basename = sp__namespace.basename(path);
|
|
2020
|
+
let active = true;
|
|
2021
|
+
let timer;
|
|
2022
|
+
const schedule = () => {
|
|
2023
|
+
timer = fsw.scheduler.setTimeout(() => {
|
|
2024
|
+
timer = undefined;
|
|
2025
|
+
void (async () => {
|
|
2026
|
+
try {
|
|
2027
|
+
await promises.lstat(path);
|
|
2028
|
+
}
|
|
2029
|
+
catch (error) {
|
|
2030
|
+
if (!active || !fsw.lifecycle.isActive(generation))
|
|
2031
|
+
return;
|
|
2032
|
+
if (isMissingError(error)) {
|
|
2033
|
+
active = false;
|
|
2034
|
+
fsw.removePath(directory, basename);
|
|
2035
|
+
return;
|
|
2036
|
+
}
|
|
2037
|
+
this.reportError(error);
|
|
2038
|
+
}
|
|
2039
|
+
if (active && fsw.lifecycle.isActive(generation))
|
|
2040
|
+
schedule();
|
|
2041
|
+
})();
|
|
2042
|
+
}, fsw.options.pollingInterval);
|
|
2043
|
+
if (!fsw.options.persistent)
|
|
2044
|
+
timer.unref();
|
|
2045
|
+
};
|
|
2046
|
+
schedule();
|
|
2047
|
+
return () => {
|
|
2048
|
+
if (!active)
|
|
2049
|
+
return;
|
|
2050
|
+
active = false;
|
|
2051
|
+
fsw.scheduler.clearTimeout(timer);
|
|
2052
|
+
timer = undefined;
|
|
2053
|
+
};
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Map one backend notification into a logical candidate and serialize its
|
|
2057
|
+
* filesystem reconciliation in the owning watcher instance.
|
|
2058
|
+
*/
|
|
2059
|
+
reconcileBackendTrigger(scope, mapRelative, trigger, reconcile, isActive = () => true, coalesce = true, queueScope = scope) {
|
|
2060
|
+
let candidate = scope;
|
|
2061
|
+
if (mapRelative && trigger.kind === 'native' && trigger.relativePath !== null) {
|
|
2062
|
+
const resolved = resolveRecursiveCandidate(scope, trigger.relativePath);
|
|
2063
|
+
if (!resolved) {
|
|
2064
|
+
this.reportError(new Error(`Watcher reported a path outside its root: ${trigger.relativePath}`));
|
|
2065
|
+
return Promise.resolve();
|
|
2066
|
+
}
|
|
2067
|
+
candidate = resolved;
|
|
674
2068
|
}
|
|
675
|
-
|
|
2069
|
+
if (!isActive() || this.fsw.isUnwatched(candidate))
|
|
2070
|
+
return Promise.resolve();
|
|
2071
|
+
const rootKey = logicalPathKey(queueScope);
|
|
2072
|
+
const candidateKey = logicalPathKey(candidate);
|
|
2073
|
+
return this.fsw.reconciliation.enqueue(rootKey, async () => {
|
|
2074
|
+
if (!isActive() || this.fsw.isUnwatched(candidateKey))
|
|
2075
|
+
return;
|
|
2076
|
+
await reconcile(candidate);
|
|
2077
|
+
}, coalesce ? candidateKey : false);
|
|
676
2078
|
}
|
|
677
2079
|
/**
|
|
678
2080
|
* Watch a file and emit add event if warranted.
|
|
679
2081
|
* @returns closer for the watcher instance
|
|
680
2082
|
*/
|
|
681
|
-
|
|
2083
|
+
handleFile(file, stats, initialAdd, watchResource = true, trigger, initialRecursive = false, retainObservation = false) {
|
|
682
2084
|
if (this.fsw.closed) {
|
|
683
2085
|
return;
|
|
684
2086
|
}
|
|
685
2087
|
const dirname = sp__namespace.dirname(file);
|
|
686
2088
|
const basename = sp__namespace.basename(file);
|
|
687
|
-
const parent = this.fsw.
|
|
2089
|
+
const parent = this.fsw.tree.getDirectory(dirname);
|
|
688
2090
|
// stats is always present
|
|
689
2091
|
let prevStats = stats;
|
|
690
2092
|
// if the file is already being watched, do nothing
|
|
691
2093
|
if (parent.has(basename))
|
|
692
2094
|
return;
|
|
693
|
-
const
|
|
694
|
-
|
|
695
|
-
|
|
2095
|
+
const pathGeneration = this.fsw.capturePathGeneration();
|
|
2096
|
+
const isActive = () => this.fsw.isPathGenerationActive(file, pathGeneration);
|
|
2097
|
+
const listener = async (_path, newStats) => {
|
|
696
2098
|
if (!newStats || newStats.mtimeMs === 0) {
|
|
697
2099
|
try {
|
|
698
|
-
const newStats =
|
|
699
|
-
|
|
2100
|
+
const newStats = prevStats.isSymbolicLink() && !this.fsw.options.followSymlinks
|
|
2101
|
+
? await promises.lstat(file)
|
|
2102
|
+
: await promises.stat(file);
|
|
2103
|
+
// unwatch() may have retired this file while the stat was in flight.
|
|
2104
|
+
if (!isActive())
|
|
700
2105
|
return;
|
|
701
2106
|
// Check that change event was not fired because of changed only accessTime.
|
|
702
2107
|
const at = newStats.atimeMs;
|
|
703
2108
|
const mt = newStats.mtimeMs;
|
|
704
2109
|
if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
|
705
|
-
this.fsw.
|
|
2110
|
+
this.fsw.emitEvent(EV.CHANGE, file, newStats);
|
|
706
2111
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
2112
|
+
this.fsw.tree.recordObserved(file, newStats, 'change');
|
|
2113
|
+
prevStats = newStats;
|
|
2114
|
+
}
|
|
2115
|
+
catch (error) {
|
|
2116
|
+
if (!isActive())
|
|
2117
|
+
return;
|
|
2118
|
+
if (isMissingError(error)) {
|
|
2119
|
+
this.fsw.removePath(dirname, basename);
|
|
713
2120
|
}
|
|
714
2121
|
else {
|
|
715
|
-
|
|
2122
|
+
this.reportError(error);
|
|
716
2123
|
}
|
|
717
2124
|
}
|
|
718
|
-
catch (error) {
|
|
719
|
-
// Fix issues where mtime is null but file is still present
|
|
720
|
-
this.fsw._remove(dirname, basename);
|
|
721
|
-
}
|
|
722
2125
|
// add is about to be emitted if file not already tracked in parent
|
|
723
2126
|
}
|
|
724
2127
|
else if (parent.has(basename)) {
|
|
@@ -726,18 +2129,20 @@ class NodeFsHandler {
|
|
|
726
2129
|
const at = newStats.atimeMs;
|
|
727
2130
|
const mt = newStats.mtimeMs;
|
|
728
2131
|
if (!at || at <= mt || mt !== prevStats.mtimeMs) {
|
|
729
|
-
this.fsw.
|
|
2132
|
+
this.fsw.emitEvent(EV.CHANGE, file, newStats);
|
|
730
2133
|
}
|
|
2134
|
+
this.fsw.tree.recordObserved(file, newStats, 'change');
|
|
731
2135
|
prevStats = newStats;
|
|
732
2136
|
}
|
|
733
2137
|
};
|
|
734
2138
|
// kick off the watcher
|
|
735
|
-
const
|
|
2139
|
+
const watchesFile = watchResource && this.fsw.options.backendCapabilities.polling;
|
|
2140
|
+
const closer = watchesFile ? this.subscribePath(file, listener, stats) : undefined;
|
|
2141
|
+
parent.add(basename);
|
|
2142
|
+
this.fsw.tree.recordObserved(file, stats, 'add', trigger, initialRecursive, retainObservation);
|
|
736
2143
|
// emit an add event if we're supposed to
|
|
737
|
-
if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw.
|
|
738
|
-
|
|
739
|
-
return;
|
|
740
|
-
this.fsw._emit(EV.ADD, file, stats);
|
|
2144
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial) && !this.fsw.isIgnored(file, stats)) {
|
|
2145
|
+
this.fsw.emitEvent(EV.ADD, file, stats);
|
|
741
2146
|
}
|
|
742
2147
|
return closer;
|
|
743
2148
|
}
|
|
@@ -749,116 +2154,729 @@ class NodeFsHandler {
|
|
|
749
2154
|
* @param item basename of this item
|
|
750
2155
|
* @returns true if no more processing is needed for this entry.
|
|
751
2156
|
*/
|
|
752
|
-
async
|
|
753
|
-
if (this.fsw.closed) {
|
|
2157
|
+
async handleSymlink(entry, directory, path, item, initialAdd, wh) {
|
|
2158
|
+
if (this.fsw.closed || (wh && !this.isHelperActive(wh, path))) {
|
|
754
2159
|
return;
|
|
755
2160
|
}
|
|
756
|
-
const full = entry.fullPath;
|
|
757
|
-
const dir = this.fsw.
|
|
2161
|
+
const full = logicalPathKey(entry.fullPath);
|
|
2162
|
+
const dir = this.fsw.tree.getDirectory(directory);
|
|
758
2163
|
if (!this.fsw.options.followSymlinks) {
|
|
759
2164
|
// watch symlink directly (don't follow) and detect changes
|
|
760
|
-
this.fsw._incrReadyCount();
|
|
761
2165
|
let linkPath;
|
|
762
2166
|
try {
|
|
763
|
-
linkPath = await promises.
|
|
2167
|
+
linkPath = await promises.readlink(path);
|
|
764
2168
|
}
|
|
765
|
-
catch (
|
|
766
|
-
|
|
2169
|
+
catch (error) {
|
|
2170
|
+
if (!isMissingError(error))
|
|
2171
|
+
this.reportError(error);
|
|
2172
|
+
return true;
|
|
2173
|
+
}
|
|
2174
|
+
if (this.fsw.closed || (wh && !this.isHelperActive(wh, path))) {
|
|
767
2175
|
return true;
|
|
768
2176
|
}
|
|
769
|
-
if (this.fsw.closed)
|
|
770
|
-
return;
|
|
771
2177
|
if (dir.has(item)) {
|
|
772
|
-
if (this.fsw.
|
|
773
|
-
this.fsw.
|
|
774
|
-
this.fsw.
|
|
2178
|
+
if (this.fsw.tree.symlinkPaths.get(full) !== linkPath) {
|
|
2179
|
+
this.fsw.tree.symlinkPaths.set(full, linkPath);
|
|
2180
|
+
this.fsw.emitEvent(EV.CHANGE, path, entry.stats);
|
|
775
2181
|
}
|
|
776
2182
|
}
|
|
777
2183
|
else {
|
|
778
2184
|
dir.add(item);
|
|
779
|
-
this.fsw.
|
|
780
|
-
this.fsw.
|
|
2185
|
+
this.fsw.tree.symlinkPaths.set(full, linkPath);
|
|
2186
|
+
this.fsw.tree.recordObserved(path, entry.stats, 'add', wh?.observationTrigger, initialAdd && Boolean(wh?.recursiveRoot));
|
|
2187
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial)) {
|
|
2188
|
+
this.fsw.emitEvent(EV.ADD, path, entry.stats);
|
|
2189
|
+
}
|
|
781
2190
|
}
|
|
782
|
-
this.fsw._emitReady();
|
|
783
2191
|
return true;
|
|
784
2192
|
}
|
|
785
2193
|
// don't follow the same symlink more than once
|
|
786
|
-
if (this.fsw.
|
|
2194
|
+
if (this.fsw.tree.symlinkPaths.has(full)) {
|
|
787
2195
|
return true;
|
|
788
2196
|
}
|
|
789
|
-
this.fsw.
|
|
2197
|
+
this.fsw.tree.symlinkPaths.set(full, true);
|
|
790
2198
|
}
|
|
791
|
-
|
|
2199
|
+
readDirectory(directory, initialAdd, wh, target, dir, depth) {
|
|
2200
|
+
if (!this.isHelperActive(wh, directory))
|
|
2201
|
+
return;
|
|
792
2202
|
// Normalize the directory name on Windows
|
|
793
2203
|
directory = sp__namespace.join(directory, '');
|
|
794
|
-
const
|
|
795
|
-
|
|
796
|
-
if (!throttler)
|
|
797
|
-
return;
|
|
798
|
-
const previous = this.fsw._getWatchedDir(wh.path);
|
|
2204
|
+
const directoryKey = logicalPathKey(directory);
|
|
2205
|
+
const previous = this.fsw.tree.getDirectory(wh.watchPath);
|
|
799
2206
|
const current = new Set();
|
|
800
|
-
|
|
2207
|
+
const stream = this.fsw.createScanStream(directory, {
|
|
801
2208
|
fileFilter: (entry) => wh.filterPath(entry),
|
|
802
2209
|
directoryFilter: (entry) => wh.filterDir(entry),
|
|
803
2210
|
});
|
|
804
2211
|
if (!stream)
|
|
805
2212
|
return;
|
|
806
|
-
stream
|
|
807
|
-
.
|
|
808
|
-
|
|
809
|
-
|
|
2213
|
+
const scan = consumeDirectoryStream(stream, (entry) => {
|
|
2214
|
+
if (!this.isHelperActive(wh, directory))
|
|
2215
|
+
return;
|
|
2216
|
+
const stats = entry.stats;
|
|
2217
|
+
if (!stats)
|
|
810
2218
|
return;
|
|
811
|
-
}
|
|
812
2219
|
const item = entry.path;
|
|
813
2220
|
let path = sp__namespace.join(directory, item);
|
|
2221
|
+
if (!this.isHelperActive(wh, path))
|
|
2222
|
+
return;
|
|
814
2223
|
current.add(item);
|
|
815
|
-
|
|
816
|
-
|
|
2224
|
+
const shouldAdd = item === target || (!target && !previous.has(item));
|
|
2225
|
+
if (target === undefined && !stats.isSymbolicLink() && !stats.isDirectory()) {
|
|
2226
|
+
if (!shouldAdd)
|
|
2227
|
+
return;
|
|
2228
|
+
path = sp__namespace.join(dir, sp__namespace.relative(dir, path));
|
|
2229
|
+
const closer = this.handleFile(path, stats, initialAdd, !wh.recursiveRoot, wh.observationTrigger, initialAdd && Boolean(wh.recursiveRoot));
|
|
2230
|
+
if (closer)
|
|
2231
|
+
this.fsw.addPathCloser(path, closer);
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
return (async () => {
|
|
2235
|
+
if (stats.isSymbolicLink() &&
|
|
2236
|
+
(await this.handleSymlink(entry, directory, path, item, initialAdd, wh))) {
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
if (!this.isHelperActive(wh, path))
|
|
2240
|
+
return;
|
|
2241
|
+
// Files that present in current directory snapshot
|
|
2242
|
+
// but absent in previous are added to watch list and
|
|
2243
|
+
// emit `add` event.
|
|
2244
|
+
if (shouldAdd) {
|
|
2245
|
+
// ensure relativeness of path is preserved in case of watcher reuse
|
|
2246
|
+
path = sp__namespace.join(dir, sp__namespace.relative(dir, path));
|
|
2247
|
+
await this.addPathOnce(path, initialAdd, wh, depth + 1);
|
|
2248
|
+
}
|
|
2249
|
+
})();
|
|
2250
|
+
});
|
|
2251
|
+
return scan.then(async ({ complete, error, failures }) => {
|
|
2252
|
+
failures.forEach(this.reportError);
|
|
2253
|
+
if (error) {
|
|
2254
|
+
if (isMissingError(error) && this.isHelperActive(wh, directory)) {
|
|
2255
|
+
const affectedPath = target ? sp__namespace.join(directory, target) : directory;
|
|
2256
|
+
let disappeared = false;
|
|
2257
|
+
let becameNonDirectory = false;
|
|
2258
|
+
try {
|
|
2259
|
+
const currentStats = target ? await promises.lstat(affectedPath) : await promises.stat(affectedPath);
|
|
2260
|
+
becameNonDirectory = !target && !currentStats.isDirectory();
|
|
2261
|
+
}
|
|
2262
|
+
catch (stateError) {
|
|
2263
|
+
if (isMissingError(stateError))
|
|
2264
|
+
disappeared = true;
|
|
2265
|
+
else
|
|
2266
|
+
this.reportError(stateError);
|
|
2267
|
+
}
|
|
2268
|
+
if (target) {
|
|
2269
|
+
if (disappeared && previous.has(target))
|
|
2270
|
+
this.fsw.removePath(directory, target);
|
|
2271
|
+
}
|
|
2272
|
+
else if ((disappeared || becameNonDirectory) &&
|
|
2273
|
+
this.fsw.tree.watched.has(directoryKey)) {
|
|
2274
|
+
this.fsw.removePath(sp__namespace.dirname(directory), sp__namespace.basename(directory), true);
|
|
2275
|
+
if (becameNonDirectory) {
|
|
2276
|
+
await this.addPathOnce(directory, false, wh, depth);
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
else {
|
|
2281
|
+
this.reportError(error);
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
if (!complete || !this.isHelperActive(wh, directory)) {
|
|
817
2285
|
return;
|
|
818
2286
|
}
|
|
819
|
-
|
|
820
|
-
|
|
2287
|
+
// Entries tracked but absent from this listing are removed only once
|
|
2288
|
+
// lstat confirms they are gone: another queue may have added them after
|
|
2289
|
+
// the listing, and readdirp reports unreadable entries as warnings.
|
|
2290
|
+
await Promise.all(previous
|
|
2291
|
+
.getChildren()
|
|
2292
|
+
.filter((item) => !current.has(item))
|
|
2293
|
+
.map((item) => this.removeIfMissing(wh, directory, item, previous)));
|
|
2294
|
+
});
|
|
2295
|
+
}
|
|
2296
|
+
async removeIfMissing(wh, directory, item, entry) {
|
|
2297
|
+
const path = sp__namespace.join(directory, item);
|
|
2298
|
+
if (!this.isHelperActive(wh, path))
|
|
2299
|
+
return;
|
|
2300
|
+
try {
|
|
2301
|
+
await promises.lstat(path);
|
|
2302
|
+
return;
|
|
2303
|
+
}
|
|
2304
|
+
catch (error) {
|
|
2305
|
+
if (!isMissingError(error))
|
|
2306
|
+
return;
|
|
2307
|
+
}
|
|
2308
|
+
if (this.isHelperActive(wh, path) && entry.has(item))
|
|
2309
|
+
this.fsw.removePath(directory, item);
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Populate the logical directory tree beneath one native recursive watcher.
|
|
2313
|
+
* Regular entries are consumed synchronously from readdirp's stat-bearing
|
|
2314
|
+
* stream; only symlinks need the full per-path add machinery.
|
|
2315
|
+
*/
|
|
2316
|
+
scanRecursiveTree(dir, initialAdd, wh, baseDepth, rescan) {
|
|
2317
|
+
if (!this.isHelperActive(wh, dir))
|
|
2318
|
+
return;
|
|
2319
|
+
const fsw = this.fsw;
|
|
2320
|
+
const maxDepth = fsw.options.depth;
|
|
2321
|
+
const remainingDepth = maxDepth === undefined ? undefined : maxDepth - baseDepth;
|
|
2322
|
+
const stream = fsw.createScanStream(dir, {
|
|
2323
|
+
fileFilter: (entry) => wh.filterPath(entry),
|
|
2324
|
+
directoryFilter: (entry) => !entry.stats?.isSymbolicLink() && wh.filterDir(entry),
|
|
2325
|
+
depth: remainingDepth,
|
|
2326
|
+
});
|
|
2327
|
+
if (!stream)
|
|
2328
|
+
return;
|
|
2329
|
+
return consumeDirectoryStream(stream, (entry) => {
|
|
2330
|
+
if (!this.isHelperActive(wh, dir))
|
|
2331
|
+
return;
|
|
2332
|
+
const stats = entry.stats;
|
|
2333
|
+
if (!stats)
|
|
821
2334
|
return;
|
|
2335
|
+
const path = sp__namespace.join(dir, entry.path);
|
|
2336
|
+
if (!this.isHelperActive(wh, path))
|
|
2337
|
+
return;
|
|
2338
|
+
const directory = sp__namespace.dirname(path);
|
|
2339
|
+
const item = sp__namespace.basename(path);
|
|
2340
|
+
if (rescan) {
|
|
2341
|
+
rescan.seen.add(logicalPathKey(path));
|
|
2342
|
+
if (stats.isDirectory())
|
|
2343
|
+
rescan.directories.push(path);
|
|
822
2344
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
2345
|
+
if (stats.isSymbolicLink()) {
|
|
2346
|
+
return (async () => {
|
|
2347
|
+
if (await this.handleSymlink(entry, directory, path, item, initialAdd, wh))
|
|
2348
|
+
return;
|
|
2349
|
+
if (!this.isHelperActive(wh, path))
|
|
2350
|
+
return;
|
|
2351
|
+
const entryDepth = baseDepth + entry.path.split(/[/\\]/).length;
|
|
2352
|
+
await this.addPathOnce(path, initialAdd, wh, entryDepth);
|
|
2353
|
+
})();
|
|
831
2354
|
}
|
|
832
|
-
|
|
833
|
-
.
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
stream = undefined;
|
|
840
|
-
return;
|
|
2355
|
+
const parent = fsw.tree.getDirectory(directory);
|
|
2356
|
+
if (parent.has(item)) {
|
|
2357
|
+
// A rescan stands in for invalidations the backend dropped, so it also
|
|
2358
|
+
// reports writes to files whose recorded stat fact no longer matches.
|
|
2359
|
+
if (rescan && stats.isFile() && fsw.tree.changedSinceObserved(path, stats)) {
|
|
2360
|
+
fsw.tree.recordObserved(path, stats, 'change', undefined, false, true);
|
|
2361
|
+
fsw.emitEvent(EV.CHANGE, path, stats);
|
|
841
2362
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
this._handleRead(directory, false, wh, target, dir, depth, throttler);
|
|
859
|
-
});
|
|
2363
|
+
return;
|
|
2364
|
+
}
|
|
2365
|
+
parent.add(item);
|
|
2366
|
+
const isDirectory = stats.isDirectory();
|
|
2367
|
+
if (isDirectory)
|
|
2368
|
+
fsw.tree.getDirectory(path);
|
|
2369
|
+
fsw.tree.recordObserved(path, stats, 'add', wh.observationTrigger, initialAdd);
|
|
2370
|
+
if (!(initialAdd && fsw.options.ignoreInitial)) {
|
|
2371
|
+
fsw.emitEvent(isDirectory ? EV.ADD_DIR : EV.ADD, path, stats);
|
|
2372
|
+
}
|
|
2373
|
+
}).then((outcome) => {
|
|
2374
|
+
if (outcome.error)
|
|
2375
|
+
this.reportError(outcome.error);
|
|
2376
|
+
outcome.failures.forEach(this.reportError);
|
|
2377
|
+
if (rescan)
|
|
2378
|
+
rescan.complete = outcome.complete;
|
|
860
2379
|
});
|
|
861
2380
|
}
|
|
2381
|
+
/**
|
|
2382
|
+
* Reconcile a whole recursive root after an invalidation that names no path
|
|
2383
|
+
* (a backend buffer overflow) or a replay buffer overflow. A shallow root
|
|
2384
|
+
* listing would miss every change below the first level.
|
|
2385
|
+
*/
|
|
2386
|
+
async rescanRecursiveTree(dir, wh, baseDepth) {
|
|
2387
|
+
const rescan = { seen: new Set(), directories: [dir], complete: false };
|
|
2388
|
+
await this.scanRecursiveTree(dir, false, wh, baseDepth, rescan);
|
|
2389
|
+
if (!rescan.complete || !this.isHelperActive(wh, dir))
|
|
2390
|
+
return;
|
|
2391
|
+
// Only fully listed real directories can prove that a child is gone;
|
|
2392
|
+
// symlinked and ignored subtrees are not descended into.
|
|
2393
|
+
const missing = [];
|
|
2394
|
+
for (const directory of rescan.directories) {
|
|
2395
|
+
for (const item of this.fsw.tree.peekDirectory(directory)?.getChildren() ?? []) {
|
|
2396
|
+
const path = sp__namespace.join(directory, item);
|
|
2397
|
+
if (!rescan.seen.has(logicalPathKey(path)))
|
|
2398
|
+
missing.push(path);
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
missing.sort((left, right) => left.length - right.length);
|
|
2402
|
+
for (const path of missing) {
|
|
2403
|
+
const entry = this.fsw.tree.peekDirectory(sp__namespace.dirname(path));
|
|
2404
|
+
// An ancestor removed earlier in this pass already took its subtree.
|
|
2405
|
+
if (entry?.has(sp__namespace.basename(path))) {
|
|
2406
|
+
await this.removeIfMissing(wh, sp__namespace.dirname(path), sp__namespace.basename(path), entry);
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
isHelperActive(wh, path = wh.watchPath) {
|
|
2411
|
+
return this.fsw.isPathGenerationActive(path, wh.pathGeneration);
|
|
2412
|
+
}
|
|
2413
|
+
candidateDepth(root, candidate) {
|
|
2414
|
+
const relative = sp__namespace.relative(root, candidate);
|
|
2415
|
+
return relative ? relative.split(sp__namespace.sep).length : 0;
|
|
2416
|
+
}
|
|
2417
|
+
candidateIgnored(root, candidate, stats) {
|
|
2418
|
+
const relative = sp__namespace.relative(root, candidate);
|
|
2419
|
+
let ancestor = root;
|
|
2420
|
+
if (this.fsw.isIgnored(ancestor))
|
|
2421
|
+
return true;
|
|
2422
|
+
if (relative) {
|
|
2423
|
+
const parts = relative.split(sp__namespace.sep);
|
|
2424
|
+
for (let index = 0; index < parts.length - 1; index++) {
|
|
2425
|
+
ancestor = sp__namespace.join(ancestor, parts[index]);
|
|
2426
|
+
if (this.fsw.isIgnored(ancestor))
|
|
2427
|
+
return true;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
return this.fsw.isIgnored(candidate, stats);
|
|
2431
|
+
}
|
|
2432
|
+
async highestMissingPath(root, candidate) {
|
|
2433
|
+
let missing = candidate;
|
|
2434
|
+
let ancestor = sp__namespace.dirname(candidate);
|
|
2435
|
+
while (true) {
|
|
2436
|
+
try {
|
|
2437
|
+
await promises.lstat(ancestor);
|
|
2438
|
+
return missing;
|
|
2439
|
+
}
|
|
2440
|
+
catch (error) {
|
|
2441
|
+
if (!isMissingError(error)) {
|
|
2442
|
+
this.reportError(error);
|
|
2443
|
+
return missing;
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
missing = ancestor;
|
|
2447
|
+
if (ancestor === root)
|
|
2448
|
+
return missing;
|
|
2449
|
+
const parent = sp__namespace.dirname(ancestor);
|
|
2450
|
+
if (parent === ancestor)
|
|
2451
|
+
return missing;
|
|
2452
|
+
ancestor = parent;
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
async reconcileNativeTrigger(root, rootHelper, trigger, candidateOverride, baseDepth = 0, exact) {
|
|
2456
|
+
// An exact subscription observes one path inside `root`; an untracked or
|
|
2457
|
+
// unreadable candidate never justifies scanning its siblings.
|
|
2458
|
+
const fsw = this.fsw;
|
|
2459
|
+
if (!this.isHelperActive(rootHelper, root))
|
|
2460
|
+
return;
|
|
2461
|
+
let localHelper;
|
|
2462
|
+
const helper = () => (localHelper ??= rootHelper.withObservation(trigger));
|
|
2463
|
+
const candidate = candidateOverride ??
|
|
2464
|
+
(trigger.relativePath === null
|
|
2465
|
+
? root
|
|
2466
|
+
: resolveRecursiveCandidate(root, trigger.relativePath));
|
|
2467
|
+
if (!candidate) {
|
|
2468
|
+
this.reportError(new Error(`Native watcher reported a path outside its root: ${trigger.relativePath}`));
|
|
2469
|
+
return;
|
|
2470
|
+
}
|
|
2471
|
+
const depth = baseDepth + this.candidateDepth(root, candidate);
|
|
2472
|
+
const maxDepth = fsw.options.depth;
|
|
2473
|
+
if (maxDepth !== undefined && depth > maxDepth + 1)
|
|
2474
|
+
return;
|
|
2475
|
+
if (this.candidateIgnored(root, candidate))
|
|
2476
|
+
return;
|
|
2477
|
+
let stats;
|
|
2478
|
+
try {
|
|
2479
|
+
stats = await promises.lstat(candidate);
|
|
2480
|
+
}
|
|
2481
|
+
catch (error) {
|
|
2482
|
+
if (!isMissingError(error)) {
|
|
2483
|
+
this.reportError(error);
|
|
2484
|
+
if (exact)
|
|
2485
|
+
return;
|
|
2486
|
+
const ancestor = candidate === root ? root : sp__namespace.dirname(candidate);
|
|
2487
|
+
const ancestorDepth = Math.max(0, baseDepth + this.candidateDepth(root, ancestor));
|
|
2488
|
+
await this.readDirectory(ancestor, false, helper().fork(ancestor), undefined, ancestor, ancestorDepth);
|
|
2489
|
+
return;
|
|
2490
|
+
}
|
|
2491
|
+
if (fsw.closed)
|
|
2492
|
+
return;
|
|
2493
|
+
const missingPath = candidate === root ? root : await this.highestMissingPath(root, candidate);
|
|
2494
|
+
if (fsw.closed)
|
|
2495
|
+
return;
|
|
2496
|
+
if (missingPath === root) {
|
|
2497
|
+
fsw.removePath(sp__namespace.dirname(root), sp__namespace.basename(root), true);
|
|
2498
|
+
exact?.onRootMissing?.();
|
|
2499
|
+
return;
|
|
2500
|
+
}
|
|
2501
|
+
const parentPath = sp__namespace.dirname(missingPath);
|
|
2502
|
+
const item = sp__namespace.basename(missingPath);
|
|
2503
|
+
if (fsw.tree.getDirectory(parentPath).has(item)) {
|
|
2504
|
+
fsw.removePath(parentPath, item);
|
|
2505
|
+
}
|
|
2506
|
+
else if (!exact) {
|
|
2507
|
+
await this.readDirectory(parentPath, false, helper().fork(parentPath), undefined, parentPath, Math.max(0, baseDepth + this.candidateDepth(root, parentPath)));
|
|
2508
|
+
}
|
|
2509
|
+
return;
|
|
2510
|
+
}
|
|
2511
|
+
if (!this.isHelperActive(rootHelper, candidate) ||
|
|
2512
|
+
this.candidateIgnored(root, candidate, stats)) {
|
|
2513
|
+
return;
|
|
2514
|
+
}
|
|
2515
|
+
if (!stats.isDirectory() && !(stats.isSymbolicLink() && fsw.options.followSymlinks)) {
|
|
2516
|
+
const initialCreate = fsw.tree.consumeInitialCreate(candidate, stats, trigger, fsw.options.ignoreInitial);
|
|
2517
|
+
if (initialCreate !== undefined) {
|
|
2518
|
+
if (initialCreate)
|
|
2519
|
+
fsw.emitEvent(initialCreate, candidate, stats);
|
|
2520
|
+
return;
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
if (candidate === root) {
|
|
2524
|
+
if (!stats.isDirectory())
|
|
2525
|
+
return;
|
|
2526
|
+
// A nameless invalidation of a recursive root can stand for any change in the tree.
|
|
2527
|
+
if (rootHelper.recursiveRoot === root) {
|
|
2528
|
+
await this.rescanRecursiveTree(root, helper(), baseDepth);
|
|
2529
|
+
}
|
|
2530
|
+
else {
|
|
2531
|
+
await this.readDirectory(root, false, helper(), undefined, root, baseDepth);
|
|
2532
|
+
}
|
|
2533
|
+
return;
|
|
2534
|
+
}
|
|
2535
|
+
const parentPath = sp__namespace.dirname(candidate);
|
|
2536
|
+
const item = sp__namespace.basename(candidate);
|
|
2537
|
+
const parent = fsw.tree.getDirectory(parentPath);
|
|
2538
|
+
const tracked = parent.has(item);
|
|
2539
|
+
if (stats.isSymbolicLink()) {
|
|
2540
|
+
if (!fsw.options.followSymlinks) {
|
|
2541
|
+
let target = true;
|
|
2542
|
+
try {
|
|
2543
|
+
target = await promises.readlink(candidate);
|
|
2544
|
+
}
|
|
2545
|
+
catch { }
|
|
2546
|
+
if (!tracked)
|
|
2547
|
+
parent.add(item);
|
|
2548
|
+
fsw.tree.symlinkPaths.set(logicalPathKey(candidate), target);
|
|
2549
|
+
if (!tracked || !fsw.tree.isDuplicateObservation(candidate, stats, trigger)) {
|
|
2550
|
+
fsw.emitEvent(tracked ? EV.CHANGE : EV.ADD, candidate, stats);
|
|
2551
|
+
}
|
|
2552
|
+
if (!tracked)
|
|
2553
|
+
fsw.tree.recordObserved(candidate, stats, 'add', trigger);
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2556
|
+
const logicalKey = logicalPathKey(candidate);
|
|
2557
|
+
const previousTarget = fsw.tree.symlinkPaths.get(logicalKey);
|
|
2558
|
+
let currentTarget;
|
|
2559
|
+
try {
|
|
2560
|
+
currentTarget = await promises.realpath(candidate);
|
|
2561
|
+
}
|
|
2562
|
+
catch (error) {
|
|
2563
|
+
if (!isMissingError(error)) {
|
|
2564
|
+
this.reportError(error);
|
|
2565
|
+
return;
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
const targetChanged = tracked &&
|
|
2569
|
+
typeof previousTarget === 'string' &&
|
|
2570
|
+
(currentTarget === undefined ||
|
|
2571
|
+
logicalPathKey(previousTarget) !== logicalPathKey(currentTarget));
|
|
2572
|
+
if (targetChanged) {
|
|
2573
|
+
const previousKind = fsw.tree.observed.get(logicalKey)?.kind;
|
|
2574
|
+
fsw.removePath(parentPath, item, previousKind === 'directory');
|
|
2575
|
+
if (!currentTarget || !this.isHelperActive(rootHelper, candidate))
|
|
2576
|
+
return;
|
|
2577
|
+
const linkHelper = helper().fork(candidate);
|
|
2578
|
+
linkHelper.recursiveRoot = undefined;
|
|
2579
|
+
await this.addPathOnce(candidate, false, linkHelper, depth);
|
|
2580
|
+
return;
|
|
2581
|
+
}
|
|
2582
|
+
const initialCreate = fsw.tree.consumeInitialCreate(candidate, stats, trigger, fsw.options.ignoreInitial);
|
|
2583
|
+
if (initialCreate !== undefined) {
|
|
2584
|
+
if (initialCreate) {
|
|
2585
|
+
fsw.closePath(candidate, true);
|
|
2586
|
+
const linkHelper = helper().fork(candidate);
|
|
2587
|
+
linkHelper.recursiveRoot = undefined;
|
|
2588
|
+
await this.addPathOnce(candidate, false, linkHelper, depth);
|
|
2589
|
+
}
|
|
2590
|
+
return;
|
|
2591
|
+
}
|
|
2592
|
+
if (!tracked) {
|
|
2593
|
+
const linkHelper = helper().fork(candidate);
|
|
2594
|
+
linkHelper.recursiveRoot = undefined;
|
|
2595
|
+
await this.addPathOnce(candidate, false, linkHelper, depth);
|
|
2596
|
+
}
|
|
2597
|
+
else {
|
|
2598
|
+
fsw.emitEvent(EV.CHANGE, candidate, stats);
|
|
2599
|
+
}
|
|
2600
|
+
return;
|
|
2601
|
+
}
|
|
2602
|
+
if (stats.isDirectory()) {
|
|
2603
|
+
const directoryHelper = helper().fork(candidate);
|
|
2604
|
+
const initialCreate = fsw.tree.consumeInitialCreate(candidate, stats, trigger, fsw.options.ignoreInitial);
|
|
2605
|
+
if (initialCreate !== undefined) {
|
|
2606
|
+
if (initialCreate) {
|
|
2607
|
+
fsw.closePath(candidate, true);
|
|
2608
|
+
await this.addPathOnce(candidate, false, directoryHelper, depth);
|
|
2609
|
+
}
|
|
2610
|
+
return;
|
|
2611
|
+
}
|
|
2612
|
+
if (!tracked) {
|
|
2613
|
+
await this.addPathOnce(candidate, false, directoryHelper, depth);
|
|
2614
|
+
}
|
|
2615
|
+
else if (fsw.tree.replacedSinceObserved(candidate, stats)) {
|
|
2616
|
+
// Deleted and recreated before this callback ran: an inode-bound
|
|
2617
|
+
// handle still watches the old directory, so replace the whole entry.
|
|
2618
|
+
fsw.removePath(parentPath, item, true);
|
|
2619
|
+
await this.addPathOnce(candidate, false, directoryHelper, depth);
|
|
2620
|
+
}
|
|
2621
|
+
else if (maxDepth === undefined || depth <= maxDepth) {
|
|
2622
|
+
await this.readDirectory(candidate, false, directoryHelper, undefined, candidate, depth);
|
|
2623
|
+
}
|
|
2624
|
+
return;
|
|
2625
|
+
}
|
|
2626
|
+
if (!tracked) {
|
|
2627
|
+
await this.addPathOnce(candidate, false, helper(), depth);
|
|
2628
|
+
}
|
|
2629
|
+
else if (!fsw.tree.isDuplicateObservation(candidate, stats, trigger)) {
|
|
2630
|
+
fsw.emitEvent(EV.CHANGE, candidate, stats);
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
/**
|
|
2634
|
+
* Keep a Windows parent handle for a directly watched directory. Windows can
|
|
2635
|
+
* silently retire the directory's own handle when an empty root is removed;
|
|
2636
|
+
* its parent still receives the namespace transition and can reconcile it.
|
|
2637
|
+
*/
|
|
2638
|
+
watchRootParent(dir, wh) {
|
|
2639
|
+
if (!this.fsw.options.backendCapabilities.perDirectory || !isWindows)
|
|
2640
|
+
return;
|
|
2641
|
+
const parentPath = sp__namespace.dirname(dir);
|
|
2642
|
+
if (sp__namespace.resolve(parentPath) === sp__namespace.resolve(dir))
|
|
2643
|
+
return;
|
|
2644
|
+
const canonicalParent = sp__namespace.normalize(sp__namespace.toNamespacedPath(sp__namespace.resolve(parentPath))).toLowerCase();
|
|
2645
|
+
const rootName = sp__namespace.basename(dir).toLowerCase();
|
|
2646
|
+
const generation = this.fsw.lifecycle.generation;
|
|
2647
|
+
let active = true;
|
|
2648
|
+
const matchesRoot = (relativePath) => {
|
|
2649
|
+
if (relativePath === null)
|
|
2650
|
+
return true;
|
|
2651
|
+
const candidate = sp__namespace.isAbsolute(relativePath)
|
|
2652
|
+
? sp__namespace.resolve(relativePath)
|
|
2653
|
+
: sp__namespace.resolve(parentPath, relativePath);
|
|
2654
|
+
return (sp__namespace.normalize(sp__namespace.toNamespacedPath(sp__namespace.dirname(candidate))).toLowerCase() ===
|
|
2655
|
+
canonicalParent && sp__namespace.basename(candidate).toLowerCase() === rootName);
|
|
2656
|
+
};
|
|
2657
|
+
const isActive = () => active && this.fsw.lifecycle.isActive(generation) && this.isHelperActive(wh, dir);
|
|
2658
|
+
const publish = (trigger) => {
|
|
2659
|
+
if (trigger.kind !== 'native' || !matchesRoot(trigger.relativePath) || !isActive())
|
|
2660
|
+
return;
|
|
2661
|
+
void this.reconcileBackendTrigger(dir, false, trigger, async () => {
|
|
2662
|
+
let rootStats;
|
|
2663
|
+
try {
|
|
2664
|
+
rootStats = await promises.lstat(dir);
|
|
2665
|
+
}
|
|
2666
|
+
catch (error) {
|
|
2667
|
+
if (isMissingError(error)) {
|
|
2668
|
+
this.fsw.removePath(parentPath, sp__namespace.basename(dir), true);
|
|
2669
|
+
}
|
|
2670
|
+
else {
|
|
2671
|
+
this.reportError(error);
|
|
2672
|
+
}
|
|
2673
|
+
return;
|
|
2674
|
+
}
|
|
2675
|
+
if (!isActive())
|
|
2676
|
+
return;
|
|
2677
|
+
if (!rootStats.isDirectory()) {
|
|
2678
|
+
this.fsw.removePath(parentPath, sp__namespace.basename(dir), true);
|
|
2679
|
+
await this.addPathOnce(dir, false, wh, 0);
|
|
2680
|
+
return;
|
|
2681
|
+
}
|
|
2682
|
+
await this.readDirectory(dir, false, wh, undefined, dir, 0);
|
|
2683
|
+
}, isActive);
|
|
2684
|
+
};
|
|
2685
|
+
const subscription = setFsWatchListener(parentPath, sp__namespace.resolve(parentPath), { persistent: this.fsw.options.persistent }, {
|
|
2686
|
+
publish,
|
|
2687
|
+
errHandler: this.reportError,
|
|
2688
|
+
rawEmitter: (event, relativePath, details) => {
|
|
2689
|
+
if (matchesRoot(relativePath))
|
|
2690
|
+
this.fsw.emitRaw(event, relativePath, details);
|
|
2691
|
+
},
|
|
2692
|
+
}, this.fsw.lifecycle.abortController.signal);
|
|
2693
|
+
if (!subscription)
|
|
2694
|
+
return;
|
|
2695
|
+
return () => {
|
|
2696
|
+
if (!active)
|
|
2697
|
+
return;
|
|
2698
|
+
active = false;
|
|
2699
|
+
return subscription.close();
|
|
2700
|
+
};
|
|
2701
|
+
}
|
|
2702
|
+
async watchRecursiveDirectory(dir, initialAdd, depth, wh) {
|
|
2703
|
+
const fsw = this.fsw;
|
|
2704
|
+
const generation = fsw.lifecycle.generation;
|
|
2705
|
+
const rootKey = logicalPathKey(dir);
|
|
2706
|
+
const buffered = new ReplayBuffer(RECURSIVE_TRIGGER_BUFFER_LIMIT);
|
|
2707
|
+
const fallbackClosers = [];
|
|
2708
|
+
let phase = 'scanning';
|
|
2709
|
+
let pendingFailure;
|
|
2710
|
+
let hasPendingFailure = false;
|
|
2711
|
+
let recovery;
|
|
2712
|
+
let subscription;
|
|
2713
|
+
const scopeActive = (path = dir) => fsw.lifecycle.isActive(generation) && fsw.isPathGenerationActive(path, wh.pathGeneration);
|
|
2714
|
+
const acceptsTrigger = (path = dir) => phase !== 'recovering' && phase !== 'closed' && scopeActive(path);
|
|
2715
|
+
const establishFallback = async (error) => {
|
|
2716
|
+
if (!scopeActive())
|
|
2717
|
+
return;
|
|
2718
|
+
wh.recursiveRoot = undefined;
|
|
2719
|
+
wh.recursiveDisabled = true;
|
|
2720
|
+
let currentStats;
|
|
2721
|
+
try {
|
|
2722
|
+
currentStats = await promises.lstat(dir);
|
|
2723
|
+
}
|
|
2724
|
+
catch (statError) {
|
|
2725
|
+
if (isMissingError(statError))
|
|
2726
|
+
fsw.removePath(sp__namespace.dirname(dir), sp__namespace.basename(dir), true);
|
|
2727
|
+
else
|
|
2728
|
+
this.reportError(statError);
|
|
2729
|
+
this.reportError(error);
|
|
2730
|
+
return;
|
|
2731
|
+
}
|
|
2732
|
+
const closer = await this.handleDirectory(dir, currentStats, false, depth, undefined, wh);
|
|
2733
|
+
if (!scopeActive()) {
|
|
2734
|
+
if (closer)
|
|
2735
|
+
await closer();
|
|
2736
|
+
return;
|
|
2737
|
+
}
|
|
2738
|
+
if (closer)
|
|
2739
|
+
fallbackClosers.push(closer);
|
|
2740
|
+
await this.subscribeTrackedDirectories(dir, wh, depth);
|
|
2741
|
+
this.reportError(error);
|
|
2742
|
+
};
|
|
2743
|
+
const handleFailure = (error) => {
|
|
2744
|
+
if (!acceptsTrigger())
|
|
2745
|
+
return;
|
|
2746
|
+
const initializing = phase !== 'live';
|
|
2747
|
+
phase = 'recovering';
|
|
2748
|
+
fsw.tree.recursiveRoots.delete(rootKey);
|
|
2749
|
+
pendingFailure = error;
|
|
2750
|
+
hasPendingFailure = true;
|
|
2751
|
+
if (!initializing) {
|
|
2752
|
+
recovery = fsw.reconciliation.enqueue(dir, () => establishFallback(error), false);
|
|
2753
|
+
}
|
|
2754
|
+
};
|
|
2755
|
+
const processTrigger = (trigger) => {
|
|
2756
|
+
if (!acceptsTrigger())
|
|
2757
|
+
return;
|
|
2758
|
+
if (phase !== 'live') {
|
|
2759
|
+
buffered.push(trigger);
|
|
2760
|
+
return;
|
|
2761
|
+
}
|
|
2762
|
+
const coalescePath = trigger.relativePath === null
|
|
2763
|
+
? dir
|
|
2764
|
+
: (resolveRecursiveCandidate(dir, trigger.relativePath) ?? dir);
|
|
2765
|
+
void this.reconcileBackendTrigger(dir, true, trigger, () => (acceptsTrigger() ? this.reconcileNativeTrigger(dir, wh, trigger) : undefined), () => acceptsTrigger(coalescePath));
|
|
2766
|
+
};
|
|
2767
|
+
const result = subscribeRecursiveNative(dir, fsw.options.persistent, {
|
|
2768
|
+
publish: processTrigger,
|
|
2769
|
+
failure: handleFailure,
|
|
2770
|
+
rawEmitter: (...args) => fsw.emitRaw(...args),
|
|
2771
|
+
}, fsw.lifecycle.abortController.signal);
|
|
2772
|
+
if (result.kind === 'unsupported')
|
|
2773
|
+
return false;
|
|
2774
|
+
if (result.kind === 'failed')
|
|
2775
|
+
throw result.error;
|
|
2776
|
+
subscription = result.subscription;
|
|
2777
|
+
fsw.tree.recursiveRoots.add(rootKey);
|
|
2778
|
+
wh.recursiveRoot = dir;
|
|
2779
|
+
const close = async () => {
|
|
2780
|
+
if (phase === 'closed')
|
|
2781
|
+
return;
|
|
2782
|
+
phase = 'closed';
|
|
2783
|
+
fsw.tree.recursiveRoots.delete(rootKey);
|
|
2784
|
+
buffered.clear();
|
|
2785
|
+
await subscription?.close();
|
|
2786
|
+
if (recovery)
|
|
2787
|
+
await Promise.allSettled([recovery]);
|
|
2788
|
+
await Promise.allSettled(fallbackClosers.map((closer) => closer()));
|
|
2789
|
+
};
|
|
2790
|
+
try {
|
|
2791
|
+
await this.scanRecursiveTree(dir, initialAdd, wh, depth);
|
|
2792
|
+
if (!hasPendingFailure) {
|
|
2793
|
+
phase = 'replaying';
|
|
2794
|
+
let index = 0;
|
|
2795
|
+
while (acceptsTrigger()) {
|
|
2796
|
+
if (buffered.overflowed) {
|
|
2797
|
+
// Collapse to one tree rescan; triggers arriving during it are
|
|
2798
|
+
// buffered again and replayed instead of being discarded.
|
|
2799
|
+
buffered.clear();
|
|
2800
|
+
index = 0;
|
|
2801
|
+
const overflowTrigger = {
|
|
2802
|
+
kind: 'native',
|
|
2803
|
+
resource: subscription.resource,
|
|
2804
|
+
rawEvent: EV.CHANGE,
|
|
2805
|
+
relativePath: null,
|
|
2806
|
+
sequence: Number.MAX_SAFE_INTEGER,
|
|
2807
|
+
observedAt: backendNow(),
|
|
2808
|
+
};
|
|
2809
|
+
await this.reconcileBackendTrigger(dir, true, overflowTrigger, () => this.reconcileNativeTrigger(dir, wh, overflowTrigger), () => acceptsTrigger(), false);
|
|
2810
|
+
continue;
|
|
2811
|
+
}
|
|
2812
|
+
if (index >= buffered.entries.length)
|
|
2813
|
+
break;
|
|
2814
|
+
const trigger = buffered.entries[index++];
|
|
2815
|
+
if (trigger.rawEvent === 'rename' && trigger.relativePath !== null) {
|
|
2816
|
+
const candidate = resolveRecursiveCandidate(dir, trigger.relativePath);
|
|
2817
|
+
if (candidate &&
|
|
2818
|
+
candidate !== dir &&
|
|
2819
|
+
fsw.tree.getDirectory(sp__namespace.dirname(candidate)).has(sp__namespace.basename(candidate))) {
|
|
2820
|
+
fsw.tree.markInitialCreate(candidate);
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
await this.reconcileBackendTrigger(dir, true, trigger, () => this.reconcileNativeTrigger(dir, wh, trigger), () => acceptsTrigger(), false);
|
|
2824
|
+
}
|
|
2825
|
+
if (phase !== 'recovering')
|
|
2826
|
+
phase = 'live';
|
|
2827
|
+
}
|
|
2828
|
+
if (hasPendingFailure) {
|
|
2829
|
+
// A failure while scanning or replaying enqueued no recovery; there is
|
|
2830
|
+
// no live handle to recover into, so fall back now.
|
|
2831
|
+
buffered.clear();
|
|
2832
|
+
await establishFallback(pendingFailure);
|
|
2833
|
+
}
|
|
2834
|
+
buffered.clear();
|
|
2835
|
+
fsw.tree.clearInitialCreates(dir);
|
|
2836
|
+
}
|
|
2837
|
+
catch (error) {
|
|
2838
|
+
await close();
|
|
2839
|
+
throw error;
|
|
2840
|
+
}
|
|
2841
|
+
if (!scopeActive()) {
|
|
2842
|
+
await close();
|
|
2843
|
+
return;
|
|
2844
|
+
}
|
|
2845
|
+
return close;
|
|
2846
|
+
}
|
|
2847
|
+
/**
|
|
2848
|
+
* A recursive root that falls back to per-directory handles already tracks
|
|
2849
|
+
* every subdirectory, so the fallback scan sees none of them as new. Give
|
|
2850
|
+
* each its own subscription; existing entries emit nothing.
|
|
2851
|
+
*/
|
|
2852
|
+
async subscribeTrackedDirectories(root, wh, baseDepth) {
|
|
2853
|
+
const fsw = this.fsw;
|
|
2854
|
+
const absoluteRoot = sp__namespace.resolve(root);
|
|
2855
|
+
const needsSubscription = (path) => {
|
|
2856
|
+
const key = logicalPathKey(path);
|
|
2857
|
+
return (this.isHelperActive(wh, path) &&
|
|
2858
|
+
!fsw.lifecycle.closers.has(key) &&
|
|
2859
|
+
!fsw.tree.symlinkPaths.has(key) &&
|
|
2860
|
+
(fsw.tree.peekDirectory(sp__namespace.dirname(path))?.has(sp__namespace.basename(path)) ?? false));
|
|
2861
|
+
};
|
|
2862
|
+
const directories = [...fsw.tree.watched.values()]
|
|
2863
|
+
.filter((entry) => isStrictlyInside(absoluteRoot, entry.path))
|
|
2864
|
+
// Keep the root's spelling so events stay relative when the root was.
|
|
2865
|
+
.map((entry) => sp__namespace.join(root, sp__namespace.relative(absoluteRoot, entry.path)))
|
|
2866
|
+
.filter(needsSubscription)
|
|
2867
|
+
.sort((left, right) => left.length - right.length);
|
|
2868
|
+
let next = 0;
|
|
2869
|
+
const subscribeNext = async () => {
|
|
2870
|
+
while (next < directories.length) {
|
|
2871
|
+
const directory = directories[next++];
|
|
2872
|
+
if (!needsSubscription(directory))
|
|
2873
|
+
continue;
|
|
2874
|
+
await this.addPathOnce(directory, false, wh, baseDepth + this.candidateDepth(root, directory));
|
|
2875
|
+
}
|
|
2876
|
+
};
|
|
2877
|
+
const workers = Math.min(FALLBACK_SUBSCRIBE_CONCURRENCY, directories.length);
|
|
2878
|
+
await Promise.all(Array.from({ length: workers }, subscribeNext));
|
|
2879
|
+
}
|
|
862
2880
|
/**
|
|
863
2881
|
* Read directory to add / remove files from `@watched` list and re-read it on change.
|
|
864
2882
|
* @param dir fs path
|
|
@@ -867,318 +2885,416 @@ class NodeFsHandler {
|
|
|
867
2885
|
* @param depth relative to user-supplied path
|
|
868
2886
|
* @param target child path targeted for watch
|
|
869
2887
|
* @param wh Common watch helpers for this path
|
|
870
|
-
* @param realpath
|
|
871
2888
|
* @returns closer for the watcher instance.
|
|
872
2889
|
*/
|
|
873
|
-
async
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
if (
|
|
877
|
-
this.fsw.
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
2890
|
+
async handleDirectory(dir, stats, initialAdd, depth, target, wh) {
|
|
2891
|
+
if (!this.isHelperActive(wh, dir))
|
|
2892
|
+
return;
|
|
2893
|
+
if (target) {
|
|
2894
|
+
this.fsw.tree.getDirectory(dir);
|
|
2895
|
+
}
|
|
2896
|
+
else {
|
|
2897
|
+
const parentDir = this.fsw.tree.getDirectory(sp__namespace.dirname(dir));
|
|
2898
|
+
const tracked = parentDir.has(sp__namespace.basename(dir));
|
|
2899
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial) && !tracked) {
|
|
2900
|
+
this.fsw.emitEvent(EV.ADD_DIR, dir, stats);
|
|
2901
|
+
}
|
|
2902
|
+
// ensure dir is tracked (harmless if redundant)
|
|
2903
|
+
parentDir.add(sp__namespace.basename(dir));
|
|
2904
|
+
this.fsw.tree.getDirectory(dir);
|
|
2905
|
+
// Always retain the directory's identity so a delete-and-recreate that
|
|
2906
|
+
// outpaces its callbacks can be told apart from a content change.
|
|
2907
|
+
this.fsw.tree.recordObserved(dir, stats, tracked ? undefined : 'add', wh.observationTrigger, initialAdd && Boolean(wh.recursiveRoot), true);
|
|
2908
|
+
}
|
|
883
2909
|
let closer;
|
|
884
2910
|
const oDepth = this.fsw.options.depth;
|
|
885
|
-
if (
|
|
886
|
-
if (
|
|
887
|
-
|
|
888
|
-
|
|
2911
|
+
if (oDepth == null || depth <= oDepth) {
|
|
2912
|
+
if (this.fsw.options.backendCapabilities.recursive &&
|
|
2913
|
+
depth === 0 &&
|
|
2914
|
+
!target &&
|
|
2915
|
+
!wh.recursiveRoot &&
|
|
2916
|
+
!wh.recursiveDisabled) {
|
|
2917
|
+
const recursiveCloser = await this.watchRecursiveDirectory(dir, initialAdd, depth, wh);
|
|
2918
|
+
if (recursiveCloser !== false)
|
|
2919
|
+
return recursiveCloser;
|
|
2920
|
+
}
|
|
2921
|
+
if (wh.recursiveRoot) {
|
|
2922
|
+
if (!target) {
|
|
2923
|
+
await this.scanRecursiveTree(dir, initialAdd, wh, depth);
|
|
2924
|
+
}
|
|
2925
|
+
return;
|
|
2926
|
+
}
|
|
2927
|
+
const buffered = new ReplayBuffer(RECURSIVE_TRIGGER_BUFFER_LIMIT);
|
|
2928
|
+
const replayCreates = new Set();
|
|
2929
|
+
let initializing = !target;
|
|
2930
|
+
let active = true;
|
|
2931
|
+
// FSEvents can drop the first callbacks after a stream starts while later
|
|
2932
|
+
// ones arrive, so the first live callback also rescans the directory.
|
|
2933
|
+
let rescanOnFirstCallback = isMacos && !target;
|
|
2934
|
+
const targetPath = target ? sp__namespace.join(dir, target) : undefined;
|
|
2935
|
+
// The directory holding the target is gone: wait from the nearest existing ancestor.
|
|
2936
|
+
const rewatchTarget = () => {
|
|
2937
|
+
if (targetPath && active && this.isHelperActive(wh, targetPath)) {
|
|
2938
|
+
this.fsw.rewatchMissingPath(targetPath);
|
|
2939
|
+
}
|
|
2940
|
+
};
|
|
2941
|
+
const removeObserved = (dirPath) => {
|
|
2942
|
+
if (target) {
|
|
2943
|
+
if (this.fsw.tree.getDirectory(dirPath).has(target))
|
|
2944
|
+
this.fsw.removePath(dirPath, target);
|
|
2945
|
+
rewatchTarget();
|
|
2946
|
+
return;
|
|
2947
|
+
}
|
|
2948
|
+
this.fsw.removePath(sp__namespace.dirname(dirPath), sp__namespace.basename(dirPath), true);
|
|
2949
|
+
};
|
|
2950
|
+
const observeDirectory = async (dirPath, currentStats, trigger) => {
|
|
2951
|
+
if (!active || !this.isHelperActive(wh, dirPath))
|
|
2952
|
+
return;
|
|
2953
|
+
if (this.fsw.options.backendCapabilities.polling && !currentStats) {
|
|
2954
|
+
removeObserved(dirPath);
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
if (trigger.kind === 'native') {
|
|
2958
|
+
let observedStats;
|
|
2959
|
+
try {
|
|
2960
|
+
const linkStats = await promises.lstat(dirPath);
|
|
2961
|
+
observedStats =
|
|
2962
|
+
linkStats.isSymbolicLink() && this.fsw.options.followSymlinks
|
|
2963
|
+
? await promises.stat(dirPath)
|
|
2964
|
+
: linkStats;
|
|
2965
|
+
}
|
|
2966
|
+
catch (error) {
|
|
2967
|
+
if (isMissingError(error)) {
|
|
2968
|
+
removeObserved(dirPath);
|
|
2969
|
+
}
|
|
2970
|
+
else {
|
|
2971
|
+
this.reportError(error);
|
|
2972
|
+
}
|
|
2973
|
+
return;
|
|
2974
|
+
}
|
|
2975
|
+
if (!observedStats.isDirectory()) {
|
|
2976
|
+
removeObserved(dirPath);
|
|
2977
|
+
if (!target)
|
|
2978
|
+
await this.addPathOnce(dirPath, false, wh, depth);
|
|
2979
|
+
return;
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
await this.readDirectory(dirPath, false, wh, target, dir, depth);
|
|
2983
|
+
};
|
|
2984
|
+
const reconcile = (candidate, currentStats, trigger) => {
|
|
2985
|
+
if (!active || !this.isHelperActive(wh, candidate))
|
|
889
2986
|
return;
|
|
2987
|
+
if (initializing) {
|
|
2988
|
+
buffered.push({ path: candidate, stats: currentStats, trigger });
|
|
2989
|
+
return;
|
|
2990
|
+
}
|
|
2991
|
+
if (trigger.kind === 'native' && !target) {
|
|
2992
|
+
if (rescanOnFirstCallback) {
|
|
2993
|
+
rescanOnFirstCallback = false;
|
|
2994
|
+
return (async () => {
|
|
2995
|
+
await this.reconcileNativeTrigger(dir, wh, trigger, candidate, depth);
|
|
2996
|
+
await observeDirectory(dir, undefined, trigger);
|
|
2997
|
+
})();
|
|
2998
|
+
}
|
|
2999
|
+
return this.reconcileNativeTrigger(dir, wh, trigger, candidate, depth);
|
|
3000
|
+
}
|
|
3001
|
+
if (trigger.kind === 'native' && target) {
|
|
3002
|
+
const matchesTarget = candidate === dir ||
|
|
3003
|
+
(isWindows
|
|
3004
|
+
? sp__namespace.basename(candidate).toLowerCase() === target.toLowerCase()
|
|
3005
|
+
: sp__namespace.basename(candidate) === target);
|
|
3006
|
+
if (matchesTarget) {
|
|
3007
|
+
return this.reconcileNativeTrigger(dir, wh, trigger, targetPath, depth, {
|
|
3008
|
+
onRootMissing: rewatchTarget,
|
|
3009
|
+
});
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
return observeDirectory(dir, currentStats, trigger);
|
|
3013
|
+
};
|
|
3014
|
+
const rootParentCloser = depth === 0 && !target ? this.watchRootParent(dir, wh) : undefined;
|
|
3015
|
+
closer = this.subscribePath(dir, reconcile, stats.isDirectory() ? stats : undefined, true, target ? sp__namespace.join(dir, target) : dir);
|
|
3016
|
+
if (target && targetPath && closer) {
|
|
3017
|
+
// The target may have appeared after the caller's failed lstat and
|
|
3018
|
+
// before this subscription existed; nothing would report it.
|
|
3019
|
+
const check = recoveryTrigger(sp__namespace.resolve(dir));
|
|
3020
|
+
await this.fsw.reconciliation.enqueue(targetPath, async () => {
|
|
3021
|
+
if (this.fsw.tree.getDirectory(dir).has(target))
|
|
3022
|
+
return;
|
|
3023
|
+
await reconcile(targetPath, undefined, check);
|
|
3024
|
+
}, targetPath);
|
|
3025
|
+
}
|
|
3026
|
+
if (!target) {
|
|
3027
|
+
await this.readDirectory(dir, initialAdd, wh, target, dir, depth);
|
|
3028
|
+
let index = 0;
|
|
3029
|
+
while (!this.fsw.closed) {
|
|
3030
|
+
if (buffered.overflow) {
|
|
3031
|
+
// Triggers arriving during the bulk rescan are buffered again and
|
|
3032
|
+
// replayed after it instead of being discarded with this overflow.
|
|
3033
|
+
const overflow = buffered.overflow;
|
|
3034
|
+
buffered.clear();
|
|
3035
|
+
index = 0;
|
|
3036
|
+
await observeDirectory(dir, overflow.stats, overflow.trigger);
|
|
3037
|
+
continue;
|
|
3038
|
+
}
|
|
3039
|
+
if (index >= buffered.entries.length)
|
|
3040
|
+
break;
|
|
3041
|
+
const event = buffered.entries[index++];
|
|
3042
|
+
if (event.trigger.kind === 'native' &&
|
|
3043
|
+
event.trigger.rawEvent === 'rename' &&
|
|
3044
|
+
event.path !== dir &&
|
|
3045
|
+
this.fsw.tree.getDirectory(sp__namespace.dirname(event.path)).has(sp__namespace.basename(event.path))) {
|
|
3046
|
+
this.fsw.tree.markInitialCreate(event.path);
|
|
3047
|
+
replayCreates.add(event.path);
|
|
3048
|
+
}
|
|
3049
|
+
await this.reconcileBackendTrigger(dir, true, event.trigger, () => {
|
|
3050
|
+
if (event.trigger.kind === 'native' && !target) {
|
|
3051
|
+
return this.reconcileNativeTrigger(dir, wh, event.trigger, event.path, depth);
|
|
3052
|
+
}
|
|
3053
|
+
return observeDirectory(dir, event.stats, event.trigger);
|
|
3054
|
+
}, () => active && this.isHelperActive(wh, event.path), false);
|
|
3055
|
+
}
|
|
3056
|
+
buffered.clear();
|
|
3057
|
+
replayCreates.forEach((path) => this.fsw.tree.clearInitialCreate(path));
|
|
3058
|
+
initializing = false;
|
|
3059
|
+
}
|
|
3060
|
+
if (!this.isHelperActive(wh, dir)) {
|
|
3061
|
+
active = false;
|
|
3062
|
+
if (closer)
|
|
3063
|
+
await closer();
|
|
3064
|
+
if (rootParentCloser)
|
|
3065
|
+
await rootParentCloser();
|
|
3066
|
+
return;
|
|
3067
|
+
}
|
|
3068
|
+
if (closer || rootParentCloser) {
|
|
3069
|
+
const backendClosers = [closer, rootParentCloser].filter((candidate) => candidate !== undefined);
|
|
3070
|
+
closer = async () => {
|
|
3071
|
+
if (!active)
|
|
3072
|
+
return;
|
|
3073
|
+
active = false;
|
|
3074
|
+
buffered.clear();
|
|
3075
|
+
await Promise.allSettled(backendClosers.map((backendCloser) => backendCloser()));
|
|
3076
|
+
};
|
|
890
3077
|
}
|
|
891
|
-
closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
|
|
892
|
-
// if current directory is removed, do nothing
|
|
893
|
-
if (stats && stats.mtimeMs === 0)
|
|
894
|
-
return;
|
|
895
|
-
this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
|
|
896
|
-
});
|
|
897
3078
|
}
|
|
898
3079
|
return closer;
|
|
899
3080
|
}
|
|
900
3081
|
/**
|
|
901
3082
|
* Handle added file, directory, or glob pattern.
|
|
902
|
-
* Delegates call to
|
|
3083
|
+
* Delegates call to handleFile / handleDirectory after checks.
|
|
903
3084
|
* @param path to file or ir
|
|
904
3085
|
* @param initialAdd was the file added at watch instantiation?
|
|
905
3086
|
* @param priorWh depth relative to user-supplied path
|
|
906
3087
|
* @param depth Child path actually targeted for watch
|
|
907
3088
|
* @param target Child path actually targeted for watch
|
|
908
3089
|
*/
|
|
909
|
-
async
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
3090
|
+
async addRoot(path, initialAdd, pathGeneration, target) {
|
|
3091
|
+
let candidate = path;
|
|
3092
|
+
let childTarget = target;
|
|
3093
|
+
const generation = this.fsw.lifecycle.generation;
|
|
3094
|
+
while (this.fsw.lifecycle.isActive(generation)) {
|
|
3095
|
+
const outcome = await this.addPathOnce(candidate, initialAdd, undefined, 0, childTarget, pathGeneration);
|
|
3096
|
+
if (outcome === 'complete')
|
|
3097
|
+
return;
|
|
3098
|
+
const parent = sp__namespace.dirname(candidate);
|
|
3099
|
+
if (parent === candidate)
|
|
3100
|
+
return;
|
|
3101
|
+
childTarget = sp__namespace.basename(candidate);
|
|
3102
|
+
candidate = parent;
|
|
914
3103
|
}
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
3104
|
+
}
|
|
3105
|
+
async addPathOnce(path, initialAdd, priorWh, depth, target, pathGeneration) {
|
|
3106
|
+
if (this.fsw.isIgnored(path) || this.fsw.closed) {
|
|
3107
|
+
return 'complete';
|
|
3108
|
+
}
|
|
3109
|
+
const wh = priorWh ? priorWh.fork(path) : this.fsw.createHelper(path);
|
|
3110
|
+
if (!priorWh && pathGeneration !== undefined)
|
|
3111
|
+
wh.pathGeneration = pathGeneration;
|
|
3112
|
+
if (!this.isHelperActive(wh)) {
|
|
3113
|
+
return 'complete';
|
|
919
3114
|
}
|
|
920
3115
|
// evaluate what is at the path we're being asked to watch
|
|
921
3116
|
try {
|
|
922
|
-
const
|
|
923
|
-
if (this.
|
|
924
|
-
return;
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
return
|
|
3117
|
+
const linkStats = await promises.lstat(wh.watchPath);
|
|
3118
|
+
if (!this.isHelperActive(wh)) {
|
|
3119
|
+
return 'complete';
|
|
3120
|
+
}
|
|
3121
|
+
if (this.fsw.isIgnored(wh.watchPath, linkStats)) {
|
|
3122
|
+
return 'complete';
|
|
928
3123
|
}
|
|
929
3124
|
const follow = this.fsw.options.followSymlinks;
|
|
3125
|
+
const isSymbolicLink = linkStats.isSymbolicLink();
|
|
3126
|
+
const stats = isSymbolicLink && follow ? await promises.stat(wh.watchPath) : linkStats;
|
|
3127
|
+
const followedTarget = isSymbolicLink && follow ? await promises.realpath(path) : undefined;
|
|
930
3128
|
let closer;
|
|
931
3129
|
if (stats.isDirectory()) {
|
|
932
|
-
const absPath =
|
|
933
|
-
const
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
3130
|
+
const absPath = logicalPathKey(path);
|
|
3131
|
+
const ancestryPath = follow ? await promises.realpath(path) : path;
|
|
3132
|
+
const targetPath = followedTarget ?? path;
|
|
3133
|
+
const targetKey = logicalPathKey(ancestryPath);
|
|
3134
|
+
if (!this.isHelperActive(wh)) {
|
|
3135
|
+
return 'complete';
|
|
3136
|
+
}
|
|
3137
|
+
if (isSymbolicLink && wh.realpathAncestry.has(targetKey)) {
|
|
3138
|
+
const parent = this.fsw.tree.getDirectory(sp__namespace.dirname(wh.watchPath));
|
|
3139
|
+
parent.add(sp__namespace.basename(wh.watchPath));
|
|
3140
|
+
this.fsw.tree.getDirectory(wh.watchPath);
|
|
3141
|
+
this.fsw.tree.symlinkPaths.set(absPath, targetPath);
|
|
3142
|
+
this.fsw.tree.recordObserved(wh.watchPath, stats, 'add', wh.observationTrigger, initialAdd && Boolean(wh.recursiveRoot));
|
|
3143
|
+
if (!(initialAdd && this.fsw.options.ignoreInitial)) {
|
|
3144
|
+
this.fsw.emitEvent(EV.ADD_DIR, wh.watchPath, stats);
|
|
3145
|
+
}
|
|
3146
|
+
return 'complete';
|
|
3147
|
+
}
|
|
3148
|
+
wh.realpathAncestry.add(targetKey);
|
|
3149
|
+
if (wh.recursiveRoot && isSymbolicLink) {
|
|
3150
|
+
wh.recursiveRoot = undefined;
|
|
3151
|
+
}
|
|
3152
|
+
if (isSymbolicLink)
|
|
3153
|
+
wh.recursiveDisabled = true;
|
|
3154
|
+
closer = await this.handleDirectory(wh.watchPath, stats, initialAdd, depth, target, wh);
|
|
3155
|
+
if (!this.isHelperActive(wh)) {
|
|
3156
|
+
if (closer)
|
|
3157
|
+
await closer();
|
|
3158
|
+
return 'complete';
|
|
3159
|
+
}
|
|
3160
|
+
// The directory vanished before it could be subscribed: keep climbing.
|
|
3161
|
+
if (target && !closer)
|
|
3162
|
+
return 'watch-parent';
|
|
939
3163
|
// preserve this symlink's target path
|
|
940
|
-
if (
|
|
941
|
-
this.fsw.
|
|
3164
|
+
if (isSymbolicLink) {
|
|
3165
|
+
this.fsw.tree.symlinkPaths.set(absPath, targetPath);
|
|
942
3166
|
}
|
|
943
3167
|
}
|
|
944
|
-
else if (
|
|
945
|
-
const
|
|
946
|
-
|
|
947
|
-
|
|
3168
|
+
else if (isSymbolicLink && !follow) {
|
|
3169
|
+
const linkTarget = await promises.readlink(path);
|
|
3170
|
+
const retainsNativeFact = !this.fsw.options.backendCapabilities.polling && !wh.recursiveRoot && !priorWh;
|
|
3171
|
+
const fileCloser = this.handleFile(wh.watchPath, stats, initialAdd, !wh.recursiveRoot, wh.observationTrigger, initialAdd && Boolean(wh.recursiveRoot), retainsNativeFact);
|
|
3172
|
+
if (wh.recursiveRoot) {
|
|
3173
|
+
this.fsw.tree.symlinkPaths.set(logicalPathKey(path), linkTarget);
|
|
3174
|
+
return 'complete';
|
|
3175
|
+
}
|
|
948
3176
|
const parent = sp__namespace.dirname(wh.watchPath);
|
|
949
|
-
|
|
950
|
-
this.fsw.
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
3177
|
+
const parentStats = await promises.stat(parent);
|
|
3178
|
+
const parentHelper = this.fsw.createHelper(parent);
|
|
3179
|
+
parentHelper.pathGeneration = wh.pathGeneration;
|
|
3180
|
+
const parentCloser = await this.handleDirectory(parent, parentStats, initialAdd, depth, sp__namespace.basename(wh.watchPath), parentHelper);
|
|
3181
|
+
const deletionCloser = this.watchSymlinkDeletion(wh.watchPath);
|
|
3182
|
+
if (!this.isHelperActive(wh)) {
|
|
3183
|
+
if (fileCloser)
|
|
3184
|
+
await fileCloser();
|
|
3185
|
+
if (parentCloser)
|
|
3186
|
+
await parentCloser();
|
|
3187
|
+
if (deletionCloser)
|
|
3188
|
+
deletionCloser();
|
|
3189
|
+
return 'complete';
|
|
957
3190
|
}
|
|
3191
|
+
closer = async () => {
|
|
3192
|
+
await Promise.allSettled([fileCloser, parentCloser, deletionCloser]
|
|
3193
|
+
.filter((candidate) => candidate !== undefined)
|
|
3194
|
+
.map((backendCloser) => backendCloser()));
|
|
3195
|
+
};
|
|
3196
|
+
this.fsw.tree.symlinkPaths.set(logicalPathKey(path), linkTarget);
|
|
958
3197
|
}
|
|
959
3198
|
else {
|
|
960
|
-
|
|
3199
|
+
if (followedTarget) {
|
|
3200
|
+
this.fsw.tree.symlinkPaths.set(logicalPathKey(path), followedTarget);
|
|
3201
|
+
}
|
|
3202
|
+
const retainsNativeFact = !this.fsw.options.backendCapabilities.polling && !wh.recursiveRoot && !priorWh;
|
|
3203
|
+
const fileCloser = this.handleFile(wh.watchPath, stats, initialAdd, !wh.recursiveRoot, wh.observationTrigger, initialAdd && Boolean(wh.recursiveRoot), retainsNativeFact);
|
|
3204
|
+
const targetPath = followedTarget ?? (retainsNativeFact ? wh.watchPath : undefined);
|
|
3205
|
+
// fs.watch cannot open sockets, FIFOs or devices on macOS (UNKNOWN), so
|
|
3206
|
+
// only regular files get the exact fallback; the parent still covers them.
|
|
3207
|
+
const targetCloser = targetPath
|
|
3208
|
+
? this.subscribeMappedFile(wh.watchPath, targetPath, wh, depth, isMacos && stats.isFile())
|
|
3209
|
+
: undefined;
|
|
3210
|
+
let parentCloser;
|
|
3211
|
+
if (retainsNativeFact && followedTarget) {
|
|
3212
|
+
const parent = sp__namespace.dirname(wh.watchPath);
|
|
3213
|
+
const parentStats = await promises.stat(parent);
|
|
3214
|
+
const parentHelper = this.fsw.createHelper(parent);
|
|
3215
|
+
parentHelper.pathGeneration = wh.pathGeneration;
|
|
3216
|
+
parentCloser = await this.handleDirectory(parent, parentStats, initialAdd, depth, sp__namespace.basename(wh.watchPath), parentHelper);
|
|
3217
|
+
}
|
|
3218
|
+
const fileClosers = [fileCloser, targetCloser, parentCloser].filter((candidate) => candidate !== undefined);
|
|
3219
|
+
closer =
|
|
3220
|
+
fileClosers.length === 0
|
|
3221
|
+
? undefined
|
|
3222
|
+
: async () => {
|
|
3223
|
+
await Promise.allSettled(fileClosers.map((fileCloser) => fileCloser()));
|
|
3224
|
+
};
|
|
961
3225
|
}
|
|
962
|
-
|
|
3226
|
+
if (!this.isHelperActive(wh)) {
|
|
3227
|
+
if (closer)
|
|
3228
|
+
await closer();
|
|
3229
|
+
return 'complete';
|
|
3230
|
+
}
|
|
3231
|
+
// A wait for a missing child belongs to that child: unwatching or
|
|
3232
|
+
// re-adding the parent must not be confused with it.
|
|
963
3233
|
if (closer)
|
|
964
|
-
this.fsw.
|
|
965
|
-
return
|
|
3234
|
+
this.fsw.addPathCloser(target ? sp__namespace.join(path, target) : path, closer);
|
|
3235
|
+
return 'complete';
|
|
966
3236
|
}
|
|
967
3237
|
catch (error) {
|
|
968
|
-
if (this.
|
|
969
|
-
|
|
970
|
-
return path;
|
|
3238
|
+
if (!this.isHelperActive(wh)) {
|
|
3239
|
+
return 'complete';
|
|
971
3240
|
}
|
|
3241
|
+
this.fsw.handleError(error);
|
|
3242
|
+
return 'watch-parent';
|
|
972
3243
|
}
|
|
973
3244
|
}
|
|
974
3245
|
}
|
|
975
3246
|
|
|
976
3247
|
/*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) */
|
|
977
|
-
const SLASH = '/';
|
|
978
|
-
const SLASH_SLASH = '//';
|
|
979
|
-
const ONE_DOT = '.';
|
|
980
|
-
const TWO_DOTS = '..';
|
|
981
|
-
const STRING_TYPE = 'string';
|
|
982
|
-
const BACK_SLASH_RE = /\\/g;
|
|
983
|
-
const DOUBLE_SLASH_RE = /\/\//g;
|
|
984
3248
|
const DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
|
|
985
|
-
const
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
return matcher;
|
|
993
|
-
if (typeof matcher === 'string')
|
|
994
|
-
return (string) => matcher === string;
|
|
995
|
-
if (matcher instanceof RegExp)
|
|
996
|
-
return (string) => matcher.test(string);
|
|
997
|
-
if (typeof matcher === 'object' && matcher !== null) {
|
|
998
|
-
return (string) => {
|
|
999
|
-
if (matcher.path === string)
|
|
1000
|
-
return true;
|
|
1001
|
-
if (matcher.recursive) {
|
|
1002
|
-
const relative = sp__namespace.relative(matcher.path, string);
|
|
1003
|
-
if (!relative) {
|
|
1004
|
-
return false;
|
|
1005
|
-
}
|
|
1006
|
-
return !relative.startsWith('..') && !sp__namespace.isAbsolute(relative);
|
|
1007
|
-
}
|
|
1008
|
-
return false;
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
return () => false;
|
|
1012
|
-
}
|
|
1013
|
-
function normalizePath(path) {
|
|
1014
|
-
if (typeof path !== 'string')
|
|
1015
|
-
throw new Error('string expected');
|
|
1016
|
-
path = sp__namespace.normalize(path);
|
|
1017
|
-
path = path.replace(/\\/g, '/');
|
|
1018
|
-
let prepend = false;
|
|
1019
|
-
if (path.startsWith('//'))
|
|
1020
|
-
prepend = true;
|
|
1021
|
-
path = path.replace(DOUBLE_SLASH_RE, '/');
|
|
1022
|
-
if (prepend)
|
|
1023
|
-
path = '/' + path;
|
|
1024
|
-
return path;
|
|
1025
|
-
}
|
|
1026
|
-
function matchPatterns(patterns, testString, stats) {
|
|
1027
|
-
const path = normalizePath(testString);
|
|
1028
|
-
for (let index = 0; index < patterns.length; index++) {
|
|
1029
|
-
const pattern = patterns[index];
|
|
1030
|
-
if (pattern(path, stats)) {
|
|
1031
|
-
return true;
|
|
1032
|
-
}
|
|
3249
|
+
const WATCH_BACKENDS = new Set(['auto', 'native', 'native-recursive', 'polling']);
|
|
3250
|
+
const DEF_AWF = Object.freeze({ stabilityThreshold: 2000, pollInterval: 100 });
|
|
3251
|
+
function* ancestorKeys(path) {
|
|
3252
|
+
let current = sp__namespace.resolve(path);
|
|
3253
|
+
for (let parent = sp__namespace.dirname(current); parent !== current; parent = sp__namespace.dirname(parent)) {
|
|
3254
|
+
yield logicalPathKey(parent);
|
|
3255
|
+
current = parent;
|
|
1033
3256
|
}
|
|
1034
|
-
return false;
|
|
1035
3257
|
}
|
|
1036
|
-
function
|
|
1037
|
-
|
|
1038
|
-
throw new TypeError('anymatch: specify first argument');
|
|
1039
|
-
}
|
|
1040
|
-
// Early cache for matchers.
|
|
1041
|
-
const matchersArray = arrify(matchers);
|
|
1042
|
-
const patterns = matchersArray.map((matcher) => createPattern(matcher));
|
|
1043
|
-
{
|
|
1044
|
-
return (testString, stats) => {
|
|
1045
|
-
return matchPatterns(patterns, testString, stats);
|
|
1046
|
-
};
|
|
1047
|
-
}
|
|
3258
|
+
function arrify(item) {
|
|
3259
|
+
return Array.isArray(item) ? item : [item];
|
|
1048
3260
|
}
|
|
1049
|
-
|
|
3261
|
+
function unifyPaths(paths_) {
|
|
1050
3262
|
const paths = arrify(paths_).flat();
|
|
1051
|
-
if (!paths.every((p) => typeof p ===
|
|
3263
|
+
if (!paths.every((p) => typeof p === 'string')) {
|
|
1052
3264
|
throw new TypeError(`Non-string provided as watch path: ${paths}`);
|
|
1053
3265
|
}
|
|
1054
|
-
return paths.map(
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
// because "//StoragePC/DrivePool/Movies" is a valid network path
|
|
1058
|
-
const toUnix = (string) => {
|
|
1059
|
-
let str = string.replace(BACK_SLASH_RE, SLASH);
|
|
1060
|
-
let prepend = false;
|
|
1061
|
-
if (str.startsWith(SLASH_SLASH)) {
|
|
1062
|
-
prepend = true;
|
|
1063
|
-
}
|
|
1064
|
-
str = str.replace(DOUBLE_SLASH_RE, SLASH);
|
|
1065
|
-
if (prepend) {
|
|
1066
|
-
str = SLASH + str;
|
|
1067
|
-
}
|
|
1068
|
-
return str;
|
|
1069
|
-
};
|
|
1070
|
-
// Our version of upath.normalize
|
|
1071
|
-
// TODO: this is not equal to path-normalize module - investigate why
|
|
1072
|
-
const normalizePathToUnix = (path) => toUnix(sp__namespace.normalize(toUnix(path)));
|
|
1073
|
-
// TODO: refactor
|
|
1074
|
-
const normalizeIgnored = (cwd = '') => (path) => {
|
|
1075
|
-
if (typeof path === 'string') {
|
|
1076
|
-
return normalizePathToUnix(sp__namespace.isAbsolute(path) ? path : sp__namespace.join(cwd, path));
|
|
1077
|
-
}
|
|
1078
|
-
else {
|
|
1079
|
-
return path;
|
|
1080
|
-
}
|
|
1081
|
-
};
|
|
1082
|
-
const getAbsolutePath = (path, cwd) => {
|
|
3266
|
+
return paths.map(normalizePath);
|
|
3267
|
+
}
|
|
3268
|
+
function getAbsolutePath(path, cwd) {
|
|
1083
3269
|
if (sp__namespace.isAbsolute(path)) {
|
|
1084
3270
|
return path;
|
|
1085
3271
|
}
|
|
1086
3272
|
return sp__namespace.join(cwd, path);
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
*/
|
|
1092
|
-
class DirEntry {
|
|
1093
|
-
path;
|
|
1094
|
-
_removeWatcher;
|
|
1095
|
-
items;
|
|
1096
|
-
constructor(dir, removeWatcher) {
|
|
1097
|
-
this.path = dir;
|
|
1098
|
-
this._removeWatcher = removeWatcher;
|
|
1099
|
-
this.items = new Set();
|
|
1100
|
-
}
|
|
1101
|
-
add(item) {
|
|
1102
|
-
const { items } = this;
|
|
1103
|
-
if (!items)
|
|
1104
|
-
return;
|
|
1105
|
-
if (item !== ONE_DOT && item !== TWO_DOTS)
|
|
1106
|
-
items.add(item);
|
|
1107
|
-
}
|
|
1108
|
-
async remove(item) {
|
|
1109
|
-
const { items } = this;
|
|
1110
|
-
if (!items)
|
|
1111
|
-
return;
|
|
1112
|
-
items.delete(item);
|
|
1113
|
-
if (items.size > 0)
|
|
1114
|
-
return;
|
|
1115
|
-
const dir = this.path;
|
|
1116
|
-
try {
|
|
1117
|
-
await promises.readdir(dir);
|
|
1118
|
-
}
|
|
1119
|
-
catch (err) {
|
|
1120
|
-
if (this._removeWatcher) {
|
|
1121
|
-
this._removeWatcher(sp__namespace.dirname(dir), sp__namespace.basename(dir));
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
has(item) {
|
|
1126
|
-
const { items } = this;
|
|
1127
|
-
if (!items)
|
|
1128
|
-
return;
|
|
1129
|
-
return items.has(item);
|
|
1130
|
-
}
|
|
1131
|
-
getChildren() {
|
|
1132
|
-
const { items } = this;
|
|
1133
|
-
if (!items)
|
|
1134
|
-
return [];
|
|
1135
|
-
return [...items.values()];
|
|
1136
|
-
}
|
|
1137
|
-
dispose() {
|
|
1138
|
-
this.items.clear();
|
|
1139
|
-
this.path = '';
|
|
1140
|
-
this._removeWatcher = EMPTY_FN;
|
|
1141
|
-
this.items = EMPTY_SET;
|
|
1142
|
-
Object.freeze(this);
|
|
3273
|
+
}
|
|
3274
|
+
function validatePositiveFinite(name, value) {
|
|
3275
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
3276
|
+
throw new TypeError(`${name} must be a finite number greater than zero`);
|
|
1143
3277
|
}
|
|
1144
3278
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
fsw;
|
|
1149
|
-
path;
|
|
1150
|
-
watchPath;
|
|
1151
|
-
fullWatchPath;
|
|
1152
|
-
dirParts;
|
|
1153
|
-
followSymlinks;
|
|
1154
|
-
statMethod;
|
|
1155
|
-
constructor(path, follow, fsw) {
|
|
1156
|
-
this.fsw = fsw;
|
|
1157
|
-
const watchPath = path;
|
|
1158
|
-
this.path = path = path.replace(REPLACER_RE, '');
|
|
1159
|
-
this.watchPath = watchPath;
|
|
1160
|
-
this.fullWatchPath = sp__namespace.resolve(watchPath);
|
|
1161
|
-
this.dirParts = [];
|
|
1162
|
-
this.dirParts.forEach((parts) => {
|
|
1163
|
-
if (parts.length > 1)
|
|
1164
|
-
parts.pop();
|
|
1165
|
-
});
|
|
1166
|
-
this.followSymlinks = follow;
|
|
1167
|
-
this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
|
|
3279
|
+
function validateNonNegativeFinite(name, value) {
|
|
3280
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
3281
|
+
throw new TypeError(`${name} must be a finite non-negative number`);
|
|
1168
3282
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
3283
|
+
}
|
|
3284
|
+
function validateOptions(opts) {
|
|
3285
|
+
validatePositiveFinite('pollingInterval', opts.pollingInterval);
|
|
3286
|
+
validatePositiveFinite('pollingBinaryInterval', opts.pollingBinaryInterval);
|
|
3287
|
+
if (typeof opts.atomic === 'number')
|
|
3288
|
+
validateNonNegativeFinite('atomic', opts.atomic);
|
|
3289
|
+
else if (typeof opts.atomic !== 'boolean') {
|
|
3290
|
+
throw new TypeError('atomic must be a boolean or finite non-negative number');
|
|
1171
3291
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
if (stats && stats.isSymbolicLink())
|
|
1175
|
-
return this.filterDir(entry);
|
|
1176
|
-
const resolvedPath = this.entryPath(entry);
|
|
1177
|
-
// TODO: what if stats is undefined? remove !
|
|
1178
|
-
return this.fsw._isntIgnored(resolvedPath, stats) && this.fsw._hasReadPermissions(stats);
|
|
3292
|
+
if (opts.depth !== undefined && (!Number.isSafeInteger(opts.depth) || opts.depth < 0)) {
|
|
3293
|
+
throw new TypeError('depth must be a non-negative safe integer');
|
|
1179
3294
|
}
|
|
1180
|
-
|
|
1181
|
-
|
|
3295
|
+
if (opts.awaitWriteFinish) {
|
|
3296
|
+
validatePositiveFinite('awaitWriteFinish.pollInterval', opts.awaitWriteFinish.pollInterval);
|
|
3297
|
+
validateNonNegativeFinite('awaitWriteFinish.stabilityThreshold', opts.awaitWriteFinish.stabilityThreshold);
|
|
1182
3298
|
}
|
|
1183
3299
|
}
|
|
1184
3300
|
/**
|
|
@@ -1190,100 +3306,148 @@ class WatchHelper {
|
|
|
1190
3306
|
* .on('add', path => log('File', path, 'was added'))
|
|
1191
3307
|
*/
|
|
1192
3308
|
class FSWatcher extends node_events.EventEmitter {
|
|
1193
|
-
closed;
|
|
1194
3309
|
options;
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
3310
|
+
/** @internal */
|
|
3311
|
+
lifecycle;
|
|
3312
|
+
/** @internal */
|
|
3313
|
+
tree;
|
|
3314
|
+
/** @internal */
|
|
3315
|
+
reconciliation;
|
|
3316
|
+
/** @internal */
|
|
3317
|
+
events;
|
|
3318
|
+
/** @internal */
|
|
3319
|
+
scheduler;
|
|
3320
|
+
/** @internal */
|
|
3321
|
+
handler;
|
|
3322
|
+
/** @internal */
|
|
3323
|
+
readyEmitted;
|
|
3324
|
+
/** @internal */
|
|
3325
|
+
streams;
|
|
3326
|
+
get closed() {
|
|
3327
|
+
return this.lifecycle !== undefined && this.lifecycle.state !== 'OPEN';
|
|
3328
|
+
}
|
|
3329
|
+
#ignoredPaths;
|
|
3330
|
+
#pendingAdds;
|
|
3331
|
+
#pathMutation;
|
|
3332
|
+
#pathBarriers;
|
|
3333
|
+
#closePromise;
|
|
3334
|
+
#userIgnored;
|
|
3335
|
+
#realDirs;
|
|
3336
|
+
#roots;
|
|
3337
|
+
#rootAncestors;
|
|
3338
|
+
#unwatchIgnored;
|
|
3339
|
+
#readyPending;
|
|
3340
|
+
#readyScheduled;
|
|
1211
3341
|
// Not indenting methods for history sake; for now.
|
|
1212
|
-
constructor(_opts = {}) {
|
|
3342
|
+
constructor(_opts = {}, scheduler = systemScheduler) {
|
|
1213
3343
|
super();
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
this
|
|
1218
|
-
this.
|
|
1219
|
-
this
|
|
1220
|
-
this
|
|
1221
|
-
this
|
|
1222
|
-
this
|
|
1223
|
-
this
|
|
1224
|
-
this
|
|
3344
|
+
if (_opts.backend !== undefined && !WATCH_BACKENDS.has(_opts.backend)) {
|
|
3345
|
+
throw new TypeError('backend must be auto, native, native-recursive, or polling');
|
|
3346
|
+
}
|
|
3347
|
+
this.#ignoredPaths = new Set();
|
|
3348
|
+
this.streams = new Set();
|
|
3349
|
+
this.#pendingAdds = new Map();
|
|
3350
|
+
this.#pathMutation = 0;
|
|
3351
|
+
this.#pathBarriers = new Map();
|
|
3352
|
+
this.#realDirs = new Map();
|
|
3353
|
+
this.#roots = new Set();
|
|
3354
|
+
this.#rootAncestors = new Map();
|
|
3355
|
+
this.readyEmitted = false;
|
|
3356
|
+
this.#readyPending = false;
|
|
3357
|
+
this.#readyScheduled = false;
|
|
3358
|
+
this.scheduler = scheduler;
|
|
1225
3359
|
const awf = _opts.awaitWriteFinish;
|
|
1226
|
-
const DEF_AWF = { stabilityThreshold: 2000, pollInterval: 100 };
|
|
1227
3360
|
const opts = {
|
|
3361
|
+
..._opts,
|
|
1228
3362
|
// Defaults
|
|
1229
|
-
persistent: true,
|
|
1230
|
-
ignoreInitial: false,
|
|
1231
|
-
ignorePermissionErrors: false,
|
|
3363
|
+
persistent: _opts.persistent ?? true,
|
|
3364
|
+
ignoreInitial: _opts.ignoreInitial ?? false,
|
|
3365
|
+
ignorePermissionErrors: _opts.ignorePermissionErrors ?? false,
|
|
3366
|
+
pollingInterval: _opts.pollingInterval ?? _opts.interval ?? 100,
|
|
3367
|
+
pollingBinaryInterval: _opts.pollingBinaryInterval ?? _opts.binaryInterval ?? 300,
|
|
1232
3368
|
interval: 100,
|
|
1233
3369
|
binaryInterval: 300,
|
|
1234
|
-
followSymlinks: true,
|
|
1235
|
-
|
|
3370
|
+
followSymlinks: _opts.followSymlinks ?? true,
|
|
3371
|
+
backend: _opts.backend ?? 'auto',
|
|
3372
|
+
usePolling: _opts.usePolling ?? false,
|
|
3373
|
+
// `undefined` already means unlimited traversal. Preserve the common
|
|
3374
|
+
// `depth: Infinity` spelling without forcing the per-directory backend.
|
|
3375
|
+
depth: _opts.depth === Number.POSITIVE_INFINITY ? undefined : _opts.depth,
|
|
3376
|
+
backendStrategy: 'native-per-directory',
|
|
3377
|
+
backendCapabilities: undefined,
|
|
1236
3378
|
// useAsync: false,
|
|
1237
|
-
atomic: true,
|
|
1238
|
-
..._opts,
|
|
3379
|
+
atomic: _opts.atomic ?? true,
|
|
1239
3380
|
// Change format
|
|
1240
|
-
ignored: _opts.ignored ? arrify(_opts.ignored) :
|
|
1241
|
-
awaitWriteFinish: awf === true
|
|
3381
|
+
ignored: Object.freeze(_opts.ignored ? arrify(_opts.ignored).map(cloneOwnedMatcher) : []),
|
|
3382
|
+
awaitWriteFinish: awf === true
|
|
3383
|
+
? DEF_AWF
|
|
3384
|
+
: typeof awf === 'object'
|
|
3385
|
+
? Object.freeze({ ...DEF_AWF, ...awf })
|
|
3386
|
+
: false,
|
|
1242
3387
|
};
|
|
1243
3388
|
// Always default to polling on IBM i because fs.watch() is not available on IBM i.
|
|
1244
|
-
if (isIBMi)
|
|
1245
|
-
opts.
|
|
1246
|
-
// Editor atomic write normalization enabled by default with fs.watch
|
|
1247
|
-
if (opts.atomic === undefined)
|
|
1248
|
-
opts.atomic = !opts.usePolling;
|
|
1249
|
-
// opts.atomic = typeof _opts.atomic === 'number' ? _opts.atomic : 100;
|
|
3389
|
+
if (_opts.usePolling === true || isIBMi)
|
|
3390
|
+
opts.backend = 'polling';
|
|
1250
3391
|
// Global override. Useful for developers, who need to force polling for all
|
|
1251
3392
|
// instances of chokidar, regardless of usage / dependency depth
|
|
1252
3393
|
const envPoll = process.env.CHOKIDAR_USEPOLLING;
|
|
1253
3394
|
if (envPoll !== undefined) {
|
|
1254
3395
|
const envLower = envPoll.toLowerCase();
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
3396
|
+
const envPolling = envLower === 'false' || envLower === '0'
|
|
3397
|
+
? false
|
|
3398
|
+
: envLower === 'true' || envLower === '1'
|
|
3399
|
+
? true
|
|
3400
|
+
: !!envLower;
|
|
3401
|
+
if (envPolling)
|
|
3402
|
+
opts.backend = 'polling';
|
|
3403
|
+
else if (opts.backend === 'polling')
|
|
3404
|
+
opts.backend = 'auto';
|
|
1261
3405
|
}
|
|
1262
3406
|
const envInterval = process.env.CHOKIDAR_INTERVAL;
|
|
1263
|
-
if (envInterval)
|
|
1264
|
-
opts.
|
|
1265
|
-
//
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
this._emitRaw = (...args) => this.emit(EVENTS.RAW, ...args);
|
|
1277
|
-
this._boundRemove = this._remove.bind(this);
|
|
3407
|
+
if (envInterval !== undefined)
|
|
3408
|
+
opts.pollingInterval = Number(envInterval);
|
|
3409
|
+
// Preserve resolved values for callers that still read the deprecated fields.
|
|
3410
|
+
opts.interval = opts.pollingInterval;
|
|
3411
|
+
opts.binaryInterval = opts.pollingBinaryInterval;
|
|
3412
|
+
opts.usePolling = opts.backend === 'polling';
|
|
3413
|
+
opts.backendCapabilities = selectBackend(opts);
|
|
3414
|
+
opts.backendStrategy = opts.backendCapabilities.kind;
|
|
3415
|
+
// Editor atomic write normalization is enabled by default only with fs.watch.
|
|
3416
|
+
// Inspect the raw option so the merged default cannot hide an implicit choice.
|
|
3417
|
+
if (_opts.atomic === undefined)
|
|
3418
|
+
opts.atomic = !opts.usePolling;
|
|
3419
|
+
validateOptions(opts);
|
|
1278
3420
|
this.options = opts;
|
|
1279
|
-
this.
|
|
3421
|
+
this.lifecycle = new LifecycleScope(() => {
|
|
3422
|
+
if (this.#readyPending)
|
|
3423
|
+
this.queueReady();
|
|
3424
|
+
}, (error) => {
|
|
3425
|
+
if (!this.closed)
|
|
3426
|
+
this.handleError(error instanceof Error ? error : new Error(String(error)));
|
|
3427
|
+
});
|
|
3428
|
+
this.tree = new TreeState(() => !this.options.backendCapabilities.polling);
|
|
3429
|
+
this.reconciliation = new ReconciliationQueue(this.lifecycle);
|
|
3430
|
+
this.events = new EventPolicy({
|
|
3431
|
+
options: this.options,
|
|
3432
|
+
scheduler: this.scheduler,
|
|
3433
|
+
lifecycle: this.lifecycle,
|
|
3434
|
+
isClosed: () => this.closed,
|
|
3435
|
+
capturePathGeneration: () => this.capturePathGeneration(),
|
|
3436
|
+
isPathGenerationActive: (path, generation) => this.isPathGenerationActive(path, generation),
|
|
3437
|
+
isReady: () => this.readyEmitted,
|
|
3438
|
+
remove: (directory, item) => this.removePath(directory, item),
|
|
3439
|
+
handleError: (error) => this.handleError(error),
|
|
3440
|
+
publish: (event, args) => this.emitWithAll(event, args),
|
|
3441
|
+
});
|
|
3442
|
+
this.handler = new ObservationEngine(this);
|
|
1280
3443
|
// You’re frozen when your heart’s not open.
|
|
1281
3444
|
Object.freeze(opts);
|
|
1282
3445
|
}
|
|
1283
|
-
|
|
3446
|
+
addIgnoredPath(matcher) {
|
|
3447
|
+
matcher = this.ignoredMatcher(matcher);
|
|
1284
3448
|
if (isMatcherObject(matcher)) {
|
|
1285
3449
|
// return early if we already have a deeply equal matcher object
|
|
1286
|
-
for (const ignored of this
|
|
3450
|
+
for (const ignored of this.#ignoredPaths) {
|
|
1287
3451
|
if (isMatcherObject(ignored) &&
|
|
1288
3452
|
ignored.path === matcher.path &&
|
|
1289
3453
|
ignored.recursive === matcher.recursive) {
|
|
@@ -1291,31 +3455,81 @@ class FSWatcher extends node_events.EventEmitter {
|
|
|
1291
3455
|
}
|
|
1292
3456
|
}
|
|
1293
3457
|
}
|
|
1294
|
-
this.
|
|
3458
|
+
this.#ignoredPaths.add(matcher);
|
|
3459
|
+
this.#unwatchIgnored = undefined;
|
|
1295
3460
|
}
|
|
1296
|
-
|
|
1297
|
-
this.
|
|
3461
|
+
removeIgnoredPath(matcher) {
|
|
3462
|
+
matcher = this.ignoredMatcher(matcher);
|
|
3463
|
+
this.#ignoredPaths.delete(matcher);
|
|
1298
3464
|
// now find any matcher objects with the matcher as path
|
|
1299
3465
|
if (typeof matcher === 'string') {
|
|
1300
|
-
for (const ignored of this
|
|
3466
|
+
for (const ignored of this.#ignoredPaths) {
|
|
1301
3467
|
// TODO (43081j): make this more efficient.
|
|
1302
3468
|
// probably just make a `this._ignoredDirectories` or some
|
|
1303
3469
|
// such thing.
|
|
1304
3470
|
if (isMatcherObject(ignored) && ignored.path === matcher) {
|
|
1305
|
-
this.
|
|
3471
|
+
this.#ignoredPaths.delete(ignored);
|
|
1306
3472
|
}
|
|
1307
3473
|
}
|
|
1308
3474
|
}
|
|
3475
|
+
this.#unwatchIgnored = undefined;
|
|
3476
|
+
}
|
|
3477
|
+
ignoredMatcher(matcher) {
|
|
3478
|
+
if (typeof matcher === 'string')
|
|
3479
|
+
return logicalPathKey(matcher);
|
|
3480
|
+
if (isMatcherObject(matcher)) {
|
|
3481
|
+
return { path: logicalPathKey(matcher.path), recursive: matcher.recursive };
|
|
3482
|
+
}
|
|
3483
|
+
return matcher;
|
|
3484
|
+
}
|
|
3485
|
+
/** @internal */
|
|
3486
|
+
capturePathGeneration() {
|
|
3487
|
+
return this.#pathMutation;
|
|
3488
|
+
}
|
|
3489
|
+
invalidatePath(path) {
|
|
3490
|
+
this.#pathBarriers.set(logicalPathKey(path), ++this.#pathMutation);
|
|
3491
|
+
}
|
|
3492
|
+
/** @internal */
|
|
3493
|
+
isPathGenerationActive(path, generation) {
|
|
3494
|
+
if (this.lifecycle.state !== 'OPEN')
|
|
3495
|
+
return false;
|
|
3496
|
+
if (this.#pathBarriers.size === 0)
|
|
3497
|
+
return true;
|
|
3498
|
+
const logicalKey = logicalPathKey(path);
|
|
3499
|
+
for (const [barrier, barrierGeneration] of this.#pathBarriers) {
|
|
3500
|
+
if (barrierGeneration <= generation)
|
|
3501
|
+
continue;
|
|
3502
|
+
if (isSameOrInside(barrier, logicalKey))
|
|
3503
|
+
return false;
|
|
3504
|
+
}
|
|
3505
|
+
return true;
|
|
3506
|
+
}
|
|
3507
|
+
queueReady() {
|
|
3508
|
+
if (this.closed || this.readyEmitted)
|
|
3509
|
+
return;
|
|
3510
|
+
this.#readyPending = true;
|
|
3511
|
+
if (this.lifecycle.tasks.size > 0 || this.#readyScheduled)
|
|
3512
|
+
return;
|
|
3513
|
+
this.#readyScheduled = true;
|
|
3514
|
+
process.nextTick(() => {
|
|
3515
|
+
this.#readyScheduled = false;
|
|
3516
|
+
if (this.closed || this.readyEmitted || this.lifecycle.tasks.size > 0)
|
|
3517
|
+
return;
|
|
3518
|
+
this.#readyPending = false;
|
|
3519
|
+
this.readyEmitted = true;
|
|
3520
|
+
this.emit(EVENTS.READY);
|
|
3521
|
+
});
|
|
1309
3522
|
}
|
|
1310
3523
|
// Public methods
|
|
1311
3524
|
/**
|
|
1312
3525
|
* Adds paths to be watched on an existing FSWatcher instance.
|
|
1313
|
-
* @param paths_ file or file list
|
|
3526
|
+
* @param paths_ file or file list
|
|
1314
3527
|
*/
|
|
1315
|
-
add(paths_
|
|
3528
|
+
add(paths_) {
|
|
3529
|
+
if (this.closed) {
|
|
3530
|
+
throw new Error('Cannot add paths after FSWatcher.close() has been called');
|
|
3531
|
+
}
|
|
1316
3532
|
const { cwd } = this.options;
|
|
1317
|
-
this.closed = false;
|
|
1318
|
-
this._closePromise = undefined;
|
|
1319
3533
|
let paths = unifyPaths(paths_);
|
|
1320
3534
|
if (cwd) {
|
|
1321
3535
|
paths = paths.map((path) => {
|
|
@@ -1325,25 +3539,29 @@ class FSWatcher extends node_events.EventEmitter {
|
|
|
1325
3539
|
});
|
|
1326
3540
|
}
|
|
1327
3541
|
paths.forEach((path) => {
|
|
1328
|
-
this.
|
|
3542
|
+
this.removeIgnoredPath(path);
|
|
3543
|
+
this.addRootAncestors(path);
|
|
1329
3544
|
});
|
|
1330
|
-
this.
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, undefined, 0, _origAdd);
|
|
1336
|
-
if (res)
|
|
1337
|
-
this._emitReady();
|
|
1338
|
-
return res;
|
|
1339
|
-
})).then((results) => {
|
|
1340
|
-
if (this.closed)
|
|
3545
|
+
if (!this.readyEmitted)
|
|
3546
|
+
this.#readyPending = true;
|
|
3547
|
+
const addTask = Promise.all(paths.map(async (path) => {
|
|
3548
|
+
const key = logicalPathKey(path);
|
|
3549
|
+
if (this.lifecycle.closers.has(key) || this.#pendingAdds.has(key))
|
|
1341
3550
|
return;
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
3551
|
+
const pendingToken = Symbol(key);
|
|
3552
|
+
const pathGeneration = this.capturePathGeneration();
|
|
3553
|
+
this.#pendingAdds.set(key, pendingToken);
|
|
3554
|
+
try {
|
|
3555
|
+
await this.handler.addRoot(path, true, pathGeneration);
|
|
3556
|
+
}
|
|
3557
|
+
finally {
|
|
3558
|
+
if (this.#pendingAdds.get(key) === pendingToken)
|
|
3559
|
+
this.#pendingAdds.delete(key);
|
|
3560
|
+
}
|
|
3561
|
+
}));
|
|
3562
|
+
this.lifecycle.track(addTask);
|
|
3563
|
+
if (!this.readyEmitted)
|
|
3564
|
+
this.queueReady();
|
|
1347
3565
|
return this;
|
|
1348
3566
|
}
|
|
1349
3567
|
/**
|
|
@@ -1352,26 +3570,26 @@ class FSWatcher extends node_events.EventEmitter {
|
|
|
1352
3570
|
unwatch(paths_) {
|
|
1353
3571
|
if (this.closed)
|
|
1354
3572
|
return this;
|
|
1355
|
-
|
|
3573
|
+
let paths = unifyPaths(paths_);
|
|
1356
3574
|
const { cwd } = this.options;
|
|
3575
|
+
if (cwd)
|
|
3576
|
+
paths = paths.map((path) => getAbsolutePath(path, cwd));
|
|
1357
3577
|
paths.forEach((path) => {
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
this.
|
|
1365
|
-
this.
|
|
1366
|
-
if (
|
|
1367
|
-
this.
|
|
1368
|
-
path,
|
|
3578
|
+
const key = logicalPathKey(path);
|
|
3579
|
+
const isDirectory = this.tree.watched.has(key);
|
|
3580
|
+
this.invalidatePath(key);
|
|
3581
|
+
this.removeRootAncestors(key);
|
|
3582
|
+
this.#pendingAdds.delete(key);
|
|
3583
|
+
this.events.cancelPath(key);
|
|
3584
|
+
this.closePath(key, isDirectory);
|
|
3585
|
+
this.addIgnoredPath(key);
|
|
3586
|
+
if (isDirectory) {
|
|
3587
|
+
this.addIgnoredPath({
|
|
3588
|
+
path: key,
|
|
1369
3589
|
recursive: true,
|
|
1370
3590
|
});
|
|
1371
3591
|
}
|
|
1372
|
-
|
|
1373
|
-
// to make ignoredPaths changes effective
|
|
1374
|
-
this._userIgnored = undefined;
|
|
3592
|
+
this.#unwatchIgnored = undefined;
|
|
1375
3593
|
});
|
|
1376
3594
|
return this;
|
|
1377
3595
|
}
|
|
@@ -1379,46 +3597,54 @@ class FSWatcher extends node_events.EventEmitter {
|
|
|
1379
3597
|
* Close watchers and remove all listeners from watched paths.
|
|
1380
3598
|
*/
|
|
1381
3599
|
close() {
|
|
1382
|
-
if (this
|
|
1383
|
-
return this
|
|
3600
|
+
if (this.#closePromise) {
|
|
3601
|
+
return this.#closePromise;
|
|
1384
3602
|
}
|
|
1385
|
-
|
|
3603
|
+
const closers = this.lifecycle.beginClose();
|
|
1386
3604
|
// Memory management.
|
|
1387
3605
|
this.removeAllListeners();
|
|
1388
|
-
|
|
1389
|
-
this.
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
this
|
|
1395
|
-
this.
|
|
1396
|
-
this.
|
|
1397
|
-
this.
|
|
1398
|
-
this.
|
|
1399
|
-
this.
|
|
1400
|
-
this.
|
|
1401
|
-
this
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
3606
|
+
this.events.close();
|
|
3607
|
+
this.#pendingAdds.clear();
|
|
3608
|
+
this.#roots.clear();
|
|
3609
|
+
this.#rootAncestors.clear();
|
|
3610
|
+
this.streams.forEach((stream) => stream.destroy());
|
|
3611
|
+
this.#userIgnored = undefined;
|
|
3612
|
+
this.#unwatchIgnored = undefined;
|
|
3613
|
+
this.readyEmitted = false;
|
|
3614
|
+
this.tree.dispose();
|
|
3615
|
+
this.#pathBarriers.clear();
|
|
3616
|
+
this.streams.clear();
|
|
3617
|
+
this.reconciliation.clear();
|
|
3618
|
+
this.#realDirs.clear();
|
|
3619
|
+
this.#closePromise = (async () => {
|
|
3620
|
+
const closerResults = await Promise.allSettled(closers);
|
|
3621
|
+
await this.lifecycle.drain();
|
|
3622
|
+
this.lifecycle.finishClose();
|
|
3623
|
+
const failed = closerResults.find((result) => result.status === 'rejected');
|
|
3624
|
+
if (failed)
|
|
3625
|
+
throw failed.reason;
|
|
3626
|
+
})();
|
|
3627
|
+
return this.#closePromise;
|
|
1408
3628
|
}
|
|
1409
3629
|
/**
|
|
1410
|
-
* Expose list of watched paths
|
|
1411
|
-
*
|
|
3630
|
+
* Expose list of watched paths. The result has a null prototype, so a
|
|
3631
|
+
* directory named `__proto__` is an ordinary own key.
|
|
1412
3632
|
*/
|
|
1413
3633
|
getWatched() {
|
|
1414
|
-
const watchList =
|
|
1415
|
-
this.
|
|
3634
|
+
const watchList = Object.create(null);
|
|
3635
|
+
this.tree.watched.forEach((entry) => {
|
|
3636
|
+
const dir = entry.path;
|
|
1416
3637
|
const key = this.options.cwd ? sp__namespace.relative(this.options.cwd, dir) : dir;
|
|
1417
|
-
const index = key ||
|
|
3638
|
+
const index = key || '.';
|
|
1418
3639
|
watchList[index] = entry.getChildren().sort();
|
|
1419
3640
|
});
|
|
1420
3641
|
return watchList;
|
|
1421
3642
|
}
|
|
3643
|
+
/** @internal */
|
|
3644
|
+
emitRaw(...args) {
|
|
3645
|
+
if (!this.closed)
|
|
3646
|
+
this.emit(EVENTS.RAW, ...args);
|
|
3647
|
+
}
|
|
1422
3648
|
emitWithAll(event, args) {
|
|
1423
3649
|
this.emit(event, ...args);
|
|
1424
3650
|
if (event !== EVENTS.ERROR)
|
|
@@ -1428,348 +3654,318 @@ class FSWatcher extends node_events.EventEmitter {
|
|
|
1428
3654
|
// --------------
|
|
1429
3655
|
/**
|
|
1430
3656
|
* Normalize and emit events.
|
|
1431
|
-
* Calling
|
|
3657
|
+
* Calling emitEvent DOES NOT MEAN emit() would be called!
|
|
1432
3658
|
* @param event Type of event
|
|
1433
3659
|
* @param path File or directory path
|
|
1434
3660
|
* @param stats arguments to be passed with event
|
|
1435
|
-
* @
|
|
3661
|
+
* @internal
|
|
1436
3662
|
*/
|
|
1437
|
-
async
|
|
3663
|
+
async emitEvent(event, path, stats) {
|
|
1438
3664
|
if (this.closed)
|
|
1439
3665
|
return;
|
|
1440
|
-
|
|
1441
|
-
if (isWindows)
|
|
1442
|
-
path = sp__namespace.normalize(path);
|
|
1443
|
-
if (opts.cwd)
|
|
1444
|
-
path = sp__namespace.relative(opts.cwd, path);
|
|
1445
|
-
const args = [path];
|
|
1446
|
-
if (stats != null)
|
|
1447
|
-
args.push(stats);
|
|
1448
|
-
const awf = opts.awaitWriteFinish;
|
|
1449
|
-
let pw;
|
|
1450
|
-
if (awf && (pw = this._pendingWrites.get(path))) {
|
|
1451
|
-
pw.lastChange = new Date();
|
|
1452
|
-
return this;
|
|
1453
|
-
}
|
|
1454
|
-
if (opts.atomic) {
|
|
1455
|
-
if (event === EVENTS.UNLINK) {
|
|
1456
|
-
this._pendingUnlinks.set(path, [event, ...args]);
|
|
1457
|
-
setTimeout(() => {
|
|
1458
|
-
this._pendingUnlinks.forEach((entry, path) => {
|
|
1459
|
-
this.emit(...entry);
|
|
1460
|
-
this.emit(EVENTS.ALL, ...entry);
|
|
1461
|
-
this._pendingUnlinks.delete(path);
|
|
1462
|
-
});
|
|
1463
|
-
}, typeof opts.atomic === 'number' ? opts.atomic : 100);
|
|
1464
|
-
return this;
|
|
1465
|
-
}
|
|
1466
|
-
if (event === EVENTS.ADD && this._pendingUnlinks.has(path)) {
|
|
1467
|
-
event = EVENTS.CHANGE;
|
|
1468
|
-
this._pendingUnlinks.delete(path);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
if (awf && (event === EVENTS.ADD || event === EVENTS.CHANGE) && this._readyEmitted) {
|
|
1472
|
-
const awfEmit = (err, stats) => {
|
|
1473
|
-
if (err) {
|
|
1474
|
-
event = EVENTS.ERROR;
|
|
1475
|
-
args[0] = err;
|
|
1476
|
-
this.emitWithAll(event, args);
|
|
1477
|
-
}
|
|
1478
|
-
else if (stats) {
|
|
1479
|
-
// if stats doesn't exist the file must have been deleted
|
|
1480
|
-
if (args.length > 1) {
|
|
1481
|
-
args[1] = stats;
|
|
1482
|
-
}
|
|
1483
|
-
else {
|
|
1484
|
-
args.push(stats);
|
|
1485
|
-
}
|
|
1486
|
-
this.emitWithAll(event, args);
|
|
1487
|
-
}
|
|
1488
|
-
};
|
|
1489
|
-
this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
|
|
1490
|
-
return this;
|
|
1491
|
-
}
|
|
1492
|
-
if (event === EVENTS.CHANGE) {
|
|
1493
|
-
const isThrottled = !this._throttle(EVENTS.CHANGE, path, 50);
|
|
1494
|
-
if (isThrottled)
|
|
1495
|
-
return this;
|
|
1496
|
-
}
|
|
1497
|
-
if (opts.alwaysStat &&
|
|
1498
|
-
stats === undefined &&
|
|
1499
|
-
(event === EVENTS.ADD || event === EVENTS.ADD_DIR || event === EVENTS.CHANGE)) {
|
|
1500
|
-
const fullPath = opts.cwd ? sp__namespace.join(opts.cwd, path) : path;
|
|
1501
|
-
let stats;
|
|
1502
|
-
try {
|
|
1503
|
-
stats = await promises.stat(fullPath);
|
|
1504
|
-
}
|
|
1505
|
-
catch (err) {
|
|
1506
|
-
// do nothing
|
|
1507
|
-
}
|
|
1508
|
-
// Suppress event when fs_stat fails, to avoid sending undefined 'stat'
|
|
1509
|
-
if (!stats || this.closed)
|
|
1510
|
-
return;
|
|
1511
|
-
args.push(stats);
|
|
1512
|
-
}
|
|
1513
|
-
this.emitWithAll(event, args);
|
|
1514
|
-
return this;
|
|
3666
|
+
await this.events.emit(event, path, stats);
|
|
1515
3667
|
}
|
|
1516
3668
|
/**
|
|
1517
|
-
* Common handler for
|
|
1518
|
-
* @
|
|
3669
|
+
* Common handler for backend and reconciliation failures.
|
|
3670
|
+
* @internal
|
|
1519
3671
|
*/
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
(!this.options.ignorePermissionErrors || (
|
|
1526
|
-
this.emit(EVENTS.ERROR,
|
|
3672
|
+
handleError(error) {
|
|
3673
|
+
if (this.closed)
|
|
3674
|
+
return;
|
|
3675
|
+
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
3676
|
+
if (!isMissingError(normalized) &&
|
|
3677
|
+
(!this.options.ignorePermissionErrors || !isPermissionError(normalized))) {
|
|
3678
|
+
this.emit(EVENTS.ERROR, normalized);
|
|
1527
3679
|
}
|
|
1528
|
-
return error || this.closed;
|
|
1529
3680
|
}
|
|
1530
3681
|
/**
|
|
1531
|
-
*
|
|
1532
|
-
* @
|
|
1533
|
-
* @param path being acted upon
|
|
1534
|
-
* @param timeout duration of time to suppress duplicate actions
|
|
1535
|
-
* @returns tracking object or false if action should be suppressed
|
|
3682
|
+
* Determines whether user has asked to ignore this path.
|
|
3683
|
+
* @internal
|
|
1536
3684
|
*/
|
|
1537
|
-
|
|
1538
|
-
if (
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
3685
|
+
isIgnored(path, stats) {
|
|
3686
|
+
if (this.options.atomic && DOT_RE.test(path))
|
|
3687
|
+
return true;
|
|
3688
|
+
if (!this.#userIgnored) {
|
|
3689
|
+
const { cwd } = this.options;
|
|
3690
|
+
const ign = this.options.ignored;
|
|
3691
|
+
const ignored = (ign || []).map((matcher) => normalizeMatcher(matcher, cwd));
|
|
3692
|
+
const direct = compileMatchers(ignored);
|
|
3693
|
+
const pathMatchers = ignored.filter((matcher) => typeof matcher === 'string' || isMatcherObject(matcher));
|
|
3694
|
+
const canonical = compileMatchers(pathMatchers);
|
|
3695
|
+
this.#userIgnored = (candidate, candidateStats) => {
|
|
3696
|
+
if (direct(candidate, candidateStats))
|
|
3697
|
+
return true;
|
|
3698
|
+
if (isWindows || pathMatchers.length === 0)
|
|
3699
|
+
return false;
|
|
3700
|
+
// macOS commonly exposes /var through the /private/var symlink. Resolve
|
|
3701
|
+
// the containing directory (memoized per directory) instead of the
|
|
3702
|
+
// path itself, so files that no longer exist still project correctly.
|
|
3703
|
+
const absoluteCandidate = sp__namespace.resolve(candidate);
|
|
3704
|
+
let realPath;
|
|
3705
|
+
if (candidateStats?.isDirectory()) {
|
|
3706
|
+
realPath = this.realDirectory(absoluteCandidate);
|
|
3707
|
+
}
|
|
3708
|
+
else {
|
|
3709
|
+
const realDir = this.realDirectory(sp__namespace.dirname(absoluteCandidate));
|
|
3710
|
+
if (realDir !== undefined)
|
|
3711
|
+
realPath = sp__namespace.join(realDir, sp__namespace.basename(absoluteCandidate));
|
|
3712
|
+
}
|
|
3713
|
+
return (realPath !== undefined &&
|
|
3714
|
+
realPath !== absoluteCandidate &&
|
|
3715
|
+
canonical(realPath, candidateStats));
|
|
3716
|
+
};
|
|
3717
|
+
}
|
|
3718
|
+
// Directories above a watched path stay watchable, so a missing path can be
|
|
3719
|
+
// observed through its parent (#1374). Their other contents are still filtered.
|
|
3720
|
+
if (this.#userIgnored(path, stats) && !this.isRootAncestor(path, stats))
|
|
3721
|
+
return true;
|
|
3722
|
+
if (this.#ignoredPaths.size === 0)
|
|
1547
3723
|
return false;
|
|
3724
|
+
if (!this.#unwatchIgnored) {
|
|
3725
|
+
this.#unwatchIgnored = compileMatchers([...this.#ignoredPaths]);
|
|
1548
3726
|
}
|
|
1549
|
-
|
|
1550
|
-
let timeoutObject;
|
|
1551
|
-
const clear = () => {
|
|
1552
|
-
const item = action.get(path);
|
|
1553
|
-
const count = item ? item.count : 0;
|
|
1554
|
-
action.delete(path);
|
|
1555
|
-
clearTimeout(timeoutObject);
|
|
1556
|
-
if (item)
|
|
1557
|
-
clearTimeout(item.timeoutObject);
|
|
1558
|
-
return count;
|
|
1559
|
-
};
|
|
1560
|
-
timeoutObject = setTimeout(clear, timeout);
|
|
1561
|
-
const thr = { timeoutObject, clear, count: 0 };
|
|
1562
|
-
action.set(path, thr);
|
|
1563
|
-
return thr;
|
|
3727
|
+
return this.#unwatchIgnored(logicalPathKey(path), stats);
|
|
1564
3728
|
}
|
|
1565
|
-
|
|
1566
|
-
|
|
3729
|
+
isRootAncestor(path, stats) {
|
|
3730
|
+
// Regular files are never ancestors; skip the resolve on the hot scan path.
|
|
3731
|
+
if (stats?.isFile())
|
|
3732
|
+
return false;
|
|
3733
|
+
return this.#rootAncestors.has(logicalPathKey(path));
|
|
1567
3734
|
}
|
|
1568
|
-
/**
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
* @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
|
|
1573
|
-
* @param event
|
|
1574
|
-
* @param awfEmit Callback to be called when ready for event to be emitted.
|
|
1575
|
-
*/
|
|
1576
|
-
_awaitWriteFinish(path, threshold, event, awfEmit) {
|
|
1577
|
-
const awf = this.options.awaitWriteFinish;
|
|
1578
|
-
if (typeof awf !== 'object')
|
|
3735
|
+
/** Records every directory above a requested root; refcounted across roots. */
|
|
3736
|
+
addRootAncestors(path) {
|
|
3737
|
+
const key = logicalPathKey(path);
|
|
3738
|
+
if (this.#roots.has(key))
|
|
1579
3739
|
return;
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
if (this.options.cwd && !sp__namespace.isAbsolute(path)) {
|
|
1584
|
-
fullPath = sp__namespace.join(this.options.cwd, path);
|
|
1585
|
-
}
|
|
1586
|
-
const now = new Date();
|
|
1587
|
-
const writes = this._pendingWrites;
|
|
1588
|
-
function awaitWriteFinishFn(prevStat) {
|
|
1589
|
-
node_fs.stat(fullPath, (err, curStat) => {
|
|
1590
|
-
if (err || !writes.has(path)) {
|
|
1591
|
-
if (err && err.code !== 'ENOENT')
|
|
1592
|
-
awfEmit(err);
|
|
1593
|
-
return;
|
|
1594
|
-
}
|
|
1595
|
-
const now = Number(new Date());
|
|
1596
|
-
if (prevStat && curStat.size !== prevStat.size) {
|
|
1597
|
-
writes.get(path).lastChange = now;
|
|
1598
|
-
}
|
|
1599
|
-
const pw = writes.get(path);
|
|
1600
|
-
const df = now - pw.lastChange;
|
|
1601
|
-
if (df >= threshold) {
|
|
1602
|
-
writes.delete(path);
|
|
1603
|
-
awfEmit(undefined, curStat);
|
|
1604
|
-
}
|
|
1605
|
-
else {
|
|
1606
|
-
timeoutHandler = setTimeout(awaitWriteFinishFn, pollInterval, curStat);
|
|
1607
|
-
}
|
|
1608
|
-
});
|
|
3740
|
+
this.#roots.add(key);
|
|
3741
|
+
for (const ancestor of ancestorKeys(path)) {
|
|
3742
|
+
this.#rootAncestors.set(ancestor, (this.#rootAncestors.get(ancestor) ?? 0) + 1);
|
|
1609
3743
|
}
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
3744
|
+
}
|
|
3745
|
+
removeRootAncestors(path) {
|
|
3746
|
+
if (!this.#roots.delete(logicalPathKey(path)))
|
|
3747
|
+
return;
|
|
3748
|
+
for (const ancestor of ancestorKeys(path)) {
|
|
3749
|
+
const count = (this.#rootAncestors.get(ancestor) ?? 1) - 1;
|
|
3750
|
+
if (count > 0)
|
|
3751
|
+
this.#rootAncestors.set(ancestor, count);
|
|
3752
|
+
else
|
|
3753
|
+
this.#rootAncestors.delete(ancestor);
|
|
1620
3754
|
}
|
|
1621
3755
|
}
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
const { cwd } = this.options;
|
|
1630
|
-
const ign = this.options.ignored;
|
|
1631
|
-
const ignored = (ign || []).map(normalizeIgnored(cwd));
|
|
1632
|
-
const ignoredPaths = [...this._ignoredPaths];
|
|
1633
|
-
const list = [...ignoredPaths.map(normalizeIgnored(cwd)), ...ignored];
|
|
1634
|
-
this._userIgnored = anymatch(list);
|
|
3756
|
+
realDirectory(directory) {
|
|
3757
|
+
const key = logicalPathKey(directory);
|
|
3758
|
+
if (this.#realDirs.has(key))
|
|
3759
|
+
return this.#realDirs.get(key);
|
|
3760
|
+
let realPath;
|
|
3761
|
+
try {
|
|
3762
|
+
realPath = node_fs.realpathSync.native(directory);
|
|
1635
3763
|
}
|
|
1636
|
-
|
|
3764
|
+
catch { }
|
|
3765
|
+
this.#realDirs.set(key, realPath);
|
|
3766
|
+
return realPath;
|
|
1637
3767
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
3768
|
+
/** @internal */
|
|
3769
|
+
isUnwatched(path) {
|
|
3770
|
+
if (this.#ignoredPaths.size === 0)
|
|
3771
|
+
return false;
|
|
3772
|
+
if (!this.#unwatchIgnored) {
|
|
3773
|
+
this.#unwatchIgnored = compileMatchers([...this.#ignoredPaths]);
|
|
3774
|
+
}
|
|
3775
|
+
return this.#unwatchIgnored(logicalPathKey(path));
|
|
1640
3776
|
}
|
|
1641
3777
|
/**
|
|
1642
3778
|
* Provides a set of common helpers and properties relating to symlink handling.
|
|
1643
3779
|
* @param path file or directory pattern being watched
|
|
3780
|
+
* @internal
|
|
1644
3781
|
*/
|
|
1645
|
-
|
|
1646
|
-
return new WatchHelper(path, this.options.followSymlinks,
|
|
3782
|
+
createHelper(path) {
|
|
3783
|
+
return new WatchHelper(path, this.options.followSymlinks, {
|
|
3784
|
+
capturePathGeneration: () => this.capturePathGeneration(),
|
|
3785
|
+
isntIgnored: (candidate, stats) => !this.isIgnored(candidate, stats),
|
|
3786
|
+
});
|
|
1647
3787
|
}
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
3788
|
+
removeTreeItem(directory, item) {
|
|
3789
|
+
const entry = this.tree.peekDirectory(directory);
|
|
3790
|
+
if (!entry?.remove(item))
|
|
3791
|
+
return;
|
|
3792
|
+
const path = entry.path;
|
|
3793
|
+
const generation = this.lifecycle.generation;
|
|
3794
|
+
this.lifecycle.track((async () => {
|
|
3795
|
+
try {
|
|
3796
|
+
await promises.readdir(path);
|
|
3797
|
+
}
|
|
3798
|
+
catch (error) {
|
|
3799
|
+
// Only a missing directory is gone; an unreadable one still exists.
|
|
3800
|
+
if (isMissingError(error) && this.lifecycle.isActive(generation)) {
|
|
3801
|
+
this.removePath(sp__namespace.dirname(path), sp__namespace.basename(path));
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
})());
|
|
1659
3805
|
}
|
|
1660
3806
|
// File helpers
|
|
1661
3807
|
// ------------
|
|
1662
|
-
/**
|
|
1663
|
-
* Check for read permissions: https://stackoverflow.com/a/11781404/1358405
|
|
1664
|
-
*/
|
|
1665
|
-
_hasReadPermissions(stats) {
|
|
1666
|
-
if (this.options.ignorePermissionErrors)
|
|
1667
|
-
return true;
|
|
1668
|
-
return Boolean(Number(stats.mode) & 0o400);
|
|
1669
|
-
}
|
|
1670
3808
|
/**
|
|
1671
3809
|
* Handles emitting unlink events for
|
|
1672
3810
|
* files and directories, and via recursion, for
|
|
1673
3811
|
* files and directories within directories that are unlinked
|
|
1674
3812
|
* @param directory within which the following item is located
|
|
1675
3813
|
* @param item base path of item/directory
|
|
3814
|
+
* @internal
|
|
1676
3815
|
*/
|
|
1677
|
-
|
|
3816
|
+
removePath(directory, item, isDirectory) {
|
|
1678
3817
|
// if what is being deleted is a directory, get that directory's paths
|
|
1679
3818
|
// for recursive deleting and cleaning of watched object
|
|
1680
3819
|
// if it is not a directory, nestedDirectoryChildren will be empty array
|
|
1681
3820
|
const path = sp__namespace.join(directory, item);
|
|
1682
|
-
const
|
|
1683
|
-
isDirectory =
|
|
1684
|
-
|
|
1685
|
-
//
|
|
1686
|
-
// via multiple paths (such as _handleFile and _handleDir)
|
|
1687
|
-
if (!this._throttle('remove', path, 100))
|
|
1688
|
-
return;
|
|
1689
|
-
// if the only watched file is removed, watch for its return
|
|
1690
|
-
if (!isDirectory && this._watched.size === 1) {
|
|
1691
|
-
this.add(directory, item, true);
|
|
1692
|
-
}
|
|
1693
|
-
// This will create a new entry in the watched object in either case
|
|
1694
|
-
// so we got to do the directory check beforehand
|
|
1695
|
-
const wp = this._getWatchedDir(path);
|
|
1696
|
-
const nestedDirectoryChildren = wp.getChildren();
|
|
3821
|
+
const logicalKey = logicalPathKey(path);
|
|
3822
|
+
isDirectory = isDirectory != null ? isDirectory : this.tree.watched.has(logicalKey);
|
|
3823
|
+
// Removal is idempotent: a second caller finds nothing tracked and emits
|
|
3824
|
+
// nothing, while a path re-added in between is truthfully removed again.
|
|
1697
3825
|
// Recursively remove children directories / files.
|
|
1698
|
-
nestedDirectoryChildren
|
|
3826
|
+
const nestedDirectoryChildren = this.tree.peekDirectory(path)?.getChildren() ?? [];
|
|
3827
|
+
for (const nested of nestedDirectoryChildren)
|
|
3828
|
+
this.removePath(path, nested);
|
|
1699
3829
|
// Check if item was on the watched list and remove it
|
|
1700
|
-
const
|
|
1701
|
-
const
|
|
1702
|
-
|
|
3830
|
+
const wasTracked = this.tree.peekDirectory(directory)?.has(item) ?? false;
|
|
3831
|
+
const hadClosers = this.lifecycle.closers.has(logicalKey);
|
|
3832
|
+
this.removeTreeItem(directory, item);
|
|
1703
3833
|
// Fixes issue #1042 -> Relative paths were detected and added as symlinks
|
|
1704
3834
|
// (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
|
|
1705
3835
|
// but never removed from the map in case the path was deleted.
|
|
1706
3836
|
// This leads to an incorrect state if the path was recreated:
|
|
1707
3837
|
// https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
|
|
1708
|
-
if (this.
|
|
1709
|
-
this.
|
|
3838
|
+
if (this.tree.symlinkPaths.has(logicalKey)) {
|
|
3839
|
+
this.tree.symlinkPaths.delete(logicalKey);
|
|
1710
3840
|
}
|
|
1711
3841
|
// If we wait for this file to be fully written, cancel the wait.
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
if (event === EVENTS.ADD)
|
|
1718
|
-
return;
|
|
3842
|
+
const pendingWrite = this.events.pendingWrites.get(logicalKey);
|
|
3843
|
+
let suppressEvent = false;
|
|
3844
|
+
if (this.options.awaitWriteFinish && pendingWrite) {
|
|
3845
|
+
const event = pendingWrite.cancelWait();
|
|
3846
|
+
suppressEvent = event === EVENTS.ADD;
|
|
1719
3847
|
}
|
|
1720
|
-
//
|
|
1721
|
-
|
|
1722
|
-
this.
|
|
1723
|
-
this._watched.delete(fullPath);
|
|
3848
|
+
// Drop the entry if what got removed was a watched directory
|
|
3849
|
+
this.tree.watched.delete(logicalKey);
|
|
3850
|
+
this.tree.observed.delete(logicalKey);
|
|
1724
3851
|
const eventName = isDirectory ? EVENTS.UNLINK_DIR : EVENTS.UNLINK;
|
|
1725
|
-
if (wasTracked && !this.
|
|
1726
|
-
this.
|
|
3852
|
+
if (wasTracked && !suppressEvent && !this.isIgnored(path))
|
|
3853
|
+
this.emitEvent(eventName, path);
|
|
1727
3854
|
// Avoid conflicts if we later create another file with the same name
|
|
1728
|
-
this.
|
|
3855
|
+
this.closePath(path);
|
|
3856
|
+
// A requested root that disappears is awaited from its nearest existing ancestor.
|
|
3857
|
+
if ((wasTracked || hadClosers) && this.#roots.has(logicalKey))
|
|
3858
|
+
this.rewatchMissingPath(path);
|
|
3859
|
+
}
|
|
3860
|
+
/**
|
|
3861
|
+
* Replaces every watch keyed at `path` with a wait from its nearest existing
|
|
3862
|
+
* ancestor, so the path is reported again when it reappears.
|
|
3863
|
+
* @internal
|
|
3864
|
+
*/
|
|
3865
|
+
rewatchMissingPath(path) {
|
|
3866
|
+
if (this.closed || this.isUnwatched(path))
|
|
3867
|
+
return;
|
|
3868
|
+
this.closePath(path);
|
|
3869
|
+
this.lifecycle.track(this.handler.addRoot(path, false, this.capturePathGeneration()));
|
|
1729
3870
|
}
|
|
1730
3871
|
/**
|
|
1731
3872
|
* Closes all watchers for a path
|
|
3873
|
+
* @internal
|
|
1732
3874
|
*/
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
const
|
|
1736
|
-
|
|
3875
|
+
closePath(path, recursive = false) {
|
|
3876
|
+
const logicalKey = logicalPathKey(path);
|
|
3877
|
+
const contains = (candidate) => {
|
|
3878
|
+
if (candidate === logicalKey)
|
|
3879
|
+
return true;
|
|
3880
|
+
if (!recursive)
|
|
3881
|
+
return false;
|
|
3882
|
+
return isStrictlyInside(logicalKey, candidate);
|
|
3883
|
+
};
|
|
3884
|
+
// Snapshot closers: closing one may register or drop others.
|
|
3885
|
+
[...this.lifecycle.closers.keys()].filter(contains).forEach((key) => this.closeFile(key));
|
|
3886
|
+
// Resolutions at or below a closed directory may be stale. Only directories
|
|
3887
|
+
// are cached, so file closes stay O(1). Deleting the current key while
|
|
3888
|
+
// iterating a Map is safe.
|
|
3889
|
+
if (this.#realDirs.delete(logicalKey) || recursive) {
|
|
3890
|
+
const prefix = logicalKey.endsWith('/') ? logicalKey : `${logicalKey}/`;
|
|
3891
|
+
for (const key of this.#realDirs.keys()) {
|
|
3892
|
+
if (key.startsWith(prefix))
|
|
3893
|
+
this.#realDirs.delete(key);
|
|
3894
|
+
}
|
|
3895
|
+
}
|
|
3896
|
+
if (recursive) {
|
|
3897
|
+
for (const [key, entry] of this.tree.watched) {
|
|
3898
|
+
if (!contains(key))
|
|
3899
|
+
continue;
|
|
3900
|
+
entry.dispose();
|
|
3901
|
+
this.tree.watched.delete(key);
|
|
3902
|
+
}
|
|
3903
|
+
for (const key of this.tree.observed.keys()) {
|
|
3904
|
+
if (contains(key))
|
|
3905
|
+
this.tree.observed.delete(key);
|
|
3906
|
+
}
|
|
3907
|
+
for (const key of this.tree.symlinkPaths.keys()) {
|
|
3908
|
+
if (contains(key))
|
|
3909
|
+
this.tree.symlinkPaths.delete(key);
|
|
3910
|
+
}
|
|
3911
|
+
this.events.cancelWhere(contains);
|
|
3912
|
+
this.reconciliation.forgetPending((scope, candidate) => contains(scope) || contains(candidate));
|
|
3913
|
+
for (const key of this.#pendingAdds.keys()) {
|
|
3914
|
+
if (contains(key))
|
|
3915
|
+
this.#pendingAdds.delete(key);
|
|
3916
|
+
}
|
|
3917
|
+
}
|
|
3918
|
+
const dir = sp__namespace.dirname(logicalKey);
|
|
3919
|
+
this.removeTreeItem(dir, sp__namespace.basename(logicalKey));
|
|
1737
3920
|
}
|
|
1738
3921
|
/**
|
|
1739
3922
|
* Closes only file-specific watchers
|
|
1740
3923
|
*/
|
|
1741
|
-
|
|
1742
|
-
const
|
|
1743
|
-
|
|
3924
|
+
closeFile(path) {
|
|
3925
|
+
const key = logicalPathKey(path);
|
|
3926
|
+
const closers = this.lifecycle.takeClosers(key);
|
|
3927
|
+
if (closers.length === 0)
|
|
1744
3928
|
return;
|
|
1745
|
-
closers.forEach((closer) =>
|
|
1746
|
-
|
|
3929
|
+
closers.forEach((closer) => {
|
|
3930
|
+
try {
|
|
3931
|
+
const result = closer();
|
|
3932
|
+
if (result instanceof Promise)
|
|
3933
|
+
this.lifecycle.track(result);
|
|
3934
|
+
}
|
|
3935
|
+
catch (error) {
|
|
3936
|
+
this.lifecycle.track(Promise.reject(error));
|
|
3937
|
+
}
|
|
3938
|
+
});
|
|
1747
3939
|
}
|
|
1748
|
-
|
|
3940
|
+
/** @internal */
|
|
3941
|
+
addPathCloser(path, closer) {
|
|
1749
3942
|
if (!closer)
|
|
1750
3943
|
return;
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
3944
|
+
if (this.closed || this.isUnwatched(path)) {
|
|
3945
|
+
try {
|
|
3946
|
+
const result = closer();
|
|
3947
|
+
if (result instanceof Promise)
|
|
3948
|
+
this.lifecycle.track(result);
|
|
3949
|
+
}
|
|
3950
|
+
catch (error) {
|
|
3951
|
+
this.lifecycle.track(Promise.reject(error));
|
|
3952
|
+
}
|
|
3953
|
+
return;
|
|
1755
3954
|
}
|
|
1756
|
-
|
|
3955
|
+
const key = logicalPathKey(path);
|
|
3956
|
+
this.lifecycle.addCloser(key, closer);
|
|
1757
3957
|
}
|
|
1758
|
-
|
|
3958
|
+
/** @internal */
|
|
3959
|
+
createScanStream(root, opts) {
|
|
1759
3960
|
if (this.closed)
|
|
1760
3961
|
return;
|
|
1761
|
-
const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true,
|
|
1762
|
-
|
|
1763
|
-
this.
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
stream.once(
|
|
1768
|
-
if (stream) {
|
|
1769
|
-
this._streams.delete(stream);
|
|
1770
|
-
stream = undefined;
|
|
1771
|
-
}
|
|
1772
|
-
});
|
|
3962
|
+
const options = { type: EVENTS.ALL, alwaysStat: true, lstat: true, depth: 0, ...opts };
|
|
3963
|
+
const stream = readdirp(root, options);
|
|
3964
|
+
this.streams.add(stream);
|
|
3965
|
+
const finalize = () => this.streams.delete(stream);
|
|
3966
|
+
stream.once('close', finalize);
|
|
3967
|
+
stream.once('end', finalize);
|
|
3968
|
+
stream.once(EVENTS.ERROR, finalize);
|
|
1773
3969
|
return stream;
|
|
1774
3970
|
}
|
|
1775
3971
|
}
|