import { readFile } from 'node:fs/promises'; import xxhash from 'xxhash-wasm'; /** * Initialize xxHash instance (singleton) */ let hasherInstance: Awaited> | null = null; async function getHasher() { if (!hasherInstance) { hasherInstance = await xxhash(); } return hasherInstance; } /** * Compute xxHash64 checksum for a file * * @param filePath - Path to file * @returns Hex string of xxHash64 checksum */ export async function computeFileChecksum(filePath: string): Promise { const hasher = await getHasher(); const content = await readFile(filePath); // Use h64Raw for binary data (returns bigint), then convert to hex string const hash = hasher.h64Raw(content); return hash.toString(16); } /** * Compute xxHash64 checksum for a buffer * * @param data - Buffer to hash * @returns Hex string of xxHash64 checksum */ export async function computeBufferChecksum(data: Buffer): Promise { const hasher = await getHasher(); // Use h64Raw for binary data (returns bigint), then convert to hex string const hash = hasher.h64Raw(data); return hash.toString(16); }