UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

68 lines (67 loc) • 2.76 kB
import { a as getWindowsSystem32ExePath } from "./windows-install-roots-Df6dQDK7.mjs"; import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs"; import "./exec-aRIvS9s4.mjs"; import { t as isWSL } from "./wsl-I_VtUW9p.mjs"; import { t as detectBinary } from "./detect-binary-B596fFLF.mjs"; import { pathToFileURL } from "node:url"; import path from "node:path"; //#region src/infra/browser-open.ts /** Resolve the platform command used to open an HTTP(S) URL in a browser. */ async function resolveBrowserOpenCommand() { const e = process.platform; const t = process.env; const n = Boolean(t.DISPLAY || t.WAYLAND_DISPLAY); if ((Boolean(t.SSH_CLIENT) || Boolean(t.SSH_TTY) || Boolean(t.SSH_CONNECTION)) && !n && e !== "win32" && e !== "darwin") return null; if (e === "win32") return [getWindowsSystem32ExePath("rundll32.exe"), "url.dll,FileProtocolHandler"]; if (e !== "darwin" && e !== "linux") return null; if (e === "linux") { const e = await isWSL(); if (!n && !e) return null; if (e) { if (await detectBinary("wslview")) return ["wslview"]; if (!n) return null; } } const r = e === "darwin" ? "open" : "xdg-open"; return await detectBinary(r) ? [r] : null; } /** Report whether browser opening is currently available. */ async function detectBrowserOpenSupport() { return { ok: Boolean(await resolveBrowserOpenCommand()) }; } /** Open a safe HTTP(S) URL in the user's browser when the platform supports it. */ async function openUrl(e) { if (process.env.VITEST || false) return false; const t = URL.parse(e); if (t?.protocol !== "http:" && t?.protocol !== "https:") return false; return await openBrowserPath(t.toString()); } /** * Open a local file in the user's browser when the platform supports it. * * Unlike {@link openUrl}, this accepts a `file://` path for private browser * handoffs: the path contains no credential, so passing it as an opener * argument does not expose secret material through process command lines. * The target file itself is the caller's responsibility (for example a * mode-0600 document inside a mode-0700 directory). */ async function openLocalFile(e) { if (process.env.VITEST || false) return false; const t = process.platform === "win32"; const n = t ? path.win32 : path.posix; if (!n.isAbsolute(e)) return false; return await openBrowserPath(pathToFileURL(n.resolve(e), { windows: t }).toString()); } async function openBrowserPath(e) { const t = await resolveBrowserOpenCommand(); if (!t) return false; const n = [...t, e]; try { const e = await runCommandWithTimeout(n, { timeoutMs: 5e3 }); return e.code === 0 && e.termination === "exit"; } catch { return false; } } //#endregion export { openLocalFile as n, openUrl as r, detectBrowserOpenSupport as t };