From b2bbf6f5ad6fff56098a6846af1c9855db40fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Tue, 6 Oct 2026 15:53:56 +0200 Subject: [PATCH] Send embed bytes over IPC without JSON Embeds went over IPC as JSON number arrays, so a 9MB image turned into ~36MB of JSON. On upload Tauri parses that on the main thread, which beachballed the app while dropping a large screenshot. --- crates/radicle-tauri/src/commands/cob.rs | 21 +++++-- crates/radicle-types/bindings/cob/Embed.ts | 3 - .../bindings/cob/EmbedWithMimeType.ts | 6 -- crates/radicle-types/src/cobs.rs | 61 +++++++++++++++++-- crates/radicle-types/src/traits/thread.rs | 6 +- crates/test-http-api/src/api.rs | 27 ++++---- src/components/ExtendedTextarea.svelte | 12 ++-- src/components/Markdown.svelte | 16 +++-- src/lib/embeds.ts | 23 ++++++- src/lib/invoke.ts | 18 ++++-- tests/e2e/repo/fileDrop.spec.ts | 10 ++- tests/e2e/repo/tasks.spec.ts | 13 ++-- tests/unit/embeds.test.ts | 34 ++++++++++- 13 files changed, 185 insertions(+), 65 deletions(-) delete mode 100644 crates/radicle-types/bindings/cob/Embed.ts delete mode 100644 crates/radicle-types/bindings/cob/EmbedWithMimeType.ts diff --git a/crates/radicle-tauri/src/commands/cob.rs b/crates/radicle-tauri/src/commands/cob.rs index 149cd9f76..0737efaeb 100644 --- a/crates/radicle-tauri/src/commands/cob.rs +++ b/crates/radicle-tauri/src/commands/cob.rs @@ -19,8 +19,10 @@ pub async fn get_embed( rid: identity::RepoId, name: Option, oid: git::Oid, -) -> Result { - ctx.get_embed(rid, name, oid) +) -> Result { + let embed = ctx.get_embed(rid, name, oid)?; + + Ok(tauri::ipc::Response::new(embed.into_bytes())) } #[tauri::command] @@ -35,10 +37,19 @@ pub async fn save_embed_by_path( #[tauri::command] pub async fn save_embed_by_bytes( ctx: tauri::State<'_, AppState>, - rid: identity::RepoId, - name: String, - bytes: Vec, + request: tauri::ipc::Request<'_>, ) -> Result { + let tauri::ipc::InvokeBody::Raw(body) = request.body() else { + return Err(Error::SaveEmbedError); + }; + let rid = request + .headers() + .get("rid") + .and_then(|rid| rid.to_str().ok()) + .and_then(|rid| rid.parse().ok()) + .ok_or(Error::SaveEmbedError)?; + let (name, bytes) = types::cobs::split_embed_upload(body).ok_or(Error::SaveEmbedError)?; + ctx.save_embed_by_bytes(rid, name, bytes) } diff --git a/crates/radicle-types/bindings/cob/Embed.ts b/crates/radicle-types/bindings/cob/Embed.ts deleted file mode 100644 index 9b5d5294a..000000000 --- a/crates/radicle-types/bindings/cob/Embed.ts +++ /dev/null @@ -1,3 +0,0 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type Embed = { content: Array; mimeType: string | null }; diff --git a/crates/radicle-types/bindings/cob/EmbedWithMimeType.ts b/crates/radicle-types/bindings/cob/EmbedWithMimeType.ts deleted file mode 100644 index 67a590fb1..000000000 --- a/crates/radicle-types/bindings/cob/EmbedWithMimeType.ts +++ /dev/null @@ -1,6 +0,0 @@ -// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. - -export type EmbedWithMimeType = { - content: Array; - mimeType: string | null; -}; diff --git a/crates/radicle-types/src/cobs.rs b/crates/radicle-types/src/cobs.rs index 04b4176e3..9f6b3da4b 100644 --- a/crates/radicle-types/src/cobs.rs +++ b/crates/radicle-types/src/cobs.rs @@ -101,15 +101,29 @@ pub struct Operation { pub timestamp: cob::Timestamp, } -#[derive(Serialize, TS)] -#[serde(rename_all = "camelCase")] -#[ts(export)] -#[ts(export_to = "cob/")] pub struct EmbedWithMimeType { pub content: Vec, pub mime_type: Option, } +impl EmbedWithMimeType { + pub fn into_bytes(self) -> Vec { + let mime_type = self.mime_type.unwrap_or_default(); + let mut bytes = Vec::with_capacity(mime_type.len() + 1 + self.content.len()); + bytes.extend_from_slice(mime_type.as_bytes()); + bytes.push(0); + bytes.extend_from_slice(&self.content); + bytes + } +} + +pub fn split_embed_upload(body: &[u8]) -> Option<(&str, &[u8])> { + let end = body.iter().position(|&byte| byte == 0)?; + let name = std::str::from_utf8(&body[..end]).ok()?; + + Some((name, &body[end + 1..])) +} + #[derive(TS, Serialize)] #[doc = "A type alias for the TS type `never`."] #[ts(export)] @@ -197,3 +211,42 @@ pub mod query { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn embed_into_bytes() { + let embed = EmbedWithMimeType { + content: vec![1, 0, 2], + mime_type: Some("image/png".to_string()), + }; + assert_eq!(embed.into_bytes(), b"image/png\0\x01\0\x02"); + + let embed = EmbedWithMimeType { + content: vec![1], + mime_type: None, + }; + assert_eq!(embed.into_bytes(), b"\0\x01"); + } + + #[test] + fn split_embed_upload_at_first_nul() { + let body = "a\u{202f}b.png\0" + .bytes() + .chain([1, 0, 2]) + .collect::>(); + assert_eq!( + split_embed_upload(&body), + Some(("a\u{202f}b.png", &[1, 0, 2][..])) + ); + assert_eq!(split_embed_upload(b"name\0"), Some(("name", &[][..]))); + } + + #[test] + fn split_embed_upload_rejects_malformed_bodies() { + assert_eq!(split_embed_upload(b"no separator"), None); + assert_eq!(split_embed_upload(b"\xff\0content"), None); + } +} diff --git a/crates/radicle-types/src/traits/thread.rs b/crates/radicle-types/src/traits/thread.rs index 593270c4d..69d601750 100644 --- a/crates/radicle-types/src/traits/thread.rs +++ b/crates/radicle-types/src/traits/thread.rs @@ -78,8 +78,8 @@ pub trait Thread: Profile { fn save_embed_by_bytes( &self, rid: identity::RepoId, - name: String, - bytes: Vec, + name: &str, + bytes: &[u8], ) -> Result { let file_size = bytes.len(); if file_size > MAX_EMBED_SIZE { @@ -87,7 +87,7 @@ pub trait Thread: Profile { } let profile = self.profile(); let repo = profile.storage.repository(rid)?; - let embed = radicle::cob::Embed::::store(&name, &bytes, &repo.backend)?; + let embed = radicle::cob::Embed::::store(name, bytes, &repo.backend)?; Ok(embed.oid()) } diff --git a/crates/test-http-api/src/api.rs b/crates/test-http-api/src/api.rs index 740d749f4..d7469e508 100644 --- a/crates/test-http-api/src/api.rs +++ b/crates/test-http-api/src/api.rs @@ -3,12 +3,14 @@ use std::path::PathBuf; use std::sync::Arc; use axum::Router; +use axum::body::Bytes; use axum::extract::{FromRequestParts, Request, State}; use axum::middleware::{self, Next}; use axum::response::{IntoResponse, Json, Response}; use axum::routing::post; +use hyper::HeaderMap; use hyper::Method; -use hyper::header::{CONTENT_TYPE, HeaderValue}; +use hyper::header::{CONTENT_TYPE, HeaderName, HeaderValue}; use hyper::http::request::Parts; use serde::{Deserialize, Serialize}; use tokio::sync::{Mutex, RwLock}; @@ -205,7 +207,7 @@ pub fn router(shared: Shared) -> Router { ALLOWED_ORIGINS.map(HeaderValue::from_static), )) .allow_methods([Method::POST, Method::GET]) - .allow_headers([CONTENT_TYPE]), + .allow_headers([CONTENT_TYPE, HeaderName::from_static("rid")]), ) .with_state(shared) } @@ -886,7 +888,10 @@ async fn get_embeds_handler( ) -> impl IntoResponse { let embed = ctx.get_embed(rid, name, oid)?; - Ok::<_, Error>(Json(embed)) + Ok::<_, Error>(( + [(CONTENT_TYPE, "application/octet-stream")], + embed.into_bytes(), + )) } #[derive(Deserialize)] @@ -904,17 +909,17 @@ async fn save_embed_by_path_handler( Ok::<_, Error>(Json(oid)) } -#[derive(Deserialize)] -struct SaveEmbedByBytesBody { - pub rid: identity::RepoId, - pub name: String, - pub bytes: Vec, -} - async fn save_embed_by_bytes_handler( Ctx(ctx): Ctx, - Json(SaveEmbedByBytesBody { rid, name, bytes }): Json, + headers: HeaderMap, + body: Bytes, ) -> impl IntoResponse { + let rid = headers + .get("rid") + .and_then(|rid| rid.to_str().ok()) + .and_then(|rid| rid.parse().ok()) + .ok_or(Error::SaveEmbedError)?; + let (name, bytes) = types::cobs::split_embed_upload(&body).ok_or(Error::SaveEmbedError)?; let oid = ctx.save_embed_by_bytes(rid, name, bytes)?; Ok::<_, Error>(Json(oid)) diff --git a/src/components/ExtendedTextarea.svelte b/src/components/ExtendedTextarea.svelte index 39ddd716f..facfe57df 100644 --- a/src/components/ExtendedTextarea.svelte +++ b/src/components/ExtendedTextarea.svelte @@ -7,7 +7,7 @@ import debounce from "lodash/debounce"; import { SvelteMap } from "svelte/reactivity"; - import { basename } from "@app/lib/embeds"; + import { basename, encodeEmbedUpload } from "@app/lib/embeds"; import { hints } from "@app/lib/hints"; import { invoke } from "@app/lib/invoke"; import { matchesShortcut } from "@app/lib/shortcuts.svelte"; @@ -180,11 +180,11 @@ if (file.size > MAX_EMBED_SIZE) { throw new Error(`${file.name} exceeds the embed size limit`); } - return invoke("save_embed_by_bytes", { - rid, - name: file.name, - bytes: new Uint8Array(await file.arrayBuffer()), - }); + return invoke( + "save_embed_by_bytes", + encodeEmbedUpload(file.name, new Uint8Array(await file.arrayBuffer())), + { headers: { rid } }, + ); } function hasFiles(event: DragEvent): boolean { diff --git a/src/components/Markdown.svelte b/src/components/Markdown.svelte index e7a1511e4..5053c4e5d 100644 --- a/src/components/Markdown.svelte +++ b/src/components/Markdown.svelte @@ -1,10 +1,8 @@