-
Notifications
You must be signed in to change notification settings - Fork 431
HTTP add stream support #2479
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
Merged
Merged
HTTP add stream support #2479
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
643039c
feat: add stream support
adrieljss 5edea81
feat: add stream support
adrieljss bdbb3d7
Revert "feat: add stream support"
adrieljss 39639ea
feat: add stream support
adrieljss 44ee279
Discard changes to pnpm-lock.yaml
FabianLars fd7bd41
Discard changes to plugins/http/package.json
FabianLars ca3651b
fix(stream): change IPC packet
adrieljss 0a0a428
fix: update stream message guest-js
adrieljss ba122e6
fix: return early when aborted
adrieljss 0a2645f
fix: use InvokeResponseBody as packet
adrieljss e747212
fix: remove serde_bytes
adrieljss 9b472c2
fix: remove reqwest response
adrieljss 57eb09b
fix: content conversion bug
adrieljss acafdd8
fix: remove ReqwestResponses along with its implementations
adrieljss d75de7b
formatting and update changelog
adrieljss 194fdee
build api-iife.js
adrieljss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
||
use std::{future::Future, pin::Pin, str::FromStr, sync::Arc, time::Duration}; | ||
|
||
use http::{header, HeaderMap, HeaderName, HeaderValue, Method, StatusCode}; | ||
|
@@ -10,7 +11,7 @@ use serde::{Deserialize, Serialize}; | |
use tauri::{ | ||
async_runtime::Mutex, | ||
command, | ||
ipc::{CommandScope, GlobalScope}, | ||
ipc::{Channel, CommandScope, GlobalScope}, | ||
Manager, ResourceId, ResourceTable, Runtime, State, Webview, | ||
}; | ||
use tokio::sync::oneshot::{channel, Receiver, Sender}; | ||
|
@@ -22,6 +23,8 @@ use crate::{ | |
|
||
const HTTP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); | ||
|
||
// reqwest::Response is never read, but might be needed for future use. | ||
#[allow(dead_code)] | ||
|
||
struct ReqwestResponse(reqwest::Response); | ||
impl tauri::Resource for ReqwestResponse {} | ||
|
||
|
@@ -181,6 +184,7 @@ pub async fn fetch<R: Runtime>( | |
client_config: ClientConfig, | ||
command_scope: CommandScope<Entry>, | ||
global_scope: GlobalScope<Entry>, | ||
stream_channel: Channel<tauri::ipc::InvokeResponseBody> | ||
) -> crate::Result<ResourceId> { | ||
let ClientConfig { | ||
method, | ||
|
@@ -314,7 +318,22 @@ pub async fn fetch<R: Runtime>( | |
#[cfg(feature = "tracing")] | ||
tracing::trace!("{:?}", request); | ||
|
||
let fut = async move { request.send().await.map_err(Into::into) }; | ||
let fut = async move { | ||
let mut res = request.send().await?; | ||
|
||
// send response through IPC channel | ||
while let Some(chunk) = res.chunk().await? { | ||
stream_channel.send(tauri::ipc::InvokeResponseBody::Raw(chunk.to_vec()))?; | ||
} | ||
|
||
// send empty vector when done | ||
stream_channel.send(tauri::ipc::InvokeResponseBody::Raw(Vec::new()))?; | ||
|
||
// return that response | ||
Ok(res) | ||
}; | ||
|
||
|
||
let mut resources_table = webview.resources_table(); | ||
let rid = resources_table.add_request(Box::pin(fut)); | ||
|
||
|
@@ -410,19 +429,6 @@ pub async fn fetch_send<R: Runtime>( | |
}) | ||
} | ||
|
||
#[tauri::command] | ||
pub(crate) async fn fetch_read_body<R: Runtime>( | ||
webview: Webview<R>, | ||
rid: ResourceId, | ||
) -> crate::Result<tauri::ipc::Response> { | ||
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())) | ||
} | ||
|
||
// forbidden headers per fetch spec https://fetch.spec.whatwg.org/#terminology-headers | ||
#[cfg(not(feature = "unsafe-headers"))] | ||
fn is_unsafe_header(header: &HeaderName) -> bool { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this should be an
ArrayBuffer
iirc, and sometimes in the case where IPC fails to use the borwserfetch
, it fallsback to Json serialization so this would beArray<number>
So we need to do some checks here, similar as what was done before
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.
Yes, you're right. I'll update that.