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
feat(mobile/ios): add XMLHttpRequest interceptor
AndreeaCorlaci7e98a46072d1
diff --git a/apps/client-standalone/src/main.ts b/apps/client-standalone/src/main.tsindex 8b44b5e88e..bb11cab0e3 100644--- a/apps/client-standalone/src/main.ts+++ b/apps/client-standalone/src/main.ts@@ -54,6 +54,119 @@ function setupFetchInterceptor() {}+// jQuery $.ajax uses XMLHttpRequest, which window.fetch interception does not+// catch. On the capacitor:// scheme there is no Service Worker to route+// requests, so XHR-bound API calls would hit the native bridge and return+// something other than the expected JSON. Route them through the local worker.+function setupXhrInterceptor() {+ const OriginalXHR = window.XMLHttpRequest;++ class PatchedXHR extends OriginalXHR {+ private _ti_method = "GET";+ private _ti_url = "";+ private _ti_intercept = false;+ private _ti_headers: Record<string, string> = {};+ private _ti_responseType: XMLHttpRequestResponseType = "";++ open(method: string, url: string | URL, async?: boolean, user?: string | null, password?: string | null) {+ const urlStr = typeof url === "string" ? url : url.href;+ const abs = new URL(urlStr, location.href);+ this._ti_method = method;+ this._ti_url = abs.href;+ this._ti_intercept = abs.origin === location.origin && isLocalApiRequest(abs);+ this._ti_headers = {};+ if (!this._ti_intercept) {+ return super.open(method, url as string, async ?? true, user ?? null, password ?? null);+ }+ }++ setRequestHeader(name: string, value: string) {+ if (!this._ti_intercept) return super.setRequestHeader(name, value);+ this._ti_headers[name] = value;+ }++ get responseType(): XMLHttpRequestResponseType {+ if (this._ti_intercept) return this._ti_responseType;+ return super.responseType;+ }++ set responseType(value: XMLHttpRequestResponseType) {+ if (this._ti_intercept) {+ this._ti_responseType = value;+ return;+ }+ super.responseType = value;+ }++ send(body?: Document | XMLHttpRequestBodyInit | null) {+ if (!this._ti_intercept) return super.send(body as any);++ const init: RequestInit = { method: this._ti_method, headers: this._ti_headers };+ if (body != null && this._ti_method !== "GET" && this._ti_method !== "HEAD") {+ init.body = body as BodyInit;+ }++ (async () => {+ try {+ const resp = await localFetch(new Request(this._ti_url, init));+ const buffer = await resp.arrayBuffer();+ const text = new TextDecoder().decode(buffer);++ let parsedResponse: unknown = text;+ if (this._ti_responseType === "json") {+ try { parsedResponse = JSON.parse(text); } catch { parsedResponse = null; }+ } else if (this._ti_responseType === "arraybuffer") {+ parsedResponse = buffer;+ } else if (this._ti_responseType === "blob") {+ parsedResponse = new Blob([buffer], { type: resp.headers.get("content-type") ?? "" });+ }++ const headerLines = [...resp.headers.entries()]+ .map(([k, v]) => `${k}: ${v}`).join("\r\n");++ Object.defineProperty(this, "readyState", { value: 4, configurable: true });+ Object.defineProperty(this, "status", { value: resp.status, configurable: true });+ Object.defineProperty(this, "statusText", { value: resp.statusText, configurable: true });+ Object.defineProperty(this, "responseURL", { value: this._ti_url, configurable: true });+ Object.defineProperty(this, "responseText", {+ get: () => {+ if (this._ti_responseType && this._ti_responseType !== "text") {+ throw new DOMException(+ "responseText is only available when responseType is '' or 'text'",+ "InvalidStateError"+ );+ }+ return text;+ },+ configurable: true,+ });+ Object.defineProperty(this, "response", { value: parsedResponse, configurable: true });+ Object.defineProperty(this, "getAllResponseHeaders", {+ value: () => headerLines,+ configurable: true,+ });+ Object.defineProperty(this, "getResponseHeader", {+ value: (name: string) => resp.headers.get(name),+ configurable: true,+ });++ this.dispatchEvent(new Event("readystatechange"));+ this.dispatchEvent(new ProgressEvent("load"));+ this.dispatchEvent(new ProgressEvent("loadend"));+ } catch (err) {+ Object.defineProperty(this, "readyState", { value: 4, configurable: true });+ Object.defineProperty(this, "status", { value: 0, configurable: true });+ this.dispatchEvent(new Event("readystatechange"));+ this.dispatchEvent(new ProgressEvent("error"));+ this.dispatchEvent(new ProgressEvent("loadend"));+ }+ })();+ }+ }++ window.XMLHttpRequest = PatchedXHR as unknown as typeof XMLHttpRequest;+}+async function bootstrap() {/* fixes https://github.com/webpack/webpack/issues/10035 */window.global = globalThis;@@ -63,9 +176,12 @@ async function bootstrap() {// iOS Capacitor loads on capacitor:// scheme — WebKit rejects service worker// registration for non-HTTP/HTTPS origins. Use a fetch interceptor instead- // to route API calls directly to the local SQLite worker.+ // to route API calls directly to the local SQLite worker. XHR must also be+ // patched because jQuery $.ajax (used by the client's server helpers) does+ // not go through window.fetch.if (location.protocol === "capacitor:") {setupFetchInterceptor();+ setupXhrInterceptor();} else {attachServiceWorkerBridge();await waitForServiceWorkerControl();