Skip to main content

web_wt_sys/webtransport/
web_transport.rs

1//! [`WebTransport`]
2//!
3//! <https://w3c.github.io/webtransport/#webtransport>
4
5use js_sys::Object;
6use wasm_bindgen::prelude::*;
7use web_sys::{DomException, ReadableStream};
8
9use super::*;
10
11#[wasm_bindgen]
12extern "C" {
13    /// The `WebTransport` interface.
14    ///
15    /// <https://w3c.github.io/webtransport/#webtransport>
16    #[wasm_bindgen(extends = Object, typescript_type = "WebTransport")]
17    #[derive(Debug, Clone, PartialEq, Eq)]
18    pub type WebTransport;
19
20    /// ```webidl
21    /// constructor(USVString url, optional WebTransportOptions options = {});
22    /// ```
23    ///
24    /// <https://w3c.github.io/webtransport/#dom-webtransport-webtransport>
25    ///
26    /// The `new WebTransport(url)` constructor, creating
27    /// a new instance of`WebTransport`.
28    #[wasm_bindgen(constructor, catch)]
29    pub fn new(url: &str) -> Result<WebTransport, DomException>;
30
31    /// ```webidl
32    /// constructor(USVString url, optional WebTransportOptions options = {});
33    /// ```
34    ///
35    /// <https://w3c.github.io/webtransport/#dom-webtransport-webtransport>
36    ///
37    /// The `new WebTransport(url, { ... })` constructor, creating
38    /// a new instance of `WebTransport`.
39    #[wasm_bindgen(constructor, catch)]
40    pub fn new_with_options(
41        url: &str,
42        options: &WebTransportOptions,
43    ) -> Result<WebTransport, DomException>;
44
45    // =====
46
47    /// ```webidl
48    /// Promise<WebTransportConnectionStats> getStats();
49    /// ```
50    ///
51    /// <https://w3c.github.io/webtransport/#dom-webtransport-getstats>
52    ///
53    /// Gathers stats for this WebTransport's underlying connection and reports
54    /// the result asynchronously.
55    #[wasm_bindgen(method, js_name = getStats)]
56    pub async fn get_stats(this: &WebTransport) -> WebTransportConnectionStats;
57
58    /// ```webidl
59    /// readonly attribute Promise<undefined> ready;
60    /// ```
61    ///
62    /// <https://w3c.github.io/webtransport/#dom-webtransport-ready>
63    ///
64    /// A promise fulfilled when the associated WebTransport session gets
65    /// established, or rejected if the establishment process failed.
66    #[wasm_bindgen(method, getter, catch)]
67    pub async fn ready(this: &WebTransport) -> Result<(), DomException>;
68
69    /// ```webidl
70    /// readonly attribute WebTransportReliabilityMode reliability;
71    /// ```
72    ///
73    /// <https://w3c.github.io/webtransport/#dom-webtransport-reliability>
74    ///
75    /// Whether connection supports unreliable (over UDP) transport or only
76    /// reliable (over TCP fallback) transport.
77    /// Returns "pending" until a connection has been established.
78    #[wasm_bindgen(method, getter)]
79    pub fn reliability(this: &WebTransport) -> WebTransportReliabilityMode;
80
81    /// ```webidl
82    /// readonly attribute WebTransportCongestionControl congestionControl;
83    /// ```
84    ///
85    /// <https://w3c.github.io/webtransport/#dom-webtransport-congestioncontrol>
86    ///
87    /// The application’s preference, if requested in the constructor, and
88    /// satisfied by the user agent, for a congestion control algorithm
89    /// optimized for either throughput or low latency
90    /// for sending on this connection.
91    /// If a preference was requested but not satisfied, then the value
92    /// is "default".
93    #[wasm_bindgen(method, getter = congestionControl)]
94    pub fn congestion_control(this: &WebTransport) -> WebTransportCongestionControl;
95
96    /// ```webidl
97    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
98    /// ```
99    ///
100    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
101    ///
102    /// Optionally lets an application specify the number of concurrently open
103    /// incoming unidirectional streams it anticipates the server creating.
104    /// If not null, the user agent SHOULD attempt to reduce future round-trips
105    /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
106    /// consideration in its negotiations with the server.
107    #[wasm_bindgen(method, getter = anticipatedConcurrentIncomingUnidirectionalStreams)]
108    pub fn anticipated_concurrent_incoming_unidirectional_streams(
109        this: &WebTransport,
110    ) -> Option<u16>;
111
112    /// ```webidl
113    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
114    /// ```
115    ///
116    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
117    ///
118    /// Optionally lets an application specify the number of concurrently open
119    /// incoming unidirectional streams it anticipates the server creating.
120    /// If not null, the user agent SHOULD attempt to reduce future round-trips
121    /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
122    /// consideration in its negotiations with the server.
123    #[wasm_bindgen(method, setter = anticipatedConcurrentIncomingUnidirectionalStreams)]
124    pub fn set_option_anticipated_concurrent_incoming_unidirectional_streams(
125        this: &WebTransport,
126        val: Option<u16>,
127    );
128
129    /// ```webidl
130    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
131    /// ```
132    ///
133    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
134    ///
135    /// Optionally lets an application specify the number of concurrently open
136    /// bidirectional streams it anticipates the server creating.
137    /// If not null, the user agent SHOULD attempt to reduce future round-trips
138    /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
139    /// consideration in its negotiations with the server.
140    #[wasm_bindgen(method, getter = anticipatedConcurrentIncomingBidirectionalStreams)]
141    pub fn anticipated_concurrent_incoming_bidirectional_streams(
142        this: &WebTransport,
143    ) -> Option<u16>;
144
145    /// ```webidl
146    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
147    /// ```
148    ///
149    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
150    ///
151    /// Optionally lets an application specify the number of concurrently open
152    /// bidirectional streams it anticipates the server creating.
153    /// If not null, the user agent SHOULD attempt to reduce future round-trips
154    /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
155    /// consideration in its negotiations with the server.
156    #[wasm_bindgen(method, setter = anticipatedConcurrentIncomingBidirectionalStreams)]
157    pub fn set_option_anticipated_concurrent_incoming_bidirectional_streams(
158        this: &WebTransport,
159        val: Option<u16>,
160    );
161
162    // =====
163
164    /// ```webidl
165    /// readonly attribute Promise<WebTransportCloseInfo> closed;
166    /// ```
167    ///
168    /// <https://w3c.github.io/webtransport/#dom-webtransport-closed>
169    ///
170    /// A promise fulfilled when the associated WebTransport object is closed
171    /// gracefully, or rejected when it is closed abruptly or
172    /// failed on initialization.
173    #[wasm_bindgen(method, getter, catch)]
174    pub async fn closed(this: &WebTransport) -> Result<WebTransportCloseInfo, DomException>;
175
176    /// ```webidl
177    /// readonly attribute Promise<undefined> draining;
178    /// ```
179    ///
180    /// <https://w3c.github.io/webtransport/#dom-webtransport-draining>
181    ///
182    /// A promise fulfilled when the associated WebTransport session receives
183    /// a DRAIN_WEBTRANSPORT_SESSION capsule or a GOAWAY frame.
184    #[wasm_bindgen(method, getter)]
185    pub async fn draining(this: &WebTransport);
186
187    /// ```webidl
188    /// undefined close(optional WebTransportCloseInfo closeInfo = {});
189    /// ```
190    ///
191    /// <https://w3c.github.io/webtransport/#dom-webtransport-close>
192    #[wasm_bindgen(method)]
193    pub fn close(this: &WebTransport);
194
195    /// ```webidl
196    /// undefined close(optional WebTransportCloseInfo closeInfo = {});
197    /// ```
198    ///
199    /// <https://w3c.github.io/webtransport/#dom-webtransport-close>
200    #[wasm_bindgen(method, js_name = close)]
201    pub fn close_with_info(this: &WebTransport, close_info: &WebTransportCloseInfo);
202
203    // =====
204
205    /// ```webidl
206    /// readonly attribute WebTransportDatagramDuplexStream datagrams;
207    /// ```
208    ///
209    /// <https://w3c.github.io/webtransport/#dom-webtransport-datagrams>
210    ///
211    /// A single duplex stream for sending and receiving datagrams over this
212    /// session.
213    #[wasm_bindgen(method, getter)]
214    pub fn datagrams(this: &WebTransport) -> WebTransportDatagramDuplexStream;
215
216    // =====
217
218    /// ```webidl
219    /// Promise<WebTransportBidirectionalStream> createBidirectionalStream(
220    ///  optional WebTransportSendStreamOptions options = {});
221    /// ```
222    ///
223    /// <https://w3c.github.io/webtransport/#dom-webtransport-createbidirectionalstream>
224    ///
225    /// Creates a [`WebTransportBidirectionalStream`] object for an outgoing
226    /// bidirectional stream.
227    /// Note that the mere creation of a stream is not immediately visible
228    /// to the peer until it is used to send data.
229    #[wasm_bindgen(method, js_name = createBidirectionalStream, catch)]
230    pub async fn create_bidirectional_stream(
231        this: &WebTransport,
232    ) -> Result<WebTransportBidirectionalStream, DomException>;
233
234    /// ```webidl
235    /// Promise<WebTransportBidirectionalStream> createBidirectionalStream(
236    ///  optional WebTransportSendStreamOptions options = {});
237    /// ```
238    ///
239    /// <https://w3c.github.io/webtransport/#dom-webtransport-createbidirectionalstream>
240    ///
241    /// Creates a [`WebTransportBidirectionalStream`] object for an outgoing
242    /// bidirectional stream.
243    /// Note that the mere creation of a stream is not immediately visible
244    /// to the peer until it is used to send data.
245    #[wasm_bindgen(method, js_name = createBidirectionalStream, catch)]
246    pub async fn create_bidirectional_stream_with_options(
247        this: &WebTransport,
248        options: WebTransportSendStreamOptions,
249    ) -> Result<WebTransportBidirectionalStream, DomException>;
250
251    /// ```webidl
252    /// /* a ReadableStream of WebTransportBidirectionalStream objects */
253    /// readonly attribute ReadableStream incomingBidirectionalStreams;
254    /// ```
255    ///
256    /// <https://w3c.github.io/webtransport/#dom-webtransport-incomingbidirectionalstreams>
257    ///
258    /// Returns a [`ReadableStream`] of [`WebTransportBidirectionalStreams`]
259    /// that have been received from the server.
260    #[wasm_bindgen(method, getter = incomingBidirectionalStreams)]
261    pub fn incoming_bidirectional_streams(this: &WebTransport) -> ReadableStream;
262
263    // =====
264
265    /// ```webidl
266    /// Promise<WebTransportSendStream> createUnidirectionalStream(
267    ///   optional WebTransportSendStreamOptions options = {});
268    /// ```
269    ///
270    /// <https://w3c.github.io/webtransport/#dom-webtransport-createunidirectionalstream>
271    ///
272    /// Creates a [`WebTransportSendStream`] for an outgoing unidirectional
273    /// stream.
274    /// Note that the mere creation of a stream is not immediately visible
275    /// to the server until it is used to send data.
276    #[wasm_bindgen(method, js_name = createUnidirectionalStream, catch)]
277    pub async fn create_unidirectional_stream(
278        this: &WebTransport,
279    ) -> Result<WebTransportSendStream, DomException>;
280
281    /// ```webidl
282    /// Promise<WebTransportSendStream> createUnidirectionalStream(
283    ///   optional WebTransportSendStreamOptions options = {});
284    /// ```
285    ///
286    /// <https://w3c.github.io/webtransport/#dom-webtransport-createunidirectionalstream>
287    ///
288    /// Creates a [`WebTransportSendStream`] for an outgoing unidirectional
289    /// stream.
290    /// Note that the mere creation of a stream is not immediately visible
291    /// to the server until it is used to send data.
292    #[wasm_bindgen(method, js_name = createUnidirectionalStream, catch)]
293    pub async fn create_unidirectional_stream_with_options(
294        this: &WebTransport,
295        options: WebTransportSendStreamOptions,
296    ) -> Result<WebTransportSendStream, DomException>;
297
298    /// ```webidl
299    /// /* a ReadableStream of WebTransportReceiveStream objects */
300    /// readonly attribute ReadableStream incomingUnidirectionalStreams;
301    /// ```
302    ///
303    /// <https://w3c.github.io/webtransport/#dom-webtransport-incomingunidirectionalstreams>
304    ///
305    /// A [`ReadableStream`] of unidirectional streams, each represented by
306    /// a [`WebTransportReceiveStream`], that have been received
307    /// from the server.
308    #[wasm_bindgen(method, getter = incomingUnidirectionalStreams)]
309    pub fn incoming_unidirectional_streams(this: &WebTransport) -> ReadableStream;
310
311    /// ```webidl
312    /// WebTransportSendGroup createSendGroup();
313    /// ```
314    ///
315    /// <https://w3c.github.io/webtransport/#dom-webtransport-createsendgroup>
316    ///
317    /// Creates a [`WebTransportSendGroup`].
318    #[wasm_bindgen(method, js_name = createSendGroup, catch)]
319    pub fn create_send_group(this: &WebTransport) -> Result<WebTransportSendGroup, DomException>;
320
321    // =====
322
323    /// ```webidl
324    /// static readonly attribute boolean supportsReliableOnly;
325    /// ```
326    ///
327    /// <https://w3c.github.io/webtransport/#dom-webtransport-supportsreliableonly>
328    ///
329    /// Returns true if the user agent supports WebTransport sessions over
330    /// exclusively reliable connections, otherwise false.
331    #[wasm_bindgen(static_method_of = WebTransport, js_name = supportsReliableOnly)]
332    pub fn supports_reliable_only() -> bool;
333}
334
335impl WebTransport {
336    crate::set_option_accessors! {
337        /// ```webidl
338        /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
339        /// ```
340        ///
341        /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
342        ///
343        /// Optionally lets an application specify the number of concurrently open
344        /// incoming unidirectional streams it anticipates the server creating.
345        /// If not null, the user agent SHOULD attempt to reduce future round-trips
346        /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
347        /// consideration in its negotiations with the server.
348        anticipated_concurrent_incoming_unidirectional_streams: u16
349    }
350
351    crate::set_option_accessors! {
352        /// ```webidl
353        /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
354        /// ```
355        ///
356        /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
357        ///
358        /// Optionally lets an application specify the number of concurrently open
359        /// bidirectional streams it anticipates the server creating.
360        /// If not null, the user agent SHOULD attempt to reduce future round-trips
361        /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
362        /// consideration in its negotiations with the server.
363        anticipated_concurrent_incoming_bidirectional_streams: u16
364    }
365}