fix: resolve WASM loading for Bun --compile single-binary executables

Both roaring-wasm and candle-wasm now work correctly in all environments:
- Node.js (fs.readFileSync)
- Bun runtime (Bun.file)
- Bun --compile (embedded assets via import { type: 'file' })
- Browser (fetch)

roaring-wasm:
- Created src/utils/roaring/index.ts wrapper
- Uses browser bundle which has WASM embedded as base64
- Top-level await ensures initialization before use
- Zero environment detection needed (works everywhere)

candle-wasm:
- Created src/embeddings/wasm/wasmLoader.ts universal loader
- Uses Bun's import { type: 'file' } to embed 93MB WASM in compiled binary
- Fixed browser detection (Bun defines 'self', check for 'document' instead)
- Simplified CandleEmbeddingEngine.ts to use wasmLoader

Binary size verification:
- Minimal Bun binary: 100MB (runtime only)
- Brainy binary: 199MB (100MB runtime + 93MB WASM + 6MB JS)
- No duplication: WASM embedded exactly once

Test results:
- Node.js: 1190/1190 tests pass
- Bun runtime: 8/8 tests pass
- Bun --compile: 8/8 tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
David Snelling 2026-01-06 14:09:02 -08:00
parent ffd18c9598
commit 5d9ec5bb16
6 changed files with 198 additions and 36 deletions

View file

@ -24,7 +24,7 @@ import {
ZoneMap
} from './metadataIndexChunking.js'
import { EntityIdMapper } from './entityIdMapper.js'
import { RoaringBitmap32 } from 'roaring-wasm'
import { RoaringBitmap32, roaringLibraryInitialize } from './roaring/index.js'
import { FieldTypeInference, FieldType } from './fieldTypeInference.js'
export interface MetadataIndexEntry {
@ -202,6 +202,9 @@ export class MetadataIndexManager {
* This must be called after construction and before any queries
*/
async init(): Promise<void> {
// Initialize roaring-wasm library (browser bundle requires async init)
await roaringLibraryInitialize()
// Load field registry to discover persisted indices (v4.2.1)
// Must run first to populate fieldIndexes directory before warming cache
await this.loadFieldRegistry()

View file

@ -22,7 +22,7 @@
import { StorageAdapter } from '../coreTypes.js'
import { prodLog } from './logger.js'
import { RoaringBitmap32 } from 'roaring-wasm'
import { RoaringBitmap32 } from './roaring/index.js'
import type { EntityIdMapper } from './entityIdMapper.js'
// ============================================================================

14
src/utils/roaring/browser.d.ts vendored Normal file
View file

@ -0,0 +1,14 @@
/**
* Type declarations for roaring-wasm browser bundle
* Re-exports types from the main roaring-wasm package
*/
declare module 'roaring-wasm/browser/index.mjs' {
export {
RoaringBitmap32,
RoaringBitmap32Iterator,
roaringLibraryInitialize,
roaringLibraryIsReady,
SerializationFormat,
DeserializationFormat
} from 'roaring-wasm'
}

View file

@ -0,0 +1,35 @@
/**
* Roaring Bitmap wrapper with embedded WASM
*
* Always uses the browser bundle which has WASM embedded as base64.
* This ensures consistent behavior across all environments:
* - Browser
* - Node.js
* - Bun
* - Bun --compile (single binary)
*
* NO filesystem access, NO runtime loading, NO environment-specific code.
*/
import {
RoaringBitmap32,
RoaringBitmap32Iterator,
roaringLibraryInitialize,
roaringLibraryIsReady,
SerializationFormat,
DeserializationFormat
} from 'roaring-wasm/browser/index.mjs'
// Initialize WASM at module load time (top-level await)
// This ensures RoaringBitmap32 is ready to use immediately after import
await roaringLibraryInitialize()
// Re-export all types and values
export {
RoaringBitmap32,
RoaringBitmap32Iterator,
roaringLibraryInitialize,
roaringLibraryIsReady,
SerializationFormat,
DeserializationFormat
}