From cd56ea7040be8940568677b92d04b33c40ea71ea Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Mon, 13 Apr 2026 14:43:32 -0300 Subject: [PATCH] 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. --- open-sse/services/claudeCodeCCH.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/open-sse/services/claudeCodeCCH.ts b/open-sse/services/claudeCodeCCH.ts index 9c4b4df7c8..567de2cc15 100644 --- a/open-sse/services/claudeCodeCCH.ts +++ b/open-sse/services/claudeCodeCCH.ts @@ -17,12 +17,18 @@ import xxhashInit from "xxhash-wasm"; const CCH_SEED = 0x6e52736ac806831en; const CCH_PATTERN = /\bcch=([0-9a-f]{5});/; +let xxhashPromise: Promise | 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 {