Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(standalone): request text from CapacitorHttp instead of re-parsing sync responses
Elian Doranb13b3cc097be
diff --git a/apps/standalone/src/services/capacitor_http_handler.spec.ts b/apps/standalone/src/services/capacitor_http_handler.spec.tsindex d348997c3e..d5015816a1 100644--- a/apps/standalone/src/services/capacitor_http_handler.spec.ts+++ b/apps/standalone/src/services/capacitor_http_handler.spec.ts@@ -24,11 +24,12 @@ describe("capacitorHttpHandler", () => {).rejects.toThrow("CapacitorHttp plugin is not available");});- it("forwards the request and lowercases response header keys (object body)", async () => {+ it("requests the raw text response and passes a JSON string through unchanged (no re-parse/re-stringify)", async () => {+ const rawJson = '{"ok":true}';const request = vi.fn().mockResolvedValue({status: 200,headers: { "Content-Type": "application/json", "X-Custom": "v" },- data: { ok: true }+ data: rawJson});installCapacitor(request);@@ -44,10 +45,19 @@ describe("capacitorHttpHandler", () => {url: "https://api/test",headers: { Authorization: "token" },data: "{}",- responseType: undefined+ responseType: "text"});expect(result.status).toBe(200);expect(result.headers).toEqual({ "content-type": "application/json", "x-custom": "v" });+ // Passed straight through — not JSON.stringify'd back.+ expect(result.body).toBe(rawJson);+ });++ it("still serializes object response data as a defensive fallback", async () => {+ const request = vi.fn().mockResolvedValue({ status: 200, headers: {}, data: { ok: true } });+ installCapacitor(request);++ const result = await capacitorHttpHandler({ method: "GET", url: "https://x", headers: {} });expect(result.body).toBe(JSON.stringify({ ok: true }));});diff --git a/apps/standalone/src/services/capacitor_http_handler.ts b/apps/standalone/src/services/capacitor_http_handler.tsindex 8ed7befd40..f4efd22b08 100644--- a/apps/standalone/src/services/capacitor_http_handler.ts+++ b/apps/standalone/src/services/capacitor_http_handler.ts@@ -33,12 +33,18 @@ function getCapacitorHttp() {* restrictions, and plain HTTP targets work from an HTTPS WebView origin.*/export const capacitorHttpHandler: NativeHttpHandler = async (request) => {+ // Ask for the raw response as text rather than CapacitorHttp's default JSON auto-parsing. The+ // worker (BridgedRequestProvider) parses the body itself, so letting the plugin parse it to an+ // object only for us to JSON.stringify it straight back is a wasted parse + serialize of the+ // entire body — doubling main-thread peak memory on a large sync response (e.g. one carrying a+ // big blob), which shares the mobile process heap with the sync worker.+ const responseType = request.responseType ?? "text";const response = await getCapacitorHttp().request({method: request.method,url: request.url,headers: request.headers,data: request.body,- responseType: request.responseType+ responseType});// Normalize header keys to lowercase for consistent access in the worker