diff --git a/materialious/src/lib/android/http/androidRequests.ts b/materialious/src/lib/fetchProxy.ts similarity index 65% rename from materialious/src/lib/android/http/androidRequests.ts rename to materialious/src/lib/fetchProxy.ts index 65c8f870..48630b95 100644 --- a/materialious/src/lib/android/http/androidRequests.ts +++ b/materialious/src/lib/fetchProxy.ts @@ -1,8 +1,12 @@ -import { timeout } from '$lib/misc'; +import { isUnrestrictedPlatform, timeout } from '$lib/misc'; import { Capacitor } from '@capacitor/core'; const originalFetch = window.fetch; -const corsProxyUrl: string = 'http://localhost:3000/'; +const corsProxyUrl = + Capacitor.getPlatform() === 'android' + ? 'http://localhost:3000/' + : `${location.origin}/api/proxy/`; + let mobileNodeLoaded = false; function needsProxying(target: string): boolean { @@ -10,11 +14,11 @@ function needsProxying(target: string): boolean { return true; } -export const androidFetch = async ( +export const fetchProxied = async ( requestInput: string | URL | Request, requestOptions?: RequestInit ): Promise => { - if (!mobileNodeLoaded) { + if (!mobileNodeLoaded && Capacitor.getPlatform() === 'android') { let mobileNodeResp: Response | undefined; while (!mobileNodeResp || !mobileNodeResp.ok) { @@ -29,11 +33,17 @@ export const androidFetch = async ( mobileNodeLoaded = true; } - const uri = requestInput instanceof Request ? requestInput.url : requestInput.toString(); + const nonProxiedUrl = + requestInput instanceof Request ? requestInput.url : requestInput.toString(); + + if (needsProxying(nonProxiedUrl)) { + const proxiedURLEncoded = + Capacitor.getPlatform() === 'android' ? nonProxiedUrl : encodeURIComponent(nonProxiedUrl); + + const proxiedUrl = corsProxyUrl + proxiedURLEncoded; - if (needsProxying(uri)) { if (requestInput instanceof Request) { - requestInput = new Request(corsProxyUrl + uri, { + requestInput = new Request(proxiedUrl, { method: requestInput.method, headers: requestInput.headers, body: requestInput.body, @@ -47,7 +57,7 @@ export const androidFetch = async ( ...(requestInput.body ? { duplex: 'half' } : {}) }); } else { - requestInput = corsProxyUrl + uri; + requestInput = proxiedUrl; } } @@ -55,8 +65,8 @@ export const androidFetch = async ( return originalFetch(requestInput, requestOptions); }; -if (Capacitor.getPlatform() === 'android') { - window.fetch = androidFetch; +if (isUnrestrictedPlatform() && Capacitor.getPlatform() !== 'electron') { + window.fetch = fetchProxied; const originalXhrOpen = XMLHttpRequest.prototype.open; diff --git a/materialious/src/routes/+layout.svelte b/materialious/src/routes/+layout.svelte index 1a07ff10..c619302d 100644 --- a/materialious/src/routes/+layout.svelte +++ b/materialious/src/routes/+layout.svelte @@ -1,7 +1,7 @@