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