(UI; tauri-http): Improve body handling in send_tauri_http_request for various types
This commit is contained in:
@@ -153,16 +153,35 @@ export async function send_tauri_http_request(
|
|||||||
return headerArray;
|
return headerArray;
|
||||||
})()
|
})()
|
||||||
: Object.entries(init.headers || {}))];
|
: Object.entries(init.headers || {}))];
|
||||||
const body = init.body
|
|
||||||
? new Uint8Array(await new Response(init.body as any).arrayBuffer())
|
let body: Uint8Array | undefined;
|
||||||
: undefined;
|
if (init.body) {
|
||||||
|
if (typeof init.body === 'string') {
|
||||||
|
// Most efficient way to convert a string to Uint8Array
|
||||||
|
body = new TextEncoder().encode(init.body);
|
||||||
|
} else if (init.body instanceof ArrayBuffer) {
|
||||||
|
body = new Uint8Array(init.body);
|
||||||
|
} else if (init.body instanceof Uint8Array) {
|
||||||
|
body = init.body;
|
||||||
|
} else if (init.body instanceof Blob) {
|
||||||
|
// Efficiently read Blob data
|
||||||
|
body = new Uint8Array(await init.body.arrayBuffer());
|
||||||
|
} else if (init.body instanceof URLSearchParams) {
|
||||||
|
body = new TextEncoder().encode(init.body.toString());
|
||||||
|
} else {
|
||||||
|
// Fallback for other types, though the inefficient path is kept for unsupported types
|
||||||
|
// This path should ideally be avoided by handling types in getRequestBody.
|
||||||
|
console.warn('[TauriHttpInterceptor] Using inefficient body conversion for unknown type.');
|
||||||
|
body = new Uint8Array(await new Response(init.body as any).arrayBuffer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const res = await invoke<{
|
const res = await invoke<{
|
||||||
status: number;
|
status: number;
|
||||||
status_text: string;
|
status_text: string;
|
||||||
headers: [string, string][];
|
headers: [string, string][];
|
||||||
body: number[];
|
body: number[];
|
||||||
}>('send_tauri_http_request', { url, opts: { method, headers, body } });
|
}>('send_tauri_http_request', { url, opts: { method, headers, body: body ? Array.from(body) : undefined } });
|
||||||
|
|
||||||
return new Response(new Uint8Array(res.body), {
|
return new Response(new Uint8Array(res.body), {
|
||||||
status: res.status,
|
status: res.status,
|
||||||
|
|||||||
Reference in New Issue
Block a user