Repository navigation
Expand file tree
/
Copy pathWatchFileSystemAdapter.ts
More file actions
497 lines (441 loc) · 16.1 KB
/
Copy pathWatchFileSystemAdapter.ts
File metadata and controls
497 lines (441 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fs from 'node:fs';
import * as path from 'node:path';
import Watchpack from 'watchpack';
import { _tryGetWatchpackPendingEventState } from './WatchpackUtilities';
/**
* Options for `fs.readdir`
* @public
*/
export interface IReaddirOptions {
/**
* If true, readdir will return `fs.Dirent` objects instead of strings.
*/
withFileTypes: true;
}
/* eslint-disable @rushstack/no-new-null */
/**
* Callback for `fs.stat` and `fs.lstat`
* @public
*/
export type StatCallback = (error: NodeJS.ErrnoException | null, stats: fs.Stats) => void;
/**
* Callback for `fs.readdir` when `withFileTypes` is not specified or false
* @public
*/
export type ReaddirStringCallback = (error: NodeJS.ErrnoException | null, files: string[]) => void;
/**
* Callback for `fs.readdir` when `withFileTypes` is true
* @public
*/
export type ReaddirDirentCallback = (error: NodeJS.ErrnoException | null, files: fs.Dirent[]) => void;
/* eslint-enable @rushstack/no-new-null */
/**
* Information about the state of a watched file.
* @public
*/
export interface IWatchedFileState {
/**
* If the file has changed since the last invocation.
*/
changed: boolean;
}
/**
* Interface contract for heft plugins to use the `WatchFileSystemAdapter`
* @public
*/
export interface IWatchFileSystem {
/**
* Synchronous readdir. Watches the directory for changes.
*
* @see fs.readdirSync
*/
readdirSync(filePath: string): string[];
readdirSync(filePath: string, options: IReaddirOptions): fs.Dirent[];
/**
* Asynchronous readdir. Watches the directory for changes.
*
* @see fs.readdir
*/
readdir(filePath: string, callback: ReaddirStringCallback): void;
readdir(filePath: string, options: IReaddirOptions, callback: ReaddirDirentCallback): void;
/**
* Asynchronous lstat. Watches the file for changes, or if it does not exist, watches to see if it is created.
* @see fs.lstat
*/
lstat(filePath: string, callback: StatCallback): void;
/**
* Synchronous lstat. Watches the file for changes, or if it does not exist, watches to see if it is created.
* @see fs.lstatSync
*/
lstatSync(filePath: string): fs.Stats;
/**
* Asynchronous stat. Watches the file for changes, or if it does not exist, watches to see if it is created.
* @see fs.stat
*/
stat(filePath: string, callback: StatCallback): void;
/**
* Synchronous stat. Watches the file for changes, or if it does not exist, watches to see if it is created.
* @see fs.statSync
*/
statSync(filePath: string): fs.Stats;
/**
* Tells the adapter to track the specified file (or folder) as used.
* Returns an object containing data about the state of said file (or folder).
* Uses promise-based API.
*/
getStateAndTrackAsync(filePath: string): Promise<IWatchedFileState>;
/**
* Tells the adapter to track the specified file (or folder) as used.
* Returns an object containing data about the state of said file (or folder).
* Uses synchronous API.
*/
getStateAndTrack(filePath: string): IWatchedFileState;
}
/**
* Interface contract for `WatchFileSystemAdapter` for cross-version compatibility
*/
export interface IWatchFileSystemAdapter extends IWatchFileSystem {
/**
* Prepares for incoming glob requests. Any file changed after this method is called
* will trigger the watch callback in the next invocation.
* File mtimes will be recorded at this time for any files that do not receive explicit
* stat() or lstat() calls.
*/
setBaseline(): void;
/**
* Watches all files that have been accessed since `prepare()` was called.
* Clears the tracked file lists.
*/
watch(onChange: () => void): void;
}
interface ITimeEntry {
timestamp: number;
safeTime: number;
}
/**
* What a run read. The run's watcher checks the changes that it reports against this.
*/
interface IRunInputs {
/**
* When the run started. The watcher reports files whose times are close to this time, or later.
*/
baseline: number | undefined;
/**
* The time of each file that the run read, as the run recorded it
*/
files: ReadonlyMap<string, number>;
/**
* The folders that the run read
*/
contexts: ReadonlyMap<string, number>;
/**
* The times that the previous watcher had when the run started
*/
times: ReadonlyMap<string, ITimeEntry> | undefined;
}
const OUTDATED_ON_ATTACH_EXPLANATION: string = 'watch (outdated on attach)';
// A new watcher's first scan reports each file whose mtime is later than the watcher's start time, less the file
// system's accuracy. A watcher that attaches to the watcher of a folder that has already scanned reports the
// folder in the same way. Watchpack's other explanations are for events from the file system.
const SCAN_EXPLANATIONS: ReadonlySet<string> = new Set(['scan (file)', OUTDATED_ON_ATTACH_EXPLANATION]);
function tryLstatSync(filePath: string): fs.Stats | undefined {
try {
return fs.lstatSync(filePath, { throwIfNoEntry: false });
} catch {
return undefined;
}
}
function getTimestamp(stats: fs.Stats): number {
return stats.mtime.getTime() || stats.ctime.getTime() || Date.now();
}
/**
* Watchpack records a file's new time only when its `fs.lstat()` after the file system's event finishes. If a
* run starts before then, the previous watcher's time for the file is from before the change. Read the time
* again, so that the run sees the change, and so that the run's watcher knows that the run saw it.
*/
function refreshPendingTimes(watcher: Watchpack, times: Map<string, ITimeEntry>): void {
const pendingEventState: ReturnType<typeof _tryGetWatchpackPendingEventState> =
_tryGetWatchpackPendingEventState(watcher);
if (!pendingEventState) {
return;
}
for (const { filePath } of pendingEventState.pendingFileEvents) {
const stats: fs.Stats | undefined = tryLstatSync(filePath);
if (!stats) {
times.delete(filePath);
} else if (!stats.isDirectory()) {
const timestamp: number = getTimestamp(stats);
times.set(filePath, { timestamp, safeTime: timestamp });
}
}
}
/**
* Returns false if the watcher reported the path only because the path's time is close to the run's start, and
* the run has already read the path as it is now.
*/
function isReportedChangeNew(filePath: string, explanation: string, inputs: IRunInputs): boolean {
if (!SCAN_EXPLANATIONS.has(explanation) || inputs.baseline === undefined) {
return true;
}
const stats: fs.Stats | undefined = tryLstatSync(filePath);
if (!stats) {
return true;
}
if (stats.isDirectory()) {
// The watcher of a folder that the run read reports the folder's files one by one
return explanation !== OUTDATED_ON_ATTACH_EXPLANATION || !inputs.contexts.has(filePath);
}
if (stats.ctimeMs >= inputs.baseline) {
return true;
}
const readTime: number | undefined = inputs.files.get(filePath) ?? inputs.times?.get(filePath)?.timestamp;
return readTime !== getTimestamp(stats);
}
/**
* A filesystem adapter for use with the "fast-glob" package. This adapter tracks file system accesses
* to initialize `watchpack`.
*/
export class WatchFileSystemAdapter implements IWatchFileSystemAdapter {
#files: Map<string, number> = new Map();
#contexts: Map<string, number> = new Map();
#missing: Map<string, number> = new Map();
#watcher: Watchpack | undefined;
#lastFiles: Map<string, number> | undefined;
#lastQueryTime: number | undefined;
#times: Map<string, ITimeEntry> | undefined;
/** { @inheritdoc fs.readdirSync } */
public readdirSync: IWatchFileSystemAdapter['readdirSync'] = ((
filePath: string,
options?: IReaddirOptions
) => {
filePath = path.normalize(filePath);
try {
if (options?.withFileTypes) {
const results: fs.Dirent[] = fs.readdirSync(filePath, options);
this.#contexts.set(filePath, Date.now());
return results;
} else {
const results: string[] = fs.readdirSync(filePath);
this.#contexts.set(filePath, Date.now());
return results;
}
} catch (err) {
this.#missing.set(filePath, Date.now());
throw err;
}
}) as IWatchFileSystemAdapter['readdirSync'];
/** { @inheritdoc fs.readdir } */
public readdir: IWatchFileSystemAdapter['readdir'] = (
filePath: string,
optionsOrCallback: IReaddirOptions | ReaddirStringCallback,
callback?: ReaddirDirentCallback | ReaddirStringCallback
) => {
filePath = path.normalize(filePath);
// Default to no options, which will return a string callback
let options: IReaddirOptions | undefined;
if (typeof optionsOrCallback === 'object') {
options = optionsOrCallback;
} else if (typeof optionsOrCallback === 'function') {
callback = optionsOrCallback;
}
if (options?.withFileTypes) {
fs.readdir(filePath, options, (err: NodeJS.ErrnoException | null, entries: fs.Dirent[]) => {
if (err) {
this.#missing.set(filePath, Date.now());
} else {
this.#contexts.set(filePath, Date.now());
}
(callback as ReaddirDirentCallback)(err, entries);
});
} else {
fs.readdir(filePath, (err: NodeJS.ErrnoException | null, entries: string[]) => {
if (err) {
this.#missing.set(filePath, Date.now());
} else {
this.#contexts.set(filePath, Date.now());
}
(callback as ReaddirStringCallback)(err, entries);
});
}
};
/** { @inheritdoc fs.lstat } */
public lstat: IWatchFileSystemAdapter['lstat'] = (filePath: string, callback: StatCallback): void => {
filePath = path.normalize(filePath);
fs.lstat(filePath, (err: NodeJS.ErrnoException | null, stats: fs.Stats) => {
if (err) {
this.#missing.set(filePath, Date.now());
} else {
this.#files.set(filePath, stats.mtime.getTime() || stats.ctime.getTime() || Date.now());
}
callback(err, stats);
});
};
/** { @inheritdoc fs.lstatSync } */
public lstatSync: IWatchFileSystemAdapter['lstatSync'] = (filePath: string): fs.Stats => {
filePath = path.normalize(filePath);
try {
const stats: fs.Stats = fs.lstatSync(filePath);
this.#files.set(filePath, stats.mtime.getTime() || stats.ctime.getTime() || Date.now());
return stats;
} catch (err) {
this.#missing.set(filePath, Date.now());
throw err;
}
};
/** { @inheritdoc fs.stat } */
public stat: IWatchFileSystemAdapter['stat'] = (filePath: string, callback: StatCallback): void => {
filePath = path.normalize(filePath);
fs.stat(filePath, (err: NodeJS.ErrnoException | null, stats: fs.Stats) => {
if (err) {
this.#missing.set(filePath, Date.now());
} else {
this.#files.set(filePath, stats.mtime.getTime() || stats.ctime.getTime() || Date.now());
}
callback(err, stats);
});
};
/** { @inheritdoc fs.statSync } */
public statSync: IWatchFileSystemAdapter['statSync'] = (filePath: string) => {
filePath = path.normalize(filePath);
try {
const stats: fs.Stats = fs.statSync(filePath);
this.#files.set(filePath, stats.mtime.getTime() || stats.ctime.getTime() || Date.now());
return stats;
} catch (err) {
this.#missing.set(filePath, Date.now());
throw err;
}
};
/**
* @inheritdoc
*/
public setBaseline(): void {
this.#lastQueryTime = Date.now();
const watcher: Watchpack | undefined = this.#watcher;
if (watcher) {
this.#watcher = undefined;
const times: Map<string, ITimeEntry> = new Map();
watcher.collectTimeInfoEntries(times, times);
refreshPendingTimes(watcher, times);
// Close the previous watcher instead of only pausing it. A paused watcher keeps its directory watchers,
// so every run would leak another set of them. A kept watcher also keeps its OS watch on a folder that
// was deleted and recreated, and later watchers on that folder would share the dead watch, so edits in
// the recreated folder would be reported one run late.
watcher.close();
this.#times = times;
} else {
// Nothing is watching, so times collected for an earlier run may be out of date.
this.#times = undefined;
}
}
/**
* @inheritdoc
*/
public watch(onChange: () => void): void {
if (this.#files.size === 0 && this.#contexts.size === 0 && this.#missing.size === 0) {
return;
}
const inputs: IRunInputs = {
baseline: this.#lastQueryTime,
files: this.#files,
contexts: this.#contexts,
times: this.#times
};
const watcher: Watchpack = new Watchpack({
aggregateTimeout: 0,
followSymlinks: false
});
// A file that changed just before the run started is reported again when the watcher starts, although the
// run has read the change. Call onChange only for a change that the run may not have seen.
let hasNewChange: boolean = false;
const onReportedChange = (filePath: string, modifiedTime: number, explanation: string): void => {
hasNewChange ||= isReportedChangeNew(filePath, explanation, inputs);
};
const onReportedRemove = (): void => {
hasNewChange = true;
};
const onAggregated = (): void => {
if (hasNewChange) {
watcher.off('change', onReportedChange);
watcher.off('remove', onReportedRemove);
watcher.off('aggregated', onAggregated);
onChange();
}
};
watcher.on('change', onReportedChange);
watcher.on('remove', onReportedRemove);
watcher.on('aggregated', onAggregated);
this.#watcher = watcher;
watcher.watch({
files: this.#files.keys(),
directories: this.#contexts.keys(),
missing: this.#missing.keys(),
startTime: this.#lastQueryTime
});
// The watcher checks its reports against `inputs`, so the next run records into new maps
this.#lastFiles = this.#files;
this.#files = new Map();
this.#contexts = new Map();
this.#missing.clear();
}
/**
* @inheritdoc
*/
public async getStateAndTrackAsync(filePath: string): Promise<IWatchedFileState> {
const normalizedSourcePath: string = path.normalize(filePath);
const oldTime: number | undefined = this.#lastFiles?.get(normalizedSourcePath);
let newTimeEntry: ITimeEntry | undefined = this.#times?.get(normalizedSourcePath);
if (!newTimeEntry) {
// Need to record a timestamp, otherwise first rerun will select everything
try {
const stats: fs.Stats = await fs.promises.lstat(normalizedSourcePath);
const rounded: number = stats.mtime.getTime() || stats.ctime.getTime() || Date.now();
newTimeEntry = {
timestamp: rounded,
safeTime: rounded
};
} catch (err) {
this.#missing.set(normalizedSourcePath, Date.now());
}
}
const newTime: number | undefined =
(newTimeEntry && (newTimeEntry.timestamp ?? newTimeEntry.safeTime)) || this.#lastQueryTime;
if (newTime) {
this.#files.set(normalizedSourcePath, newTime);
}
return {
changed: newTime !== oldTime
};
}
/**
* @inheritdoc
*/
public getStateAndTrack(filePath: string): IWatchedFileState {
const normalizedSourcePath: string = path.normalize(filePath);
const oldTime: number | undefined = this.#lastFiles?.get(normalizedSourcePath);
let newTimeEntry: ITimeEntry | undefined = this.#times?.get(normalizedSourcePath);
if (!newTimeEntry) {
// Need to record a timestamp, otherwise first rerun will select everything
const stats: fs.Stats | undefined = fs.lstatSync(normalizedSourcePath, { throwIfNoEntry: false });
if (stats) {
const rounded: number = stats.mtime.getTime() || stats.ctime.getTime() || Date.now();
newTimeEntry = {
timestamp: rounded,
safeTime: rounded
};
} else {
this.#missing.set(normalizedSourcePath, Date.now());
}
}
const newTime: number | undefined =
(newTimeEntry && (newTimeEntry.timestamp ?? newTimeEntry.safeTime)) || this.#lastQueryTime;
if (newTime) {
this.#files.set(normalizedSourcePath, newTime);
}
return {
changed: newTime !== oldTime
};
}
}