Skip to content

Commit

Permalink
Add Socket.opened implementation (cloudflare#509)
Browse files Browse the repository at this point in the history
* Add opened implementation

* Add binding attribute 'getter'.

* Remove opened option.
  • Loading branch information
Kakapio authored Mar 30, 2024
1 parent 969c14e commit 31727a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
3 changes: 3 additions & 0 deletions worker-sys/src/types/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 11 additions & 18 deletions worker/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,28 @@ use web_sys::{
ReadableStream, ReadableStreamDefaultReader, WritableStream, WritableStreamDefaultWriter,
};

#[derive(Default)]
enum Reading {
#[default]
None,
Pending(JsFuture, ReadableStreamDefaultReader),
Ready(Vec<u8>),
}

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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 31727a1

Please sign in to comment.