Skip to content

Commit

Permalink
Update compute.wit and the adapter for some api fixes (#414)
Browse files Browse the repository at this point in the history
While implementing compute.wit elsewhere, I made a few changes that better reflect the translation to the underlying hostcalls. This PR backports those changes to compute.wit, and folds them in to the adapter and component implementation.
  • Loading branch information
elliottt authored Aug 13, 2024
1 parent 9a2e8fe commit ab7f0f0
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 58 deletions.
13 changes: 10 additions & 3 deletions crates/adapter/src/fastly/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{convert_result, BodyHandle, FastlyStatus, RequestHandle};
use crate::{alloc_result, TrappingUnwrap};
use crate::{alloc_result_opt, TrappingUnwrap};

pub type CacheHandle = u32;

Expand Down Expand Up @@ -35,6 +35,7 @@ pub struct CacheWriteOptions {
pub length: CacheObjectLength,
pub user_metadata_ptr: *const u8,
pub user_metadata_len: usize,
pub edge_max_age_ns: u64,
}

bitflags::bitflags! {
Expand Down Expand Up @@ -198,6 +199,7 @@ mod cache {
surrogate_keys: make_vec!(surrogate_keys_ptr, surrogate_keys_len),
length: (*options).length,
user_metadata: make_vec!(user_metadata_ptr, user_metadata_len),
edge_max_age_ns: (*options).edge_max_age_ns,
}
}

Expand Down Expand Up @@ -345,11 +347,16 @@ mod cache {
user_metadata_out_len: usize,
nwritten_out: *mut usize,
) -> FastlyStatus {
alloc_result!(
alloc_result_opt!(
user_metadata_out_ptr,
user_metadata_out_len,
nwritten_out,
{ cache::get_user_metadata(handle) }
{
cache::get_user_metadata(
handle,
u64::try_from(user_metadata_out_len).trapping_unwrap(),
)
}
)
}

Expand Down
34 changes: 20 additions & 14 deletions crates/adapter/src/fastly/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,16 +1346,17 @@ pub mod fastly_http_req {

FastlyStatus::OK
}
Err((detail, e)) => {
Err(err) => {
unsafe {
*error_detail = detail
*error_detail = err
.detail
.unwrap_or_else(|| http_req::SendErrorDetailTag::Uninitialized.into())
.into();
*resp_handle_out = INVALID_HANDLE;
*resp_body_handle_out = INVALID_HANDLE;
}

e.into()
err.error.into()
}
}
}
Expand Down Expand Up @@ -1556,16 +1557,17 @@ pub mod fastly_http_req {

FastlyStatus::OK
},
Err((detail, e)) => {
Err(err) => {
unsafe {
*error_detail = detail
*error_detail = err
.detail
.unwrap_or_else(|| http_req::SendErrorDetailTag::Uninitialized.into())
.into();
*is_done_out = 0;
*resp_handle_out = INVALID_HANDLE;
*resp_body_handle_out = INVALID_HANDLE;
}
e.into()
err.error.into()
}
}
}
Expand Down Expand Up @@ -1614,15 +1616,16 @@ pub mod fastly_http_req {
}
FastlyStatus::OK
}
Err((detail, e)) => {
Err(err) => {
unsafe {
*error_detail = detail
*error_detail = err
.detail
.unwrap_or_else(|| http_req::SendErrorDetailTag::Uninitialized.into())
.into();
*resp_handle_out = INVALID_HANDLE;
*resp_body_handle_out = INVALID_HANDLE;
}
e.into()
err.error.into()
}
}
}
Expand All @@ -1644,15 +1647,16 @@ pub mod fastly_http_req {

FastlyStatus::OK
}
Err((detail, e)) => {
Err(err) => {
unsafe {
*error_detail = detail
*error_detail = err
.detail
.unwrap_or_else(|| http_req::SendErrorDetailTag::Uninitialized.into())
.into();
*resp_handle_out = INVALID_HANDLE;
*resp_body_handle_out = INVALID_HANDLE;
}
e.into()
err.error.into()
}
}
}
Expand Down Expand Up @@ -2654,12 +2658,13 @@ pub mod fastly_backend {
) -> FastlyStatus {
let backend = unsafe { slice::from_raw_parts(backend_ptr, backend_len) };
match backend::get_ssl_min_version(backend) {
Ok(res) => {
Ok(Some(res)) => {
unsafe {
*value = u32::from(res);
}
FastlyStatus::OK
}
Ok(None) => FastlyStatus::NONE,
Err(e) => e.into(),
}
}
Expand All @@ -2672,12 +2677,13 @@ pub mod fastly_backend {
) -> FastlyStatus {
let backend = unsafe { slice::from_raw_parts(backend_ptr, backend_len) };
match backend::get_ssl_max_version(backend) {
Ok(res) => {
Ok(Some(res)) => {
unsafe {
*value = u32::from(res);
}
FastlyStatus::OK
}
Ok(None) => FastlyStatus::NONE,
Err(e) => e.into(),
}
}
Expand Down
Binary file modified lib/data/viceroy-component-adapter.wasm
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/src/component/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl backend::Host for Session {
&mut self,
backend: String,
max_len: u64,
) -> Result<Option<String>, types::Error> {
) -> Result<Option<Vec<u8>>, types::Error> {
let backend = self.backend(&backend).ok_or(Error::InvalidArgument)?;
if let Some(host) = backend.override_host.as_ref() {
let host = host.to_str()?;
Expand All @@ -61,7 +61,7 @@ impl backend::Host for Session {
.into());
}

Ok(Some(String::from(host)))
Ok(Some(host.as_bytes().to_owned()))
} else {
Ok(None)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ impl backend::Host for Session {
async fn get_ssl_min_version(
&mut self,
backend: String,
) -> Result<http_types::TlsVersion, types::Error> {
) -> Result<Option<http_types::TlsVersion>, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
// health checks are not enabled in Viceroy :(
Expand All @@ -129,7 +129,7 @@ impl backend::Host for Session {
async fn get_ssl_max_version(
&mut self,
backend: String,
) -> Result<http_types::TlsVersion, types::Error> {
) -> Result<Option<http_types::TlsVersion>, types::Error> {
// just doing this to get a different error if the backend doesn't exist
let _ = self.backend(&backend).ok_or(Error::InvalidArgument)?;
// health checks are not enabled in Viceroy :(
Expand Down
6 changes: 5 additions & 1 deletion lib/src/component/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ impl cache::Host for Session {
.into())
}

async fn get_user_metadata(&mut self, _handle: cache::Handle) -> Result<String, types::Error> {
async fn get_user_metadata(
&mut self,
_handle: cache::Handle,
_max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
Err(Error::Unsupported {
msg: "Cache API primitives not yet supported",
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl config_store::Host for Session {
store: config_store::Handle,
name: String,
max_len: u64,
) -> Result<Option<String>, types::Error> {
) -> Result<Option<Vec<u8>>, types::Error> {
let dict = &self.dictionary(store.into())?.contents;

let item = if let Some(item) = dict.get(&name) {
Expand All @@ -28,6 +28,6 @@ impl config_store::Host for Session {
return Err(types::Error::BufferLen(u64::try_from(item.len()).unwrap()));
}

Ok(Some(item.clone()))
Ok(Some(item.as_bytes().to_owned()))
}
}
4 changes: 2 additions & 2 deletions lib/src/component/device_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ impl device_detection::Host for Session {
&mut self,
user_agent: String,
max_len: u64,
) -> Result<Option<String>, types::Error> {
) -> Result<Option<Vec<u8>>, types::Error> {
if let Some(result) = self.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),
));
}

Ok(Some(result))
Ok(Some(result.into_bytes()))
} else {
Ok(None)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl dictionary::Host for Session {
h: dictionary::Handle,
key: String,
max_len: u64,
) -> Result<Option<String>, types::Error> {
) -> Result<Option<Vec<u8>>, types::Error> {
let dict = &self.dictionary(h.into())?.contents;

let item = if let Some(item) = dict.get(&key) {
Expand All @@ -28,6 +28,6 @@ impl dictionary::Host for Session {
return Err(types::Error::BufferLen(u64::try_from(item.len()).unwrap()));
}

Ok(Some(item.clone()))
Ok(Some(item.as_bytes().to_owned()))
}
}
7 changes: 5 additions & 2 deletions lib/src/component/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use {
};

impl types::Error {
pub fn with_empty_detail(self) -> (Option<http_req::SendErrorDetail>, Self) {
(None, self)
pub fn with_empty_detail(self) -> http_req::ErrorWithDetail {
http_req::ErrorWithDetail {
detail: None,
error: self,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/component/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {

#[async_trait::async_trait]
impl geo::Host for Session {
async fn lookup(&mut self, octets: Vec<u8>, max_len: u64) -> Result<String, types::Error> {
async fn lookup(&mut self, octets: Vec<u8>, max_len: u64) -> Result<Vec<u8>, types::Error> {
let ip_addr: IpAddr = match octets.len() {
4 => IpAddr::V4(Ipv4Addr::from(
TryInto::<[u8; 4]>::try_into(octets).unwrap(),
Expand All @@ -29,6 +29,6 @@ impl geo::Host for Session {
.into());
}

Ok(json)
Ok(json.into_bytes())
}
}
10 changes: 4 additions & 6 deletions lib/src/component/http_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl http_req::Host for Session {
h: http_types::RequestHandle,
b: http_types::BodyHandle,
backend_name: String,
) -> Result<http_types::Response, (Option<http_req::SendErrorDetail>, types::Error)> {
) -> Result<http_types::Response, http_req::ErrorWithDetail> {
// This initial implementation ignores the error detail field
self.send(h, b, backend_name)
.await
Expand Down Expand Up @@ -478,8 +478,7 @@ impl http_req::Host for Session {
async fn pending_req_poll_v2(
&mut self,
h: http_types::PendingRequestHandle,
) -> Result<Option<http_types::Response>, (Option<http_req::SendErrorDetail>, types::Error)>
{
) -> Result<Option<http_types::Response>, http_req::ErrorWithDetail> {
self.pending_req_poll(h)
.await
.map_err(types::Error::with_empty_detail)
Expand All @@ -497,7 +496,7 @@ impl http_req::Host for Session {
async fn pending_req_wait_v2(
&mut self,
h: http_types::PendingRequestHandle,
) -> Result<http_types::Response, (Option<http_req::SendErrorDetail>, types::Error)> {
) -> Result<http_types::Response, http_req::ErrorWithDetail> {
self.pending_req_wait(h)
.await
.map_err(types::Error::with_empty_detail)
Expand Down Expand Up @@ -550,8 +549,7 @@ impl http_req::Host for Session {
async fn pending_req_select_v2(
&mut self,
h: Vec<http_types::PendingRequestHandle>,
) -> Result<(u32, http_types::Response), (Option<http_req::SendErrorDetail>, types::Error)>
{
) -> Result<(u32, http_types::Response), http_req::ErrorWithDetail> {
self.pending_req_select(h)
.await
.map_err(types::Error::with_empty_detail)
Expand Down
8 changes: 4 additions & 4 deletions lib/src/component/secret_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl secret_store::Host for Session {
&mut self,
secret: secret_store::SecretHandle,
max_len: u64,
) -> Result<Option<String>, types::Error> {
) -> Result<Option<Vec<u8>>, types::Error> {
let lookup = self
.secret_lookup(secret.into())
.ok_or(Error::SecretStoreError(
Expand Down Expand Up @@ -67,13 +67,13 @@ impl secret_store::Host for Session {
.into());
}

Ok(Some(String::from(std::str::from_utf8(plaintext)?)))
Ok(Some(plaintext.to_owned()))
}

async fn from_bytes(
&mut self,
plaintext: String,
plaintext: Vec<u8>,
) -> Result<secret_store::SecretHandle, types::Error> {
Ok(self.add_secret(Vec::from(plaintext.as_bytes())).into())
Ok(self.add_secret(plaintext).into())
}
}
Loading

0 comments on commit ab7f0f0

Please sign in to comment.