| name | wasm-constraints |
| description | WASM build constraints for the crates/xberg-wasm crate — the wasm-target feature set, no-tokio sync-only internal APIs, the mandatory SyncExtractor trait for WASM-compatible extractors, the 2 MB HTML size limit, size-optimized build config (opt-level="z"), and the async-wrapper/sync-internal API pattern. Load when building for wasm32, adding or modifying a WASM-compatible extractor, or debugging WASM build/runtime failures. |
WASM Build Constraints
Overview
WASM target in crates/xberg-wasm/. Uses wasm-bindgen with sync-only internal APIs.
Feature Flags
[features]
wasm-target = ["no-ort-target", "excel-wasm", "ocr-wasm", "layout-tract", "auto-rotate-tract"]
Critical Constraints
1. No Tokio Runtime
All operations must be synchronous internally. Use #[cfg(not(feature = "tokio-runtime"))] paths.
2. Internal Sync Extractor Required
Every WASM-compatible built-in extractor MUST implement the internal SyncExtractor trait. This is not part of the public V1 extraction API; public callers still use unified extract / extract_batch.
impl SyncExtractor for MyExtractor {
fn extract_sync(&self, content: &[u8], mime_type: &str, config: &ExtractionConfig)
-> Result<InternalDocument> { }
}
impl DocumentExtractor for MyExtractor {
fn as_sync_extractor(&self) -> Option<&dyn SyncExtractor> {
Some(self)
}
}
3. HTML Size Limit
const MAX_HTML_SIZE: usize = 2 * 1024 * 1024;
Build Config
[lib]
crate-type = ["cdylib", "rlib"]
[profile.release.package.xberg-wasm]
opt-level = "z"
codegen-units = 1
API Pattern
#[wasm_bindgen]
pub async fn extract_from_bytes(content: Vec<u8>, config: JsValue) -> Result<JsValue, JsValue> {
let config: ExtractionConfig = serde_wasm_bindgen::from_value(config)?;
let result = extract_bytes_sync(&content, mime_type, &config)?;
Ok(serde_wasm_bindgen::to_value(&result)?)
}
Functions can be async for JS compatibility, but internal extraction is sync.
Critical Rules
- No tokio -- all operations synchronous
- Implement SyncExtractor for all WASM-compatible extractors
- HTML limited to 2MB due to stack constraints
- Size optimization via
opt-level = "z"
- Feature gate with
#[cfg(target_arch = "wasm32")]