diff --git a/lib/src/component/async_io.rs b/lib/src/component/async_io.rs index 0456c068..55d6b622 100644 --- a/lib/src/component/async_io.rs +++ b/lib/src/component/async_io.rs @@ -1,12 +1,12 @@ use { super::fastly::api::{async_io, types}, - crate::{session::Session, wiggle_abi}, + crate::{linking::ComponentCtx, wiggle_abi}, futures::FutureExt, std::time::Duration, }; #[async_trait::async_trait] -impl async_io::Host for Session { +impl async_io::Host for ComponentCtx { async fn select( &mut self, hs: Vec, @@ -16,7 +16,7 @@ impl async_io::Host for Session { return Err(types::Error::InvalidArgument.into()); } - let select_fut = self.select_impl( + let select_fut = self.session.select_impl( hs.iter() .copied() .map(|i| wiggle_abi::types::AsyncItemHandle::from(i).into()), @@ -44,6 +44,7 @@ impl async_io::Host for Session { async fn is_ready(&mut self, handle: async_io::Handle) -> Result { let handle = wiggle_abi::types::AsyncItemHandle::from(handle); Ok(self + .session .async_item_mut(handle.into())? .await_ready() .now_or_never() diff --git a/lib/src/component/backend.rs b/lib/src/component/backend.rs index 0fdadd5a..53f3937c 100644 --- a/lib/src/component/backend.rs +++ b/lib/src/component/backend.rs @@ -1,12 +1,12 @@ use { super::fastly::api::{backend, http_types, types}, - crate::{error::Error, session::Session}, + crate::{error::Error, linking::ComponentCtx}, }; #[async_trait::async_trait] -impl backend::Host for Session { +impl backend::Host for ComponentCtx { async fn exists(&mut self, backend: String) -> Result { - Ok(self.backend(&backend).is_some()) + Ok(self.session.backend(&backend).is_some()) } async fn is_healthy( @@ -14,14 +14,17 @@ impl backend::Host for Session { backend: String, ) -> Result { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; Ok(backend::BackendHealth::Unknown) } async fn is_dynamic(&mut self, backend: String) -> Result { - if self.dynamic_backend(&backend).is_some() { + if self.session.dynamic_backend(&backend).is_some() { Ok(true) - } else if self.backend(&backend).is_some() { + } else if self.session.backend(&backend).is_some() { Ok(false) } else { Err(Error::InvalidArgument.into()) @@ -29,7 +32,10 @@ impl backend::Host for Session { } async fn get_host(&mut self, backend: String, max_len: u64) -> Result { - let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let backend = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; let host = backend.uri.host().expect("backend uri has host"); @@ -49,7 +55,10 @@ impl backend::Host for Session { backend: String, max_len: u64, ) -> Result>, types::Error> { - let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let backend = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; if let Some(host) = backend.override_host.as_ref() { let host = host.to_str()?; @@ -68,7 +77,10 @@ impl backend::Host for Session { } async fn get_port(&mut self, backend: String) -> Result { - let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let backend = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; match backend.uri.port_u16() { Some(port) => Ok(port), None => { @@ -83,7 +95,10 @@ impl backend::Host for Session { async fn get_connect_timeout_ms(&mut self, backend: String) -> Result { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; Err(Error::Unsupported { msg: "connection timing is not actually supported in Viceroy", } @@ -92,7 +107,10 @@ impl backend::Host for Session { async fn get_first_byte_timeout_ms(&mut self, backend: String) -> Result { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; Err(Error::Unsupported { msg: "connection timing is not actually supported in Viceroy", } @@ -101,7 +119,10 @@ impl backend::Host for Session { async fn get_between_bytes_timeout_ms(&mut self, backend: String) -> Result { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; Err(Error::Unsupported { msg: "connection timing is not actually supported in Viceroy", } @@ -109,7 +130,10 @@ impl backend::Host for Session { } async fn is_ssl(&mut self, backend: String) -> Result { - let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let backend = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; Ok(backend.uri.scheme() == Some(&http::uri::Scheme::HTTPS)) } @@ -118,7 +142,10 @@ impl backend::Host for Session { backend: String, ) -> Result, types::Error> { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; // health checks are not enabled in Viceroy :( Err(Error::Unsupported { msg: "ssl version flags are not supported in Viceroy", @@ -131,7 +158,10 @@ impl backend::Host for Session { backend: String, ) -> Result, types::Error> { // just doing this to get a different error if the backend doesn't exist - let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?; + let _ = self + .session + .backend(&backend) + .ok_or(Error::InvalidArgument)?; // health checks are not enabled in Viceroy :( Err(Error::Unsupported { msg: "ssl version flags are not supported in Viceroy", diff --git a/lib/src/component/cache.rs b/lib/src/component/cache.rs index 920f2e69..6222c32d 100644 --- a/lib/src/component/cache.rs +++ b/lib/src/component/cache.rs @@ -1,10 +1,10 @@ use { super::fastly::api::{cache, http_types, types}, - crate::{error::Error, session::Session}, + crate::{error::Error, linking::ComponentCtx}, }; #[async_trait::async_trait] -impl cache::Host for Session { +impl cache::Host for ComponentCtx { async fn lookup( &mut self, _key: String, diff --git a/lib/src/component/compute_runtime.rs b/lib/src/component/compute_runtime.rs index 0f2d6d8b..041a9c8a 100644 --- a/lib/src/component/compute_runtime.rs +++ b/lib/src/component/compute_runtime.rs @@ -1,10 +1,10 @@ use super::fastly::api::{compute_runtime, types}; -use crate::session::Session; +use crate::linking::ComponentCtx; use std::sync::atomic::Ordering; #[async_trait::async_trait] -impl compute_runtime::Host for Session { +impl compute_runtime::Host for ComponentCtx { async fn get_vcpu_ms(&mut self) -> Result { - Ok(self.active_cpu_time_us.load(Ordering::SeqCst) / 1000) + Ok(self.session.active_cpu_time_us.load(Ordering::SeqCst) / 1000) } } diff --git a/lib/src/component/config_store.rs b/lib/src/component/config_store.rs index b97293e7..b6b8d552 100644 --- a/lib/src/component/config_store.rs +++ b/lib/src/component/config_store.rs @@ -1,12 +1,12 @@ use { super::fastly::api::{config_store, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; #[async_trait::async_trait] -impl config_store::Host for Session { +impl config_store::Host for ComponentCtx { async fn open(&mut self, name: String) -> Result { - let handle = self.dictionary_handle(name.as_str())?; + let handle = self.session.dictionary_handle(name.as_str())?; Ok(handle.into()) } @@ -16,7 +16,7 @@ impl config_store::Host for Session { name: String, max_len: u64, ) -> Result>, types::Error> { - let dict = &self.dictionary(store.into())?.contents; + let dict = &self.session.dictionary(store.into())?.contents; let item = if let Some(item) = dict.get(&name) { item diff --git a/lib/src/component/device_detection.rs b/lib/src/component/device_detection.rs index efd83939..2b3f2469 100644 --- a/lib/src/component/device_detection.rs +++ b/lib/src/component/device_detection.rs @@ -1,16 +1,16 @@ use { super::fastly::api::{device_detection, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; #[async_trait::async_trait] -impl device_detection::Host for Session { +impl device_detection::Host for ComponentCtx { async fn lookup( &mut self, user_agent: String, max_len: u64, ) -> Result>, types::Error> { - if let Some(result) = self.device_detection_lookup(&user_agent) { + if let Some(result) = self.session.device_detection_lookup(&user_agent) { if result.len() > max_len as usize { return Err(types::Error::BufferLen( u64::try_from(result.len()).unwrap_or(0), diff --git a/lib/src/component/dictionary.rs b/lib/src/component/dictionary.rs index e9126619..677d7fc5 100644 --- a/lib/src/component/dictionary.rs +++ b/lib/src/component/dictionary.rs @@ -1,12 +1,12 @@ use { super::fastly::api::{dictionary, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; #[async_trait::async_trait] -impl dictionary::Host for Session { +impl dictionary::Host for ComponentCtx { async fn open(&mut self, name: String) -> Result { - let handle = self.dictionary_handle(name.as_str())?; + let handle = self.session.dictionary_handle(name.as_str())?; Ok(handle.into()) } @@ -16,7 +16,7 @@ impl dictionary::Host for Session { key: String, max_len: u64, ) -> Result>, types::Error> { - let dict = &self.dictionary(h.into())?.contents; + let dict = &self.session.dictionary(h.into())?.contents; let item = if let Some(item) = dict.get(&key) { item diff --git a/lib/src/component/erl.rs b/lib/src/component/erl.rs index 9667197e..e08ece6b 100644 --- a/lib/src/component/erl.rs +++ b/lib/src/component/erl.rs @@ -1,10 +1,10 @@ use { super::fastly::api::{erl, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; #[async_trait::async_trait] -impl erl::Host for Session { +impl erl::Host for ComponentCtx { async fn check_rate( &mut self, _rc: String, diff --git a/lib/src/component/geo.rs b/lib/src/component/geo.rs index 5b5af5ba..00e63aa4 100644 --- a/lib/src/component/geo.rs +++ b/lib/src/component/geo.rs @@ -1,11 +1,11 @@ use { super::fastly::api::{geo, types}, - crate::{error, session::Session}, + crate::{error, linking::ComponentCtx}, std::net::{IpAddr, Ipv4Addr, Ipv6Addr}, }; #[async_trait::async_trait] -impl geo::Host for Session { +impl geo::Host for ComponentCtx { async fn lookup(&mut self, octets: Vec, max_len: u64) -> Result, types::Error> { let ip_addr: IpAddr = match octets.len() { 4 => IpAddr::V4(Ipv4Addr::from( @@ -18,6 +18,7 @@ impl geo::Host for Session { }; let json = self + .session .geolocation_lookup(&ip_addr) .ok_or(geo::Error::UnknownError)?; diff --git a/lib/src/component/http_body.rs b/lib/src/component/http_body.rs index e1d43fc4..aad9ac0c 100644 --- a/lib/src/component/http_body.rs +++ b/lib/src/component/http_body.rs @@ -3,7 +3,7 @@ use { fastly::api::{http_body, http_types, types}, headers, }, - crate::{body::Body, error::Error, session::Session}, + crate::{body::Body, error::Error, linking::ComponentCtx}, ::http_body::Body as HttpBody, http::header::{HeaderName, HeaderValue}, }; @@ -13,9 +13,9 @@ use { pub const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; #[async_trait::async_trait] -impl http_body::Host for Session { +impl http_body::Host for ComponentCtx { async fn new(&mut self) -> Result { - Ok(self.insert_body(Body::empty()).into()) + Ok(self.session.insert_body(Body::empty()).into()) } async fn write( @@ -31,15 +31,15 @@ impl http_body::Host for Session { match end { http_body::WriteEnd::Front => { // Only normal bodies can be front-written - let body = self.body_mut(h.into())?; + let body = self.session.body_mut(h.into())?; body.push_front(buf); } http_body::WriteEnd::Back => { - if self.is_streaming_body(h.into()) { - let body = self.streaming_body_mut(h.into())?; + if self.session.is_streaming_body(h.into()) { + let body = self.session.streaming_body_mut(h.into())?; body.send_chunk(buf).await?; } else { - let body = self.body_mut(h.into())?; + let body = self.session.body_mut(h.into())?; body.push_back(buf); } } @@ -59,15 +59,15 @@ impl http_body::Host for Session { ) -> Result<(), types::Error> { // Take the `src` body out of the session, and get a mutable reference // to the `dest` body we will append to. - let src = self.take_body(src.into())?; + let src = self.session.take_body(src.into())?; - if self.is_streaming_body(dest.into()) { - let dest = self.streaming_body_mut(dest.into())?; + if self.session.is_streaming_body(dest.into()) { + let dest = self.session.streaming_body_mut(dest.into())?; for chunk in src { dest.send_chunk(chunk).await?; } } else { - let dest = self.body_mut(dest.into())?; + let dest = self.session.body_mut(dest.into())?; dest.append(src); } Ok(()) @@ -79,7 +79,7 @@ impl http_body::Host for Session { chunk_size: u32, ) -> Result, types::Error> { // only normal bodies (not streaming bodies) can be read from - let body = self.body_mut(h.into())?; + let body = self.session.body_mut(h.into())?; if let Some(chunk) = body.data().await { // pass up any error encountered when reading a chunk @@ -101,19 +101,19 @@ impl http_body::Host for Session { async fn close(&mut self, h: http_types::BodyHandle) -> Result<(), types::Error> { // Drop the body and pass up an error if the handle does not exist - if self.is_streaming_body(h.into()) { + if self.session.is_streaming_body(h.into()) { // Make sure a streaming body gets a `finish` message - self.take_streaming_body(h.into())?.finish()?; + self.session.take_streaming_body(h.into())?.finish()?; Ok(()) } else { - Ok(self.drop_body(h.into())?) + Ok(self.session.drop_body(h.into())?) } } async fn known_length(&mut self, h: http_types::BodyHandle) -> Result { - if self.is_streaming_body(h.into()) { + if self.session.is_streaming_body(h.into()) { Err(Error::ValueAbsent.into()) - } else if let Some(len) = self.body_mut(h.into())?.len() { + } else if let Some(len) = self.session.body_mut(h.into())?.len() { Ok(len) } else { Err(Error::ValueAbsent.into()) @@ -127,14 +127,14 @@ impl http_body::Host for Session { value: Vec, ) -> Result<(), types::Error> { // Appending trailers is always allowed for bodies and streaming bodies. - if self.is_streaming_body(h.into()) { - let body = self.streaming_body_mut(h.into())?; + if self.session.is_streaming_body(h.into()) { + let body = self.session.streaming_body_mut(h.into())?; let name = HeaderName::from_bytes(name.as_bytes())?; let value = HeaderValue::from_bytes(value.as_slice())?; body.append_trailer(name, value); Ok(()) } else { - let trailers = &mut self.body_mut(h.into())?.trailers; + let trailers = &mut self.session.body_mut(h.into())?.trailers; if name.len() > MAX_HEADER_NAME_LEN { return Err(Error::InvalidArgument.into()); } @@ -153,11 +153,11 @@ impl http_body::Host for Session { cursor: u32, ) -> Result, Option)>, types::Error> { // Read operations are not allowed on streaming bodies. - if self.is_streaming_body(h.into()) { + if self.session.is_streaming_body(h.into()) { return Err(Error::InvalidArgument.into()); } - let body = self.body_mut(h.into())?; + let body = self.session.body_mut(h.into())?; if !body.trailers_ready { return Err(Error::Again.into()); } @@ -188,11 +188,11 @@ impl http_body::Host for Session { max_len: u64, ) -> Result>, types::Error> { // Read operations are not allowed on streaming bodies. - if self.is_streaming_body(h.into()) { + if self.session.is_streaming_body(h.into()) { return Err(Error::InvalidArgument.into()); } - let body = &mut self.body_mut(h.into())?; + let body = &mut self.session.body_mut(h.into())?; if !body.trailers_ready { return Err(Error::Again.into()); } @@ -230,11 +230,11 @@ impl http_body::Host for Session { cursor: u32, ) -> Result, Option)>, types::Error> { // Read operations are not allowed on streaming bodies. - if self.is_streaming_body(h.into()) { + if self.session.is_streaming_body(h.into()) { return Err(Error::InvalidArgument.into()); } - let body = &mut self.body_mut(h.into())?; + let body = &mut self.session.body_mut(h.into())?; if !body.trailers_ready { return Err(Error::Again.into()); } diff --git a/lib/src/component/http_req.rs b/lib/src/component/http_req.rs index f3157f0d..f67e3d2a 100644 --- a/lib/src/component/http_req.rs +++ b/lib/src/component/http_req.rs @@ -7,6 +7,7 @@ use { crate::{ config::{Backend, ClientCertInfo}, error::Error, + linking::ComponentCtx, secret_store::SecretLookup, session::{AsyncItem, AsyncItemHandle, PeekableTask, Session, ViceroyRequestMetadata}, upstream, @@ -50,13 +51,13 @@ use { const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; #[async_trait::async_trait] -impl http_req::Host for Session { +impl http_req::Host for ComponentCtx { async fn method_get( &mut self, h: http_types::RequestHandle, max_len: u64, ) -> Result { - let req = self.request_parts(h.into())?; + let req = self.session.request_parts(h.into())?; let req_method = &req.method; if req_method.as_str().len() > usize::try_from(max_len).unwrap() { @@ -73,7 +74,7 @@ impl http_req::Host for Session { h: http_types::RequestHandle, max_len: u64, ) -> Result { - let req = self.request_parts(h.into())?; + let req = self.session.request_parts(h.into())?; let req_uri = &req.uri; let res = req_uri.to_string(); @@ -108,7 +109,7 @@ impl http_req::Host for Session { } async fn downstream_client_ip_addr(&mut self) -> Result, types::Error> { - match self.downstream_client_ip() { + match self.session.downstream_client_ip() { IpAddr::V4(addr) => { let octets = addr.octets(); debug_assert_eq!(octets.len(), 4); @@ -123,7 +124,7 @@ impl http_req::Host for Session { } async fn downstream_server_ip_addr(&mut self) -> Result, types::Error> { - match self.downstream_server_ip() { + match self.session.downstream_server_ip() { IpAddr::V4(addr) => { let octets = addr.octets(); debug_assert_eq!(octets.len(), 4); @@ -174,7 +175,7 @@ impl http_req::Host for Session { async fn new(&mut self) -> Result { let (parts, _) = Request::new(()).into_parts(); - Ok(self.insert_request_parts(parts).into()) + Ok(self.session.insert_request_parts(parts).into()) } async fn header_names_get( @@ -183,7 +184,7 @@ impl http_req::Host for Session { max_len: u64, cursor: u32, ) -> Result, Option)>, types::Error> { - let headers = &self.request_parts(h.into())?.headers; + let headers = &self.session.request_parts(h.into())?.headers; let (buf, next) = write_values( headers.keys(), @@ -213,7 +214,7 @@ impl http_req::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &self.request_parts(h.into())?.headers; + let headers = &self.session.request_parts(h.into())?.headers; let value = if let Some(value) = headers.get(&name) { value } else { @@ -234,7 +235,7 @@ impl http_req::Host for Session { max_len: u64, cursor: u32, ) -> Result, Option)>, TrappableError> { - let headers = &self.request_parts(h.into())?.headers; + let headers = &self.session.request_parts(h.into())?.headers; let values = headers.get_all(HeaderName::from_str(&name)?); @@ -266,7 +267,7 @@ impl http_req::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.request_parts_mut(h.into())?.headers; + let headers = &mut self.session.request_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let values = { @@ -300,7 +301,7 @@ impl http_req::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.request_parts_mut(h.into())?.headers; + let headers = &mut self.session.request_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let value = HeaderValue::from_bytes(value.as_slice())?; headers.insert(name, value); @@ -318,7 +319,7 @@ impl http_req::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.request_parts_mut(h.into())?.headers; + let headers = &mut self.session.request_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let value = HeaderValue::from_bytes(value.as_slice())?; headers.append(name, value); @@ -335,7 +336,7 @@ impl http_req::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.request_parts_mut(h.into())?.headers; + let headers = &mut self.session.request_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; headers .remove(name) @@ -349,7 +350,7 @@ impl http_req::Host for Session { h: http_types::RequestHandle, method: String, ) -> Result<(), types::Error> { - let method_ref = &mut self.request_parts_mut(h.into())?.method; + let method_ref = &mut self.session.request_parts_mut(h.into())?.method; *method_ref = Method::from_bytes(method.as_bytes())?; Ok(()) } @@ -359,7 +360,7 @@ impl http_req::Host for Session { h: http_types::RequestHandle, uri: String, ) -> Result<(), types::Error> { - let uri_ref = &mut self.request_parts_mut(h.into())?.uri; + let uri_ref = &mut self.session.request_parts_mut(h.into())?.uri; *uri_ref = Uri::try_from(uri.as_bytes())?; Ok(()) } @@ -368,7 +369,7 @@ impl http_req::Host for Session { &mut self, h: http_types::RequestHandle, ) -> Result { - let req = self.request_parts(h.into())?; + let req = self.session.request_parts(h.into())?; let version = http_types::HttpVersion::try_from(req.version)?; Ok(version) } @@ -378,7 +379,7 @@ impl http_req::Host for Session { h: http_types::RequestHandle, version: http_types::HttpVersion, ) -> Result<(), types::Error> { - let req = self.request_parts_mut(h.into())?; + let req = self.session.request_parts_mut(h.into())?; req.version = hyper::Version::from(version); Ok(()) } @@ -390,16 +391,17 @@ impl http_req::Host for Session { backend_name: String, ) -> Result { // prepare the request - let req_parts = self.take_request_parts(h.into())?; - let req_body = self.take_body(b.into())?; + let req_parts = self.session.take_request_parts(h.into())?; + let req_body = self.session.take_body(b.into())?; let req = Request::from_parts(req_parts, req_body); let backend = self + .session .backend(&backend_name) .ok_or_else(|| Error::UnknownBackend(backend_name))?; // synchronously send the request - let resp = upstream::send_request(req, backend, self.tls_config()).await?; - let (resp_handle, body_handle) = self.insert_response(resp); + let resp = upstream::send_request(req, backend, self.session.tls_config()).await?; + let (resp_handle, body_handle) = self.session.insert_response(resp); Ok((resp_handle.into(), body_handle.into())) } @@ -422,19 +424,24 @@ impl http_req::Host for Session { backend_name: String, ) -> Result { // prepare the request - let req_parts = self.take_request_parts(h.into())?; - let req_body = self.take_body(b.into())?; + let req_parts = self.session.take_request_parts(h.into())?; + let req_body = self.session.take_body(b.into())?; let req = Request::from_parts(req_parts, req_body); let backend = self + .session .backend(&backend_name) .ok_or(types::Error::from(types::Error::UnknownError))?; // asynchronously send the request - let task = - PeekableTask::spawn(upstream::send_request(req, backend, self.tls_config())).await; + let task = PeekableTask::spawn(upstream::send_request( + req, + backend, + self.session.tls_config(), + )) + .await; // return a handle to the pending request - Ok(self.insert_pending_request(task).into()) + Ok(self.session.insert_pending_request(task).into()) } async fn send_async_streaming( @@ -444,19 +451,24 @@ impl http_req::Host for Session { backend_name: String, ) -> Result { // prepare the request - let req_parts = self.take_request_parts(h.into())?; - let req_body = self.begin_streaming(b.into())?; + let req_parts = self.session.take_request_parts(h.into())?; + let req_body = self.session.begin_streaming(b.into())?; let req = Request::from_parts(req_parts, req_body); let backend = self + .session .backend(&backend_name) .ok_or(types::Error::from(types::Error::UnknownError))?; // asynchronously send the request - let task = - PeekableTask::spawn(upstream::send_request(req, backend, self.tls_config())).await; + let task = PeekableTask::spawn(upstream::send_request( + req, + backend, + self.session.tls_config(), + )) + .await; // return a handle to the pending request - Ok(self.insert_pending_request(task).into()) + Ok(self.session.insert_pending_request(task).into()) } async fn pending_req_poll( @@ -464,11 +476,12 @@ impl http_req::Host for Session { h: http_types::PendingRequestHandle, ) -> Result, types::Error> { if self + .session .async_item_mut(AsyncItemHandle::from_u32(h))? .is_ready() { - let resp = self.take_pending_request(h.into())?.recv().await?; - let (resp_handle, resp_body_handle) = self.insert_response(resp); + let resp = self.session.take_pending_request(h.into())?.recv().await?; + let (resp_handle, resp_body_handle) = self.session.insert_response(resp); Ok(Some((resp_handle.into(), resp_body_handle.into()))) } else { Ok(None) @@ -488,8 +501,8 @@ impl http_req::Host for Session { &mut self, h: http_types::PendingRequestHandle, ) -> Result { - let pending_req = self.take_pending_request(h.into())?.recv().await?; - let (resp_handle, body_handle) = self.insert_response(pending_req); + let pending_req = self.session.take_pending_request(h.into())?.recv().await?; + let (resp_handle, body_handle) = self.session.insert_response(pending_req); Ok((resp_handle.into(), body_handle.into())) } @@ -514,13 +527,14 @@ impl http_req::Host for Session { // perform the select operation let done_index = self + .session .select_impl( h.iter() .map(|handle| types::PendingRequestHandle::from(*handle).into()), ) .await?; - let item = self.take_async_item( + let item = self.session.take_async_item( types::PendingRequestHandle::from(h.get(done_index).cloned().unwrap()).into(), )?; @@ -528,7 +542,7 @@ impl http_req::Host for Session { AsyncItem::PendingReq(res) => match res { PeekableTask::Complete(resp) => match resp { Ok(resp) => { - let (resp_handle, body_handle) = self.insert_response(resp); + let (resp_handle, body_handle) = self.session.insert_response(resp); (done_index as u32, (resp_handle.into(), body_handle.into())) } // Unfortunately, the ABI provides no means of returning error information @@ -562,7 +576,7 @@ impl http_req::Host for Session { async fn close(&mut self, h: http_types::RequestHandle) -> Result<(), types::Error> { // We don't do anything with the parts, but we do pass the error up if // the handle given doesn't exist - self.take_request_parts(h.into())?; + self.session.take_request_parts(h.into())?; Ok(()) } @@ -575,7 +589,7 @@ impl http_req::Host for Session { // NOTE: We're going to hide this flag in the extensions of the request in order to decrease // the book-keeping burden inside Session. The flag will get picked up later, in `send_request`. - let extensions = &mut self.request_parts_mut(h.into())?.extensions; + let extensions = &mut self.session.request_parts_mut(h.into())?.extensions; let encodings = types::ContentEncodings::try_from(encodings.as_array()[0])?; @@ -724,16 +738,17 @@ impl http_req::Host for Session { }; let client_cert = if options.contains(http_types::BackendConfigOptions::CLIENT_CERT) { - let key_lookup = - self.secret_lookup(config.client_key.into()) - .ok_or(Error::SecretStoreError( - SecretStoreError::InvalidSecretHandle(config.client_key.into()), - ))?; + let key_lookup = self.session.secret_lookup(config.client_key.into()).ok_or( + Error::SecretStoreError(SecretStoreError::InvalidSecretHandle( + config.client_key.into(), + )), + )?; let key = match &key_lookup { SecretLookup::Standard { store_name, secret_name, } => self + .session .secret_stores() .get_store(store_name) .ok_or(Error::SecretStoreError( @@ -769,7 +784,7 @@ impl http_req::Host for Session { ca_certs, }; - if !self.add_backend(name, new_backend) { + if !self.session.add_backend(name, new_backend) { return Err(Error::BackendNameRegistryError(name.to_string()).into()); } @@ -784,7 +799,7 @@ impl http_req::Host for Session { } async fn downstream_client_request_id(&mut self, max_len: u64) -> Result { - let result = format!("{:032x}", self.req_id()); + let result = format!("{:032x}", self.session.req_id()); if result.len() > usize::try_from(max_len).unwrap() { return Err(types::Error::BufferLen( @@ -810,7 +825,7 @@ impl http_req::Host for Session { &mut self, region_max_len: u64, ) -> Result, types::Error> { - let region = Session::downstream_compliance_region(self); + let region = Session::downstream_compliance_region(&self.session); let region_len = region.len(); match u64::try_from(region_len) { @@ -824,7 +839,7 @@ impl http_req::Host for Session { max_len: u64, cursor: u32, ) -> Result, Option)>, types::Error> { - let headers = self.downstream_original_headers(); + let headers = self.session.downstream_original_headers(); let (buf, next) = write_values( headers.keys(), b'\0', @@ -845,6 +860,7 @@ impl http_req::Host for Session { async fn original_header_count(&mut self) -> Result { Ok(self + .session .downstream_original_headers() .len() .try_into() @@ -862,8 +878,8 @@ impl http_req::Host for Session { use http_req::InspectConfigOptions as Flags; // Make sure we're given valid handles, even though we won't use them. - let _ = self.request_parts(ds_req.into())?; - let _ = self.body(ds_body.into())?; + let _ = self.session.request_parts(ds_req.into())?; + let _ = self.session.body(ds_body.into())?; // For now, corp and workspace arguments are required to actually generate the hostname, // but in the future the lookaside service will be generated using the customer ID, and @@ -877,7 +893,7 @@ impl http_req::Host for Session { } // Return the mock NGWAF response. - let ngwaf_resp = self.ngwaf_response(); + let ngwaf_resp = self.session.ngwaf_response(); let ngwaf_resp_len = ngwaf_resp.len(); match u64::try_from(ngwaf_resp_len) { diff --git a/lib/src/component/http_resp.rs b/lib/src/component/http_resp.rs index e196d03d..58352ed9 100644 --- a/lib/src/component/http_resp.rs +++ b/lib/src/component/http_resp.rs @@ -1,7 +1,7 @@ use { super::fastly::api::{http_resp, http_types, types}, super::{headers::write_values, types::TrappableError}, - crate::{error::Error, session::Session, upstream}, + crate::{error::Error, linking::ComponentCtx, upstream}, cfg_if::cfg_if, http::{HeaderName, HeaderValue}, hyper::http::response::Response, @@ -11,17 +11,17 @@ use { const MAX_HEADER_NAME_LEN: usize = (1 << 16) - 1; #[async_trait::async_trait] -impl http_resp::Host for Session { +impl http_resp::Host for ComponentCtx { async fn new(&mut self) -> Result { let (parts, _) = Response::new(()).into_parts(); - Ok(self.insert_response_parts(parts).into()) + Ok(self.session.insert_response_parts(parts).into()) } async fn status_get( &mut self, h: http_types::ResponseHandle, ) -> Result { - let parts = self.response_parts(h.into())?; + let parts = self.session.response_parts(h.into())?; Ok(parts.status.as_u16()) } @@ -30,7 +30,7 @@ impl http_resp::Host for Session { h: http_types::ResponseHandle, status: http_types::HttpStatus, ) -> Result<(), types::Error> { - let resp = self.response_parts_mut(h.into())?; + let resp = self.session.response_parts_mut(h.into())?; let status = hyper::StatusCode::from_u16(status)?; resp.status = status; Ok(()) @@ -46,7 +46,7 @@ impl http_resp::Host for Session { Err(types::Error::InvalidArgument)?; } - let headers = &mut self.response_parts_mut(h.into())?.headers; + let headers = &mut self.session.response_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let value = HeaderValue::from_bytes(value.as_slice())?; headers.append(name, value); @@ -62,15 +62,15 @@ impl http_resp::Host for Session { let resp = { // Take the response parts and body from the session, and use them to build a response. // Return an `FastlyStatus::Badf` error code if either of the given handles are invalid. - let resp_parts = self.take_response_parts(h.into())?; + let resp_parts = self.session.take_response_parts(h.into())?; let body = if streaming { - self.begin_streaming(b.into())? + self.session.begin_streaming(b.into())? } else { - self.take_body(b.into())? + self.session.take_body(b.into())? }; Response::from_parts(resp_parts, body) }; // Set the downstream response, and return. - self.send_downstream_response(resp)?; + self.session.send_downstream_response(resp)?; Ok(()) } @@ -80,7 +80,7 @@ impl http_resp::Host for Session { max_len: u64, cursor: u32, ) -> Result, Option)>, types::Error> { - let headers = &self.response_parts(h.into())?.headers; + let headers = &self.session.response_parts(h.into())?.headers; let (buf, next) = write_values( headers.keys(), @@ -110,7 +110,7 @@ impl http_resp::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &self.response_parts(h.into())?.headers; + let headers = &self.session.response_parts(h.into())?.headers; let value = if let Some(value) = headers.get(&name) { value } else { @@ -142,7 +142,7 @@ impl http_resp::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &self.response_parts(h.into())?.headers; + let headers = &self.session.response_parts(h.into())?.headers; let values = headers.get_all(HeaderName::from_str(&name)?); @@ -176,7 +176,7 @@ impl http_resp::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.response_parts_mut(h.into())?.headers; + let headers = &mut self.session.response_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let values = { @@ -210,7 +210,7 @@ impl http_resp::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.response_parts_mut(h.into())?.headers; + let headers = &mut self.session.response_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; let value = HeaderValue::from_bytes(value.as_slice())?; headers.insert(name, value); @@ -227,7 +227,7 @@ impl http_resp::Host for Session { return Err(Error::InvalidArgument.into()); } - let headers = &mut self.response_parts_mut(h.into())?.headers; + let headers = &mut self.session.response_parts_mut(h.into())?.headers; let name = HeaderName::from_bytes(name.as_bytes())?; headers .remove(name) @@ -240,7 +240,7 @@ impl http_resp::Host for Session { &mut self, h: http_types::ResponseHandle, ) -> Result { - let req = self.response_parts(h.into())?; + let req = self.session.response_parts(h.into())?; let version = http_types::HttpVersion::try_from(req.version)?; Ok(version) } @@ -250,7 +250,7 @@ impl http_resp::Host for Session { h: http_types::ResponseHandle, version: http_types::HttpVersion, ) -> Result<(), types::Error> { - let req = self.response_parts_mut(h.into())?; + let req = self.session.response_parts_mut(h.into())?; req.version = hyper::Version::from(version); Ok(()) } @@ -258,7 +258,7 @@ impl http_resp::Host for Session { async fn close(&mut self, h: http_types::ResponseHandle) -> Result<(), types::Error> { // We don't do anything with the parts, but we do pass the error up if // the handle given doesn't exist - self.take_response_parts(h.into())?; + self.session.take_response_parts(h.into())?; Ok(()) } @@ -292,7 +292,7 @@ impl http_resp::Host for Session { &mut self, resp_handle: http_types::ResponseHandle, ) -> Result, types::Error> { - let resp = self.response_parts(resp_handle.into())?; + let resp = self.session.response_parts(resp_handle.into())?; let md = resp .extensions .get::() @@ -316,7 +316,7 @@ impl http_resp::Host for Session { &mut self, resp_handle: http_types::ResponseHandle, ) -> Result { - let resp = self.response_parts(resp_handle.into())?; + let resp = self.session.response_parts(resp_handle.into())?; let md = resp .extensions .get::() diff --git a/lib/src/component/http_types.rs b/lib/src/component/http_types.rs index 042a9540..6b0eada1 100644 --- a/lib/src/component/http_types.rs +++ b/lib/src/component/http_types.rs @@ -1,9 +1,9 @@ use { super::fastly::api::{http_types, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; -impl http_types::Host for Session {} +impl http_types::Host for ComponentCtx {} // The http crate's `Version` is a struct that has a bunch of // associated constants, not an enum; this is only a partial conversion. diff --git a/lib/src/component/kv_store.rs b/lib/src/component/kv_store.rs index 2cc6e1b2..7a73db7c 100644 --- a/lib/src/component/kv_store.rs +++ b/lib/src/component/kv_store.rs @@ -1,12 +1,12 @@ use { super::fastly::api::{http_body, kv_store, types}, - crate::session::Session, + crate::linking::ComponentCtx, }; pub struct LookupResult; #[async_trait::async_trait] -impl kv_store::HostLookupResult for Session { +impl kv_store::HostLookupResult for ComponentCtx { async fn body( &mut self, _self_: wasmtime::component::Resource, @@ -38,7 +38,7 @@ impl kv_store::HostLookupResult for Session { } #[async_trait::async_trait] -impl kv_store::Host for Session { +impl kv_store::Host for ComponentCtx { async fn open(&mut self, _name: String) -> Result, types::Error> { todo!() } diff --git a/lib/src/component/log.rs b/lib/src/component/log.rs index 896cfec5..cc8037f2 100644 --- a/lib/src/component/log.rs +++ b/lib/src/component/log.rs @@ -1,6 +1,6 @@ use { super::fastly::api::{log, types}, - crate::session::Session, + crate::linking::ComponentCtx, lazy_static::lazy_static, }; @@ -17,7 +17,7 @@ fn is_reserved_endpoint(name: &[u8]) -> bool { } #[async_trait::async_trait] -impl log::Host for Session { +impl log::Host for ComponentCtx { async fn endpoint_get(&mut self, name: String) -> Result { let name = name.as_bytes(); @@ -25,11 +25,11 @@ impl log::Host for Session { return Err(types::Error::InvalidArgument.into()); } - Ok(self.log_endpoint_handle(name).into()) + Ok(self.session.log_endpoint_handle(name).into()) } async fn write(&mut self, h: log::Handle, msg: String) -> Result { - let endpoint = self.log_endpoint(h.into())?; + let endpoint = self.session.log_endpoint(h.into())?; let msg = msg.as_bytes(); endpoint.write_entry(&msg)?; Ok(u32::try_from(msg.len()).unwrap()) diff --git a/lib/src/component/mod.rs b/lib/src/component/mod.rs index f8c7e62e..4dcac2c5 100644 --- a/lib/src/component/mod.rs +++ b/lib/src/component/mod.rs @@ -41,26 +41,26 @@ pub fn link_host_functions(linker: &mut component::Linker) -> anyh wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(linker, wrap)?; wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(linker, wrap)?; - fastly::api::async_io::add_to_linker(linker, |x| x.session())?; - fastly::api::backend::add_to_linker(linker, |x| x.session())?; - fastly::api::cache::add_to_linker(linker, |x| x.session())?; - fastly::api::device_detection::add_to_linker(linker, |x| x.session())?; - fastly::api::dictionary::add_to_linker(linker, |x| x.session())?; - fastly::api::erl::add_to_linker(linker, |x| x.session())?; - fastly::api::geo::add_to_linker(linker, |x| x.session())?; - fastly::api::http_body::add_to_linker(linker, |x| x.session())?; - fastly::api::http_req::add_to_linker(linker, |x| x.session())?; - fastly::api::http_resp::add_to_linker(linker, |x| x.session())?; - fastly::api::http_types::add_to_linker(linker, |x| x.session())?; - fastly::api::log::add_to_linker(linker, |x| x.session())?; - fastly::api::object_store::add_to_linker(linker, |x| x.session())?; - fastly::api::kv_store::add_to_linker(linker, |x| x.session())?; - fastly::api::purge::add_to_linker(linker, |x| x.session())?; - fastly::api::secret_store::add_to_linker(linker, |x| x.session())?; - fastly::api::types::add_to_linker(linker, |x| x.session())?; - fastly::api::uap::add_to_linker(linker, |x| x.session())?; - fastly::api::config_store::add_to_linker(linker, |x| x.session())?; - fastly::api::compute_runtime::add_to_linker(linker, |x| x.session())?; + fastly::api::async_io::add_to_linker(linker, |x| x)?; + fastly::api::backend::add_to_linker(linker, |x| x)?; + fastly::api::cache::add_to_linker(linker, |x| x)?; + fastly::api::device_detection::add_to_linker(linker, |x| x)?; + fastly::api::dictionary::add_to_linker(linker, |x| x)?; + fastly::api::erl::add_to_linker(linker, |x| x)?; + fastly::api::geo::add_to_linker(linker, |x| x)?; + fastly::api::http_body::add_to_linker(linker, |x| x)?; + fastly::api::http_req::add_to_linker(linker, |x| x)?; + fastly::api::http_resp::add_to_linker(linker, |x| x)?; + fastly::api::http_types::add_to_linker(linker, |x| x)?; + fastly::api::log::add_to_linker(linker, |x| x)?; + fastly::api::object_store::add_to_linker(linker, |x| x)?; + fastly::api::kv_store::add_to_linker(linker, |x| x)?; + fastly::api::purge::add_to_linker(linker, |x| x)?; + fastly::api::secret_store::add_to_linker(linker, |x| x)?; + fastly::api::types::add_to_linker(linker, |x| x)?; + fastly::api::uap::add_to_linker(linker, |x| x)?; + fastly::api::config_store::add_to_linker(linker, |x| x)?; + fastly::api::compute_runtime::add_to_linker(linker, |x| x)?; Ok(()) } diff --git a/lib/src/component/object_store.rs b/lib/src/component/object_store.rs index 49a423d2..95eb1177 100644 --- a/lib/src/component/object_store.rs +++ b/lib/src/component/object_store.rs @@ -2,18 +2,17 @@ use { super::fastly::api::{http_types, object_store, types}, crate::{ body::Body, + linking::ComponentCtx, object_store::{ObjectKey, ObjectStoreError}, - session::{ - PeekableTask, PendingKvDeleteTask, PendingKvInsertTask, PendingKvLookupTask, Session, - }, + session::{PeekableTask, PendingKvDeleteTask, PendingKvInsertTask, PendingKvLookupTask}, }, }; #[async_trait::async_trait] -impl object_store::Host for Session { +impl object_store::Host for ComponentCtx { async fn open(&mut self, name: String) -> Result, types::Error> { - if self.object_store.store_exists(&name)? { - let handle = self.obj_store_handle(&name)?; + if self.session.object_store.store_exists(&name)? { + let handle = self.session.obj_store_handle(&name)?; Ok(Some(handle.into())) } else { Ok(None) @@ -25,11 +24,11 @@ impl object_store::Host for Session { store: object_store::Handle, key: String, ) -> Result, types::Error> { - let store = self.get_obj_store_key(store.into()).unwrap(); + let store = self.session.get_obj_store_key(store.into()).unwrap(); let key = ObjectKey::new(&key)?; - match self.obj_lookup(store, &key) { + match self.session.obj_lookup(store, &key) { Ok(obj) => { - let new_handle = self.insert_body(Body::from(obj)); + let new_handle = self.session.insert_body(Body::from(obj)); Ok(Some(new_handle.into())) } // Don't write to the invalid handle as the SDK will return Ok(None) @@ -45,12 +44,12 @@ impl object_store::Host for Session { store: object_store::Handle, key: String, ) -> Result { - let store = self.get_obj_store_key(store.into()).unwrap(); + let store = self.session.get_obj_store_key(store.into()).unwrap(); let key = ObjectKey::new(key)?; // just create a future that's already ready - let fut = futures::future::ok(self.obj_lookup(store, &key)); + let fut = futures::future::ok(self.session.obj_lookup(store, &key)); let task = PendingKvLookupTask::new(PeekableTask::spawn(fut).await); - Ok(self.insert_pending_kv_lookup(task).into()) + Ok(self.session.insert_pending_kv_lookup(task).into()) } async fn pending_lookup_wait( @@ -58,13 +57,14 @@ impl object_store::Host for Session { pending: object_store::PendingLookupHandle, ) -> Result, types::Error> { let pending_obj = self + .session .take_pending_kv_lookup(pending.into())? .task() .recv() .await?; // proceed with the normal match from lookup() match pending_obj { - Ok(obj) => Ok(Some(self.insert_body(Body::from(obj)).into())), + Ok(obj) => Ok(Some(self.session.insert_body(Body::from(obj)).into())), Err(ObjectStoreError::MissingObject) => Ok(None), Err(err) => Err(err.into()), } @@ -76,10 +76,18 @@ impl object_store::Host for Session { key: String, body_handle: http_types::BodyHandle, ) -> Result<(), types::Error> { - let store = self.get_obj_store_key(store.into()).unwrap().clone(); + let store = self + .session + .get_obj_store_key(store.into()) + .unwrap() + .clone(); let key = ObjectKey::new(&key)?; - let bytes = self.take_body(body_handle.into())?.read_into_vec().await?; - self.obj_insert(store, key, bytes)?; + let bytes = self + .session + .take_body(body_handle.into())? + .read_into_vec() + .await?; + self.session.obj_insert(store, key, bytes)?; Ok(()) } @@ -90,13 +98,22 @@ impl object_store::Host for Session { key: String, body_handle: http_types::BodyHandle, ) -> Result { - let store = self.get_obj_store_key(store.into()).unwrap().clone(); + let store = self + .session + .get_obj_store_key(store.into()) + .unwrap() + .clone(); let key = ObjectKey::new(&key)?; - let bytes = self.take_body(body_handle.into())?.read_into_vec().await?; - let fut = futures::future::ok(self.obj_insert(store, key, bytes)); + let bytes = self + .session + .take_body(body_handle.into())? + .read_into_vec() + .await?; + let fut = futures::future::ok(self.session.obj_insert(store, key, bytes)); let task = PeekableTask::spawn(fut).await; Ok(self + .session .insert_pending_kv_insert(PendingKvInsertTask::new(task)) .into()) } @@ -106,6 +123,7 @@ impl object_store::Host for Session { handle: object_store::PendingInsertHandle, ) -> Result<(), types::Error> { Ok((self + .session .take_pending_kv_insert(handle.into())? .task() .recv() @@ -117,12 +135,17 @@ impl object_store::Host for Session { store: object_store::Handle, key: String, ) -> Result { - let store = self.get_obj_store_key(store.into()).unwrap().clone(); + let store = self + .session + .get_obj_store_key(store.into()) + .unwrap() + .clone(); let key = ObjectKey::new(&key)?; - let fut = futures::future::ok(self.obj_delete(store, key)); + let fut = futures::future::ok(self.session.obj_delete(store, key)); let task = PeekableTask::spawn(fut).await; Ok(self + .session .insert_pending_kv_delete(PendingKvDeleteTask::new(task)) .into()) } @@ -132,6 +155,7 @@ impl object_store::Host for Session { handle: object_store::PendingDeleteHandle, ) -> Result<(), types::Error> { Ok((self + .session .take_pending_kv_delete(handle.into())? .task() .recv() diff --git a/lib/src/component/purge.rs b/lib/src/component/purge.rs index 4d581559..275593c8 100644 --- a/lib/src/component/purge.rs +++ b/lib/src/component/purge.rs @@ -1,10 +1,10 @@ use { super::fastly::api::{purge, types}, - crate::{error::Error, session::Session}, + crate::{error::Error, linking::ComponentCtx}, }; #[async_trait::async_trait] -impl purge::Host for Session { +impl purge::Host for ComponentCtx { async fn purge_surrogate_key( &mut self, _surrogate_key: String, diff --git a/lib/src/component/secret_store.rs b/lib/src/component/secret_store.rs index b33f1dc6..eca1a567 100644 --- a/lib/src/component/secret_store.rs +++ b/lib/src/component/secret_store.rs @@ -1,14 +1,16 @@ use { super::fastly::api::{secret_store, types}, crate::{ - error::Error, secret_store::SecretLookup, session::Session, wiggle_abi::SecretStoreError, + error::Error, linking::ComponentCtx, secret_store::SecretLookup, + wiggle_abi::SecretStoreError, }, }; #[async_trait::async_trait] -impl secret_store::Host for Session { +impl secret_store::Host for ComponentCtx { async fn open(&mut self, name: String) -> Result { let handle = self + .session .secret_store_handle(&name) .ok_or(Error::SecretStoreError( SecretStoreError::UnknownSecretStore(name.to_string()), @@ -21,10 +23,14 @@ impl secret_store::Host for Session { store: secret_store::StoreHandle, key: String, ) -> Result, types::Error> { - let store_name = self.secret_store_name(store.into()).ok_or_else(|| { - types::Error::from(SecretStoreError::InvalidSecretStoreHandle(store.into())) - })?; + let store_name = self + .session + .secret_store_name(store.into()) + .ok_or_else(|| { + types::Error::from(SecretStoreError::InvalidSecretStoreHandle(store.into())) + })?; Ok(self + .session .secret_handle(&store_name, &key) .map(secret_store::SecretHandle::from)) } @@ -35,6 +41,7 @@ impl secret_store::Host for Session { max_len: u64, ) -> Result>, types::Error> { let lookup = self + .session .secret_lookup(secret.into()) .ok_or(Error::SecretStoreError( SecretStoreError::InvalidSecretHandle(secret.into()), @@ -45,6 +52,7 @@ impl secret_store::Host for Session { store_name, secret_name, } => self + .session .secret_stores() .get_store(store_name) .ok_or(Error::SecretStoreError( @@ -74,6 +82,6 @@ impl secret_store::Host for Session { &mut self, plaintext: Vec, ) -> Result { - Ok(self.add_secret(plaintext).into()) + Ok(self.session.add_secret(plaintext).into()) } } diff --git a/lib/src/component/types.rs b/lib/src/component/types.rs index 7b290ae1..bb35934a 100644 --- a/lib/src/component/types.rs +++ b/lib/src/component/types.rs @@ -2,7 +2,7 @@ use { super::fastly::api::types, crate::{ error::{self, HandleError}, - session::Session, + linking::ComponentCtx, }, http::header::InvalidHeaderName, }; @@ -12,7 +12,7 @@ pub enum TrappableError { Trap(anyhow::Error), } -impl types::Host for Session { +impl types::Host for ComponentCtx { fn convert_error(&mut self, err: TrappableError) -> wasmtime::Result { match err { TrappableError::Error(err) => Ok(err), diff --git a/lib/src/component/uap.rs b/lib/src/component/uap.rs index 5b893c21..4e7cd74b 100644 --- a/lib/src/component/uap.rs +++ b/lib/src/component/uap.rs @@ -1,6 +1,6 @@ use { super::fastly::api::{types, uap}, - crate::{error::Error, session::Session}, + crate::{error::Error, linking::ComponentCtx}, wasmtime::component::Resource, }; @@ -8,7 +8,7 @@ use { pub struct UserAgent {} #[async_trait::async_trait] -impl uap::HostUserAgent for Session { +impl uap::HostUserAgent for ComponentCtx { async fn family( &mut self, _agent: Resource, @@ -47,7 +47,7 @@ impl uap::HostUserAgent for Session { } #[async_trait::async_trait] -impl uap::Host for Session { +impl uap::Host for ComponentCtx { async fn parse(&mut self, _user_agent: String) -> Result, types::Error> { // not available Err(Error::NotAvailable("User-agent parsing is not available").into()) diff --git a/lib/src/linking.rs b/lib/src/linking.rs index e81bed4d..e2ffd80c 100644 --- a/lib/src/linking.rs +++ b/lib/src/linking.rs @@ -95,7 +95,7 @@ impl wasmtime::ResourceLimiter for Limiter { pub struct ComponentCtx { table: wasmtime_wasi::ResourceTable, wasi: wasmtime_wasi::WasiCtx, - session: Session, + pub(crate) session: Session, guest_profiler: Option>, limiter: Limiter, }