From b0d7a377d01fd0bd341fe362131fd72ad9403461 Mon Sep 17 00:00:00 2001 From: "Enzo \"raskyld\" Nocera" Date: Fri, 11 Oct 2024 23:12:23 +0200 Subject: [PATCH] chore: use `wasi` crate Signed-off-by: Enzo "raskyld" Nocera --- integrations/wasi/Cargo.toml | 1 + integrations/wasi/src/handler.rs | 9 ++-- integrations/wasi/src/lib.rs | 35 +++------------ integrations/wasi/src/request.rs | 73 ++++++++++++++----------------- integrations/wasi/src/response.rs | 2 +- 5 files changed, 46 insertions(+), 74 deletions(-) diff --git a/integrations/wasi/Cargo.toml b/integrations/wasi/Cargo.toml index 0fafd9a249..6a266ffbbf 100644 --- a/integrations/wasi/Cargo.toml +++ b/integrations/wasi/Cargo.toml @@ -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"] } diff --git a/integrations/wasi/src/handler.rs b/integrations/wasi/src/handler.rs index 7b34d6f9c9..4cbfd5cf48 100644 --- a/integrations/wasi/src/handler.rs +++ b/integrations/wasi/src/handler.rs @@ -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, @@ -120,7 +121,7 @@ impl Handler { res_out: ResponseOutparam, ) -> Result { Ok(Self { - req: req.try_into()?, + req: crate::request::Request(req).try_into()?, res_out, server_fn: None, preset_res: None, diff --git a/integrations/wasi/src/lib.rs b/integrations/wasi/src/lib.rs index 5ac679f85b..04a3eb16bf 100644 --- a/integrations/wasi/src/lib.rs +++ b/integrations/wasi/src/lib.rs @@ -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; //! @@ -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; @@ -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 diff --git a/integrations/wasi/src/request.rs b/integrations/wasi/src/request.rs index 06ae28bcdd..30346fcbbb 100644 --- a/integrations/wasi/src/request.rs +++ b/integrations/wasi/src/request.rs @@ -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 for http::Request { +use crate::CHUNK_BYTE_SIZE; + +pub struct Request(pub IncomingRequest); + +impl TryFrom for http::Request { type Error = Error; - fn try_from(req: IncomingRequest) -> Result { + fn try_from(req: Request) -> Result { 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() { @@ -52,8 +51,7 @@ impl TryFrom for http::Request { 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| { @@ -79,32 +77,29 @@ impl TryFrom for http::Request { } } -impl TryFrom for http::Method { - type Error = http::method::InvalidMethod; - - fn try_from(value: Method) -> Result { - 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 { + 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 for http::uri::Scheme { - type Error = InvalidUri; - fn try_from(value: Scheme) -> Result { - 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 { + 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()), } } diff --git a/integrations/wasi/src/response.rs b/integrations/wasi/src/response.rs index 9e96ce0b04..b222372712 100644 --- a/integrations/wasi/src/response.rs +++ b/integrations/wasi/src/response.rs @@ -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