import Xxhash from './NativeXxhash'; declare global { var __xxhash128: (input: string) => string; var __xxhash64: (input: string) => string; } // Importing the spec above runs TurboModuleRegistry.getEnforcing('xxhash'), which // instantiates the module synchronously on the JS thread. As a real codegen // TurboModule it is created during runtime setup, so the native // installJSIBindingsWithRuntime hook fires and installs __xxhash128/__xxhash64 // before the first hash. The Xxhash.hash128/hash64 spec methods are the reliable // synchronous fallback if the JSI globals still aren't present. /** * Hashes the input string using the xxhash128 algorithm. * * @param {string} input - The string to hash. * @returns {string} The 128-bit hash as a hex string. * @throws {Error} If the input is missing or not a string. */ export const hash128 = (input: string): string => { if (!input) { throw new Error('Input is required'); } if (typeof input !== 'string') { throw new Error('Input must be a string'); } if (typeof globalThis.__xxhash128 === 'function') { return globalThis.__xxhash128(input); } return Xxhash.hash128(input); }; /** * Hashes the input string using the xxhash64 algorithm. * * @param {string} input - The string to hash. * @returns {string} The 64-bit hash as a hex string. * @throws {Error} If the input is missing or not a string. */ export const hash64 = (input: string): string => { if (!input) { throw new Error('Input is required'); } if (typeof input !== 'string') { throw new Error('Input must be a string'); } if (typeof globalThis.__xxhash64 === 'function') { return globalThis.__xxhash64(input); } return Xxhash.hash64(input); };