diff --git a/Cargo.lock b/Cargo.lock index 114c8d0b..9cbde3d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,7 +598,7 @@ dependencies = [ "futures-core", "futures-sink", "gloo-utils 0.1.7", - "http", + "http 0.2.11", "js-sys", "pin-project", "serde", @@ -619,7 +619,7 @@ dependencies = [ "futures-io", "futures-sink", "gloo-utils 0.2.0", - "http 1.0.0", + "http 1.1.0", "js-sys", "once_cell", "pin-project", @@ -785,7 +785,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", "indexmap 2.1.0", "slab", "tokio", @@ -814,7 +814,7 @@ dependencies = [ "base64", "bytes", "headers-core", - "http", + "http 0.2.11", "httpdate", "mime", "sha1", @@ -826,7 +826,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" dependencies = [ - "http", + "http 0.2.11", ] [[package]] @@ -852,6 +852,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -859,7 +870,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.11", "pin-project-lite", ] @@ -886,7 +897,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.11", "http-body", "httparse", "httpdate", @@ -1049,7 +1060,7 @@ dependencies = [ "bytes", "encoding_rs", "futures-util", - "http", + "http 0.2.11", "httparse", "log", "memchr", @@ -1699,7 +1710,7 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http", + "http 0.2.11", "httparse", "log", "rand", @@ -1787,7 +1798,7 @@ dependencies = [ "futures-channel", "futures-util", "headers", - "http", + "http 0.2.11", "hyper", "log", "mime", diff --git a/crates/net/Cargo.toml b/crates/net/Cargo.toml index 73681ee5..018c5e91 100644 --- a/crates/net/Cargo.toml +++ b/crates/net/Cargo.toml @@ -78,7 +78,6 @@ http = [ 'web-sys/Response', 'web-sys/ResponseInit', 'web-sys/ResponseType', - 'web-sys/Window', 'web-sys/RequestCache', 'web-sys/RequestCredentials', 'web-sys/ObserverCallback', @@ -88,7 +87,6 @@ http = [ 'web-sys/ReadableStream', 'web-sys/Blob', 'web-sys/FormData', - 'web-sys/WorkerGlobalScope', ] # Enables the EventSource API eventsource = [ diff --git a/crates/net/src/http/request.rs b/crates/net/src/http/request.rs index 537fc264..d41fee92 100644 --- a/crates/net/src/http/request.rs +++ b/crates/net/src/http/request.rs @@ -1,11 +1,11 @@ use crate::http::{Headers, QueryParams, Response}; use crate::{js_to_error, Error}; use http::Method; -use js_sys::{ArrayBuffer, Reflect, Uint8Array}; +use js_sys::{ArrayBuffer, Uint8Array}; use std::convert::{From, TryFrom, TryInto}; use std::fmt; use std::str::FromStr; -use wasm_bindgen::{JsCast, JsValue}; +use wasm_bindgen::{JsCast, JsValue, prelude::wasm_bindgen}; use wasm_bindgen_futures::JsFuture; use web_sys::{ AbortSignal, FormData, ObserverCallback, ReadableStream, ReferrerPolicy, RequestCache, @@ -16,6 +16,18 @@ use web_sys::{ #[cfg_attr(docsrs, doc(cfg(feature = "json")))] use serde::de::DeserializeOwned; +#[wasm_bindgen] +extern "C" { + // Create a separate binding for `fetch` as a global, rather than using the + // existing Window/WorkerGlobalScope bindings defined by web_sys, for + // greater efficiency. + // + // https://github.com/rustwasm/wasm-bindgen/discussions/3863 + #[wasm_bindgen(js_name = "fetch")] + fn fetch_with_request(request: &web_sys::Request) -> js_sys::Promise; +} + + /// A wrapper round `web_sys::Request`: an http request to be used with the `fetch` API. pub struct RequestBuilder { options: web_sys::RequestInit, @@ -320,23 +332,7 @@ impl Request { /// Executes the request. pub async fn send(self) -> Result { let request = self.0; - let global = js_sys::global(); - let maybe_window = - Reflect::get(&global, &JsValue::from_str("Window")).map_err(js_to_error)?; - let promise = if !maybe_window.is_undefined() { - let window = global.dyn_into::().unwrap(); - window.fetch_with_request(&request) - } else { - let maybe_worker = Reflect::get(&global, &JsValue::from_str("WorkerGlobalScope")) - .map_err(js_to_error)?; - if !maybe_worker.is_undefined() { - let worker = global.dyn_into::().unwrap(); - worker.fetch_with_request(&request) - } else { - panic!("Unsupported JavaScript global context"); - } - }; - + let promise = fetch_with_request(&request); let response = JsFuture::from(promise).await.map_err(js_to_error)?; response .dyn_into::()