worker_sys/ext/
response.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use wasm_bindgen::prelude::*;

mod glue {

    use super::*;

    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen]
        pub type Response;

        #[wasm_bindgen(method, catch, getter)]
        pub fn webSocket(this: &Response) -> Result<Option<web_sys::WebSocket>, JsValue>;

        #[wasm_bindgen(method, catch, getter)]
        pub fn cf(this: &Response) -> Result<Option<js_sys::Object>, JsValue>;
    }
}

pub trait ResponseExt {
    /// Getter for the `webSocket` field of this object.
    fn websocket(&self) -> Option<web_sys::WebSocket>;

    /// Getter for the `cf` field of this object.
    fn cf(&self) -> Option<js_sys::Object>;
}

impl ResponseExt for web_sys::Response {
    fn websocket(&self) -> Option<web_sys::WebSocket> {
        self.unchecked_ref::<glue::Response>()
            .webSocket()
            .expect("read response.webSocket")
    }

    fn cf(&self) -> Option<js_sys::Object> {
        self.unchecked_ref::<glue::Response>()
            .cf()
            .expect("read response.cf")
    }
}