From b5de9b534a44568392df6f347a6700d242a62e0b Mon Sep 17 00:00:00 2001 From: Marc Mettke Date: Fri, 11 Oct 2024 02:06:18 +0200 Subject: [PATCH] Adding host_metadata to Cloudflare Request Data (#348) * Adding host_metadata to Cloudflare Request Data * Update incoming_request_cf_properties.rs Make `host_metadata` method `catch` * Update cf.rs --------- Co-authored-by: Kevin Flansburg Co-authored-by: Kevin Flansburg --- .../src/types/incoming_request_cf_properties.rs | 3 +++ worker/src/cf.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/worker-sys/src/types/incoming_request_cf_properties.rs b/worker-sys/src/types/incoming_request_cf_properties.rs index 2d41beb2..4321d0f8 100644 --- a/worker-sys/src/types/incoming_request_cf_properties.rs +++ b/worker-sys/src/types/incoming_request_cf_properties.rs @@ -61,6 +61,9 @@ extern "C" { #[wasm_bindgen(method, catch, getter, js_name=regionCode)] pub fn region_code(this: &IncomingRequestCfProperties) -> Result, JsValue>; + #[wasm_bindgen(method, catch, getter, js_name=hostMetadata)] + pub fn host_metadata(this: &IncomingRequestCfProperties) -> Result; + #[wasm_bindgen(method, catch, getter)] pub fn timezone(this: &IncomingRequestCfProperties) -> Result; diff --git a/worker/src/cf.rs b/worker/src/cf.rs index 39175ec8..9bf1251e 100644 --- a/worker/src/cf.rs +++ b/worker/src/cf.rs @@ -172,6 +172,18 @@ impl Cf { pub fn is_eu_country(&self) -> bool { self.inner.is_eu_country().unwrap() == Some("1".to_string()) } + + pub fn host_metadata(&self) -> crate::Result> { + let host_metadata = self.inner.host_metadata()?; + if host_metadata.is_undefined() { + Ok(None) + } else { + serde_wasm_bindgen::from_value(host_metadata) + .map(Some) + .map_err(|e| wasm_bindgen::JsValue::from(e.to_string())) + } + .map_err(crate::Error::from) + } } /// Browser-requested prioritization information.