Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
351 changes: 174 additions & 177 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ name = "hyperlight-wasm-http-example"
edition = "2024"

[dependencies]
hyperlight-component-macro = { version = "0.9.0" }
hyperlight-common = { version = "0.9.0" }
hyperlight-host = { version = "0.9.0", default-features = false, features = [ "kvm", "mshv2" ] }
hyperlight-wasm = { version = "0.9.0" }

hyper = "1.7"
reqwest = { version = "0.12", features = ["stream", "gzip", "brotli", "deflate", "rustls-tls", "blocking"] }
bytes = "1"
tokio = { version = "1.47", features = ["full"] }
anyhow = "1.0"
bytes = "1"
getrandom = "0.3.3"
http-body-util = "0.1"
hyper = "1.7"
hyper-util = { version = "0.1", features = ["full"] }
getrandom = "0.3.3"
hyperlight-component-macro = { version = "0.12.0" }
hyperlight-common = { version = "0.12.0" }
hyperlight-host = { version = "0.12.0", default-features = false, features = [ "kvm", "mshv3" ] }
hyperlight-wasm = { version = "0.12.0" }
once_cell = "1.21.3"
reqwest = { version = "0.12", features = ["stream", "gzip", "brotli", "deflate", "rustls-tls", "blocking"] }
tokio = { version = "1.47", features = ["full"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ cargo run -- out/sample_wasi_http_rust.aot

JS:
```sh
cargo run -- out/sample_wasi_http_js.aot
cargo run -- out/sample-wasi-http-js.aot
```

## Try it yourself!
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install-hyperlight-wasm-aot:
test -f {{ BIN_DIR }}/hyperlight-wasm-aot || \
cargo install hyperlight-wasm-aot \
--locked \
--version 0.9.0 \
--version 0.12.0 \
--root {{ TARGET_DIR }}

build-js-component: make-out-dir install-hyperlight-wasm-aot
Expand Down
9,933 changes: 0 additions & 9,933 deletions src/bindings.rs

This file was deleted.

65 changes: 27 additions & 38 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
extern crate alloc;

mod bindings {
hyperlight_component_macro::host_bindgen!();
}
mod wasi_impl;

mod resource;
mod types;
mod worker;
use wasi_impl::{
Resource,
bindings::{RootSandbox, register_host_functions, wasi, wasi::http::IncomingHandler},
types,
types::{WasiImpl, http_incoming_body::IncomingBody, io_stream::Stream},
worker::RUNTIME,
};

use std::{convert::Infallible, net::SocketAddr, str::FromStr, sync::Arc};

use bindings::RootSandbox;
use bytes::Bytes;
use http_body_util::{BodyExt, Full};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::TokioIo;
use hyperlight_host::sandbox::SandboxConfiguration;
use hyperlight_wasm::LoadedWasmSandbox;
use resource::Resource;
use tokio::{net::TcpListener, sync::Mutex};
use types::{WasiImpl, http_incoming_body::IncomingBody, io_stream::Stream};
use worker::RUNTIME;

use crate::bindings::wasi::http::IncomingHandler;

fn main() {
let args = std::env::args().collect::<Vec<_>>();
Expand All @@ -35,24 +30,18 @@ fn main() {

let builder = hyperlight_wasm::SandboxBuilder::new()
.with_guest_heap_size(30 * 1024 * 1024)
.with_guest_stack_size(1 * 1024 * 1024);

// hyperlight wasm currently doesn't expose a way to set the host
// function definition size, so we do it manually here with a
// horrible hack to get a mutable reference to the config
let config = builder.get_config() as *const _ as *mut SandboxConfiguration;
let config = unsafe { config.as_mut().unwrap() };
config.set_host_function_definition_size(20 * 1024);
.with_guest_stack_size(1 * 1024 * 1024)
.with_function_definition_size(20 * 1024);

let mut sb = builder.build().unwrap();

let state = types::WasiImpl::new();
let rt = bindings::register_host_functions(&mut sb, state);
let state = WasiImpl::new();
let rt = register_host_functions(&mut sb, state);

let sb = sb.load_runtime().unwrap();
let sb = sb.load_module(wasm_path).unwrap();

let sb = bindings::RootSandbox { sb, rt };
let sb = RootSandbox { sb, rt };
let sb = Arc::new(Mutex::new(sb));

RUNTIME.block_on(async move {
Expand Down Expand Up @@ -92,7 +81,7 @@ async fn hello(
mut req: hyper::Request<hyper::body::Incoming>,
) -> Result<hyper::Response<Full<Bytes>>, Infallible> {
let mut sb = sb.lock().await;
let inst = bindings::root::component::RootExports::incoming_handler(&mut *sb);
let inst = wasi_impl::bindings::root::component::RootExports::incoming_handler(&mut *sb);

let body = req.body_mut();
let mut full_body = Vec::new();
Expand Down Expand Up @@ -124,9 +113,9 @@ async fn hello(
.unwrap_or_default(),
),
scheme: Some(if req.uri().scheme_str() == Some("https") {
bindings::wasi::http::types::Scheme::HTTPS
wasi_impl::bindings::wasi::http::types::Scheme::HTTPS
} else {
bindings::wasi::http::types::Scheme::HTTP
wasi_impl::bindings::wasi::http::types::Scheme::HTTP
}),
authority: req
.uri()
Expand Down Expand Up @@ -188,19 +177,19 @@ async fn hello(
}
}

impl From<&hyper::Method> for bindings::wasi::http::types::Method {
impl From<&hyper::Method> for wasi::http::types::Method {
fn from(method: &hyper::Method) -> Self {
match method.as_str() {
"GET" => bindings::wasi::http::types::Method::Get,
"POST" => bindings::wasi::http::types::Method::Post,
"PUT" => bindings::wasi::http::types::Method::Put,
"DELETE" => bindings::wasi::http::types::Method::Delete,
"HEAD" => bindings::wasi::http::types::Method::Head,
"OPTIONS" => bindings::wasi::http::types::Method::Options,
"CONNECT" => bindings::wasi::http::types::Method::Connect,
"TRACE" => bindings::wasi::http::types::Method::Trace,
"PATCH" => bindings::wasi::http::types::Method::Patch,
other => bindings::wasi::http::types::Method::Other(other.to_string()),
"GET" => wasi_impl::bindings::wasi::http::types::Method::Get,
"POST" => wasi_impl::bindings::wasi::http::types::Method::Post,
"PUT" => wasi_impl::bindings::wasi::http::types::Method::Put,
"DELETE" => wasi_impl::bindings::wasi::http::types::Method::Delete,
"HEAD" => wasi_impl::bindings::wasi::http::types::Method::Head,
"OPTIONS" => wasi_impl::bindings::wasi::http::types::Method::Options,
"CONNECT" => wasi_impl::bindings::wasi::http::types::Method::Connect,
"TRACE" => wasi_impl::bindings::wasi::http::types::Method::Trace,
"PATCH" => wasi_impl::bindings::wasi::http::types::Method::Patch,
other => wasi_impl::bindings::wasi::http::types::Method::Other(other.to_string()),
}
}
}
11 changes: 11 additions & 0 deletions src/wasi_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate alloc;

pub mod bindings {
hyperlight_component_macro::host_bindgen!();
}

pub mod resource;
pub mod types;
pub mod worker;

pub use resource::Resource;
2 changes: 1 addition & 1 deletion src/resource.rs → src/wasi_impl/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
sync::Arc,
};

use crate::{types::io_poll::AnyPollable, worker::RUNTIME};
use crate::wasi_impl::{types::io_poll::AnyPollable, worker::RUNTIME};

pub struct Resource<T> {
inner: Arc<tokio::sync::RwLock<T>>,
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs → src/wasi_impl/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::BorrowMut;

use crate::bindings::root::component::RootImports;
use crate::wasi_impl::bindings::root::component::RootImports;

pub mod buffer;
pub mod cli;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/types/cli.rs → src/wasi_impl/types/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::LazyLock;

use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _, stderr, stdin, stdout};

use crate::{bindings::wasi, resource::Resource, worker::RUNTIME};
use crate::wasi_impl::{bindings::wasi, resource::Resource, worker::RUNTIME};

use super::{WasiImpl, io_stream::Stream};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{sync::LazyLock, time::Duration};

use crate::{bindings::wasi, resource::Resource};
use crate::wasi_impl::{bindings::wasi, resource::Resource};

use super::{WasiImpl, io_poll::AnyPollable};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::WasiImpl;
use crate::bindings::wasi;
use crate::wasi_impl::bindings::wasi;

impl wasi::clocks::WallClock for WasiImpl {
fn now(&mut self) -> wasi::clocks::wall_clock::Datetime {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn as _, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
WasiImpl,
headers::{HeaderError, Headers},
};
use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn as _, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn as _, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::bindings::wasi::{self};
use crate::resource::{BlockOn, Resource};
use crate::wasi_impl::bindings::wasi;
use crate::wasi_impl::resource::{BlockOn, Resource};

use wasi::http::types::ErrorCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn as _, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hyperlight_common::resource::BorrowedResourceGuard;

use super::WasiImpl;

use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{
use crate::wasi_impl::{
bindings::wasi,
resource::{BlockOn as _, Resource},
};
Expand Down
4 changes: 2 additions & 2 deletions src/types/io_error.rs → src/wasi_impl/types/io_error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::types::WasiImpl;
use crate::wasi_impl::types::WasiImpl;

use crate::bindings::wasi;
use crate::wasi_impl::bindings::wasi;

impl wasi::io::error::Error for WasiImpl {
type T = anyhow::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/types/io_poll.rs → src/wasi_impl/types/io_poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi::{self},
resource::{BlockOn, Resource},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{
use crate::wasi_impl::{
bindings::wasi::{self, io::streams::StreamError},
resource::{BlockOn, Resource},
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/random.rs → src/wasi_impl/types/random.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bindings::wasi;
use crate::wasi_impl::bindings::wasi;

use super::WasiImpl;

Expand Down
2 changes: 1 addition & 1 deletion src/types/types.rs → src/wasi_impl/types/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyperlight_common::resource::BorrowedResourceGuard;

use crate::{bindings::wasi, resource::Resource};
use crate::wasi_impl::{bindings::wasi, resource::Resource};

use super::{WasiImpl, io_poll::AnyPollable, io_stream::Stream};

Expand Down
File renamed without changes.
Loading