diff --git a/crates/net/Cargo.toml b/crates/net/Cargo.toml index 177d0d77..9f63d3f2 100644 --- a/crates/net/Cargo.toml +++ b/crates/net/Cargo.toml @@ -96,6 +96,7 @@ eventsource = [ 'web-sys/Event', 'web-sys/EventTarget', 'web-sys/EventSource', + 'web-sys/EventSourceInit', 'web-sys/MessageEvent', ] # As of now, only implements `AsyncRead` and `AsyncWrite` on `WebSocket` diff --git a/crates/net/src/eventsource/futures.rs b/crates/net/src/eventsource/futures.rs index daedc576..2bda0ce0 100644 --- a/crates/net/src/eventsource/futures.rs +++ b/crates/net/src/eventsource/futures.rs @@ -86,6 +86,41 @@ impl fmt::Debug for EventSourceSubscription { } } +/// Builder for creating an [`EventSource`] with custom options. +#[derive(Debug, Default, Copy, Clone)] +pub struct EventSourceBuilder { + with_credentials: bool, +} + +impl EventSourceBuilder { + /// Create a new builder with default options. + pub fn new() -> Self { + Self::default() + } + + /// Set whether cross-origin requests should include credentials. + /// + /// When `true`, cookies, authorization headers, or TLS client certificates + /// will be included in cross-origin requests. + /// + /// Default is `false`. + pub fn with_credentials(mut self, value: bool) -> Self { + self.with_credentials = value; + self + } + + /// Build the EventSource and connect to the given URL. + pub fn build(self, url: &str) -> Result { + let mut init = web_sys::EventSourceInit::new(); + init.with_credentials(self.with_credentials); + + let es = web_sys::EventSource::new_with_event_source_init_dict(url, &init) + .map_err(js_to_js_error)?; + + Ok(EventSource { es }) + } +} + impl EventSource { /// Establish an EventSource. /// @@ -96,9 +131,7 @@ impl EventSource { /// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#exceptions_thrown) /// to learn more. pub fn new(url: &str) -> Result { - let es = web_sys::EventSource::new(url).map_err(js_to_js_error)?; - - Ok(Self { es }) + EventSourceBuilder::new().build(url) } /// Subscribes to listening for a specific type of event.