worker_sys/ext/
response.rs

1use wasm_bindgen::prelude::*;
2
3mod glue {
4
5    use super::*;
6
7    #[wasm_bindgen]
8    extern "C" {
9        #[wasm_bindgen]
10        pub type Response;
11
12        #[wasm_bindgen(method, catch, getter)]
13        pub fn webSocket(this: &Response) -> Result<Option<web_sys::WebSocket>, JsValue>;
14
15        #[wasm_bindgen(method, catch, getter)]
16        pub fn cf(this: &Response) -> Result<Option<js_sys::Object>, JsValue>;
17    }
18}
19
20pub trait ResponseExt {
21    /// Getter for the `webSocket` field of this object.
22    fn websocket(&self) -> Option<web_sys::WebSocket>;
23
24    /// Getter for the `cf` field of this object.
25    fn cf(&self) -> Option<js_sys::Object>;
26}
27
28impl ResponseExt for web_sys::Response {
29    fn websocket(&self) -> Option<web_sys::WebSocket> {
30        self.unchecked_ref::<glue::Response>()
31            .webSocket()
32            .expect("read response.webSocket")
33    }
34
35    fn cf(&self) -> Option<js_sys::Object> {
36        self.unchecked_ref::<glue::Response>()
37            .cf()
38            .expect("read response.cf")
39    }
40}