mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
fix(http): fix aborting a streaming response (#2562)
This commit is contained in:
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
|
||||
use tauri::{
|
||||
async_runtime::Mutex,
|
||||
command,
|
||||
ipc::{Channel, CommandScope, GlobalScope},
|
||||
ipc::{CommandScope, GlobalScope},
|
||||
Manager, ResourceId, ResourceTable, Runtime, State, Webview,
|
||||
};
|
||||
use tokio::sync::oneshot::{channel, Receiver, Sender};
|
||||
@@ -415,26 +415,42 @@ pub async fn fetch_send<R: Runtime>(
|
||||
pub async fn fetch_read_body<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
rid: ResourceId,
|
||||
stream_channel: Channel<tauri::ipc::InvokeResponseBody>,
|
||||
) -> crate::Result<()> {
|
||||
) -> crate::Result<tauri::ipc::Response> {
|
||||
let res = {
|
||||
let mut resources_table = webview.resources_table();
|
||||
resources_table.take::<ReqwestResponse>(rid)?
|
||||
let resources_table = webview.resources_table();
|
||||
resources_table.get::<ReqwestResponse>(rid)?
|
||||
};
|
||||
|
||||
let mut res = Arc::into_inner(res).unwrap().0;
|
||||
// SAFETY: we can access the inner value mutably
|
||||
// because we are the only ones with a reference to it
|
||||
// and we don't want to use `Arc::into_inner` because we want to keep the value in the table
|
||||
// for potential future calls to `fetch_cancel_body`
|
||||
let res_ptr = Arc::as_ptr(&res) as *mut ReqwestResponse;
|
||||
let res = unsafe { &mut *res_ptr };
|
||||
let res = &mut res.0;
|
||||
|
||||
// send response through IPC channel
|
||||
while let Some(chunk) = res.chunk().await? {
|
||||
let mut chunk = chunk.to_vec();
|
||||
// append 0 to indicate we are not done yet
|
||||
chunk.push(0);
|
||||
stream_channel.send(tauri::ipc::InvokeResponseBody::Raw(chunk))?;
|
||||
}
|
||||
let Some(chunk) = res.chunk().await? else {
|
||||
let mut resources_table = webview.resources_table();
|
||||
resources_table.close(rid)?;
|
||||
|
||||
// send 1 to indicate we are done
|
||||
stream_channel.send(tauri::ipc::InvokeResponseBody::Raw(vec![1]))?;
|
||||
// return a response with a single byte to indicate that the body is empty
|
||||
return Ok(tauri::ipc::Response::new(vec![1]));
|
||||
};
|
||||
|
||||
let mut chunk = chunk.to_vec();
|
||||
// append a 0 byte to indicate that the body is not empty
|
||||
chunk.push(0);
|
||||
|
||||
Ok(tauri::ipc::Response::new(chunk))
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub async fn fetch_cancel_body<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
rid: ResourceId,
|
||||
) -> crate::Result<()> {
|
||||
let mut resources_table = webview.resources_table();
|
||||
resources_table.close(rid)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
commands::fetch,
|
||||
commands::fetch_cancel,
|
||||
commands::fetch_send,
|
||||
commands::fetch_read_body
|
||||
commands::fetch_read_body,
|
||||
commands::fetch_cancel_body,
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user