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, Promise};
6use wasm_bindgen::prelude::*;
7use web_sys::{DomException, Headers, 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 fn get_stats(this: &WebTransport) -> Promise<WebTransportConnectionStats>;
57
58    /// ```webidl
59    /// [NewObject] Promise<Uint8Array> exportKeyingMaterial(BufferSource label, BufferSource context, unsigned long outputLength);
60    /// ```
61    ///
62    /// <https://w3c.github.io/webtransport/#dom-webtransport-exportkeyingmaterial>
63    ///
64    /// Exports keying material from a TLS Keying Material Exporter
65    /// ([RFC9846] Section 7.3) for the TLS session uniquely associated with
66    /// this WebTransport's underlying connection.
67    /// Both `label` and `context` must be at most 255 bytes long.
68    #[wasm_bindgen(method, js_name = exportKeyingMaterial)]
69    pub fn export_keying_material(
70        this: &WebTransport,
71        label: &[u8],
72        context: &[u8],
73        output_length: u32,
74    ) -> Promise<js_sys::Uint8Array>;
75
76    /// ```webidl
77    /// readonly attribute Promise<undefined> ready;
78    /// ```
79    ///
80    /// <https://w3c.github.io/webtransport/#dom-webtransport-ready>
81    ///
82    /// A promise fulfilled when the associated WebTransport session gets
83    /// established, or rejected if the establishment process failed.
84    #[wasm_bindgen(method, getter)]
85    pub fn ready(this: &WebTransport) -> Promise;
86
87    /// ```webidl
88    /// readonly attribute WebTransportReliabilityMode reliability;
89    /// ```
90    ///
91    /// <https://w3c.github.io/webtransport/#dom-webtransport-reliability>
92    ///
93    /// Whether connection supports unreliable (over UDP) transport or only
94    /// reliable (over TCP fallback) transport.
95    /// Returns "pending" until a connection has been established.
96    #[wasm_bindgen(method, getter)]
97    pub fn reliability(this: &WebTransport) -> WebTransportReliabilityMode;
98
99    /// ```webidl
100    /// readonly attribute WebTransportCongestionControl congestionControl;
101    /// ```
102    ///
103    /// <https://w3c.github.io/webtransport/#dom-webtransport-congestioncontrol>
104    ///
105    /// The application’s preference, if requested in the constructor, and
106    /// satisfied by the user agent, for a congestion control algorithm
107    /// optimized for either throughput or low latency
108    /// for sending on this connection.
109    /// If a preference was requested but not satisfied, then the value
110    /// is "default".
111    #[wasm_bindgen(method, getter = congestionControl)]
112    pub fn congestion_control(this: &WebTransport) -> WebTransportCongestionControl;
113
114    /// ```webidl
115    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
116    /// ```
117    ///
118    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
119    ///
120    /// Optionally lets an application specify the number of concurrently open
121    /// incoming unidirectional streams it anticipates the server creating.
122    /// If not null, the user agent SHOULD attempt to reduce future round-trips
123    /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
124    /// consideration in its negotiations with the server.
125    #[wasm_bindgen(method, getter = anticipatedConcurrentIncomingUnidirectionalStreams)]
126    pub fn anticipated_concurrent_incoming_unidirectional_streams(
127        this: &WebTransport,
128    ) -> Option<u16>;
129
130    /// ```webidl
131    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
132    /// ```
133    ///
134    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
135    ///
136    /// Optionally lets an application specify the number of concurrently open
137    /// incoming unidirectional streams it anticipates the server creating.
138    /// If not null, the user agent SHOULD attempt to reduce future round-trips
139    /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
140    /// consideration in its negotiations with the server.
141    #[wasm_bindgen(method, setter = anticipatedConcurrentIncomingUnidirectionalStreams)]
142    pub fn set_option_anticipated_concurrent_incoming_unidirectional_streams(
143        this: &WebTransport,
144        val: Option<u16>,
145    );
146
147    /// ```webidl
148    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
149    /// ```
150    ///
151    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
152    ///
153    /// Optionally lets an application specify the number of concurrently open
154    /// bidirectional streams it anticipates the server creating.
155    /// If not null, the user agent SHOULD attempt to reduce future round-trips
156    /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
157    /// consideration in its negotiations with the server.
158    #[wasm_bindgen(method, getter = anticipatedConcurrentIncomingBidirectionalStreams)]
159    pub fn anticipated_concurrent_incoming_bidirectional_streams(
160        this: &WebTransport,
161    ) -> Option<u16>;
162
163    /// ```webidl
164    /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
165    /// ```
166    ///
167    /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
168    ///
169    /// Optionally lets an application specify the number of concurrently open
170    /// bidirectional streams it anticipates the server creating.
171    /// If not null, the user agent SHOULD attempt to reduce future round-trips
172    /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
173    /// consideration in its negotiations with the server.
174    #[wasm_bindgen(method, setter = anticipatedConcurrentIncomingBidirectionalStreams)]
175    pub fn set_option_anticipated_concurrent_incoming_bidirectional_streams(
176        this: &WebTransport,
177        val: Option<u16>,
178    );
179
180    /// ```webidl
181    /// [SameObject] readonly attribute Headers? responseHeaders;
182    /// ```
183    ///
184    /// <https://w3c.github.io/webtransport/#dom-webtransport-responseheaders>
185    ///
186    /// Once a WebTransport session has been established, holds
187    /// the application-level response headers set by the server, if any.
188    /// Initially null.
189    #[wasm_bindgen(method, getter = responseHeaders)]
190    pub fn response_headers(this: &WebTransport) -> Option<Headers>;
191
192    /// ```webidl
193    /// readonly attribute DOMString protocol;
194    /// ```
195    ///
196    /// <https://w3c.github.io/webtransport/#dom-webtransport-protocol>
197    ///
198    /// Once a WebTransport session has been established and the `protocols`
199    /// constructor option was used to provide a non-empty array, returns
200    /// the application-level protocol selected by the server, if any.
201    /// Otherwise, an empty string.
202    #[wasm_bindgen(method, getter)]
203    pub fn protocol(this: &WebTransport) -> js_sys::JsString;
204
205    // =====
206
207    /// ```webidl
208    /// readonly attribute Promise<WebTransportCloseInfo> closed;
209    /// ```
210    ///
211    /// <https://w3c.github.io/webtransport/#dom-webtransport-closed>
212    ///
213    /// A promise fulfilled when the associated WebTransport object is closed
214    /// gracefully, or rejected when it is closed abruptly or
215    /// failed on initialization.
216    #[wasm_bindgen(method, getter)]
217    pub fn closed(this: &WebTransport) -> Promise<WebTransportCloseInfo>;
218
219    /// ```webidl
220    /// readonly attribute Promise<undefined> draining;
221    /// ```
222    ///
223    /// <https://w3c.github.io/webtransport/#dom-webtransport-draining>
224    ///
225    /// A promise fulfilled when the associated WebTransport session receives
226    /// a DRAIN_WEBTRANSPORT_SESSION capsule or a GOAWAY frame.
227    #[wasm_bindgen(method, getter)]
228    pub fn draining(this: &WebTransport) -> Promise;
229
230    /// ```webidl
231    /// undefined close(optional WebTransportCloseInfo closeInfo = {});
232    /// ```
233    ///
234    /// <https://w3c.github.io/webtransport/#dom-webtransport-close>
235    #[wasm_bindgen(method)]
236    pub fn close(this: &WebTransport);
237
238    /// ```webidl
239    /// undefined close(optional WebTransportCloseInfo closeInfo = {});
240    /// ```
241    ///
242    /// <https://w3c.github.io/webtransport/#dom-webtransport-close>
243    #[wasm_bindgen(method, js_name = close)]
244    pub fn close_with_info(this: &WebTransport, close_info: &WebTransportCloseInfo);
245
246    // =====
247
248    /// ```webidl
249    /// readonly attribute WebTransportDatagramDuplexStream datagrams;
250    /// ```
251    ///
252    /// <https://w3c.github.io/webtransport/#dom-webtransport-datagrams>
253    ///
254    /// A single duplex stream for sending and receiving datagrams over this
255    /// session.
256    #[wasm_bindgen(method, getter)]
257    pub fn datagrams(this: &WebTransport) -> WebTransportDatagramDuplexStream;
258
259    // =====
260
261    /// ```webidl
262    /// Promise<WebTransportBidirectionalStream> createBidirectionalStream(
263    ///  optional WebTransportSendStreamOptions options = {});
264    /// ```
265    ///
266    /// <https://w3c.github.io/webtransport/#dom-webtransport-createbidirectionalstream>
267    ///
268    /// Creates a [`WebTransportBidirectionalStream`] object for an outgoing
269    /// bidirectional stream.
270    /// Note that the mere creation of a stream is not immediately visible
271    /// to the peer until it is used to send data.
272    #[wasm_bindgen(method, js_name = createBidirectionalStream)]
273    pub fn create_bidirectional_stream(
274        this: &WebTransport,
275    ) -> Promise<WebTransportBidirectionalStream>;
276
277    /// ```webidl
278    /// Promise<WebTransportBidirectionalStream> createBidirectionalStream(
279    ///  optional WebTransportSendStreamOptions options = {});
280    /// ```
281    ///
282    /// <https://w3c.github.io/webtransport/#dom-webtransport-createbidirectionalstream>
283    ///
284    /// Creates a [`WebTransportBidirectionalStream`] object for an outgoing
285    /// bidirectional stream.
286    /// Note that the mere creation of a stream is not immediately visible
287    /// to the peer until it is used to send data.
288    #[wasm_bindgen(method, js_name = createBidirectionalStream)]
289    pub fn create_bidirectional_stream_with_options(
290        this: &WebTransport,
291        options: WebTransportSendStreamOptions,
292    ) -> Promise<WebTransportBidirectionalStream>;
293
294    /// ```webidl
295    /// /* a ReadableStream of WebTransportBidirectionalStream objects */
296    /// readonly attribute ReadableStream incomingBidirectionalStreams;
297    /// ```
298    ///
299    /// <https://w3c.github.io/webtransport/#dom-webtransport-incomingbidirectionalstreams>
300    ///
301    /// Returns a [`ReadableStream`] of [`WebTransportBidirectionalStreams`]
302    /// that have been received from the server.
303    #[wasm_bindgen(method, getter = incomingBidirectionalStreams)]
304    pub fn incoming_bidirectional_streams(this: &WebTransport) -> ReadableStream;
305
306    // =====
307
308    /// ```webidl
309    /// Promise<WebTransportSendStream> createUnidirectionalStream(
310    ///   optional WebTransportSendStreamOptions options = {});
311    /// ```
312    ///
313    /// <https://w3c.github.io/webtransport/#dom-webtransport-createunidirectionalstream>
314    ///
315    /// Creates a [`WebTransportSendStream`] for an outgoing unidirectional
316    /// stream.
317    /// Note that the mere creation of a stream is not immediately visible
318    /// to the server until it is used to send data.
319    #[wasm_bindgen(method, js_name = createUnidirectionalStream)]
320    pub fn create_unidirectional_stream(this: &WebTransport) -> Promise<WebTransportSendStream>;
321
322    /// ```webidl
323    /// Promise<WebTransportSendStream> createUnidirectionalStream(
324    ///   optional WebTransportSendStreamOptions options = {});
325    /// ```
326    ///
327    /// <https://w3c.github.io/webtransport/#dom-webtransport-createunidirectionalstream>
328    ///
329    /// Creates a [`WebTransportSendStream`] for an outgoing unidirectional
330    /// stream.
331    /// Note that the mere creation of a stream is not immediately visible
332    /// to the server until it is used to send data.
333    #[wasm_bindgen(method, js_name = createUnidirectionalStream)]
334    pub fn create_unidirectional_stream_with_options(
335        this: &WebTransport,
336        options: WebTransportSendStreamOptions,
337    ) -> Promise<WebTransportSendStream>;
338
339    /// ```webidl
340    /// /* a ReadableStream of WebTransportReceiveStream objects */
341    /// readonly attribute ReadableStream incomingUnidirectionalStreams;
342    /// ```
343    ///
344    /// <https://w3c.github.io/webtransport/#dom-webtransport-incomingunidirectionalstreams>
345    ///
346    /// A [`ReadableStream`] of unidirectional streams, each represented by
347    /// a [`WebTransportReceiveStream`], that have been received
348    /// from the server.
349    #[wasm_bindgen(method, getter = incomingUnidirectionalStreams)]
350    pub fn incoming_unidirectional_streams(this: &WebTransport) -> ReadableStream;
351
352    /// ```webidl
353    /// WebTransportSendGroup createSendGroup();
354    /// ```
355    ///
356    /// <https://w3c.github.io/webtransport/#dom-webtransport-createsendgroup>
357    ///
358    /// Creates a [`WebTransportSendGroup`].
359    #[wasm_bindgen(method, js_name = createSendGroup, catch)]
360    pub fn create_send_group(this: &WebTransport) -> Result<WebTransportSendGroup, DomException>;
361
362    // =====
363
364    /// ```webidl
365    /// static readonly attribute boolean supportsReliableOnly;
366    /// ```
367    ///
368    /// <https://w3c.github.io/webtransport/#dom-webtransport-supportsreliableonly>
369    ///
370    /// Returns true if the user agent supports WebTransport sessions over
371    /// exclusively reliable connections, otherwise false.
372    #[wasm_bindgen(static_method_of = WebTransport, js_name = supportsReliableOnly)]
373    pub fn supports_reliable_only() -> bool;
374}
375
376impl WebTransport {
377    crate::set_option_accessors! {
378        /// ```webidl
379        /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingUnidirectionalStreams;
380        /// ```
381        ///
382        /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingunidirectionalstreams>
383        ///
384        /// Optionally lets an application specify the number of concurrently open
385        /// incoming unidirectional streams it anticipates the server creating.
386        /// If not null, the user agent SHOULD attempt to reduce future round-trips
387        /// by taking `[[AnticipatedConcurrentIncomingUnidirectionalStreams]]` into
388        /// consideration in its negotiations with the server.
389        anticipated_concurrent_incoming_unidirectional_streams: u16
390    }
391
392    crate::set_option_accessors! {
393        /// ```webidl
394        /// [EnforceRange] attribute unsigned short? anticipatedConcurrentIncomingBidirectionalStreams;
395        /// ```
396        ///
397        /// <https://w3c.github.io/webtransport/#dom-webtransport-anticipatedconcurrentincomingbidirectionalstreams>
398        ///
399        /// Optionally lets an application specify the number of concurrently open
400        /// bidirectional streams it anticipates the server creating.
401        /// If not null, the user agent SHOULD attempt to reduce future round-trips
402        /// by taking `[[AnticipatedConcurrentIncomingBidirectionalStreams]]` into
403        /// consideration in its negotiations with the server.
404        anticipated_concurrent_incoming_bidirectional_streams: u16
405    }
406}