Merge v3-t39: Rust builder behind host.sausage/cargo and the world cargo CLI (T39, P4)
# Conflicts: # flake.nix # host/src/process/mod.rs # host/tests/hook_git.rs # host/tests/hook_guest.rs # host/tests/process.rs # userland/Cargo.toml
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
//! A streaming Cargo response. Dropping the body cancels the builder's native process group.
|
||||
use crate::builder::Event;
|
||||
use bytes::Bytes;
|
||||
use http::{Request, Response};
|
||||
use http_body_util::BodyExt;
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
struct Body(tokio::sync::mpsc::Receiver<Event>);
|
||||
impl http_body::Body for Body {
|
||||
type Data = Bytes;
|
||||
type Error = wasmtime_wasi_http::Error;
|
||||
fn poll_frame(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<http_body::Frame<Bytes>, Self::Error>>> {
|
||||
self.0.poll_recv(cx).map(|event| {
|
||||
event.map(|event| {
|
||||
let mut bytes = serde_json::to_vec(&event).expect("Cargo event");
|
||||
bytes.push(b'\n');
|
||||
Ok(http_body::Frame::data(Bytes::from(bytes)))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
pub(crate) fn stream(
|
||||
api: &super::Api,
|
||||
request: Request<Bytes>,
|
||||
) -> Response<wasmtime_wasi_http::WasiBody> {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(16);
|
||||
let builder = api.builder.get().cloned();
|
||||
let request = serde_json::from_slice::<crate::builder::Request>(request.body());
|
||||
tokio::spawn(async move {
|
||||
let result = async {
|
||||
builder
|
||||
.ok_or_else(|| anyhow::anyhow!("builder unavailable"))?
|
||||
.execute(request?, &tx)
|
||||
.await
|
||||
}
|
||||
.await;
|
||||
if let Err(error) = result {
|
||||
let _ = tx
|
||||
.send(Event::Stderr {
|
||||
bytes: format!("cargo: {error:#}\n").into_bytes(),
|
||||
})
|
||||
.await;
|
||||
let _ = tx
|
||||
.send(Event::Exit {
|
||||
code: 1,
|
||||
executables: Vec::new(),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
});
|
||||
Response::builder()
|
||||
.header("content-type", "application/x-ndjson")
|
||||
.body(Body(rx).boxed_unsync())
|
||||
.unwrap()
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
//! JSON routes for host.sausage. The transport supplies the trusted caller.
|
||||
//! Host-owned pages use passkeys and typed proofs for every authorization ceremony.
|
||||
mod auth;
|
||||
mod cargo;
|
||||
pub(crate) use cargo::stream;
|
||||
mod pages;
|
||||
mod server;
|
||||
mod ssh;
|
||||
@@ -20,6 +22,7 @@ use std::{
|
||||
|
||||
pub struct Api {
|
||||
pub cred: Arc<Cred>,
|
||||
pub(crate) builder: std::sync::OnceLock<Arc<crate::builder::Builder>>,
|
||||
script: cred::quickjs::QuickJs,
|
||||
pub passkeys: Mutex<Passkeys<rusqlite::Connection>>,
|
||||
pub entry: EntryKey,
|
||||
@@ -49,6 +52,7 @@ impl Api {
|
||||
) -> Self {
|
||||
Self {
|
||||
cred: cred.into(),
|
||||
builder: Default::default(),
|
||||
script: cred::quickjs::QuickJs::default(),
|
||||
passkeys: Mutex::new(passkeys),
|
||||
entry,
|
||||
|
||||
Reference in New Issue
Block a user