-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathCacheManager.ts
More file actions
834 lines (677 loc) · 25 KB
/
Copy pathCacheManager.ts
File metadata and controls
834 lines (677 loc) · 25 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import { BrowserWindow, dialog, type Protocol } from 'electron';
import { EventEmitter } from 'events';
import crypto from 'node:crypto';
import { createReadStream } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { Readable } from 'node:stream';
import debug from 'debug';
import type CacheInfo from '../@types/CacheInfo';
import type CacheInfoBase from '../@types/CacheInfoBase';
import type Headers from '../@types/Headers';
import CacheState from '../constants/CacheState';
import limit from '../util/limit';
import CacheAPI from './constants/CacheAPI';
import downloadFile, { MAX_FILE_SIZE_EXCEEDED_ERROR, isDownloadTimeoutError } from './utils/downloadFile';
import ensureDirectoryExists from './utils/ensureDirectoryExists';
import getChecksum from './utils/getChecksum';
import ipcMainHandle from './utils/ipcMainHandle';
import { IpfsGatewayDisabledError } from './utils/ipfsGateway';
import isValidURL from './utils/isValidURL';
import sanitizeFilename from './utils/sanitizeFilename';
import sanitizeNumber from './utils/sanitizeNumber';
const log = debug('chia-gui:CacheManager');
export const CACHE_PROTOCOL = 'cache';
// A single-range `bytes=start-end` Range header, parsed against the file size.
// 'ignore' means the header is absent or uses a form we do not support
// (e.g. multiple ranges), in which case the full file is served with a 200.
type ParsedRange = { start: number; end: number } | 'invalid' | 'ignore';
function parseRangeHeader(rangeHeader: string | null, fileSize: number): ParsedRange {
if (!rangeHeader) {
return 'ignore';
}
const match = /^bytes=(\d*)-(\d*)$/.exec(rangeHeader.trim());
if (!match) {
return 'ignore';
}
const [, startString, endString] = match;
if (startString === '' && endString === '') {
return 'invalid';
}
if (startString === '') {
// suffix range: the last N bytes of the file
const suffixLength = Number.parseInt(endString, 10);
if (suffixLength === 0 || fileSize === 0) {
return 'invalid';
}
return { start: Math.max(fileSize - suffixLength, 0), end: fileSize - 1 };
}
const start = Number.parseInt(startString, 10);
if (start >= fileSize) {
return 'invalid';
}
const end = endString === '' ? fileSize - 1 : Math.min(Number.parseInt(endString, 10), fileSize - 1);
if (start > end) {
return 'invalid';
}
return { start, end };
}
async function safeUnlink(filePath: string) {
try {
await fs.unlink(filePath);
} catch (error) {
// Ignore
}
}
const INFO_SUFFIX = '-info';
const FILE_SUFFIX = '-chiacache';
const MAX_TOTAL_SIZE = 1024 * 1024 * 1024; // 1GB
const MAX_FILE_SIZE = 1024 * 1024 * 100; // 100MB
const SUFFIXES = [FILE_SUFFIX, `${FILE_SUFFIX}${INFO_SUFFIX}`];
function isChiaCacheFile(filePath: string) {
return SUFFIXES.some((suffix) => filePath.endsWith(suffix));
}
function isChiaCacheInfoFile(filePath: string) {
return isChiaCacheFile(filePath) && filePath.endsWith(INFO_SUFFIX);
}
function getInfoFilePath(filePath: string) {
return `${filePath}${INFO_SUFFIX}`;
}
export default class CacheManager extends EventEmitter {
#cacheDirectory: string = './cache';
#maxCacheSize: number = 0;
#downloadLimit;
private ongoingRequests: Map<
string,
{
promise: Promise<CacheInfo>;
abort: () => void;
}
> = new Map();
// URLs whose download timed out during this session. A persisted timeout is
// retried once per session — the set keeps a stalled host from being retried
// (and holding a download slot) on every access within the same session.
private timedOutUrls: Set<string> = new Set();
constructor(
options: {
cacheDirectory?: string;
maxCacheSize?: number | string;
concurrency?: number;
} = {},
) {
super();
const { cacheDirectory = './cache', maxCacheSize = MAX_TOTAL_SIZE, concurrency = 10 } = options;
this.cacheDirectory = cacheDirectory;
this.maxCacheSize = maxCacheSize;
// LIFO: downloads for what the user is currently viewing (an offer
// preview, a just-opened detail page) are requested last and must not
// wait behind a long gallery-wide rebuild of earlier requests.
this.#downloadLimit = limit(concurrency, { lifo: true });
this.setMaxListeners(50);
this.prepareElectron();
}
private prepareElectron() {
this.prepareIPC();
}
prepareProtocol(protocol: Protocol) {
protocol.handle(CACHE_PROTOCOL, async (request: Request) => {
const requestUrl = request.url;
const url = new URL(requestUrl);
const fileName = sanitizeFilename(url.hostname);
const filePath = path.join(this.cacheDirectory, fileName);
const infoFilePath = getInfoFilePath(filePath);
const cacheInfo = await this.getCacheInfo(infoFilePath, requestUrl);
if (cacheInfo.state !== CacheState.CACHED) {
return new Response('Not found', {
status: 404,
headers: {
'content-type': 'text/plain',
},
});
}
let fileSize: number;
try {
const stats = await fs.stat(filePath);
fileSize = stats.size;
} catch (error) {
return new Response('Not found', {
status: 404,
headers: {
'content-type': 'text/plain',
},
});
}
const contentTypeHeader = cacheInfo.headers?.['content-type'];
const contentType =
(Array.isArray(contentTypeHeader) ? contentTypeHeader[0] : contentTypeHeader) || 'application/octet-stream';
const responseHeaders: Record<string, string> = {
'content-type': contentType,
'accept-ranges': 'bytes',
};
// Media elements seek by sending Range requests. Without 206 responses
// seeking is broken and MP4 files with the moov atom at the end of the
// file never start playing.
const range = parseRangeHeader(request.headers.get('range'), fileSize);
if (range === 'invalid') {
return new Response('Range Not Satisfiable', {
status: 416,
headers: {
'content-type': 'text/plain',
'content-range': `bytes */${fileSize}`,
},
});
}
if (range !== 'ignore') {
responseHeaders['content-length'] = String(range.end - range.start + 1);
responseHeaders['content-range'] = `bytes ${range.start}-${range.end}/${fileSize}`;
const partialStream = createReadStream(filePath, { start: range.start, end: range.end });
return new Response(Readable.toWeb(partialStream) as unknown as ReadableStream, {
status: 206,
headers: responseHeaders,
});
}
responseHeaders['content-length'] = String(fileSize);
return new Response(Readable.toWeb(createReadStream(filePath)) as unknown as ReadableStream, {
headers: responseHeaders,
});
});
}
private prepareIPC() {
ipcMainHandle(CacheAPI.GET_CACHE_SIZE, () => this.getCacheSize());
ipcMainHandle(CacheAPI.CLEAR_CACHE, () => this.clearCache());
ipcMainHandle(CacheAPI.SET_CACHE_DIRECTORY, () => this.setCacheDirectory());
ipcMainHandle(CacheAPI.SET_MAX_CACHE_SIZE, (newSize: number) => this.setMaxCacheSize(newSize));
ipcMainHandle(CacheAPI.GET_CONTENT, (url: string, options?: { maxSize?: number; timeout?: number }) =>
this.getContent(url, options),
);
ipcMainHandle(CacheAPI.GET_HEADERS, (url: string, options?: { maxSize?: number; timeout?: number }) =>
this.getHeaders(url, options),
);
ipcMainHandle(CacheAPI.GET_CHECKSUM, (url: string, options?: { maxSize?: number; timeout?: number }) =>
this.getChecksum(url, options),
);
ipcMainHandle(CacheAPI.GET_URI, (url: string, options?: { maxSize?: number; timeout?: number }) =>
this.getURI(url, options),
);
ipcMainHandle(CacheAPI.INVALIDATE, (url: string) => this.invalidate(url));
ipcMainHandle(CacheAPI.GET_CACHE_DIRECTORY, () => this.cacheDirectory);
ipcMainHandle(CacheAPI.GET_MAX_CACHE_SIZE, () => this.maxCacheSize);
}
public bindEvents(window: BrowserWindow) {
function onCacheDirectoryChanged(newDirectory: string) {
window.webContents.send(CacheAPI.ON_CACHE_DIRECTORY_CHANGED, newDirectory);
}
function onMaxCacheSizeChanged(newSize: number) {
window.webContents.send(CacheAPI.ON_MAX_CACHE_SIZE_CHANGED, newSize);
}
// Download and invalidation bursts emit sizeChanged per file, and every
// notification triggers a full cache-directory scan (here and again in the
// renderer), so coalesce bursts into one trailing notification. Scans are
// also serialized: events arriving while a scan is running only mark it
// stale, and one follow-up scan is scheduled after it finishes, so a scan
// that outlives the coalescing window cannot overlap the next one.
let sizeChangedTimeout: NodeJS.Timeout | undefined;
let sizeScanRunning = false;
let sizeChangedDuringScan = false;
const onSizeChanged = () => {
if (sizeChangedTimeout) {
return;
}
if (sizeScanRunning) {
sizeChangedDuringScan = true;
return;
}
sizeChangedTimeout = setTimeout(async () => {
sizeChangedTimeout = undefined;
sizeScanRunning = true;
try {
const size = await this.getCacheSize();
if (!window.isDestroyed()) {
window.webContents.send(CacheAPI.ON_SIZE_CHANGED, size);
}
} catch {
// the next sizeChanged event delivers a fresh value
} finally {
sizeScanRunning = false;
if (sizeChangedDuringScan) {
sizeChangedDuringScan = false;
onSizeChanged();
}
}
}, 500);
};
this.on('cacheDirectoryChanged', onCacheDirectoryChanged);
this.on('maxCacheSizeChanged', onMaxCacheSizeChanged);
this.on('sizeChanged', onSizeChanged);
const unbind = () => {
this.off('cacheDirectoryChanged', onCacheDirectoryChanged);
this.off('maxCacheSizeChanged', onMaxCacheSizeChanged);
this.off('sizeChanged', onSizeChanged);
sizeChangedDuringScan = false;
if (sizeChangedTimeout) {
clearTimeout(sizeChangedTimeout);
sizeChangedTimeout = undefined;
}
};
window.on('close', () => {
unbind();
});
return unbind;
}
async init() {
await ensureDirectoryExists(this.cacheDirectory);
}
public get maxCacheSize(): number {
return this.#maxCacheSize;
}
public set maxCacheSize(newSize: number | string) {
const value = sanitizeNumber(newSize);
this.#maxCacheSize = value;
this.emit('maxCacheSizeChanged', this.#maxCacheSize);
}
public get cacheDirectory(): string {
return this.#cacheDirectory;
}
public set cacheDirectory(cacheDirectory: string) {
this.#cacheDirectory = cacheDirectory;
this.emit('cacheDirectoryChanged', this.#cacheDirectory);
}
private getCacheFilePath(url: string) {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const urlHash = crypto.createHash('md5').update(url).digest('hex');
const fileName = `${urlHash}${FILE_SUFFIX}`;
return path.join(this.cacheDirectory, fileName);
}
private getCacheInfoFilePath(url: string) {
const filePath = this.getCacheFilePath(url);
return getInfoFilePath(filePath);
}
// url is here cache://filename
private async getCacheInfo(filePath: string, url: string): Promise<CacheInfo> {
try {
const infoString = await fs.readFile(filePath, 'utf-8');
return JSON.parse(infoString) as CacheInfo;
} catch (error) {
const currentError = (error as Error) ?? new Error('Unknown error');
if ((currentError as { code?: string }).code === 'ENOENT') {
return {
url,
state: CacheState.NOT_CACHED,
timestamp: Date.now(),
};
}
return {
url,
state: CacheState.ERROR,
error: currentError.message,
timestamp: Date.now(),
};
}
}
private async getCacheInfoByURL(url: string): Promise<CacheInfo> {
const filePath = this.getCacheInfoFilePath(url);
return this.getCacheInfo(filePath, url);
}
private async setCacheInfo(url: string, infoBase: CacheInfoBase) {
const infoFilePath = this.getCacheInfoFilePath(url);
const cacheInfo: CacheInfo = {
...infoBase,
url,
timestamp: Date.now(),
};
await fs.writeFile(infoFilePath, JSON.stringify(cacheInfo), 'utf-8');
return cacheInfo;
}
abort(url: string) {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const ongoingRequest = this.ongoingRequests.get(url);
if (ongoingRequest) {
ongoingRequest.abort();
}
}
async fetchRemoteContent(
url: string,
options: {
maxSize?: number;
timeout?: number;
} = {},
): Promise<CacheInfo> {
const { maxSize = MAX_FILE_SIZE, timeout = 30_000 } = options;
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const ongoingRequest = this.ongoingRequests.get(url);
if (ongoingRequest) {
log('Request already ongoing', url);
return ongoingRequest.promise;
}
const abortController = new AbortController();
const process = async (): Promise<CacheInfo> => {
try {
// From isValidURL.ts
// isURL returns false for URLs with unencoded spaces. We can't use
// encodeURI if the URL is already encoded, so we attempt to decode
// the URL first and then encode it if it wasn't already encoded.
const normalizedURL = decodeURI(url) === url ? encodeURI(url) : url;
if (!isValidURL(normalizedURL)) {
throw new Error(`Invalid URL: ${normalizedURL}`);
}
const cacheInfo = await this.getCacheInfoByURL(url);
if (cacheInfo.state === CacheState.CACHED) {
log('Url already downloaded', url);
return cacheInfo;
}
if (cacheInfo.state === CacheState.ERROR) {
log(`Url already downloaded with error: ${cacheInfo.error}`, url);
const isTransientError = ['Response aborted', 'Request aborted'].includes(cacheInfo.error);
// A persisted timeout settles for the rest of the session, but is
// retried in later sessions — a one-off network problem must not
// disable the preview until the whole cache is cleared.
const isRetriableTimeout = isDownloadTimeoutError(cacheInfo.error) && !this.timedOutUrls.has(url);
// A persisted size-limit error is only retried when the caller lifts
// the limit, so oversized files are not re-downloaded on every visit.
const isSizeLimitLifted = cacheInfo.error === MAX_FILE_SIZE_EXCEEDED_ERROR && maxSize <= 0;
if (!isTransientError && !isRetriableTimeout && !isSizeLimitLifted) {
return cacheInfo;
}
log('Retrying download', url);
}
const limitedRemoteFileDownload = async (): Promise<CacheInfo> => {
const cacheFilePath = this.getCacheFilePath(url);
log('Starting download', url);
const headers = await downloadFile(url, cacheFilePath, {
timeout,
maxSize,
signal: abortController.signal,
overrideFile: true,
});
log('Download finished', url);
// compute checksum
const checksum = await getChecksum(cacheFilePath);
log('Checksum computed', url);
// save headers to a local JSON file
const updatedCacheInfo = await this.setCacheInfo(url, {
state: CacheState.CACHED,
headers,
checksum,
});
log('Cache info saved', url);
try {
// remove old files if the cache is full
const currentCacheSize = await this.getCacheSize();
if (this.maxCacheSize > 0 && currentCacheSize > this.maxCacheSize) {
// The current size already includes the file that was just
// downloaded. Keep that file available to the caller and evict
// older entries down to the configured total-size target.
await this.removeOldestFiles(this.maxCacheSize, cacheFilePath);
}
} catch (housekeepingError) {
// The download and its cache info are already saved — a failure in
// cache bookkeeping must not overwrite that state with an error.
log(`Cache housekeeping failed: ${(housekeepingError as Error).message}`, url);
}
// todo just add size and save it locally
this.emit('sizeChanged');
return updatedCacheInfo;
};
return await this.#downloadLimit<CacheInfo>(() => limitedRemoteFileDownload());
} catch (error) {
// Not a property of the URL, just of the current preference: while
// the IPFS gateway option is off the fetch is refused before it
// starts. Persisting that as a cache error would keep the entry
// poisoned after the user turns the option on, so it propagates
// instead — already-cached content was served above regardless.
if (error instanceof IpfsGatewayDisabledError) {
throw error;
}
const currentError = (error as Error) ?? new Error('Unknown fetchRemoteContent error');
if (isDownloadTimeoutError(currentError.message)) {
this.timedOutUrls.add(url);
}
return await this.setCacheInfo(url, {
state: CacheState.ERROR,
error: currentError.message,
});
} finally {
this.ongoingRequests.delete(url);
}
};
const promise = process();
this.ongoingRequests.set(url, {
abort: () => abortController.abort(),
promise,
});
return promise;
}
async getHeaders(
url: string,
options?: {
maxSize?: number;
timeout?: number;
},
): Promise<Headers> {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const cacheInfo = await this.fetchRemoteContent(url, options);
if (cacheInfo.state === CacheState.ERROR) {
throw new Error(cacheInfo.error);
}
if (cacheInfo.state === CacheState.NOT_CACHED) {
throw new Error('Url is not cached');
}
if (cacheInfo.state === CacheState.CACHED) {
return cacheInfo.headers;
}
throw new Error('Unknown cache state');
}
async getContent(
url: string,
options?: {
maxSize?: number;
timeout?: number;
},
): Promise<Buffer> {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const cacheInfo = await this.fetchRemoteContent(url, options);
if (cacheInfo.state === CacheState.ERROR) {
throw new Error(cacheInfo.error);
}
if (cacheInfo.state === CacheState.NOT_CACHED) {
throw new Error('Url is not cached');
}
if (cacheInfo.state === CacheState.CACHED) {
const filePath = this.getCacheFilePath(url);
return fs.readFile(filePath);
}
throw new Error('Unknown cache state');
}
async getChecksum(
url: string,
options?: {
maxSize?: number;
timeout?: number;
},
): Promise<string> {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const cacheInfo = await this.fetchRemoteContent(url, options);
if (cacheInfo.state === CacheState.ERROR) {
throw new Error(cacheInfo.error);
}
if (cacheInfo.state === CacheState.NOT_CACHED) {
throw new Error('Url is not cached');
}
if (cacheInfo.state === CacheState.CACHED) {
return cacheInfo.checksum;
}
throw new Error('Unknown cache state');
}
async getURI(
url: string,
options?: {
maxSize?: number;
timeout?: number;
},
) {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
const cacheInfo = await this.fetchRemoteContent(url, options);
if (cacheInfo.state === CacheState.ERROR) {
throw new Error(cacheInfo.error);
}
if (cacheInfo.state === CacheState.NOT_CACHED) {
throw new Error('Url is not cached');
}
if (cacheInfo.state === CacheState.CACHED) {
const filePath = this.getCacheFilePath(url);
return `${CACHE_PROTOCOL}://${path.basename(filePath)}`;
}
throw new Error('Unknown cache state');
}
async clearCache() {
// cancel all ongoing requests
for (const ongoingRequest of this.ongoingRequests.values()) {
ongoingRequest.abort();
}
this.ongoingRequests.clear();
const files = await fs.readdir(this.cacheDirectory);
const unlinkPromises = files.map(async (file) => {
const hasSuffix = SUFFIXES.some((suffix) => file.endsWith(suffix));
if (hasSuffix) {
const filePath = path.join(this.cacheDirectory, file);
await safeUnlink(filePath);
}
});
await Promise.all(unlinkPromises);
this.emit('sizeChanged');
}
async setCacheDirectory() {
const { cacheDirectory } = this;
const result = await dialog.showOpenDialog({
properties: ['openDirectory'],
defaultPath: cacheDirectory,
});
if (result.canceled || !result.filePaths[0]) {
return;
}
const newDirectory = result.filePaths[0];
await ensureDirectoryExists(newDirectory);
// move the files from the current cache directory to the new directory
const files = await fs.readdir(this.cacheDirectory);
const movePromises = files.map(async (file) => {
if (!isChiaCacheFile(file)) {
return;
}
const oldFilePath = path.join(this.cacheDirectory, file);
const newFilePath = path.join(newDirectory, file);
const stat = await fs.lstat(oldFilePath);
if (stat.isFile()) {
await fs.rename(oldFilePath, newFilePath);
}
});
await Promise.all(movePromises);
this.cacheDirectory = newDirectory;
}
private async removeOldestFiles(targetSize: number, preserveFilePath?: string): Promise<void> {
const files = await fs.readdir(this.cacheDirectory);
const filePaths = files
.filter((file) => isChiaCacheFile(file) && !isChiaCacheInfoFile(file))
.map((file) => path.join(this.cacheDirectory, file));
// Include the sidecar metadata in each entry's size so the eviction total
// uses the same accounting as getCacheSize().
const fileStats = (
await Promise.all(
filePaths.map(async (filePath) => {
try {
const stats = await fs.stat(filePath);
let infoSize = 0;
try {
infoSize = (await fs.stat(getInfoFilePath(filePath))).size;
} catch {
// A missing sidecar is cleaned up with the data file as usual.
}
return {
filePath,
size: stats.size + infoSize,
mtime: stats.mtime,
};
} catch {
// Deleted by invalidation while scanning — nothing left to evict.
return undefined;
}
}),
)
).filter((entry): entry is { filePath: string; size: number; mtime: Date } => entry !== undefined);
// sort the file paths based on their last modified time (oldest first)
fileStats.sort((a, b) => a.mtime.getTime() - b.mtime.getTime());
// remove files until the total size is below the new max total size
let totalSize = fileStats.reduce((sum, { size }) => sum + size, 0);
const filesToRemove: typeof fileStats = [];
for (const fileStat of fileStats) {
if (totalSize <= targetSize) {
break;
}
if (fileStat.filePath !== preserveFilePath) {
totalSize -= fileStat.size;
filesToRemove.push(fileStat);
}
}
await Promise.all(
filesToRemove.map(async ({ filePath }) => {
await safeUnlink(filePath);
await safeUnlink(getInfoFilePath(filePath));
}),
);
this.emit('sizeChanged');
}
async invalidate(url: string) {
if (!isValidURL(url)) {
throw new Error(`Invalid URL: ${url}`);
}
// cancel the ongoing request
const ongoingRequest = this.ongoingRequests.get(url);
if (ongoingRequest) {
ongoingRequest.abort();
}
// prepare invalidation
const filePath = this.getCacheFilePath(url);
// remove the file
await safeUnlink(filePath);
await safeUnlink(getInfoFilePath(filePath));
this.emit('sizeChanged');
}
async setMaxCacheSize(maxCacheSize: number | string) {
this.maxCacheSize = maxCacheSize;
if (this.maxCacheSize > 0) {
await this.removeOldestFiles(this.maxCacheSize);
}
}
async getCacheSize() {
const files = await fs.readdir(this.cacheDirectory);
const filePaths = files
.filter((filename) => isChiaCacheFile(filename))
.map((filename) => path.join(this.cacheDirectory, filename));
// Invalidation and eviction delete files while this scan runs — a file
// that vanished between readdir and stat no longer occupies space.
const fileSizes = await Promise.all(
filePaths.map(async (filePath) => {
try {
return (await fs.stat(filePath)).size;
} catch {
return 0;
}
}),
);
const totalSize = fileSizes.reduce((sum, size) => sum + size, 0);
return totalSize;
}
}