Skip to content

Commit

Permalink
⚗️ Try to use wasmtime-wasi-http.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Sep 29, 2024
1 parent 00ee66c commit 9d4fd47
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ flume = "^0.11"
async-std = { version = "^1", features = ["attributes", "tokio1"] }
uuid = "^1"

hyper = "^1"
http = "^1"
http-body = "^1"
http-body-util = "^0.1"
wit-component = "*"
wasmtime = { version = "^25", features = ["component-model", "async"] }
wasmtime-wasi = "^25"
wasmtime-wasi-http = "^25"
sea-orm = { version = "^1", features = ["proxy"] }
gluesql = { version = "^0.15" }

Expand Down
50 changes: 48 additions & 2 deletions packages/vm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use anyhow::{Context, Result};
use bytes::Bytes;
use flume::{Receiver, Sender};
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
use std::{
sync::{Arc, Mutex},
time::Duration,
};

use http_body_util::{combinators::BoxBody, BodyExt, Full};
use wasmtime::{
component::{Component, Linker},
Config, Engine, Store,
Expand All @@ -12,6 +16,12 @@ use wasmtime_wasi::{
add_to_linker_sync, bindings::sync::Command, DirPerms, FilePerms, ResourceTable, WasiCtx,
WasiCtxBuilder, WasiView,
};
use wasmtime_wasi_http::{
bindings::http::outgoing_handler::FutureIncomingResponse,
body::HyperOutgoingBody,
types::{IncomingResponse, OutgoingRequestConfig},
HttpResult, WasiHttpCtx, WasiHttpView,
};
use wit_component::ComponentEncoder;

use crate::stream::{HostInputStreamBox, HostOutputStreamBox};
Expand All @@ -25,17 +35,46 @@ lazy_static! {
pub struct WasiContext {
wasi: WasiCtx,
table: ResourceTable,
http: WasiHttpCtx,
}

impl WasiView for WasiContext {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.wasi
}

fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
}

impl WasiHttpView for WasiContext {
fn ctx(&mut self) -> &mut WasiHttpCtx {
&mut self.http
}

fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}

fn send_request(
&mut self,
_request: hyper::Request<HyperOutgoingBody>,
_config: OutgoingRequestConfig,
) -> HttpResult<FutureIncomingResponse> {
HttpResult::Ok(FutureIncomingResponse::Ready(Ok(Ok(IncomingResponse {
resp: hyper::Response::builder()
.status(200)
.body(BoxBody::new(
Full::new(Bytes::from_static(b"Hello!")).map_err(|_| unreachable!()),
))
.unwrap(),
worker: None,
between_bytes_timeout: Duration::from_micros(1),
}))))
}
}

#[derive(Clone)]
pub struct Image {
pub engine: Engine,
Expand Down Expand Up @@ -127,7 +166,14 @@ impl Image {

let wasi = wasi.build();
let table = ResourceTable::new();
let store = Store::new(&self.engine, WasiContext { wasi, table });
let store = Store::new(
&self.engine,
WasiContext {
wasi,
table,
http: WasiHttpCtx::new(),
},
);

Ok(Container {
store: Arc::new(Mutex::new(store)),
Expand Down

0 comments on commit 9d4fd47

Please sign in to comment.