mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-20 12:45:05 +00:00
Use msgpack for API (#1176)
This commit is contained in:
@@ -10,6 +10,7 @@ use std::{
|
||||
use hyper_tungstenite::tungstenite::{connect, Message};
|
||||
use rbx_dom_weak::types::Ref;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use librojo::{
|
||||
@@ -161,22 +162,16 @@ impl TestServeSession {
|
||||
|
||||
pub fn get_api_rojo(&self) -> Result<ServerInfoResponse, reqwest::Error> {
|
||||
let url = format!("http://localhost:{}/api/rojo", self.port);
|
||||
let body = reqwest::blocking::get(url)?.text()?;
|
||||
let body = reqwest::blocking::get(url)?.bytes()?;
|
||||
|
||||
let value = jsonc_parser::parse_to_serde_value(&body, &Default::default())
|
||||
.expect("Failed to parse JSON")
|
||||
.expect("No JSON value");
|
||||
Ok(serde_json::from_value(value).expect("Server returned malformed response"))
|
||||
Ok(deserialize_msgpack(&body).expect("Server returned malformed response"))
|
||||
}
|
||||
|
||||
pub fn get_api_read(&self, id: Ref) -> Result<ReadResponse<'_>, reqwest::Error> {
|
||||
let url = format!("http://localhost:{}/api/read/{}", self.port, id);
|
||||
let body = reqwest::blocking::get(url)?.text()?;
|
||||
let body = reqwest::blocking::get(url)?.bytes()?;
|
||||
|
||||
let value = jsonc_parser::parse_to_serde_value(&body, &Default::default())
|
||||
.expect("Failed to parse JSON")
|
||||
.expect("No JSON value");
|
||||
Ok(serde_json::from_value(value).expect("Server returned malformed response"))
|
||||
Ok(deserialize_msgpack(&body).expect("Server returned malformed response"))
|
||||
}
|
||||
|
||||
pub fn get_api_socket_packet(
|
||||
@@ -198,8 +193,8 @@ impl TestServeSession {
|
||||
}
|
||||
|
||||
match socket.read() {
|
||||
Ok(Message::Text(text)) => {
|
||||
let packet: SocketPacket = serde_json::from_str(&text)?;
|
||||
Ok(Message::Binary(binary)) => {
|
||||
let packet: SocketPacket = deserialize_msgpack(&binary)?;
|
||||
if packet.packet_type != packet_type {
|
||||
continue;
|
||||
}
|
||||
@@ -212,7 +207,7 @@ impl TestServeSession {
|
||||
return Err("WebSocket closed before receiving messages".into());
|
||||
}
|
||||
Ok(_) => {
|
||||
// Ignore other message types (ping, pong, binary)
|
||||
// Ignore other message types (ping, pong, text)
|
||||
continue;
|
||||
}
|
||||
Err(hyper_tungstenite::tungstenite::Error::Io(e))
|
||||
@@ -236,15 +231,37 @@ impl TestServeSession {
|
||||
) -> Result<SerializeResponse, reqwest::Error> {
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let url = format!("http://localhost:{}/api/serialize", self.port);
|
||||
let body = serde_json::to_string(&SerializeRequest {
|
||||
let body = serialize_msgpack(&SerializeRequest {
|
||||
session_id,
|
||||
ids: ids.to_vec(),
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
client.post(url).body((body).unwrap()).send()?.json()
|
||||
let body = client.post(url).body(body).send()?.bytes()?;
|
||||
|
||||
Ok(deserialize_msgpack(&body).expect("Server returned malformed response"))
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_msgpack<T: Serialize>(value: T) -> Result<Vec<u8>, rmp_serde::encode::Error> {
|
||||
let mut serialized = Vec::new();
|
||||
let mut serializer = rmp_serde::Serializer::new(&mut serialized)
|
||||
.with_human_readable()
|
||||
.with_struct_map();
|
||||
|
||||
value.serialize(&mut serializer)?;
|
||||
|
||||
Ok(serialized)
|
||||
}
|
||||
|
||||
fn deserialize_msgpack<'a, T: Deserialize<'a>>(
|
||||
input: &'a [u8],
|
||||
) -> Result<T, rmp_serde::decode::Error> {
|
||||
let mut deserializer = rmp_serde::Deserializer::new(input).with_human_readable();
|
||||
|
||||
T::deserialize(&mut deserializer)
|
||||
}
|
||||
|
||||
/// Probably-okay way to generate random enough port numbers for running the
|
||||
/// Rojo live server.
|
||||
///
|
||||
@@ -262,11 +279,7 @@ fn get_port_number() -> usize {
|
||||
/// Since the provided structure intentionally includes unredacted referents,
|
||||
/// some post-processing is done to ensure they don't show up in the model.
|
||||
pub fn serialize_to_xml_model(response: &SerializeResponse, redactions: &RedactionMap) -> String {
|
||||
let model_content = data_encoding::BASE64
|
||||
.decode(response.model_contents.model().as_bytes())
|
||||
.unwrap();
|
||||
|
||||
let mut dom = rbx_binary::from_reader(model_content.as_slice()).unwrap();
|
||||
let mut dom = rbx_binary::from_reader(response.model_contents.as_slice()).unwrap();
|
||||
// This makes me realize that maybe we need a `descendants_mut` iter.
|
||||
let ref_list: Vec<Ref> = dom.descendants().map(|inst| inst.referent()).collect();
|
||||
for referent in ref_list {
|
||||
|
||||
Reference in New Issue
Block a user