-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace file-entry-cache with custom impl + built-in serializer
- Loading branch information
Showing
8 changed files
with
179 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import fs from 'node:fs'; | ||
import { timerify } from './Performance.js'; | ||
import { debugLog } from './debug.js'; | ||
import { isDirectory, isFile } from './fs.js'; | ||
import { dirname, isAbsolute, resolve } from './path.js'; | ||
import { deserialize, serialize } from './serialize.js'; | ||
|
||
type MetaData<T> = { size: number; mtime: number; data?: T }; | ||
|
||
export type FileDescriptor<T> = { | ||
key: string; | ||
changed?: boolean; | ||
notFound?: boolean; | ||
err?: unknown; | ||
meta?: MetaData<T>; | ||
}; | ||
|
||
const cwd = process.cwd(); | ||
|
||
const createCache = (filePath: string) => { | ||
try { | ||
return deserialize(fs.readFileSync(filePath)); | ||
} catch (_err) { | ||
debugLog('*', `Error reading cache from ${filePath}`); | ||
} | ||
}; | ||
|
||
const create = timerify(createCache); | ||
|
||
export class FileEntryCache<T> { | ||
filePath: string; | ||
cache = new Map<string, MetaData<T>>(); | ||
normalizedEntries = new Map<string, FileDescriptor<T>>(); | ||
|
||
constructor(cacheId: string, _path: string) { | ||
this.filePath = isAbsolute(_path) ? resolve(_path, cacheId) : resolve(cwd, _path, cacheId); | ||
if (isFile(this.filePath)) this.cache = create(this.filePath); | ||
this.removeNotFoundFiles(); | ||
} | ||
|
||
removeNotFoundFiles() { | ||
for (const filePath of this.normalizedEntries.keys()) { | ||
try { | ||
fs.statSync(filePath); | ||
} catch (error) { | ||
// @ts-expect-error | ||
if (error.code === 'ENOENT') this.cache.delete(filePath); | ||
} | ||
} | ||
} | ||
|
||
getFileDescriptor(filePath: string): FileDescriptor<T> { | ||
let fstat: fs.Stats; | ||
|
||
try { | ||
if (!isAbsolute(filePath)) filePath = resolve(filePath); | ||
fstat = fs.statSync(filePath); | ||
} catch (error: unknown) { | ||
this.removeEntry(filePath); | ||
return { key: filePath, notFound: true, err: error }; | ||
} | ||
|
||
return this._getFileDescriptorUsingMtimeAndSize(filePath, fstat); | ||
} | ||
|
||
_getFileDescriptorUsingMtimeAndSize(filePath: string, fstat: fs.Stats) { | ||
let meta = this.cache.get(filePath); | ||
const cacheExists = Boolean(meta); | ||
|
||
const cSize = fstat.size; | ||
const cTime = fstat.mtime.getTime(); | ||
|
||
let isDifferentDate: undefined | boolean; | ||
let isDifferentSize: undefined | boolean; | ||
|
||
if (meta) { | ||
isDifferentDate = cTime !== meta.mtime; | ||
isDifferentSize = cSize !== meta.size; | ||
} else { | ||
meta = { size: cSize, mtime: cTime }; | ||
} | ||
|
||
const fd: FileDescriptor<T> = { | ||
key: filePath, | ||
changed: !cacheExists || isDifferentDate || isDifferentSize, | ||
meta, | ||
}; | ||
|
||
this.normalizedEntries.set(filePath, fd); | ||
|
||
return fd; | ||
} | ||
|
||
removeEntry(entryName: string) { | ||
if (!isAbsolute(entryName)) entryName = resolve(cwd, entryName); | ||
this.normalizedEntries.delete(entryName); | ||
this.cache.delete(entryName); | ||
} | ||
|
||
_getMetaForFileUsingMtimeAndSize(cacheEntry: FileDescriptor<T>) { | ||
const stat = fs.statSync(cacheEntry.key); | ||
const meta = Object.assign(cacheEntry.meta ?? {}, { | ||
size: stat.size, | ||
mtime: stat.mtime.getTime(), | ||
}); | ||
return meta; | ||
} | ||
|
||
reconcile() { | ||
this.removeNotFoundFiles(); | ||
|
||
for (const [entryName, cacheEntry] of this.normalizedEntries.entries()) { | ||
try { | ||
const meta = this._getMetaForFileUsingMtimeAndSize(cacheEntry); | ||
this.cache.set(entryName, meta); | ||
} catch (error) { | ||
// @ts-expect-error | ||
if (error.code !== 'ENOENT') throw error; | ||
} | ||
} | ||
|
||
try { | ||
const dir = dirname(this.filePath); | ||
if (!isDirectory(dir)) fs.mkdirSync(dir, { recursive: true }); | ||
fs.writeFileSync(this.filePath, serialize(this.cache)); | ||
} catch (_err) { | ||
debugLog('*', `Error writing cache to ${this.filePath}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,14 @@ | ||
import type { FileNode } from '../types/dependency-graph.js'; | ||
import { timerify } from './Performance.js'; | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: deal with it | ||
const serializeObj = (obj: any): any => { | ||
if (!obj) return obj; | ||
if (obj instanceof Set) return Array.from(obj); | ||
if (obj instanceof Map) { | ||
const o: { [key: string]: unknown } = { _m: 1 }; | ||
for (const [key, value] of obj) o[key] = serializeObj(value); | ||
return o; | ||
} | ||
if (typeof obj === 'object') for (const key in obj) obj[key] = serializeObj(obj[key]); | ||
return obj; | ||
}; | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: deal with it | ||
const deserializeObj = (obj: any): any => { | ||
if (!obj) return obj; | ||
if (Array.isArray(obj)) return new Set(obj); | ||
if (obj._m) { | ||
const map = new Map(); | ||
for (const key in obj) key !== '_m' && map.set(key, deserializeObj(obj[key])); | ||
return map; | ||
} | ||
if (typeof obj === 'object') for (const key in obj) obj[key] = deserializeObj(obj[key]); | ||
return obj; | ||
}; | ||
|
||
const serialize = (data: FileNode): FileNode => { | ||
const clone = structuredClone(data); | ||
clone.imported = undefined; | ||
clone.internalImportCache = undefined; | ||
return serializeObj(clone); | ||
}; | ||
|
||
const deserialize = (data: FileNode): FileNode => deserializeObj(data); | ||
|
||
export const _serialize = timerify(serialize); | ||
|
||
export const _deserialize = timerify(deserialize); | ||
// biome-ignore lint: well | ||
let s: (data: any) => Buffer, d: (buffer: Buffer) => any; | ||
|
||
if (typeof Bun !== 'undefined') { | ||
const { serialize, deserialize } = await import('bun:jsc'); | ||
s = serialize; | ||
d = deserialize; | ||
} else { | ||
const { serialize, deserialize } = await import('node:v8'); | ||
s = serialize; | ||
d = deserialize; | ||
} | ||
|
||
export { s as serialize, d as deserialize }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters