Skip to content

Add wasm32 support to the rust sdk #2704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

thlsrms
Copy link

@thlsrms thlsrms commented May 5, 2025

Description of Changes

Enabled the Rust SDK wasm32 compilation target for web usage.

  • Users targeting wasm32-unknown-unknown must set: RUSTFLAGS='--cfg getrandom_backend="wasm_js"' to enable getrandom's webassembly support.
  • Added web feature flag, incompatible with non-wasm32 targets.

API and ABI breaking changes

None. Most changes apply only to the wasm32 target, so there are no global breaking changes. The main differences to the native targets are:

  • DBConnectionBuilder::build is now async on the web.
  • Authorization token is sent as a query parameter instead of in a header.
  • Credentials handling:
    • File struct unavailable on the web.
    • Two browser-friendly options are provided:
      • The re-exported API from gloo_storage for local/session storage.
      • A Cookie builder to manage cookies (except HttpOnly cookies).

Expected complexity level and risk

Estimated at 2:

  • Most work involves adjusting dependencies and feature flags.
  • Non-blocking alternatives replace a few native SDK code paths.

Opening this PR as a draft to get comments on it.

Testing

I built 2 demos to exercise the changes:

  1. Bevy https://github.com/thlsrms/tictactoe-stdb-bevy
  2. Leptos https://github.com/thlsrms/stdb-leptos-quickstart

Note: No automated tests have been written yet.

thlsrms added 10 commits May 1, 2025 23:03
- Using my fork of `tokio-tungstenite-wasm` due to the addition of the
function `connect_with_protocols`.
- `DbConnectionBuilder::build` becomes async without tokio's
block_in_place.

Still need to add `web` feature flag.
Replaces the unmaintained `anymap` dependency to fix a panic caused by
`copy_nonoverlapping::precondition_check` due to alignment in
`<TypeIdHasher as Hasher>::write` specific to web (wasm32) debug
builds.

The updated `anymap` fork drops its custom `Any` in favor of the
standard library's `std::any::Any`.
@CLAassistant
Copy link

CLAassistant commented May 5, 2025

CLA assistant check
All committers have signed the CLA.

thlsrms added 2 commits May 7, 2025 18:33
Renamed the `run_threaded` method on `wasm32` to better reflect its
behavior of spawning a background task.

The generated `DbConnection` methods `run_threaded`, `run_background`,
and `advance_one_message_blocking` now include runtime panics with a
clear error feedback when called on unsupported targets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants