Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(tauri): route native fetches by service and origin, not by name (#9597)
Peter Chinmana1848c87e07e
diff --git a/apps/web/src/lib/core/util/platformFetch.test.ts b/apps/web/src/lib/core/util/platformFetch.test.tsindex 99a1881d7a..f943a70de4 100644--- a/apps/web/src/lib/core/util/platformFetch.test.ts+++ b/apps/web/src/lib/core/util/platformFetch.test.ts@@ -95,6 +95,24 @@ it('keeps remote authentication and arbitrary email images on the native transpoexpect(browser).not.toHaveBeenCalled();});+it('passes the abort signal of a Request to the native transport', async () => {+ const request = createPlatformFetch(vi.fn<typeof fetch>());+ const requestAbort = new AbortController();+ const initAbort = new AbortController();+ const input = new Request('https://gateway.example/auth', {+ signal: requestAbort.signal,+ });+ await request(input);+ await request(input, { signal: initAbort.signal });+ // A Request copies its signal, so assert on the abort reaching the transport.+ const [fromRequest, fromInit] = nativeFetch.mock.calls.map(+ ([, init]) => init?.signal+ );+ requestAbort.abort();+ expect(fromRequest?.aborted).toBe(true);+ expect(fromInit).toBe(initAbort.signal);+});+it('uses the captured browser fetch after the global override', async () => {const browser = vi.fn<typeof fetch>()@@ -137,3 +155,38 @@ it('routes the installed global fetch to the native transport without recursing'expect(nativeFetch).toHaveBeenCalledTimes(2);expect(browser).toHaveBeenCalledTimes(1);});++it('routes installed loopback and localhost-named URLs by service and origin', async () => {+ const browser = vi+ .fn<typeof fetch>()+ .mockResolvedValue(new Response('browser'));+ vi.stubGlobal('fetch', browser);+ installPlatformFetch();+ await fetch('/auth/user/legacy_user_permissions');+ await fetch('https://images.example/image?localhost=true');+ await fetch('/src/index.tsx');+ expect(nativeFetch).toHaveBeenCalledTimes(2);+ expect(browser).toHaveBeenCalledTimes(1);+});++it.each([+ 'asset://localhost/',+ 'http://asset.localhost/',+ 'https://asset.localhost/',+])(+ 'keeps installed fetches of local files on the webview asset protocol %s',+ async (endpoint) => {+ vi.stubGlobal('__TAURI_INTERNALS__', {+ convertFileSrc: (path: string, protocol: string) =>+ `${endpoint.replace('asset', protocol)}${encodeURIComponent(path)}`,+ });+ const browser = vi+ .fn<typeof fetch>()+ .mockResolvedValue(new Response('browser'));+ vi.stubGlobal('fetch', browser);+ installPlatformFetch();+ await fetch(convertFileSrc('/media/preview.jpg'));+ expect(browser).toHaveBeenCalledTimes(1);+ expect(nativeFetch).not.toHaveBeenCalled();+ }+);diff --git a/apps/web/src/lib/core/util/platformFetch.ts b/apps/web/src/lib/core/util/platformFetch.tsindex ddbc40d177..ae25ed3446 100644--- a/apps/web/src/lib/core/util/platformFetch.ts+++ b/apps/web/src/lib/core/util/platformFetch.ts@@ -10,8 +10,8 @@ let originalFetch: typeof fetch | undefined;const WEBVIEW_FETCH_HOSTS = new Set(['api.klipy.com']);/**- * Called once by the entry point: routes Tauri's global fetch through the- * native transport. Localhost requests (dev server) skip it to keep HMR working.+ * Called once by the entry point: routes Tauri's global fetch through+ * `createPlatformFetch`, the only place that chooses a transport.*/export function installPlatformFetch() {if (!isTauri()) return;@@ -19,28 +19,10 @@ export function installPlatformFetch() {const browserFetch = window.fetch;// The global override must call the transport, never `platformFetch`: that// reads the global back and would recurse until the stack overflows.- const nativeFetch = initializePlatformFetch(browserFetch);+ const transport = initializePlatformFetch(browserFetch);window.fetch = new Proxy(browserFetch, {- apply: (target, thisArg, args: Parameters<typeof fetch>) => {- const url = args[0];- // Resolve bundled asset paths before checking the existing localhost bypass.- let resolvedUrl: URL;- try {- resolvedUrl = new URL(- url instanceof Request ? url.url : String(url),- document.baseURI- );- } catch (error) {- return Promise.reject(error);- }- if (- resolvedUrl.href.includes('localhost') ||- WEBVIEW_FETCH_HOSTS.has(resolvedUrl.hostname)- ) {- return target.apply(thisArg, args);- }- return nativeFetch.apply(thisArg, args);- },+ apply: (_target, thisArg, args: Parameters<typeof fetch>) =>+ transport.apply(thisArg, args),});}@@ -62,7 +44,11 @@ export function createPlatformFetch(): typeof window.fetch {// Tauri sends invoke calls through global fetch. Preserve its webview-owned// endpoint or native HTTP recursively invokes itself and expands each payload.- const ipc = isTauri() ? new URL(convertFileSrc('', 'ipc')) : undefined;+ // Local files (`asset`) are also served only to the webview. Each protocol is+ // `<name>://localhost` or `http(s)://<name>.localhost`, depending on platform.+ const tauriProtocols = isTauri()+ ? ['ipc', 'asset'].map((protocol) => new URL(convertFileSrc('', protocol)))+ : [];return async (input, init) => {if (!isTauri()) return webviewFetch(input, init);const url = new URL(@@ -80,15 +66,22 @@ export function createPlatformFetch(// Assets share the webview origin; authenticated service prefixes take// precedence even when the development proxy shares that origin.if (- (ipc && url.protocol === ipc.protocol && url.host === ipc.host) ||+ tauriProtocols.some(+ (endpoint) =>+ url.protocol === endpoint.protocol && url.host === endpoint.host+ ) ||(!service && url.origin === new URL(document.baseURI).origin) ||url.protocol === 'data:' ||- url.protocol === 'blob:'+ url.protocol === 'blob:' ||+ WEBVIEW_FETCH_HOSTS.has(url.hostname)) {return webviewFetch(input, init);}const { fetch } = await import('@tauri-apps/plugin-http');- return fetch(input, init);+ // The plugin cancels only on `init.signal`, never on a `Request`'s own.+ const signal =+ init?.signal ?? (input instanceof Request ? input.signal : undefined);+ return fetch(input, { ...init, signal });};}