-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add WASM support #255
Add WASM support #255
Conversation
Wow-eeeee, this is amazing & exciting. I love that it's basically a 4-line diff hiding a ton of hard work by many people, especially you (: I'll try to read up on what the implications are, but I'm super excited to get this merged asap - thank you for this! |
Looks like my upstream github workflows use some deprecated version or something, which will prevent some runs from succeeding. I'm looking into it! |
Looking at the performance from this pr vs an older run from master performance seems to indeed be the same which is great! This PR:
Master:
So it is an effectively "free" change for anyone using the crate same as before, outside of WASM. |
I'm on Windows and only 1 test is failing by being too slow but I'm pretty sure that's because I use WSL on a sluggish laptop, everything else passes so once the CI upload artifact v3 thing is resolved everything should be green!
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #255 +/- ##
=======================================
Coverage 97.75% 97.75%
=======================================
Files 31 31
Lines 2049 2049
=======================================
Hits 2003 2003
Misses 46 46 ☔ View full report in Codecov by Sentry. |
Looks like there are just no-std builds left due to a downstream dep not being fully no-std compatible (and a cargo-deny migration); I'll fix those on master in a bit. |
OK, think master's CI status should be fixed now; please rebase & we can get this merged! |
Oh, just one thought: How did you try this (what toolchains / tools)? I'd love to add that configuration to CI so we keep it working. |
It provides assert_gt and assert_in_range, and works in non-std tests apparently.
Apparently *my* version of cargo-deny can't see that; doesn't mean github's can't. Ugh.
Testing is a bit weird because from what I understand your code might need to be evaluated for it to crash, i.e. just because something compiles for WASM does not mean it will not panic when targeting WASM. What I did was follow these instructions for the HTTP example and just run To illustrate what I mentioned earlier about things compiling even though they might lead to a panic, the following code compiles just fine use axum::{routing::get, Router};
use tower_service::Service;
use worker::*;
fn router() -> Router {
Router::new().route("/", get(root))
}
#[event(fetch)]
async fn fetch(
req: HttpRequest,
_env: Env,
_ctx: Context,
) -> Result<axum::http::Response<axum::body::Body>> {
console_error_panic_hook::set_once();
let tmp = std::time::Instant::now();
Ok(router().call(req).await?)
}
pub async fn root() -> &'static str {
"Hello Axum!"
} But once you visit the URL it panics because of the use of |
I tried adding some tests via |
Ah, yeah, I suppose we can make a sub-crate here that is only for testing. Thankfully the repo is already organized as a workspace. That's just a follow-up item, though. No need to get busy on that in this PR! (: |
Nice! Is there anything else left to do here? |
Think that should be all - I'll merge this and later see about testing & cutting a release. Thanks so much! |
If anything interesting pops up in the testing phase let me know as I'll also need to write some wasm tests in my own project 😅 thanks! |
81755bc
Closes #34!
This was a bit of a rabbit hole to get into, even though the changes are (thankfully) tiny but this adds WASM support to the crate. My use case was using a downstream crate in Cloudflare workers and this crate was causing panics because of its time APIs.
The PR uses
web-time
which was made specifically for this purpose;MonotonicClock
is now compatible with WASM environments at, from what I understand, no extra cost to non-WASM environments.^ From web-time's README
One small point that might cause confusion is when trying to type out a
RateLimiter
type which can look something likelimiter: Arc<RateLimiter<NotKeyed, InMemoryState, MonotonicClock, NoOpMiddleware<web_time::Instant>>>
. If someone accidentally usesstd::time::Instant
, the errors are about Instant not implementing the traits that are outlined here which can be confusing to those trying to use this. For this I think for one documenting this use case is a must but also re-exportingweb_time::Instant
should come in handy.My reasoning for the above is that while the existing docs are good and self explanatory, only someone that would try implementing his own Clock would likely take a look there; since this is an existing, internal Clock provided by the crate, there's a good chance most people using it won't have read that part of the docs.
You will also notice this PR adds
getrandom = { version = "0.2", features = ["js"] }
, this is required to getrand
to compile in a WASM environment (see here).Lastly, nothing of the included changes is put behind a feature flag but I personally don't think any of the changes could cause issues. Let me know what you think!