2023-07-19 14:35:56 +00:00
|
|
|
use axum::extract::ws::Message;
|
2023-08-16 12:27:53 +00:00
|
|
|
use opentelemetry::Context as TelemetryContext;
|
2022-07-04 01:03:26 +00:00
|
|
|
use serde::Serialize;
|
2023-07-01 19:09:15 +00:00
|
|
|
use serde_json::{json, Value as Json};
|
2022-07-04 01:03:26 +00:00
|
|
|
use std::borrow::Cow;
|
2022-07-07 10:25:22 +00:00
|
|
|
use surrealdb::channel::Sender;
|
2023-07-01 19:09:15 +00:00
|
|
|
use surrealdb::dbs;
|
|
|
|
use surrealdb::dbs::Notification;
|
2023-04-29 15:58:22 +00:00
|
|
|
use surrealdb::sql;
|
2022-07-04 01:03:26 +00:00
|
|
|
use surrealdb::sql::Value;
|
2023-08-03 14:59:05 +00:00
|
|
|
use tracing::Span;
|
2022-07-04 01:03:26 +00:00
|
|
|
|
2023-08-03 14:59:05 +00:00
|
|
|
use crate::err;
|
|
|
|
use crate::rpc::CONN_CLOSED_ERR;
|
2023-08-16 12:27:53 +00:00
|
|
|
use crate::telemetry::metrics::ws::record_rpc;
|
2023-08-03 14:59:05 +00:00
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
2023-08-03 14:59:05 +00:00
|
|
|
pub enum OutputFormat {
|
2022-10-25 13:19:44 +00:00
|
|
|
Json, // JSON
|
|
|
|
Cbor, // CBOR
|
2023-03-27 12:52:28 +00:00
|
|
|
Pack, // MessagePack
|
2022-10-25 13:19:44 +00:00
|
|
|
Full, // Full type serialization
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 19:09:15 +00:00
|
|
|
/// The data returned by the database
|
|
|
|
// The variants here should be in exactly the same order as `surrealdb::engine::remote::ws::Data`
|
|
|
|
// In future, they will possibly be merged to avoid having to keep them in sync.
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub enum Data {
|
|
|
|
/// Generally methods return a `sql::Value`
|
|
|
|
Other(Value),
|
|
|
|
/// The query methods, `query` and `query_with` return a `Vec` of responses
|
|
|
|
Query(Vec<dbs::Response>),
|
|
|
|
/// Live queries return a notification
|
|
|
|
Live(Notification),
|
|
|
|
// Add new variants here
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Value> for Data {
|
|
|
|
fn from(v: Value) -> Self {
|
|
|
|
Data::Other(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:59:05 +00:00
|
|
|
impl From<String> for Data {
|
|
|
|
fn from(v: String) -> Self {
|
|
|
|
Data::Other(Value::from(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 19:09:15 +00:00
|
|
|
impl From<Vec<dbs::Response>> for Data {
|
|
|
|
fn from(v: Vec<dbs::Response>) -> Self {
|
|
|
|
Data::Query(v)
|
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 19:09:15 +00:00
|
|
|
impl From<Notification> for Data {
|
|
|
|
fn from(n: Notification) -> Self {
|
|
|
|
Data::Live(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct Response {
|
|
|
|
id: Option<Value>,
|
|
|
|
result: Result<Data, Failure>,
|
2022-10-25 13:19:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 19:09:15 +00:00
|
|
|
impl Response {
|
2023-05-05 18:54:49 +00:00
|
|
|
/// Convert and simplify the value into JSON
|
2023-05-02 11:17:27 +00:00
|
|
|
#[inline]
|
2023-05-05 18:54:49 +00:00
|
|
|
fn simplify(self) -> Json {
|
2023-07-01 19:09:15 +00:00
|
|
|
let mut value = match self.result {
|
|
|
|
Ok(data) => {
|
|
|
|
let value = match data {
|
|
|
|
Data::Query(vec) => sql::to_value(vec).unwrap(),
|
2023-08-18 15:21:01 +00:00
|
|
|
Data::Live(notification) => sql::to_value(notification).unwrap(),
|
2023-07-01 19:09:15 +00:00
|
|
|
Data::Other(value) => value,
|
|
|
|
};
|
|
|
|
json!({
|
|
|
|
"result": Json::from(value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(failure) => json!({
|
|
|
|
"error": failure,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
if let Some(id) = self.id {
|
|
|
|
value["id"] = id.into();
|
|
|
|
}
|
|
|
|
value
|
2023-04-29 15:58:22 +00:00
|
|
|
}
|
2023-07-01 19:09:15 +00:00
|
|
|
|
2023-05-05 18:54:49 +00:00
|
|
|
/// Send the response to the WebSocket channel
|
2023-11-13 17:19:47 +00:00
|
|
|
pub async fn send(self, out: OutputFormat, chn: &Sender<Message>) {
|
2023-08-03 14:59:05 +00:00
|
|
|
let span = Span::current();
|
|
|
|
|
2023-09-08 15:48:12 +00:00
|
|
|
debug!("Process RPC response");
|
2023-08-03 14:59:05 +00:00
|
|
|
|
2023-08-16 12:27:53 +00:00
|
|
|
let is_error = self.result.is_err();
|
2023-08-03 14:59:05 +00:00
|
|
|
if let Err(err) = &self.result {
|
|
|
|
span.record("otel.status_code", "Error");
|
|
|
|
span.record(
|
|
|
|
"otel.status_message",
|
|
|
|
format!("code: {}, message: {}", err.code, err.message),
|
|
|
|
);
|
|
|
|
span.record("rpc.jsonrpc.error_code", err.code);
|
|
|
|
span.record("rpc.jsonrpc.error_message", err.message.as_ref());
|
|
|
|
}
|
|
|
|
|
2023-08-16 12:27:53 +00:00
|
|
|
let (res_size, message) = match out {
|
2023-08-03 14:59:05 +00:00
|
|
|
OutputFormat::Json => {
|
2023-05-05 18:54:49 +00:00
|
|
|
let res = serde_json::to_string(&self.simplify()).unwrap();
|
2023-08-16 12:27:53 +00:00
|
|
|
(res.len(), Message::Text(res))
|
2022-10-25 13:19:44 +00:00
|
|
|
}
|
2023-08-03 14:59:05 +00:00
|
|
|
OutputFormat::Cbor => {
|
2023-05-05 18:54:49 +00:00
|
|
|
let res = serde_cbor::to_vec(&self.simplify()).unwrap();
|
2023-08-16 12:27:53 +00:00
|
|
|
(res.len(), Message::Binary(res))
|
2022-10-25 13:19:44 +00:00
|
|
|
}
|
2023-08-03 14:59:05 +00:00
|
|
|
OutputFormat::Pack => {
|
2023-05-05 18:54:49 +00:00
|
|
|
let res = serde_pack::to_vec(&self.simplify()).unwrap();
|
2023-08-16 12:27:53 +00:00
|
|
|
(res.len(), Message::Binary(res))
|
2022-10-25 13:19:44 +00:00
|
|
|
}
|
2023-08-03 14:59:05 +00:00
|
|
|
OutputFormat::Full => {
|
2023-07-01 19:09:15 +00:00
|
|
|
let res = surrealdb::sql::serde::serialize(&self).unwrap();
|
2023-08-16 12:27:53 +00:00
|
|
|
(res.len(), Message::Binary(res))
|
2022-10-25 13:38:23 +00:00
|
|
|
}
|
2023-07-01 19:09:15 +00:00
|
|
|
};
|
2023-08-03 14:59:05 +00:00
|
|
|
|
|
|
|
if let Err(err) = chn.send(message).await {
|
|
|
|
if err.to_string() != CONN_CLOSED_ERR {
|
|
|
|
error!("Error sending response: {}", err);
|
2023-08-16 12:27:53 +00:00
|
|
|
return;
|
2023-08-03 14:59:05 +00:00
|
|
|
}
|
|
|
|
};
|
2023-08-16 12:27:53 +00:00
|
|
|
|
|
|
|
record_rpc(&TelemetryContext::current(), res_size, is_error);
|
2022-07-07 10:25:22 +00:00
|
|
|
}
|
2022-07-04 01:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
|
|
|
pub struct Failure {
|
|
|
|
code: i64,
|
|
|
|
message: Cow<'static, str>,
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:59:05 +00:00
|
|
|
#[allow(dead_code)]
|
2022-07-04 01:03:26 +00:00
|
|
|
impl Failure {
|
|
|
|
pub const PARSE_ERROR: Failure = Failure {
|
|
|
|
code: -32700,
|
|
|
|
message: Cow::Borrowed("Parse error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INVALID_REQUEST: Failure = Failure {
|
|
|
|
code: -32600,
|
|
|
|
message: Cow::Borrowed("Invalid Request"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const METHOD_NOT_FOUND: Failure = Failure {
|
|
|
|
code: -32601,
|
|
|
|
message: Cow::Borrowed("Method not found"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INVALID_PARAMS: Failure = Failure {
|
|
|
|
code: -32602,
|
|
|
|
message: Cow::Borrowed("Invalid params"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const INTERNAL_ERROR: Failure = Failure {
|
|
|
|
code: -32603,
|
|
|
|
message: Cow::Borrowed("Internal error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn custom<S>(message: S) -> Failure
|
|
|
|
where
|
|
|
|
Cow<'static, str>: From<S>,
|
|
|
|
{
|
|
|
|
Failure {
|
|
|
|
code: -32000,
|
|
|
|
message: message.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 22:54:41 +00:00
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Create a JSON RPC result response
|
2023-07-01 19:09:15 +00:00
|
|
|
pub fn success<T: Into<Data>>(id: Option<Value>, data: T) -> Response {
|
2022-10-19 22:54:41 +00:00
|
|
|
Response {
|
|
|
|
id,
|
2023-07-01 19:09:15 +00:00
|
|
|
result: Ok(data.into()),
|
2022-10-19 22:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// Create a JSON RPC failure response
|
2023-07-01 19:09:15 +00:00
|
|
|
pub fn failure(id: Option<Value>, err: Failure) -> Response {
|
2022-10-19 22:54:41 +00:00
|
|
|
Response {
|
|
|
|
id,
|
2023-07-01 19:09:15 +00:00
|
|
|
result: Err(err),
|
2022-10-19 22:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-03 14:59:05 +00:00
|
|
|
|
|
|
|
impl From<err::Error> for Failure {
|
|
|
|
fn from(err: err::Error) -> Self {
|
|
|
|
Failure::custom(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoRpcResponse {
|
|
|
|
fn into_response(self, id: Option<Value>) -> Response;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> IntoRpcResponse for Result<T, E>
|
|
|
|
where
|
|
|
|
T: Into<Data>,
|
|
|
|
E: Into<Failure>,
|
|
|
|
{
|
|
|
|
fn into_response(self, id: Option<Value>) -> Response {
|
|
|
|
match self {
|
|
|
|
Ok(v) => success(id, v.into()),
|
|
|
|
Err(err) => failure(id, err.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|