# Ruvector WASM Build Guide ## Overview This guide provides instructions for building the Ruvector WASM bindings. The WASM module enables high-performance vector database operations directly in web browsers and Node.js environments. ## Implementation Status ✅ **Completed Components:** 1. **Core WASM Bindings** (`/crates/ruvector-wasm/src/lib.rs`) - Full VectorDB API (insert, search, delete, batch operations) - Proper error handling with WasmResult types - Console panic hook for debugging - JavaScript-compatible types (JsVectorEntry, JsSearchResult) 2. **SIMD Support** - Dual build configuration (with/without SIMD) - Feature flags in Cargo.toml - Runtime SIMD detection via `detectSIMD()` function 3. **Web Workers Integration** (`/crates/ruvector-wasm/src/worker.js`) - Message passing for async operations - Support for insert, search, delete, batch operations - Zero-copy transfers preparation 4. **Worker Pool Management** (`/crates/ruvector-wasm/src/worker-pool.js`) - Automatic pool sizing (4-8 workers based on CPU cores) - Round-robin task distribution - Promise-based API - Error handling and timeouts 5. **IndexedDB Persistence** (`/crates/ruvector-wasm/src/indexeddb.js`) - Save/load vectors to IndexedDB - Batch operations for performance - Progressive loading with callbacks - LRU cache implementation (1000 hot vectors) 6. **Examples** - Vanilla JavaScript example (`/examples/wasm-vanilla/index.html`) - React + Web Workers example (`/examples/wasm-react/`) 7. **Tests** - Comprehensive WASM tests (`/crates/ruvector-wasm/tests/wasm.rs`) - Browser-based testing with wasm-bindgen-test 8. **Build Configuration** - Optimized for size (target: <500KB gzipped) - Multiple build targets (web, nodejs, bundler) - Size verification scripts ## Prerequisites ```bash # Install Rust with wasm32 target rustup target add wasm32-unknown-unknown # Install wasm-pack cargo install wasm-pack # Optional: Install wasm-opt for further optimization npm install -g wasm-opt ``` ## Building ### Standard Web Build ```bash cd crates/ruvector-wasm wasm-pack build --target web --out-dir pkg --release ``` ### SIMD-Enabled Build ```bash cd crates/ruvector-wasm wasm-pack build --target web --out-dir pkg-simd --release -- --features simd ``` ### All Targets ```bash cd crates/ruvector-wasm npm run build:all ``` This will build for: - Web (`pkg/`) - Web with SIMD (`pkg-simd/`) - Node.js (`pkg-node/`) - Bundler (`pkg-bundler/`) ## Known Build Issues & Solutions ### Issue: getrandom 0.3 Compatibility **Problem:** Some dependencies (notably `rand` via `uuid`) pull in `getrandom` 0.3.4, which requires the `wasm_js` feature flag that must be set via `RUSTFLAGS` configuration flags, not just Cargo features. **Solution Options:** 1. **Use .cargo/config.toml** (Already configured): ```toml [target.wasm32-unknown-unknown] rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] ``` 2. **Disable uuid feature** (Implemented): ```toml # In ruvector-core/Cargo.toml [features] default = ["simd", "uuid-support"] uuid-support = ["uuid"] # In ruvector-wasm/Cargo.toml [dependencies] ruvector-core = { path = "../ruvector-core", default-features = false } ``` 3. **Alternative: Use timestamp-based IDs** (Fallback): For WASM builds, use `Date.now()` + random suffixes instead of UUIDs ### Issue: Large Binary Size **Solution:** 1. Enable LTO and size optimization (already configured): ```toml [profile.release] opt-level = "z" lto = true codegen-units = 1 panic = "abort" ``` 2. Run wasm-opt: ```bash npm run optimize ``` 3. Verify size: ```bash npm run size ``` ## Usage Examples ### Vanilla JavaScript ```html