megalodon-rs icon indicating copy to clipboard operation
megalodon-rs copied to clipboard

Compatibility with wasm

Open silicology1 opened this issue 2 years ago • 15 comments

Can megalodon-rs support wasm so that it can used by rust framework like yew?

silicology1 avatar Sep 08 '23 13:09 silicology1

Please read: https://github.com/h3poteto/megalodon-rs/issues/53

h3poteto avatar Sep 23 '23 15:09 h3poteto

duplicated

h3poteto avatar Sep 23 '23 15:09 h3poteto

Still it doesn't support error

megalodon= {version="0.11.8"}

error[E0432]: unresolved import crate::sys::IoSourceState --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/io_source.rs:12:5 | 12 | use crate::sys::IoSourceState; | ^^^^^^^^^^^^^^^^^^^^^^^^^ no IoSourceState in sys

error[E0432]: unresolved import crate::sys::tcp --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/net/tcp/listener.rs:15:17 | 15 | use crate::sys::tcp::{bind, listen, new_for_addr}; | ^^^ could not find tcp in sys

error[E0432]: unresolved import crate::sys::tcp --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/net/tcp/stream.rs:13:17 | 13 | use crate::sys::tcp::{connect, new_for_addr}; | ^^^ could not find tcp in sys

error[E0433]: failed to resolve: could not find Selector in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/poll.rs:312:18 | 312 | sys::Selector::new().map(|selector| Poll { | ^^^^^^^^ could not find Selector in sys

error[E0433]: failed to resolve: could not find event in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/event/event.rs:24:14 | 24 | sys::event::token(&self.inner) | ^^^^^ could not find event in sys

error[E0433]: failed to resolve: could not find event in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/event/event.rs:38:14 | 38 | sys::event::is_readable(&self.inner) | ^^^^^ could not find event in sys

error[E0433]: failed to resolve: could not find event in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/event/event.rs:43:14 | 43 | sys::event::is_writable(&self.inner) | ^^^^^ could not find event in sys

error[E0433]: failed to resolve: could not find event in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/event/event.rs:68:14 | 68 | sys::event::is_error(&self.inner) | ^^^^^ could not find event in sys

error[E0433]: failed to resolve: could not find event in sys --> /home/amiya/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-0.8.10/src/event/event.rs:99:14 | 99 | sys::event::is_read_closed(&self.inner) | ^^^^^ could not find event in sys

silicology1 avatar Feb 02 '24 16:02 silicology1

@h3poteto would you consider re-opening this? In order for megalodon to work in wasm, I believe more functionality is required. I haven't yet managed to get reqwest to work from wasm. oauth2-rs support providing your own FnOnce for making HTTP requests. Using that I was able to get oauth2-rs and openidconnect-rs to work. Does megalodon currently support any sort of similar flow?

anderspitman avatar Nov 26 '24 18:11 anderspitman

No. I've tried to build megalodon with wasm, but it was the same.

h3poteto avatar Dec 16 '24 16:12 h3poteto

In the last few weeks I found another library that follows a similar pattern to oauth2-rs/openidconnect-rs: https://github.com/sugyan/atrium. Atrium makes multiple network requests internally, but it's simple to provide your own implementations for the network calls. For example you can see here the relatively few lines of code necessary for me to implement my own HTTP client function for wasm32 (running in Extism):

https://github.com/lastlogin-net/decent-auth-rs/blob/176bb8737179de9bfeecf9424a4354d91f06cbd0/src/atproto.rs#L283

This allowed me to add Bluesky/ATProto login to my library with far less effort and increased security.

I believe this is similar to the concept of sans-io development, but I'm not an expert in that. Would you be interested in adding this sort of flexibility to megalodon?

anderspitman avatar Dec 16 '24 16:12 anderspitman

Hmm, in the first place, why do you want to use megalodon-rs with wasm? Why not JavaScript version megalodon? https://github.com/h3poteto/megalodon

h3poteto avatar Dec 17 '24 15:12 h3poteto

I'm actually targeting backend, not frontend. I'm building an authentication library for multiple languages. Currently have it working for JS, Go, and Rust, but adding support for any of the Extism host languages (https://extism.org/) should be relatively straight forward.

anderspitman avatar Dec 17 '24 16:12 anderspitman

If it's backend, why do you need to use wasm instead of compiling with rust?

h3poteto avatar Dec 18 '24 00:12 h3poteto

You mean compile the Rust to FFI via extern C? Primarily because wasm is far more portable (especially for JS and Golang). You can run the same wasm binary unchanged on many platforms and languages.

anderspitman avatar Dec 18 '24 00:12 anderspitman

Sorry, I don't really get it, why do you need wasm if you're targeting the backend?

h3poteto avatar Dec 18 '24 09:12 h3poteto

Say I'm building a JS app and want to add ATProto login. I need to find a JS library that supports that. Next imagine I'm writing a Golang app and want to do the same thing. Now I need to find a completely different library, which likely has a different feature set. The library I'm working on works in JS, Golang, Rust, and others are easy to add. It also supports login with ATProto, Fediverse, OIDC, etc. One auth library that provides a consistent feature set and API in many languages.

If I wasn't using wasm, I would have to implement each feature in every language. Using wasm I implement a feature once and it immediately works in every language. Building for wasm is the best way I've found to accomplish this.

anderspitman avatar Dec 18 '24 16:12 anderspitman

But to do that, each library needs to be wasm-compatible, right? It would be fine if it were very easy, but isn't this just pushing the costs of implementing it onto the libraries side?

h3poteto avatar Dec 19 '24 13:12 h3poteto

Technically, yes. But this is becoming more and more common. ATrium and oauth2-rs already were compatible. And there are other benefits to designing APIs this way. It makes things more modular, and reduces your dependency on reqwest, while still retaining a high-level API for those who desire the default settings.

But I totally get it if this isn't a priority for you.

anderspitman avatar Dec 19 '24 18:12 anderspitman

Hmm, I understand the situation. However, since the situation where it is needed is limited, this issue is not a high priority for me.

Also, according to my investigation, the compile errors are not coming from reqwest and oauth2. Those errors are coming from tokio-tungstenite. It depends on tokio/net. We can't compile tokio/net with wasm. If wasm environment is browser, we can use wasm-bindgen and js-sys. But I'm not sure if it will work with Extism you require.

h3poteto avatar Dec 20 '24 01:12 h3poteto