MCPcopy
hub / github.com/claude-code-best/claude-code / readFile

Method readFile

src/utils/fileReadCache.ts:22–68  ·  view source on GitHub ↗

* Reads a file with caching. Returns both content and encoding. * Cache key includes file path and modification time for automatic invalidation.

(filePath: string)

Source from the content-addressed store, hash-verified

20 * Cache key includes file path and modification time for automatic invalidation.
21 */
22 readFile(filePath: string): { content: string; encoding: BufferEncoding } {
23 const fs = getFsImplementation()
24
25 // Get file stats for cache invalidation
26 let stats
27 try {
28 stats = fs.statSync(filePath)
29 } catch (error) {
30 // File was deleted, remove from cache and re-throw
31 this.cache.delete(filePath)
32 throw error
33 }
34
35 const cacheKey = filePath
36 const cachedData = this.cache.get(cacheKey)
37
38 // Check if we have valid cached data
39 if (cachedData && cachedData.mtime === stats.mtimeMs) {
40 return {
41 content: cachedData.content,
42 encoding: cachedData.encoding,
43 }
44 }
45
46 // Cache miss or stale data - read the file
47 const encoding = detectFileEncoding(filePath)
48 const content = fs
49 .readFileSync(filePath, { encoding })
50 .replaceAll('\r\n', '\n')
51
52 // Update cache
53 this.cache.set(cacheKey, {
54 content,
55 encoding,
56 mtime: stats.mtimeMs,
57 })
58
59 // Evict oldest entries if cache is too large
60 if (this.cache.size > this.maxCacheSize) {
61 const firstKey = this.cache.keys().next().value
62 if (firstKey) {
63 this.cache.delete(firstKey)
64 }
65 }
66
67 return { content, encoding }
68 }
69
70 /**
71 * Clears the entire cache. Useful for testing or memory management.

Callers 15

applySedEditFunction · 0.80
callFunction · 0.80
NotebookEditToolDiffFunction · 0.80
SedEditPermissionRequestFunction · 0.80
readCronTasksFunction · 0.80
listAutonomyFlowsFunction · 0.80
listAutonomyRunsFunction · 0.80
readFileSyncCachedFunction · 0.80
readIdeLockfileFunction · 0.80
installFromArtifactoryFunction · 0.80

Calls 7

getFsImplementationFunction · 0.85
detectFileEncodingFunction · 0.85
setMethod · 0.80
nextMethod · 0.80
deleteMethod · 0.65
getMethod · 0.65
keysMethod · 0.65

Tested by

no test coverage detected