libp2p_webtransport_websys/
bindings.rs

1//! This file is an extract from `web-sys` crate. It is a temporary
2//! solution until `web_sys::WebTransport` and related structs get stabilized.
3//!
4//! Only the methods that are used by this crate are extracted.
5
6#![allow(clippy::all)]
7use js_sys::{Object, Promise, Reflect};
8use wasm_bindgen::prelude::*;
9use web_sys::{ReadableStream, WritableStream};
10
11// WebTransport bindings
12#[wasm_bindgen]
13extern "C" {
14    #[wasm_bindgen(extends = Object, js_name = WebTransport, typescript_type = "WebTransport")]
15    #[derive(Debug, Clone, PartialEq, Eq)]
16    pub type WebTransport;
17
18    #[wasm_bindgen(structural, method, getter, js_class = "WebTransport", js_name = ready)]
19    pub fn ready(this: &WebTransport) -> Promise;
20
21    #[wasm_bindgen(structural, method, getter, js_class = "WebTransport", js_name = closed)]
22    pub fn closed(this: &WebTransport) -> Promise;
23
24    #[wasm_bindgen(structural, method, getter, js_class = "WebTransport" , js_name = incomingBidirectionalStreams)]
25    pub fn incoming_bidirectional_streams(this: &WebTransport) -> ReadableStream;
26
27    #[wasm_bindgen(catch, constructor, js_class = "WebTransport")]
28    pub fn new(url: &str) -> Result<WebTransport, JsValue>;
29
30    #[wasm_bindgen(catch, constructor, js_class = "WebTransport")]
31    pub fn new_with_options(
32        url: &str,
33        options: &WebTransportOptions,
34    ) -> Result<WebTransport, JsValue>;
35
36    #[wasm_bindgen(method, structural, js_class = "WebTransport", js_name = close)]
37    pub fn close(this: &WebTransport);
38
39    #[wasm_bindgen (method, structural, js_class = "WebTransport", js_name = createBidirectionalStream)]
40    pub fn create_bidirectional_stream(this: &WebTransport) -> Promise;
41}
42
43// WebTransportBidirectionalStream bindings
44#[wasm_bindgen]
45extern "C" {
46    #[wasm_bindgen(extends = Object, js_name = WebTransportBidirectionalStream, typescript_type = "WebTransportBidirectionalStream")]
47    #[derive(Debug, Clone, PartialEq, Eq)]
48    pub type WebTransportBidirectionalStream;
49
50    #[wasm_bindgen(structural, method, getter, js_class = "WebTransportBidirectionalStream", js_name = readable)]
51    pub fn readable(this: &WebTransportBidirectionalStream) -> WebTransportReceiveStream;
52
53    #[wasm_bindgen(structural, method, getter, js_class = "WebTransportBidirectionalStream", js_name = writable)]
54    pub fn writable(this: &WebTransportBidirectionalStream) -> WebTransportSendStream;
55}
56
57// WebTransportReceiveStream bindings
58#[wasm_bindgen]
59extern "C" {
60    #[wasm_bindgen(extends = ReadableStream, extends = Object, js_name = WebTransportReceiveStream, typescript_type = "WebTransportReceiveStream")]
61    #[derive(Debug, Clone, PartialEq, Eq)]
62    pub type WebTransportReceiveStream;
63}
64
65// WebTransportSendStream bindings
66#[wasm_bindgen]
67extern "C" {
68    #[wasm_bindgen(extends = WritableStream, extends = Object, js_name = WebTransportSendStream, typescript_type = "WebTransportSendStream")]
69    #[derive(Debug, Clone, PartialEq, Eq)]
70    pub type WebTransportSendStream;
71}
72
73// WebTransportOptions bindings
74#[wasm_bindgen]
75extern "C" {
76    #[wasm_bindgen(extends = Object, js_name = WebTransportOptions)]
77    #[derive(Debug, Clone, PartialEq, Eq)]
78    pub type WebTransportOptions;
79}
80
81impl WebTransportOptions {
82    pub fn new() -> Self {
83        #[allow(unused_mut)]
84        let mut ret: Self = JsCast::unchecked_into(Object::new());
85        ret
86    }
87
88    pub fn server_certificate_hashes(&mut self, val: &JsValue) -> &mut Self {
89        let r = ::js_sys::Reflect::set(
90            self.as_ref(),
91            &JsValue::from("serverCertificateHashes"),
92            &JsValue::from(val),
93        );
94        debug_assert!(
95            r.is_ok(),
96            "setting properties should never fail on our dictionary objects"
97        );
98        let _ = r;
99        self
100    }
101}
102
103// WebTransportHash bindings
104#[wasm_bindgen]
105extern "C" {
106    #[wasm_bindgen(extends = Object, js_name = WebTransportHash)]
107    #[derive(Debug, Clone, PartialEq, Eq)]
108    pub type WebTransportHash;
109}
110
111impl WebTransportHash {
112    pub fn new() -> Self {
113        #[allow(unused_mut)]
114        let mut ret: Self = JsCast::unchecked_into(Object::new());
115        ret
116    }
117
118    pub fn algorithm(&mut self, val: &str) -> &mut Self {
119        let r = Reflect::set(
120            self.as_ref(),
121            &JsValue::from("algorithm"),
122            &JsValue::from(val),
123        );
124        debug_assert!(
125            r.is_ok(),
126            "setting properties should never fail on our dictionary objects"
127        );
128        let _ = r;
129        self
130    }
131
132    pub fn value(&mut self, val: &::js_sys::Object) -> &mut Self {
133        let r = Reflect::set(self.as_ref(), &JsValue::from("value"), &JsValue::from(val));
134        debug_assert!(
135            r.is_ok(),
136            "setting properties should never fail on our dictionary objects"
137        );
138        let _ = r;
139        self
140    }
141}