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 fetch in nodejs #3377

Closed
CEbbinghaus opened this issue Apr 1, 2023 · 3 comments
Closed

Support for fetch in nodejs #3377

CEbbinghaus opened this issue Apr 1, 2023 · 3 comments

Comments

@CEbbinghaus
Copy link

CEbbinghaus commented Apr 1, 2023

Motivation

fetch has been part of the nodejs runtime since v17.5.0 (behind an experimental flag) and v18.0.0 (without the flag) and as such is available in the "global" scope in both web and node. Furthermore, it is also available in the global scope in both deno and bun.js making it almost entirely cross runtime/environment. As such wasm-bindgen should also support fetch without referencing the window object as is shown in the docs

Proposed Solution

allowing access to the fetch method via the "global" scope in any runtime, This would be the window object for browser & the global object for nodejs. (not too sure about deno & bun.js)

Alternatives

Leave it as is and limit using fetch to only the browser

Additional Context

Old Outdated forum post saying it doesn't exist in node
New forum post created by me in parallel to this issue

@daxpedda
Copy link
Collaborator

wasm-bindgen wouldn't fit the bill, this would probably belong into web-sys, but it's specifically made for Web APIs, not Node.js.

So the missing part here is probably something like node-sys, which would then be able to implement the Node.js global object and all it's methods. This can be a completely standalone library and doesn't need to live in this repository.

Considering the developer and maintenance bandwidth available, this is very much out of scope.

@daxpedda daxpedda closed this as not planned Won't fix, can't repro, duplicate, stale May 12, 2023
@stan-irl
Copy link

stan-irl commented Jun 7, 2023

@CEbbinghaus just bind straight fetch as an external. if its there the call will be successful. Here is an example: https://github.com/devashishdxt/tonic-web-wasm-client/blob/main/src/fetch.rs

use web_sys::{Request, RequestInit, Response};
use js_sys::Promise;
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_name = fetch)]
    fn fetch_with_request_and_init(input: &Request, init: &RequestInit) -> Promise;
}

@CEbbinghaus
Copy link
Author

That is a solid solution. Probably better to move it into a crate at some point but that is absolutely the way to go in the intrum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants