HTTP add stream support (#2479)

* feat: add stream support

* feat: add stream support

* Revert "feat: add stream support"

This reverts commit 5edea81680.

* feat: add stream support

* Discard changes to pnpm-lock.yaml

* Discard changes to plugins/http/package.json

* fix(stream): change IPC packet

* fix: update stream message guest-js

* fix: return early when aborted

* fix: use InvokeResponseBody as packet

* fix: remove serde_bytes

* fix: remove reqwest response

* fix: content conversion bug

* fix: remove ReqwestResponses along with its implementations

* formatting and update changelog

* build api-iife.js

---------

Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
Adriel Jansen Siahaya
2025-03-08 15:45:05 -03:00
committed by GitHub
co-authored by Fabian-Lars
parent d37bbdef8d
commit cb38f54f4a
6 changed files with 67 additions and 51 deletions
+37 -22
View File
@@ -26,7 +26,7 @@
* @module
*/
import { invoke } from '@tauri-apps/api/core'
import { Channel, invoke } from '@tauri-apps/api/core'
/**
* Configuration of a proxy that a Client should pass requests to.
@@ -186,6 +186,35 @@ export async function fetch(
throw new Error(ERROR_REQUEST_CANCELLED)
}
const streamChannel = new Channel<ArrayBuffer | number[]>()
const readableStreamBody = new ReadableStream({
start: (controller) => {
streamChannel.onmessage = (res: ArrayBuffer | number[]) => {
// close early if aborted
if (signal?.aborted) {
controller.error(ERROR_REQUEST_CANCELLED)
controller.close()
return
}
// close when the signal to close (an empty chunk)
// is sent from the IPC.
if (
res instanceof ArrayBuffer ? res.byteLength == 0 : res.length == 0
) {
controller.close()
return
}
// the content conversion (like .text(), .json(), etc.) in Response
// must have Uint8Array as its content, else it will
// have untraceable error that's hard to debug.
controller.enqueue(new Uint8Array(res))
}
}
})
const rid = await invoke<number>('plugin:http|fetch', {
clientConfig: {
method: req.method,
@@ -196,7 +225,8 @@ export async function fetch(
connectTimeout,
proxy,
danger
}
},
streamChannel
})
const abort = () => invoke('plugin:http|fetch_cancel', { rid })
@@ -223,30 +253,15 @@ export async function fetch(
status,
statusText,
url,
headers: responseHeaders,
rid: responseRid
headers: responseHeaders
} = await invoke<FetchSendResponse>('plugin:http|fetch_send', {
rid
})
const body = await invoke<ArrayBuffer | number[]>(
'plugin:http|fetch_read_body',
{
rid: responseRid
}
)
const res = new Response(
body instanceof ArrayBuffer && body.byteLength !== 0
? body
: body instanceof Array && body.length > 0
? new Uint8Array(body)
: null,
{
status,
statusText
}
)
const res = new Response(readableStreamBody, {
status,
statusText
})
// url and headers are read only properties
// but seems like we can set them like this