fix: replace static whatsapp-rust-bridge imports with lazy dynamic loading

The whatsapp-rust-bridge package contains a top-level await on an inline
WASM binary, which propagates through the ESM graph and causes
ERR_REQUIRE_ASYNC_MODULE when CJS consumers try to require() this package.

Replace all static imports from whatsapp-rust-bridge with a lazy-loading
bridge module (wasm-bridge.ts) that uses import().then() instead of
top-level await, keeping the dependency out of the static ESM graph.

https://claude.ai/code/session_01XaA7GwNaB6azTHFYQ8WEpB
This commit is contained in:
Claude
2026-02-09 02:31:52 +00:00
parent ffd3c7735f
commit 38575fb2d8
5 changed files with 46 additions and 5 deletions
+40
View File
@@ -0,0 +1,40 @@
type WasmBridgeModule = typeof import('whatsapp-rust-bridge')
let _bridge: WasmBridgeModule | undefined
// Start loading eagerly using .then() instead of top-level await.
// This prevents the whatsapp-rust-bridge top-level await from propagating
// through the ESM graph, which would break CJS require() consumers.
const _bridgeReady = import('whatsapp-rust-bridge').then(m => {
_bridge = m
return m
})
export { _bridgeReady as wasmBridgeReady }
function getBridge(): WasmBridgeModule {
if (!_bridge) {
throw new Error(
'whatsapp-rust-bridge not yet loaded. ' +
'Ensure async operations have started before calling crypto functions.'
)
}
return _bridge
}
export const hkdf: WasmBridgeModule['hkdf'] = (...args) => getBridge().hkdf(...args)
export const md5: WasmBridgeModule['md5'] = (...args) => getBridge().md5(...args)
export const expandAppStateKeys: WasmBridgeModule['expandAppStateKeys'] = (...args) => getBridge().expandAppStateKeys(...args)
let _ltHash: InstanceType<WasmBridgeModule['LTHashAntiTampering']> | undefined
export function getLTHashAntiTampering(): InstanceType<WasmBridgeModule['LTHashAntiTampering']> {
if (!_ltHash) {
_ltHash = new (getBridge().LTHashAntiTampering)()
}
return _ltHash
}