Revert "Decode binary native preview replies without SVG text conversion"
This reverts commit 4df8983ff5.
This commit is contained in:
1 parent
4df8983ff5
commit
6adadd7237
3 files changed
+11
-73
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use super::{Edit, EditorSession, MAX_EDIT_BODY, PreviewReply, reply};
|
||||
use serde_json::{Value, json};
|
||||
use serde_json::json;
|
||||
use std::{
|
||||
sync::{Mutex, Weak, mpsc},
|
||||
time::Instant,
|
||||
@@ -67,7 +67,6 @@ pub(super) fn open(request: Request, session: Weak<Mutex<EditorSession>>) {
|
||||
break;
|
||||
};
|
||||
let sequence = edit.sequence;
|
||||
let binary_preview = edit.binary_preview;
|
||||
let (status, body) = if let Some(session) = session.upgrade() {
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
session
|
||||
@@ -86,7 +85,9 @@ pub(super) fn open(request: Request, session: Weak<Mutex<EditorSession>>) {
|
||||
)
|
||||
};
|
||||
if socket
|
||||
.send(preview_message(sequence, status, body, binary_preview))
|
||||
.send(Message::text(
|
||||
json!({ "sequence": sequence, "status": status, "body": body }).to_string(),
|
||||
))
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
@@ -95,41 +96,6 @@ pub(super) fn open(request: Request, session: Weak<Mutex<EditorSession>>) {
|
||||
});
|
||||
}
|
||||
|
||||
fn preview_message(sequence: u64, status: u16, mut body: Value, binary: bool) -> Message {
|
||||
if binary
|
||||
&& (200..300).contains(&status)
|
||||
&& let Some(pages) = body
|
||||
.pointer_mut("/update/pages")
|
||||
.and_then(Value::as_array_mut)
|
||||
&& pages.iter().all(|page| page["svg"].is_string())
|
||||
{
|
||||
// The frame starts with a little-endian JSON length. After the JSON,
|
||||
// the page SVGs are concatenated in the same order as their byte lengths.
|
||||
let mut svgs = Vec::with_capacity(pages.len());
|
||||
for page in pages {
|
||||
let Value::String(svg) = std::mem::take(&mut page["svg"]) else {
|
||||
unreachable!("page SVG strings were checked before encoding");
|
||||
};
|
||||
page["svg"] = svg.len().into();
|
||||
svgs.push(svg);
|
||||
}
|
||||
|
||||
let metadata = json!({ "sequence": sequence, "status": status, "body": body });
|
||||
let mut frame = vec![0; 4];
|
||||
serde_json::to_writer(&mut frame, &metadata)
|
||||
.expect("serializing preview metadata to memory");
|
||||
let metadata_len = u32::try_from(frame.len() - 4).expect("preview metadata fits in u32");
|
||||
frame[..4].copy_from_slice(&metadata_len.to_le_bytes());
|
||||
frame.reserve(svgs.iter().map(String::len).sum());
|
||||
for svg in svgs {
|
||||
frame.extend_from_slice(svg.as_bytes());
|
||||
}
|
||||
return Message::binary(frame);
|
||||
}
|
||||
|
||||
Message::text(json!({ "sequence": sequence, "status": status, "body": body }).to_string())
|
||||
}
|
||||
|
||||
fn header<'a>(request: &'a Request, name: &'static str) -> Option<&'a str> {
|
||||
let mut matches = request
|
||||
.headers()
|
||||
|
||||
Reference in new issue
Block a user