Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outbound TCP Sockets #324

Merged
merged 11 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -6,6 +6,7 @@ members = [
"worker-sandbox",
"worker-sys",
]
resolver = "2"

[profile.release]
# rustc supports two "optimize for size" levels: opt-level = "s" and "z".
Expand Down
1 change: 1 addition & 0 deletions worker-build/src/main.rs
Expand Up @@ -134,6 +134,7 @@ fn bundle(esbuild_path: &Path) -> Result<()> {
let mut command = Command::new(esbuild_path);
command.args([
"--external:./index.wasm",
"--external:cloudflare:sockets",
"--format=esm",
"--bundle",
"./shim.js",
Expand Down
2 changes: 2 additions & 0 deletions worker-sys/src/types.rs
Expand Up @@ -8,6 +8,7 @@ mod incoming_request_cf_properties;
mod queue;
mod r2;
mod schedule;
mod socket;
mod tls_client_auth;
mod websocket_pair;

Expand All @@ -21,5 +22,6 @@ pub use incoming_request_cf_properties::*;
pub use queue::*;
pub use r2::*;
pub use schedule::*;
pub use socket::*;
pub use tls_client_auth::*;
pub use websocket_pair::*;
29 changes: 29 additions & 0 deletions worker-sys/src/types/socket.rs
@@ -0,0 +1,29 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "cloudflare:sockets")]
extern "C" {
#[wasm_bindgen]
pub fn connect(address: JsValue, options: JsValue) -> Socket;
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub type Socket;

#[wasm_bindgen(method)]
pub fn close(this: &Socket) -> js_sys::Promise;

#[wasm_bindgen(method)]
pub fn closed(this: &Socket) -> js_sys::Promise;

#[wasm_bindgen(method, js_name=startTls)]
pub fn start_tls(this: &Socket) -> Socket;

#[wasm_bindgen(method, getter)]
pub fn readable(this: &Socket) -> web_sys::ReadableStream;

#[wasm_bindgen(method, getter)]
pub fn writable(this: &Socket) -> web_sys::WritableStream;
}
5 changes: 4 additions & 1 deletion worker/Cargo.toml
Expand Up @@ -23,6 +23,7 @@ matchit = "0.4.2"
pin-project = "1.0.12"
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
tokio = { version = "1.0", default-features = false }
url = "2.2.2"
wasm-bindgen = "=0.2.84"
wasm-bindgen-futures = "0.4.30"
Expand All @@ -36,7 +37,9 @@ worker-sys = { path = "../worker-sys", version = "0.0.8" }
version = "0.3.57"
features = [
"File",
"WorkerGlobalScope"
"WorkerGlobalScope",
"ReadableStreamDefaultReader",
"WritableStreamDefaultWriter"
]

[features]
Expand Down
8 changes: 8 additions & 0 deletions worker/src/error.rs
Expand Up @@ -9,6 +9,7 @@ pub enum Error {
Json((String, u16)),
JsError(String),
Internal(JsValue),
Io(std::io::Error),
BindingError(String),
RouteInsertError(matchit::InsertError),
RouteNoDataError,
Expand Down Expand Up @@ -48,6 +49,7 @@ impl std::fmt::Display for Error {
write!(f, "{s}")
}
Error::Internal(_) => write!(f, "unrecognized JavaScript object"),
Error::Io(e) => write!(f, "IO Error: {e}"),
Error::BindingError(name) => write!(f, "no binding found for `{name}`"),
Error::RouteInsertError(e) => write!(f, "failed to insert route: {e}"),
Error::RouteNoDataError => write!(f, "route has no corresponding shared data"),
Expand All @@ -72,6 +74,12 @@ impl From<JsValue> for Error {
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Io(error)
}
}

impl From<Error> for JsValue {
fn from(e: Error) -> Self {
JsValue::from_str(&e.to_string())
Expand Down
2 changes: 2 additions & 0 deletions worker/src/lib.rs
Expand Up @@ -44,6 +44,7 @@ pub use crate::request_init::*;
pub use crate::response::{Response, ResponseBody};
pub use crate::router::{RouteContext, RouteParams, Router};
pub use crate::schedule::*;
pub use crate::socket::*;
pub use crate::streams::*;
pub use crate::websocket::*;

Expand Down Expand Up @@ -71,6 +72,7 @@ mod request_init;
mod response;
mod router;
mod schedule;
mod socket;
mod streams;
mod websocket;

Expand Down