web-thread 0.2.1

Convenient Web worker threading library with transferable support
Documentation

web-thread

A crate for long-running, shared-memory threads in a browser context for use with wasm-bindgen. Supports sending non-Send data across the boundary using postMessage and transfer.

Requirements

Like all Web threading solutions, this crate requires Wasm atomics, bulk memory, and mutable globals:

.cargo/config.toml

[target.wasm32-unknown-unknown]
rustflags = [
    "-C", "target-feature=+atomics,+bulk-memory,+mutable-globals",
]

as well as cross-origin isolation on the serving Web page in order to enable the use of SharedArrayBuffer, i.e. the HTTP headers

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

The credentialless value for Cross-Origin-Embedder-Policy should also work, but at the time of writing is not supported in Safari.

Linking the binary

Since this crate can't know the location of your shim script and Wasm binary ahead of time, you must make the module identifier web-thread:wasm-shim resolve to the path of your wasm-bindgen shim script. This can be done with a bundler such as Vite or Webpack, or by using a source-transformation tool such as tsc-alias:

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./",
        "paths": {
            "web-thread:wasm-shim": ["./src/wasm/my-library.js"]
        }
    },
    "tsc-alias": {
        "resolveFullPaths": true
    }
}

Turbopack is currently not supported due to an open issue when processing cyclic dependencies. See the following discussions for more information:

License

This project is available under the terms of the Apache 2.0 license.