refactor(web): extract isTauri into a leaf platform module

This commit is contained in:
Offending Commit
2026-06-02 11:36:21 -05:00
parent 3bb1150773
commit d4452abcea
4 changed files with 27 additions and 11 deletions

View File

@@ -1,14 +1,13 @@
import { httpFetch } from "@/lib/http";
import { isTauri } from "@/lib/platform";
export { isTauri } from "@/lib/platform";
export interface DiscoveredInstance {
port: number;
base_url: string;
}
export function isTauri(): boolean {
return typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
}
/**
* Probe localhost ports for running Honcho instances. Desktop-only — the web
* build can't port-scan due to CORS, so this returns an empty list when not

View File

@@ -1,12 +1,8 @@
import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
import { isTauri } from "@/lib/platform";
// Route fetch through Rust (reqwest) when running in Tauri — bypasses WebView CORS enforcement.
// Falls back to native browser fetch during plain web dev (`pnpm dev:web`).
const isTauri = Boolean(
typeof window !== "undefined" &&
(window as unknown as Record<string, unknown>).__TAURI_INTERNALS__,
);
export const httpFetch: typeof globalThis.fetch = isTauri
// Falls back to native browser fetch during plain web dev.
export const httpFetch: typeof globalThis.fetch = isTauri()
? (tauriFetch as typeof globalThis.fetch)
: globalThis.fetch;

View File

@@ -0,0 +1,4 @@
/** True when running inside the Tauri desktop shell (WebView with injected internals). */
export function isTauri(): boolean {
return typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
}

View File

@@ -0,0 +1,17 @@
import { afterEach, describe, expect, it } from "vitest";
import { isTauri } from "@/lib/platform";
describe("isTauri", () => {
afterEach(() => {
delete (window as unknown as Record<string, unknown>).__TAURI_INTERNALS__;
});
it("returns false in a plain browser/jsdom environment", () => {
expect(isTauri()).toBe(false);
});
it("returns true when the Tauri internals global is present", () => {
(window as unknown as Record<string, unknown>).__TAURI_INTERNALS__ = {};
expect(isTauri()).toBe(true);
});
});