Skip to content

Commit

Permalink
chore: use wasi crate
Browse files Browse the repository at this point in the history
Signed-off-by: Enzo "raskyld" Nocera <[email protected]>
  • Loading branch information
raskyld committed Oct 11, 2024
1 parent 6db1637 commit b0d7a37
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 74 deletions.
1 change: 1 addition & 0 deletions integrations/wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ any_spawner = { workspace = true, features = ["futures-executor"] }
throw_error = { workspace = true }
hydration_context = { workspace = true }
futures = "0.3.30"
wasi = "0.13.1+wasi-0.2.0"
leptos = { workspace = true, features = ["nonce", "ssr"] }
leptos_meta = { workspace = true, features = ["ssr"] }
leptos_router = { workspace = true, features = ["ssr"] }
Expand Down
9 changes: 5 additions & 4 deletions integrations/wasi/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ use routefinder::Router;
use server_fn::middleware::Service;
use throw_error::Error;

use wasi::http::types::{
IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
};

use crate::{
bindings::wasi::http::types::{
IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
},
response::{Body, Response, ResponseOptions},
utils::redirect,
CHUNK_BYTE_SIZE,
Expand Down Expand Up @@ -120,7 +121,7 @@ impl Handler {
res_out: ResponseOutparam,
) -> Result<Self, Error> {
Ok(Self {
req: req.try_into()?,
req: crate::request::Request(req).try_into()?,
res_out,
server_fn: None,
preset_res: None,
Expand Down
35 changes: 5 additions & 30 deletions integrations/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! that you can leverage to use this crate.
//!
//! ```
//! use leptos_wasi::{bindings::exports::wasi::http::incoming_handler::Guest, prelude::{IncomingRequest, ResponseOutparam}};
//! use wasi::exports::http::incoming_handler::*;
//!
//! struct LeptosServer;
//!
Expand All @@ -36,32 +36,7 @@
//!
//! # WASI Bindings
//!
//! You are free to use any WIT imports and export any WIT exports but at the moment,
//! when interacting with this crate, you must use the types that you can find in
//! this crate [`bindings`].
//!
//! You then need to export your implementation using:
//!
//! ```
//! export!(LeptosServer with_types_in leptos_wasi::bindings);
//! ```
//!
//! If you want to use your own bindings for `wasi:http`,
//! then you need to implement `From` traits
//! to convert your own bindings into the one in [`bindings`].
//! Please, note that it will likely implies doing `unsafe`
//! operations to wrap the resource's `handle() -> u64` in
//! another type.

#[allow(warnings)]
pub mod bindings {
wit_bindgen::generate!({
path: "wit",
pub_export_macro: true,
world: "http",
generate_all,
});
}
//! We are using the bindings provided by the `wasi` crate.

pub mod handler;
pub mod request;
Expand All @@ -70,12 +45,12 @@ pub mod utils;

#[allow(clippy::pub_use)]
pub mod prelude {
pub use crate::bindings::exports::wasi::http::incoming_handler::{
IncomingRequest, ResponseOutparam,
};
pub use crate::handler::Handler;
pub use crate::response::Body;
pub use crate::utils::redirect;
pub use wasi::exports::wasi::http::incoming_handler::{
IncomingRequest, ResponseOutparam,
};
}

/// When working with streams, this crate will try to chunk bytes with
Expand Down
73 changes: 34 additions & 39 deletions integrations/wasi/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use bytes::Bytes;
use http::{
uri::{InvalidUri, Parts},
Uri,
};
use http::{uri::Parts, Uri};
use throw_error::Error;

use crate::{
bindings::wasi::{
http::types::{IncomingBody, IncomingRequest, Method, Scheme},
io::streams::StreamError,
},
CHUNK_BYTE_SIZE,
use wasi::{
http::types::{IncomingBody, IncomingRequest, Method, Scheme},
io::streams::StreamError,
};

impl TryFrom<IncomingRequest> for http::Request<Bytes> {
use crate::CHUNK_BYTE_SIZE;

pub struct Request(pub IncomingRequest);

impl TryFrom<Request> for http::Request<Bytes> {
type Error = Error;

fn try_from(req: IncomingRequest) -> Result<Self, Self::Error> {
fn try_from(req: Request) -> Result<Self, Self::Error> {
let mut builder = http::Request::builder();
let req_method = req.method();
let req = req.0;
let req_method = method_wasi_to_http(req.method())?;
let headers = req.headers();

for (header_name, header_value) in headers.entries() {
Expand Down Expand Up @@ -52,8 +51,7 @@ impl TryFrom<IncomingRequest> for http::Request<Bytes> {

let mut uri_parts = Parts::default();

uri_parts.scheme =
req.scheme().map(http::uri::Scheme::try_from).transpose()?;
uri_parts.scheme = req.scheme().map(scheme_wasi_to_http).transpose()?;
uri_parts.authority = req
.authority()
.map(|aut| {
Expand All @@ -79,32 +77,29 @@ impl TryFrom<IncomingRequest> for http::Request<Bytes> {
}
}

impl TryFrom<Method> for http::Method {
type Error = http::method::InvalidMethod;

fn try_from(value: Method) -> Result<Self, Self::Error> {
match value {
Method::Connect => Ok(Self::CONNECT),
Method::Delete => Ok(Self::DELETE),
Method::Get => Ok(Self::GET),
Method::Head => Ok(Self::HEAD),
Method::Options => Ok(Self::OPTIONS),
Method::Patch => Ok(Self::PATCH),
Method::Post => Ok(Self::POST),
Method::Put => Ok(Self::PUT),
Method::Trace => Ok(Self::TRACE),
Method::Other(mtd) => Self::from_bytes(mtd.as_bytes()),
}
pub fn method_wasi_to_http(
value: Method,
) -> Result<http::Method, http::method::InvalidMethod> {
match value {
Method::Connect => Ok(http::Method::CONNECT),
Method::Delete => Ok(http::Method::DELETE),
Method::Get => Ok(http::Method::GET),
Method::Head => Ok(http::Method::HEAD),
Method::Options => Ok(http::Method::OPTIONS),
Method::Patch => Ok(http::Method::PATCH),
Method::Post => Ok(http::Method::POST),
Method::Put => Ok(http::Method::PUT),
Method::Trace => Ok(http::Method::TRACE),
Method::Other(mtd) => http::Method::from_bytes(mtd.as_bytes()),
}
}

impl TryFrom<Scheme> for http::uri::Scheme {
type Error = InvalidUri;
fn try_from(value: Scheme) -> Result<Self, Self::Error> {
match value {
Scheme::Http => Ok(Self::HTTP),
Scheme::Https => Ok(Self::HTTPS),
Scheme::Other(oth) => Self::try_from(oth.as_bytes()),
}
pub fn scheme_wasi_to_http(
value: Scheme,
) -> Result<http::uri::Scheme, http::uri::InvalidUri> {
match value {
Scheme::Http => Ok(http::uri::Scheme::HTTP),
Scheme::Https => Ok(http::uri::Scheme::HTTPS),
Scheme::Other(oth) => http::uri::Scheme::try_from(oth.as_bytes()),
}
}
2 changes: 1 addition & 1 deletion integrations/wasi/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use parking_lot::RwLock;
use server_fn::response::generic::Body as ServerFnBody;
use throw_error::Error;

use crate::bindings::wasi::http::types::Headers;
use wasi::http::types::Headers;

/// This crate uses platform-agnostic [`http::Response`]
/// with a custom [`Body`] and convert them under the hood to
Expand Down

0 comments on commit b0d7a37

Please sign in to comment.