let MMKV: any = null try { MMKV = require('react-native-mmkv').MMKV } catch {} const processedCache = new Map() const rawBytesCache = new Map() function djb2(str: string): string { let hash = 5381 for (let i = 0; i < str.length; i++) { hash = ((hash << 5) + hash) + str.charCodeAt(i) } return (hash >>> 0).toString(36) } function getInstance(): any { if (!MMKV) return null try { const instance = new MMKV() return instance } catch { return null } } function base64ToBytes(b64: string): Uint8Array { const bin = atob(b64) const bytes = new Uint8Array(bin.length) for (let i = 0; i < bin.length; i++) { bytes[i] = bin.charCodeAt(i) } return bytes } function bytesToBase64(bytes: Uint8Array): string { let bin = '' for (let i = 0; i < bytes.length; i++) { bin += String.fromCharCode(bytes[i]) } return btoa(bin) } export const ExodeCache = { makeKey(src: string | undefined, identifier: string | number, size?: number): string { const raw = src || String(identifier) return `exode:v3:${djb2(raw)}:${size || 0}` }, rawKey(cacheKey: string): string { return `${cacheKey}:raw` }, processedKey(cacheKey: string): string { return `${cacheKey}:proc` }, getProcessed(key: string): any | null { const cached = processedCache.get(key) if (cached !== undefined) return cached const mmkv = getInstance() if (mmkv) { const raw = mmkv.getString(this.processedKey(key)) if (raw) { try { const parsed = JSON.parse(raw) processedCache.set(key, parsed) return parsed } catch {} } } return null }, setProcessed(key: string, data: any) { processedCache.set(key, data) const json = JSON.stringify(data) if (json.length > 30 * 1024 * 1024) return const mmkv = getInstance() if (mmkv) { mmkv.set(this.processedKey(key), json) } }, getRawBytes(key: string): Uint8Array | null { const cached = rawBytesCache.get(key) if (cached !== undefined) return cached const mmkv = getInstance() if (mmkv) { const b64 = mmkv.getString(this.rawKey(key)) if (b64) { try { const bytes = base64ToBytes(b64) rawBytesCache.set(key, bytes) return bytes } catch {} } } return null }, setRawBytes(key: string, bytes: Uint8Array) { rawBytesCache.set(key, bytes) if (bytes.length > 30 * 1024 * 1024) return const mmkv = getInstance() if (mmkv) { const b64 = bytesToBase64(bytes) mmkv.set(this.rawKey(key), b64) } }, clear() { processedCache.clear() rawBytesCache.clear() const mmkv = getInstance() if (mmkv) mmkv.clearAll() }, }