Skip to content
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

Support for nodejs #31

Closed
robrennie opened this issue Aug 8, 2023 · 7 comments · Fixed by #58
Closed

Support for nodejs #31

robrennie opened this issue Aug 8, 2023 · 7 comments · Fixed by #58

Comments

@robrennie
Copy link

Perhaps I'm doing something wrong, but when I use wasm-pack with a --target of nodejs and then run in a simple node test script, it panics as shown below.

panicked at 'called Option::unwrap() on a None value', ...\ehttp-0.3.0\src\web.rs:37:36

It seems this points to code that is expecting it to be run in a browser. Any chance there may be a way to support node?

Thanks!

@emilk
Copy link
Owner

emilk commented Sep 7, 2023

Yes, ehttp currently expects to run in a browser.

If you want node support, you'll have to build it :) It shouldn't be too hard - just remove the calls to web_sys::window() and replace the fetch_with_request with whatever the equivalent is in nodejs

@robrennie
Copy link
Author

Yes, I could write it, but I figured I'd ask you to write it first :)

The hard part isn't the code, it's detecting what wasm-pack is targeting from within Rust: rustwasm/wasm-pack#795 (comment)

@lvauvillier
Copy link
Contributor

Hey @robrennie, @emilk, as Node.js now supports the fetch API (since v18.0), we can easily bypass the web_sys::window() + fetch_with_request call by directly mapping fetch as external function (this is what web_sys does internally).

This simple code does the trick:

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_name = fetch)]
    fn fetch_with_request(input: &web_sys::Request) -> Promise;
} 

If I find some time in the coming days, I'll make a PR.

@robrennie
Copy link
Author

Hi @lvauvillier - I think you'll still run into the conditional compilation problem with wasm-pack (see the comment above) where you can't access the --target parameter to tell whether you're targeting nodejs or web.

@lvauvillier
Copy link
Contributor

lvauvillier commented Jul 18, 2024

The code is isomorphic, functioning seamlessly on both Node (since v18.0) and the browser. So, no conditional compilation is needed.

in browsers fetch === window.fetch

@robrennie
Copy link
Author

@lvauvillier - well that's pretty great! Didn't realize - I'm more a Rustacean than a javascripter. Looking forward to a new version with this!

lvauvillier added a commit to lvauvillier/ehttp that referenced this issue Jul 19, 2024
@lvauvillier
Copy link
Contributor

@robrennie I made the PR

@emilk emilk closed this as completed in #58 Sep 2, 2024
@emilk emilk closed this as completed in 476b5c3 Sep 2, 2024
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 a pull request may close this issue.

3 participants