Closed mycognosist closed 3 years ago
Seems like a good time to post some updated notes on this process:
We are using wasm-bindgen to generate bindings from our Rust code. This is primarily used via the #[wasm_bindgen]
attribute (used to mark functions for export and assign a JS function name) which is used in much the same way as the #[node_bindgen]
attribute in our src/lib.rs
for native.
Then we use the wasm-pack tool to "generate an npm wasm pkg from a rustwasm crate". wasm-pack
uses wasm_bindgen
under the hood.
Because our validation code relies on ssb-validate (rs) - which employs parallelization with rayon) - we use wasm-bindgen-rayon to auto-generate WebWorker shim code. These WebWorkers act as threads, with one WebWorker spawned for each thread invoked by the parallel validation code.
We are also using module-workers-polyfill to add support for Firefox (more context can be found in this issue)
At this point, the WASM code for ssb-validate2-rsjs
is working, but only due to some convenient bugs 🙃 Additional context here.
I am now working to move the WASM initialization and WebWorker threadpool initialization off the main thread. Contextual details appear in this issue comment.
@mycognosist I guess we can close this one? :)
@staltz Yes! :D
Just for the sake of any future readers:
We ended up using the Comlink library to move the WASM initialization and WebWorker threadpool initialization off the main thread. We also introduced bundling with webpack (https://github.com/ssb-ngi-pointer/ssb-validate2-rsjs-wasm/pull/1) which conveniently offers us hassle-free Firefox support (no polyfill needed).
Leaving this as a prompt for further investigation in the future. Relevant link: Compiling from Rust to WebAssembly