use std::ops::Deref;
use std::sync::Arc;
use futures::StreamExt;
use plexus_client::{DenyReason, ItemStream, MethodRef, RemoteError, StreamItem, Transport};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use super::auth::AuthContext;
use super::hub_transport::HubTransport;
use super::in_process::to_client_item;
use super::peer::PeerError;
use super::plexus::{DynamicHub, PlexusError};
use super::schema::Need;
use super::types::{PlexusStreamItem, StreamMetadata};
pub const HOST_SCHEMA: &str = "host.schema";
pub const HOST_WHOAMI: &str = "host.whoami";
pub const HOST_LOG: &str = "host.log";
pub const HOST_NOTIFY: &str = "host.notify";
pub const STANDARD: [&str; 4] = [HOST_SCHEMA, HOST_WHOAMI, HOST_LOG, HOST_NOTIFY];
pub fn is_standard(method: &str) -> bool {
STANDARD.contains(&method) || method.ends_with(".schema")
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Notice {
pub child: String,
pub topic: String,
#[serde(default)]
pub data: Value,
#[serde(default, rename = "for", skip_serializing_if = "Option::is_none")]
pub for_: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NeedsMiss {
pub child: String,
pub service: String,
pub missing: Vec<String>,
pub changed: Vec<String>,
}
impl NeedsMiss {
pub fn methods(&self) -> Vec<String> {
self.missing.iter().chain(self.changed.iter()).cloned().collect()
}
}
impl std::fmt::Display for NeedsMiss {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "`{}` cannot be grafted onto `{}`:", self.child, self.service)?;
if !self.missing.is_empty() {
write!(f, " it needs {}, which `{}` does not serve", self.missing.join(", "), self.service)?;
}
if !self.changed.is_empty() {
if !self.missing.is_empty() {
f.write_str(";")?;
}
write!(f, " the shape of {} moved since it was built", self.changed.join(", "))?;
}
Ok(())
}
}
impl std::error::Error for NeedsMiss {}
impl From<NeedsMiss> for PeerError {
fn from(m: NeedsMiss) -> Self {
PeerError::ShapeMismatch { wanted: format!("the needs of `{}`", m.child), got: m.service.clone(), methods: m.methods() }
}
}
pub fn check_needs(child: &str, service: &str, needs: &[Need], served: impl Fn(&str) -> Option<String>) -> Result<(), NeedsMiss> {
let (mut missing, mut changed) = (Vec::new(), Vec::new());
for need in needs {
if is_standard(&need.method) {
continue;
}
match served(&need.method) {
None => missing.push(need.method.clone()),
Some(h) if !need.hash.is_empty() && !h.is_empty() && h != need.hash => changed.push(need.method.clone()),
Some(_) => {}
}
}
if missing.is_empty() && changed.is_empty() {
Ok(())
} else {
Err(NeedsMiss { child: child.to_string(), service: service.to_string(), missing, changed })
}
}
pub fn not_needed(method: &str) -> PlexusError {
PlexusError::Forbidden { reason: super::plexus::AuthzDenyReason::NotNeeded { method: method.to_string() } }
}
fn meta(child: &str) -> StreamMetadata {
StreamMetadata::new(vec![child.to_string()], super::context::PlexusContext::hash())
}
pub(crate) fn standard_answer(service: &DynamicHub, child: &str, method: &str, params: &Value, caller: Option<&AuthContext>) -> Option<Vec<PlexusStreamItem>> {
let who = caller.and_then(|a| a.principal()).map(|p| p.to_string());
let content = match method {
HOST_WHOAMI => json!({ "for": who, "child": child, "service": service.runtime_namespace() }),
HOST_LOG => {
let level = params.get("level").and_then(Value::as_str).unwrap_or("info");
let message = params.get("message").and_then(Value::as_str).unwrap_or_default();
match level {
"error" => tracing::error!(target: "plexus::child", child = %child, r#for = ?who, "{message}"),
"warn" => tracing::warn!(target: "plexus::child", child = %child, r#for = ?who, "{message}"),
"debug" => tracing::debug!(target: "plexus::child", child = %child, r#for = ?who, "{message}"),
"trace" => tracing::trace!(target: "plexus::child", child = %child, r#for = ?who, "{message}"),
_ => tracing::info!(target: "plexus::child", child = %child, r#for = ?who, "{message}"),
}
json!({ "logged": true })
}
HOST_NOTIFY => {
let notice = Notice {
child: child.to_string(),
topic: params.get("topic").and_then(Value::as_str).unwrap_or_default().to_string(),
data: params.get("data").cloned().unwrap_or(Value::Null),
for_: who,
};
let heard = service.notify(notice);
json!({ "heard": heard })
}
_ => return None,
};
Some(vec![PlexusStreamItem::data(meta(child), method.to_string(), content), PlexusStreamItem::done(meta(child))])
}
#[derive(Clone)]
enum Route {
#[cfg(not(target_arch = "wasm32"))]
Host(super::remote_child::HostTransport),
Local(HubTransport),
}
#[derive(Clone)]
pub struct ParentTransport {
route: Route,
child: Arc<str>,
needs: Arc<[String]>,
}
impl std::fmt::Debug for ParentTransport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let how = match &self.route {
#[cfg(not(target_arch = "wasm32"))]
Route::Host(_) => "host",
Route::Local(_) => "local",
};
f.debug_struct("ParentTransport").field("child", &self.child).field("route", &how).field("needs", &self.needs).finish()
}
}
fn refused(error: RemoteError, message: String) -> ItemStream<'static> {
let item = StreamItem::Error { metadata: None, recoverable: false, message: Some(message), code: None, error, fields: Default::default() };
Box::pin(futures::stream::once(async move { Ok(item) }))
}
impl ParentTransport {
pub fn in_call(child: &str, needs: Arc<[String]>) -> Result<Self, PlexusError> {
let ctx = crate::call::context().map_err(|e| PlexusError::ExecutionError(format!("`{child}` reaches its parent only inside a call: {e}")))?;
#[cfg(not(target_arch = "wasm32"))]
if ctx.host_link().is_some() {
let t = super::remote_child::host().map_err(|e| PlexusError::ExecutionError(format!("`{child}`: {e}")))?;
return Ok(Self { route: Route::Host(t), child: Arc::from(child), needs });
}
let service = super::service_scope::current().and_then(|s| s.upgrade()).ok_or_else(|| {
PlexusError::ExecutionError(format!("`{child}` reaches its parent only in a call a service routed or forwarded"))
})?;
Ok(Self::local(Arc::new(service), ctx.auth().cloned(), child, needs))
}
pub fn local(service: Arc<DynamicHub>, caller: Option<AuthContext>, child: &str, needs: Arc<[String]>) -> Self {
Self { route: Route::Local(HubTransport::with(service, caller)), child: Arc::from(child), needs }
}
pub fn needs(&self) -> &[String] {
&self.needs
}
pub fn is_remote(&self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
if let Route::Host(_) = self.route {
return true;
}
false
}
}
impl Transport for ParentTransport {
fn call(&self, method: MethodRef, params: Value) -> ItemStream<'_> {
let q = method.qualified();
if !is_standard(&q) && !self.needs.iter().any(|n| *n == q) {
let e = not_needed(&q);
return refused(RemoteError::Forbidden { reason: DenyReason::NotNeeded { method: q.clone() } }, e.to_string());
}
match &self.route {
#[cfg(not(target_arch = "wasm32"))]
Route::Host(t) => t.call(method, params),
Route::Local(t) => {
if let Some(items) = standard_answer(t.hub(), &self.child, &q, ¶ms, t.caller()) {
return Box::pin(futures::stream::iter(items.into_iter().map(|i| Ok(to_client_item(i)))));
}
if q == HOST_SCHEMA {
let m = MethodRef::named(t.hub().runtime_namespace().to_string(), "schema");
return t.call(m, params);
}
t.call(method, params)
}
}
}
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("{0}")]
pub struct UpwardError(pub String);
pub struct Upward<'a> {
t: &'a ParentTransport,
}
impl Upward<'_> {
async fn one(&self, method: &'static str, params: Value) -> Result<Value, UpwardError> {
let (path, name) = method.split_once('.').expect("a standard method is `host.<name>`");
let mut s = self.t.call(MethodRef::named(path, name), params);
while let Some(item) = s.next().await {
match item {
Ok(StreamItem::Data { content, .. }) => return Ok(content),
Ok(StreamItem::Error { error, message, .. }) => return Err(UpwardError(message.unwrap_or_else(|| error.to_string()))),
Ok(StreamItem::Done { .. }) => break,
Ok(_) => {}
Err(e) => return Err(UpwardError(e.to_string())),
}
}
Err(UpwardError(format!("`{method}` answered nothing")))
}
pub async fn schema(&self) -> Result<Value, UpwardError> {
self.one(HOST_SCHEMA, json!({})).await
}
pub async fn whoami(&self) -> Result<Value, UpwardError> {
self.one(HOST_WHOAMI, json!({})).await
}
pub async fn log(&self, level: &str, message: &str) -> Result<(), UpwardError> {
self.one(HOST_LOG, json!({ "level": level, "message": message })).await.map(|_| ())
}
pub async fn notify(&self, topic: &str, data: Value) -> Result<u64, UpwardError> {
self.one(HOST_NOTIFY, json!({ "topic": topic, "data": data })).await.map(|v| v["heard"].as_u64().unwrap_or(0))
}
}
pub struct Parent<C> {
client: C,
transport: ParentTransport,
}
impl<C> Parent<C> {
pub fn new(transport: ParentTransport, make: impl FnOnce(ParentTransport) -> C) -> Self {
Self { client: make(transport.clone()), transport }
}
#[doc(hidden)]
pub fn __in_call(child: &str, needs: Arc<[String]>, make: impl FnOnce(ParentTransport) -> C) -> Result<Self, PlexusError> {
Ok(Self::new(ParentTransport::in_call(child, needs)?, make))
}
pub fn client(&self) -> &C {
&self.client
}
pub fn into_inner(self) -> C {
self.client
}
pub fn transport(&self) -> &ParentTransport {
&self.transport
}
pub fn host(&self) -> Upward<'_> {
Upward { t: &self.transport }
}
}
impl<C> Deref for Parent<C> {
type Target = C;
fn deref(&self) -> &C {
&self.client
}
}
impl<C: std::fmt::Debug> std::fmt::Debug for Parent<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Parent").field("client", &self.client).field("transport", &self.transport).finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_need_missing_or_moved_is_named() {
let needs = [Need::new("shop.items", "aaaa"), Need::new("shop.price", "bbbb"), Need::new("shop.tags", ""), Need::new("host.whoami", "")];
let served = |p: &str| match p {
"shop.items" => Some("aaaa".to_string()),
"shop.price" => Some("cccc".to_string()),
_ => None,
};
let miss = check_needs("restock", "svc", &needs, served).unwrap_err();
assert_eq!(miss.missing, ["shop.tags"]);
assert_eq!(miss.changed, ["shop.price"]);
let text = miss.to_string();
assert!(text.contains("shop.tags") && text.contains("shop.price") && text.contains("restock"), "{text}");
assert!(check_needs("restock", "svc", &needs[..1], served).is_ok());
}
#[test]
fn the_standard_four_and_schemas_need_no_declaration() {
for m in STANDARD {
assert!(is_standard(m));
}
assert!(is_standard("shop.schema"));
assert!(!is_standard("shop.items"));
assert!(!is_standard("host.restart"));
}
}