fix(open-sse): serialize xxhash startup in cch hashing

Cache the in-flight xxhash-wasm initialization so concurrent calls reuse
the same promise before the raw hash function is available.

This avoids redundant loader work and prevents races during early CCH
computations.
This commit is contained in:
diegosouzapw
2026-04-13 14:43:32 -03:00
parent 7efa672976
commit cd56ea7040

View File

@@ -17,12 +17,18 @@ import xxhashInit from "xxhash-wasm";
const CCH_SEED = 0x6e52736ac806831en;
const CCH_PATTERN = /\bcch=([0-9a-f]{5});/;
let xxhashPromise: Promise<void> | null = null;
let xxhash64Fn: ((input: Uint8Array, seed: bigint) => bigint) | null = null;
async function ensureXxhash() {
if (xxhash64Fn) return;
const hasher = await xxhashInit();
xxhash64Fn = hasher.h64Raw;
if (!xxhashPromise) {
xxhashPromise = (async () => {
const hasher = await xxhashInit();
xxhash64Fn = hasher.h64Raw;
})();
}
return xxhashPromise;
}
export async function computeCCH(bodyBytes: Uint8Array): Promise<string> {