From 795d99cc121d95cdaa0c6e849c33091d6357b410 Mon Sep 17 00:00:00 2001 From: Alexandr Stelnykovych Date: Fri, 6 Jun 2025 18:46:05 +0300 Subject: [PATCH] (UI; tauri-http): Improve body handling in send_tauri_http_request for various types --- .../tauri/tauri-http-interceptor.ts | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/desktop/angular/projects/safing/portmaster-api/src/lib/platform-specific/tauri/tauri-http-interceptor.ts b/desktop/angular/projects/safing/portmaster-api/src/lib/platform-specific/tauri/tauri-http-interceptor.ts index 26ddaa87..730a255b 100644 --- a/desktop/angular/projects/safing/portmaster-api/src/lib/platform-specific/tauri/tauri-http-interceptor.ts +++ b/desktop/angular/projects/safing/portmaster-api/src/lib/platform-specific/tauri/tauri-http-interceptor.ts @@ -153,17 +153,36 @@ export async function send_tauri_http_request( return headerArray; })() : Object.entries(init.headers || {}))]; - const body = init.body - ? new Uint8Array(await new Response(init.body as any).arrayBuffer()) - : undefined; + + let body: Uint8Array | 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<{ status: number; status_text: string; headers: [string, string][]; 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), { status: res.status, statusText: res.status_text,