diff --git a/worker-sys/src/types/socket.rs b/worker-sys/src/types/socket.rs index 2c0c75a3..78622bb6 100644 --- a/worker-sys/src/types/socket.rs +++ b/worker-sys/src/types/socket.rs @@ -18,6 +18,9 @@ extern "C" { #[wasm_bindgen(method)] pub fn closed(this: &Socket) -> js_sys::Promise; + #[wasm_bindgen(method)] + pub fn opened(this: &Socket) -> js_sys::Promise; + #[wasm_bindgen(method, js_name=startTls)] pub fn start_tls(this: &Socket) -> Socket; diff --git a/worker/src/socket.rs b/worker/src/socket.rs index 99f43a20..5bbfaaaf 100644 --- a/worker/src/socket.rs +++ b/worker/src/socket.rs @@ -20,40 +20,28 @@ use web_sys::{ ReadableStream, ReadableStreamDefaultReader, WritableStream, WritableStreamDefaultWriter, }; +#[derive(Default)] enum Reading { + #[default] None, Pending(JsFuture, ReadableStreamDefaultReader), Ready(Vec), } -impl Default for Reading { - fn default() -> Self { - Self::None - } -} - +#[derive(Default)] enum Writing { Pending(JsFuture, WritableStreamDefaultWriter, usize), + #[default] None, } -impl Default for Writing { - fn default() -> Self { - Self::None - } -} - +#[derive(Default)] enum Closing { Pending(JsFuture), + #[default] None, } -impl Default for Closing { - fn default() -> Self { - Self::None - } -} - /// Represents an outbound TCP connection from your Worker. pub struct Socket { inner: worker_sys::Socket, @@ -95,6 +83,11 @@ impl Socket { Ok(()) } + pub async fn opened(&self) -> Result<()> { + JsFuture::from(self.inner.opened()).await?; + Ok(()) + } + /// Upgrades an insecure socket to a secure one that uses TLS, /// returning a new Socket. Note that in order to call this method, /// you must set [`secure_transport`](SocketOptions::secure_transport)