mirror of
https://github.com/smogon/sprites.git
synced 2026-12-04 22:18:12 -06:00
hash and cas move to node:fs/promises: source reconciliation runs a small worker pool over the stat cache, the executor's clean check gathers CAS stats concurrently, and inserts/sweeps await their way through. The sqlite store stays sync; that is all node:sqlite offers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
|
|
import {createHash} from 'node:crypto';
|
|
import * as fs from 'node:fs/promises';
|
|
|
|
export type FileStat = {
|
|
size: bigint,
|
|
mtimeNs: bigint,
|
|
hash: Buffer,
|
|
};
|
|
|
|
export async function hashFile(path: string): Promise<Buffer> {
|
|
return createHash('sha256').update(await fs.readFile(path)).digest();
|
|
}
|
|
|
|
export type ReconcileResult = {
|
|
hashes: Map<string, Buffer>, // current content hash for every extant path
|
|
updated: Map<string, FileStat>, // cache entries that changed (to persist)
|
|
missing: string[], // paths that don't exist or aren't files
|
|
};
|
|
|
|
// Content hashes with a stat cache: only files whose (size, mtime_ns) changed
|
|
// since the recorded cache entry are rehashed. mtime_ns exceeds 2^53, hence
|
|
// bigint stats throughout. A small worker pool keeps the fs thread pool fed
|
|
// without holding thousands of files open at once.
|
|
export async function reconcileHashes(paths: Iterable<string>,
|
|
cache: Map<string, FileStat>): Promise<ReconcileResult> {
|
|
let queue = [...paths];
|
|
let hashes = new Map<string, Buffer>();
|
|
let updated = new Map<string, FileStat>();
|
|
let missing: string[] = [];
|
|
let work = async () => {
|
|
for (;;) {
|
|
let path = queue.pop();
|
|
if (path === undefined) {
|
|
return;
|
|
}
|
|
let st;
|
|
try {
|
|
st = await fs.stat(path, {bigint: true});
|
|
} catch {
|
|
missing.push(path);
|
|
continue;
|
|
}
|
|
if (!st.isFile()) {
|
|
missing.push(path);
|
|
continue;
|
|
}
|
|
let cached = cache.get(path);
|
|
let hash;
|
|
if (cached !== undefined && cached.size === st.size && cached.mtimeNs === st.mtimeNs) {
|
|
hash = cached.hash;
|
|
} else {
|
|
hash = await hashFile(path);
|
|
updated.set(path, {size: st.size, mtimeNs: st.mtimeNs, hash});
|
|
}
|
|
hashes.set(path, hash);
|
|
}
|
|
};
|
|
await Promise.all(Array.from({length: 16}, work));
|
|
return {hashes, updated, missing};
|
|
}
|