Skip to content
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

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ reqwest = { version = "0.12", default-features = false }
url = { workspace = true }
data-url = "0.3"
tracing = { workspace = true, optional = true }
futures-util = "0.3.31"

[features]
default = [
Expand Down
2 changes: 1 addition & 1 deletion plugins/http/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 37 additions & 17 deletions plugins/http/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @module
*/

import { invoke } from '@tauri-apps/api/core'
import { invoke, Channel } from '@tauri-apps/api/core'

/**
* Configuration of a proxy that a Client should pass requests to.
Expand Down Expand Up @@ -206,24 +206,44 @@ export async function fetch(
rid
})

const body = await invoke<ArrayBuffer | number[]>(
'plugin:http|fetch_read_body',
{
rid: responseRid
}
)
const channel = new Channel<number[]>()

// Create ReadableStream from channel messages
const stream = new ReadableStream({
start(controller) {
channel.onmessage = (arr) => {
const chunk = new Uint8Array(arr)

// End the stream if the chunk is empty
if (chunk.length === 0) {
controller.close()
} else {
controller.enqueue(chunk)
}
}

// Start reading body in background
const readPromise = invoke('plugin:http|fetch_read_body', {
rid: responseRid,
channel
})

const res = new Response(
body instanceof ArrayBuffer && body.byteLength !== 0
? body
: body instanceof Array && body.length > 0
? new Uint8Array(body)
: null,
{
status,
statusText
// If the promise fails, make sure the stream is closed
readPromise.catch((e) => {
console.error('error reading body', e)
Copy link
Member

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?

channel.onmessage = () => {}
controller.error(e)
})
},
cancel() {
channel.onmessage = () => {}
}
)
})

const res = new Response(stream, {
status,
statusText
})

// url and headers are read only properties
// but seems like we can set them like this
Expand Down
26 changes: 24 additions & 2 deletions plugins/http/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Copy link
Member

@amrbashir amrbashir Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we do the same streaming behavior using Response::chunk method, instead of pulling another crate to iterate over the stream?


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
Expand Down
Loading