https://github.com/dunnock/wasi-worker
WASM / WASI interface for browser service workers
https://github.com/dunnock/wasi-worker
javascript rollup rust typescript wasi wasm yew
Last synced: 6 months ago
JSON representation
WASM / WASI interface for browser service workers
- Host: GitHub
- URL: https://github.com/dunnock/wasi-worker
- Owner: dunnock
- License: mit
- Created: 2019-10-28T14:26:39.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-04-11T19:42:59.000Z (over 3 years ago)
- Last Synced: 2025-04-13T18:14:11.094Z (6 months ago)
- Topics: javascript, rollup, rust, typescript, wasi, wasm, yew
- Language: Rust
- Homepage:
- Size: 707 KB
- Stars: 59
- Watchers: 3
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
The proper way to create WASM browser service workers.
This crate provides rust library and JS glue code allowing to wrap POSIX compatible code into WASM/WASI target to be able to run in the browser service worker. It also provides imput/output message channel with main web application.
# Why WASI?
> WebAssembly System Interfaces (WASI) is an exciting new specification that allows running POSIX-like applications anywhere, safely and securely with WebAssembly. [-> Medium / Running WASI in Javascript with Wasmer-JS](https://medium.com/wasmer/wasmer-js-9a53e837b80)
POSIX-compatible applications compiled to WASI can now also run in browser, think of code-reuse and delegating server workload to client side. On top of that it appears code compiled to wasm32-wasi target is executing about 2 times faster than code compiled to other wasm32 targets with web bindings, CPU intensive workloads can now execute with performance close to native targets (try http://wabench.com:8080).
# Why might I need wasi-worker?
WASI target allows to compile many crates which are using standard library, except threads and networking which is not supported yet. The only problem is that WASI is not built to be executed from browser, rather it is standard which aims to run WASM code on server side. Leveraging [@wasmer/wasi](https://github.com/wasmerio/wasmer-js) this crate provides browser service worker WASI runtime as well as communication bridge to/from web application.
Another possible reason is WASM code which executes as part of web application occupies same javascript thread, hence if wasm code is running complex calculations it will block browser application while working. To make it working in separate thread we can employ [browser service workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers).
# Usage example
This example requires WASI JavaScript bindings [which can be deployed with wasi-worker-cli](https://github.com/dunnock/wasi-worker/tree/master/crates/wasi-worker-cli) or [WASI environment](https://github.com/dunnock/wasi-worker/tree/master/examples/myworker) with properly preconfigured filesystem
```rust
use wasi_worker::*;struct MyWorker;
impl Handler for MyWorker {
fn on_message(&self, msg: &[u8]) -> std::io::Result<()> {
println!("My Worker got message: {:?}", msg);
Ok(())
}
}fn main() {
// JS glue code will hook to /output.bin
ServiceWorker::initialize(ServiceOptions::default());
ServiceWorker::set_message_handler(Box::new(MyWorker {}));
// Send binary message to main browser application
ServiceWorker::post_message(b"message");
}
```# JavaScript WASI bindings
JavaScript WASI bindings are built on top of [@wasmer](https://www.npmjs.com/search?q=%40wasmer) libraries can be easily deployed with [wasiworker](https://github.com/dunnock/wasi-worker/tree/master/wasi-worker-cli) tool:
```
cargo install wasi-worker-cli
````wasiworker install` will add sample `worker` code and bin target to current crate directory:
```
wasiworker install
````wasiworker deploy` will build `worker` bin target and deploy it with JS glue code under `./dist`:
```
wasiworker deploy
```For hacking [JS glue code source code](https://github.com/dunnock/wasi-worker/tree/master/wasi-worker-cli/js) is located in the same repository.
# More detailed example
```rust
use wasi_worker::*;struct MyWorker {}
impl Handler for MyWorker {
fn on_message(&self, msg: &[u8]) -> std::io::Result<()> {
// Process incoming message
println!("My Worker got message: {:?}", msg);
Ok(())
}
}fn main() {
// In WASI setup output will go to /output.bin
#[cfg(target_os="wasi")]
let opt = ServiceOptions::default();
// In user filesystem we operate under current dir
#[cfg(not(target_os="wasi"))]
let opt = ServiceOptions {
output: FileOptions::File("./testdata/output.bin".to_string())
};
let output_file = match &opt.output {
FileOptions::File(path) => path.clone()
};
ServiceWorker::initialize(opt)
.expect("ServiceWorker::initialize");// Attach Agent to ServiceWorker as message handler singleton
ServiceWorker::set_message_handler(Box::new(MyWorker {}));// Send binary message to main browser application
// this requires JS glue see wasi-worker-cli
ServiceWorker::post_message(b"message")
.expect("ServiceWorker::post_message");// It does not autodelete output file
std::fs::remove_file(output_file)
.expect("Remove output.bin");
}
```# TODO
- [X] library code with WASI fs interface
- [X] basic example
- [X] documentation
- [X] CLI for worker setup
- [X] drop output file on exit