mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Pasting an image into a terminal or a Markdown file, or saving bytes from the UI, sent the data as a JSON array of numbers (3.6x the size), which Tauri and the remote server parse into a serde_json::Value first (~50x the bytes in memory: a 20 MB image took ~2.2 GB peak heap and 640 ms). Binary command arguments (fs_create_file_bytes, term_store_image) now travel as base64 text (IpcBytes): the same 20 MB takes ~112 MB and 32 ms. Native image drops reuse the base64 they already have. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
71 lines
2.9 KiB
Rust
71 lines
2.9 KiB
Rust
//! Binary command arguments over the remote protocol: base64 strings, not arrays of numbers.
|
|
|
|
use ide_core::{IpcBytes, NullSink};
|
|
use ide_host::rpc::{self, Dispatcher, Invoke};
|
|
use ide_host::{Host, HostConfig};
|
|
use ide_jsonrpc::{Handler, Peer};
|
|
use serde_json::{Value, json};
|
|
use std::sync::{Arc, OnceLock};
|
|
|
|
struct Client;
|
|
|
|
impl Handler for Client {
|
|
fn on_notification(&self, _: &str, _: Value) {}
|
|
fn on_request(&self, method: &str, _: Value) -> Result<Value, (i64, String)> {
|
|
Err((ide_jsonrpc::codes::METHOD_NOT_FOUND, method.to_owned()))
|
|
}
|
|
}
|
|
|
|
async fn connect(base: &std::path::Path) -> Peer {
|
|
let host = Arc::new(Host::new(HostConfig {
|
|
config_dir: base.join("config"),
|
|
data_dir: base.join("data"),
|
|
local_data_dir: base.join("data"),
|
|
languages: Arc::new(ide_language::LanguageRegistry::load(None).unwrap()),
|
|
syntax: Arc::default(),
|
|
sink_for: Box::new(|_| Arc::new(NullSink)),
|
|
owner: |window| window,
|
|
runtime: tokio::runtime::Handle::current(),
|
|
project_environment: false,
|
|
}));
|
|
let (client_io, server_io) = tokio::io::duplex(1 << 20);
|
|
let (sr, sw) = tokio::io::split(server_io);
|
|
tokio::spawn(rpc::serve(host, Dispatcher::standard(), Arc::new(OnceLock::new()), sr, sw));
|
|
let (cr, cw) = tokio::io::split(client_io);
|
|
Peer::connect(cr, cw, Arc::new(Client))
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn file_bytes_cross_the_protocol_as_base64() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let peer = connect(tmp.path()).await;
|
|
let bytes: Vec<u8> = (0..=255).cycle().take(3 * 1024 * 1024 + 1).collect();
|
|
let path = tmp.path().join("image.bin");
|
|
|
|
let args = json!({ "path": path, "data": IpcBytes(bytes.clone()) });
|
|
assert!(args["data"].is_string());
|
|
// Measure the envelope separately so the assertion is independent of the temp directory.
|
|
let envelope = serde_json::to_vec(&json!({ "path": path, "data": "" })).unwrap();
|
|
let body = serde_json::to_vec(&args).unwrap();
|
|
assert_eq!(body.len(), envelope.len() + bytes.len().div_ceil(3) * 4);
|
|
|
|
let call = Invoke { window: "main".into(), cmd: "fs_create_file_bytes".into(), args };
|
|
peer.request::<_, Value>("invoke", call, None).await.map_err(rpc::error_from_reply).unwrap();
|
|
assert_eq!(std::fs::read(&path).unwrap(), bytes);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn an_array_of_numbers_is_not_accepted_as_bytes() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let peer = connect(tmp.path()).await;
|
|
let path = tmp.path().join("old.bin");
|
|
let call = Invoke {
|
|
window: "main".into(),
|
|
cmd: "fs_create_file_bytes".into(),
|
|
args: json!({ "path": path, "data": [1, 2, 3] }),
|
|
};
|
|
let error = peer.request::<_, Value>("invoke", call, None).await.map_err(rpc::error_from_reply).unwrap_err();
|
|
assert!(error.message.contains("argument data"), "{error:?}");
|
|
assert!(!path.exists());
|
|
}
|