-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for HTTP Streaming #2140
base: v2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use std::{future::Future, pin::Pin, str::FromStr, sync::Arc, time::Duration}; | ||
|
||
use futures_util::StreamExt; | ||
use http::{header, HeaderMap, HeaderName, HeaderValue, Method, StatusCode}; | ||
use reqwest::{redirect::Policy, NoProxy}; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -386,13 +387,34 @@ pub async fn fetch_send<R: Runtime>( | |
pub(crate) async fn fetch_read_body<R: Runtime>( | ||
webview: Webview<R>, | ||
rid: ResourceId, | ||
) -> crate::Result<tauri::ipc::Response> { | ||
channel: tauri::ipc::Channel<&[u8]>, | ||
) -> crate::Result<()> { | ||
let res = { | ||
let mut resources_table = webview.resources_table(); | ||
resources_table.take::<ReqwestResponse>(rid)? | ||
}; | ||
|
||
let res = Arc::into_inner(res).unwrap().0; | ||
Ok(tauri::ipc::Response::new(res.bytes().await?.to_vec())) | ||
let mut stream = res.bytes_stream(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we do the same streaming behavior using |
||
|
||
while let Some(chunk) = stream.next().await { | ||
match chunk { | ||
Ok(bytes) => { | ||
// Skip empty chunks | ||
if !bytes.is_empty() { | ||
channel.send(&bytes)?; | ||
} | ||
} | ||
Err(e) => { | ||
return Err(e.into()); | ||
} | ||
} | ||
} | ||
|
||
// Send an empty chunk to signal the end of the stream | ||
channel.send(&[])?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// forbidden headers per fetch spec https://fetch.spec.whatwg.org/#terminology-headers | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to console.error here? won't controller.error be enough?