All files / filesystem cached-file-system.ts

80.76% Statements 42/52
56.25% Branches 18/32
83.33% Functions 5/6
81.25% Lines 39/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                6x 6x       6x       6x       6x               3x 1x 2x       3x   3x 3x 3x     2x           2x 2x     2x       2x   2x 2x 2x     1x 1x   1x 1x     1x       4x 4x 4x     2x 2x 1x 1x 1x   1x   1x 1x 1x   1x                                           2x      
// src/filesystem/cached-file-system.ts
// Cached implementation
 
import * as vscode from 'vscode';
import { FileSystem } from './file-system';
import { LRUCache } from '../cache/lru-cache';
import { ExtensionConfig } from '../types/config';
 
export class CachedFileSystem implements FileSystem {
  private findFilesCache: LRUCache<string, vscode.Uri[]>;
  private readFileCache: LRUCache<string, string>;
  private fileExistsCache: LRUCache<string, boolean>;
  private statCache: LRUCache<string, vscode.FileStat>;
  private config: ExtensionConfig;
 
  constructor(config: ExtensionConfig) {
    this.config = config;
    this.findFilesCache = new LRUCache(
      config.cache.maxSize,
      config.cache.ttl.fileExists
    );
    this.readFileCache = new LRUCache(
      config.cache.maxSize,
      config.cache.ttl.fileContent
    );
    this.fileExistsCache = new LRUCache(
      config.cache.maxSize,
      config.cache.ttl.fileExists
    );
    this.statCache = new LRUCache(
      config.cache.maxSize,
      config.cache.ttl.fileExists
    );
  }
 
  async findFiles(pattern: string, excludeOrMaxResults?: string | number, maxResults?: number): Promise<vscode.Uri[]> {
    let exclude: string | undefined;
    if (typeof excludeOrMaxResults === 'string') {
      exclude = excludeOrMaxResults;
    } else Iif (typeof excludeOrMaxResults === 'number') {
      maxResults = excludeOrMaxResults;
    }
 
    const cacheKey = `${pattern}:${exclude}:${maxResults}`;
 
    Eif (this.config.cache.enabled) {
      const cached = this.findFilesCache.get(cacheKey);
      if (cached) return cached;
    }
 
    const result = await vscode.workspace.findFiles(
      pattern,
      exclude || this.config.watch.excludePatterns.join(','),
      maxResults
    );
 
    Eif (this.config.cache.enabled) {
      this.findFilesCache.set(cacheKey, result);
    }
 
    return result;
  }
 
  async readFile(uri: vscode.Uri): Promise<string> {
    const cacheKey = uri.toString();
 
    Eif (this.config.cache.enabled) {
      const cached = this.readFileCache.get(cacheKey);
      if (cached) return cached;
    }
 
    const bytes = await vscode.workspace.fs.readFile(uri);
    const content = Buffer.from(bytes).toString('utf8');
 
    Eif (this.config.cache.enabled) {
      this.readFileCache.set(cacheKey, content);
    }
 
    return content;
  }
 
  async fileExists(fsPath: string): Promise<boolean> {
    Eif (this.config.cache.enabled) {
      const cached = this.fileExistsCache.get(fsPath);
      if (cached !== undefined) return cached;
    }
 
    try {
      await vscode.workspace.fs.stat(vscode.Uri.file(fsPath));
      const result = true;
      Eif (this.config.cache.enabled) {
        this.fileExistsCache.set(fsPath, result);
      }
      return result;
    } catch {
      const result = false;
      Eif (this.config.cache.enabled) {
        this.fileExistsCache.set(fsPath, result);
      }
      return result;
    }
  }
 
  async stat(uri: vscode.Uri): Promise<vscode.FileStat> {
    const cacheKey = uri.toString();
 
    if (this.config.cache.enabled) {
      const cached = this.statCache.get(cacheKey);
      if (cached) return cached;
    }
 
    const result = await vscode.workspace.fs.stat(uri);
 
    if (this.config.cache.enabled) {
      this.statCache.set(cacheKey, result);
    }
 
    return result;
  }
 
  updateConfig(config: ExtensionConfig): void {
    this.config = config;
  }
}