From 1b34b63656801c48ff66ca10f838f69be9ea8b7b Mon Sep 17 00:00:00 2001 From: SATVIKsynopsis Date: Tue, 20 Jan 2026 17:25:56 +0530 Subject: [PATCH 1/2] fixed injecting default headers and cf options --- worker/src/headers.rs | 5 +++++ worker/src/request_init.rs | 42 ++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/worker/src/headers.rs b/worker/src/headers.rs index ea7205dfe..f2b216fa8 100644 --- a/worker/src/headers.rs +++ b/worker/src/headers.rs @@ -37,6 +37,11 @@ impl Headers { self.0.get(name).map_err(Error::from) } + /// Returns true if the headers object has no entries. + pub fn is_empty(&self) -> bool { + self.keys().next().is_none() + } + /// Returns a boolean stating whether a `Headers` object contains a certain header. /// Returns an error if the name is invalid (e.g. contains spaces) pub fn has(&self, name: &str) -> Result { diff --git a/worker/src/request_init.rs b/worker/src/request_init.rs index debd098da..2b4f8d0a5 100644 --- a/worker/src/request_init.rs +++ b/worker/src/request_init.rs @@ -66,7 +66,11 @@ impl RequestInit { impl From<&RequestInit> for web_sys::RequestInit { fn from(req: &RequestInit) -> Self { let inner = web_sys::RequestInit::new(); - inner.set_headers(req.headers.as_ref()); + + if !req.headers.is_empty() { + inner.set_headers(req.headers.as_ref()); + } + inner.set_method(req.method.as_ref()); inner.set_redirect(req.redirect.into()); if let Some(cache) = req.cache { @@ -77,7 +81,8 @@ impl From<&RequestInit> for web_sys::RequestInit { } // set the Cloudflare-specific `cf` property on FFI RequestInit - let r = ::js_sys::Reflect::set( + if !req.cf.is_default() { + let r = ::js_sys::Reflect::set( inner.as_ref(), &JsValue::from("cf"), &JsValue::from(&req.cf), @@ -87,6 +92,7 @@ impl From<&RequestInit> for web_sys::RequestInit { "setting properties should never fail on our dictionary objects" ); let _ = r; + } inner } @@ -278,6 +284,21 @@ impl CfProperties { pub fn new() -> Self { Default::default() } + + pub fn is_default(&self) -> bool { + let de = CfProperties::default(); + self.apps == de.apps + && self.cache_everything == de.cache_everything + && self.cache_key == de.cache_key + && self.cache_ttl == de.cache_ttl + && self.cache_ttl_by_status == de.cache_ttl_by_status + && self.minify == de.minify + && self.mirage == de.mirage + && self.image.is_none() + && self.polish == de.polish + && self.resolve_override == de.resolve_override + && self.scrape_shield == de.scrape_shield + } } impl Default for CfProperties { @@ -301,7 +322,7 @@ impl Default for CfProperties { /// Configuration options for Cloudflare's minification features: /// #[wasm_bindgen] -#[derive(Clone, Copy, Debug, Default, Serialize)] +#[derive(Clone, Copy, Debug, Default, Serialize, PartialEq)] #[serde(rename_all = "kebab-case")] pub struct MinifyConfig { pub js: bool, @@ -311,7 +332,7 @@ pub struct MinifyConfig { /// Configuration options for Cloudflare's image optimization feature: /// -#[derive(Clone, Copy, Debug, Default, Serialize)] +#[derive(Clone, Copy, Debug, Default, Serialize, PartialEq)] #[serde(rename_all = "kebab-case")] pub enum PolishConfig { #[default] @@ -568,3 +589,16 @@ impl From for web_sys::RequestCache { } } } + +#[test] +fn request_init_no_invalid_options() { + let mut init = RequestInit::new(); + init.method = Method::Post; + + let js_init: web_sys::RequestInit = (&init).into(); + + let _ = web_sys::Request::new_with_str_and_init( + "https://httpbin.org/post", + &js_init, + ).unwrap(); +} From 1ca358a564160a56dcebf20189b474f1098abd99 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 20 Jan 2026 15:47:58 -0800 Subject: [PATCH 2/2] cargo fmt --- worker/src/request_init.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/worker/src/request_init.rs b/worker/src/request_init.rs index 2b4f8d0a5..5b568499f 100644 --- a/worker/src/request_init.rs +++ b/worker/src/request_init.rs @@ -83,16 +83,16 @@ impl From<&RequestInit> for web_sys::RequestInit { // set the Cloudflare-specific `cf` property on FFI RequestInit if !req.cf.is_default() { let r = ::js_sys::Reflect::set( - inner.as_ref(), - &JsValue::from("cf"), - &JsValue::from(&req.cf), - ); - debug_assert!( - r.is_ok(), - "setting properties should never fail on our dictionary objects" - ); - let _ = r; - } + inner.as_ref(), + &JsValue::from("cf"), + &JsValue::from(&req.cf), + ); + debug_assert!( + r.is_ok(), + "setting properties should never fail on our dictionary objects" + ); + let _ = r; + } inner } @@ -597,8 +597,5 @@ fn request_init_no_invalid_options() { let js_init: web_sys::RequestInit = (&init).into(); - let _ = web_sys::Request::new_with_str_and_init( - "https://httpbin.org/post", - &js_init, - ).unwrap(); + let _ = web_sys::Request::new_with_str_and_init("https://httpbin.org/post", &js_init).unwrap(); }