From 31727a1f3e7ff0cfac41015d262b74337205cf40 Mon Sep 17 00:00:00 2001 From: Roshan P Date: Sat, 30 Mar 2024 08:46:26 -0400 Subject: [PATCH] Add Socket.opened implementation (#509) * Add opened implementation * Add binding attribute 'getter'. * Remove opened option. --- worker-sys/src/types/socket.rs | 3 +++ worker/src/socket.rs | 29 +++++++++++------------------ 2 files changed, 14 insertions(+), 18 deletions(-) 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)