UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

91 lines (90 loc) • 3.02 kB
import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs"; import { n as createPrivateWindowsFile, t as createPrivateWindowsDirectory } from "./windows-private-directory-ChadGb3L.mjs"; import { n as openLocalFile } from "./browser-open-3VrAKw25.mjs"; import fs from "node:fs"; import path from "node:path"; import { randomUUID } from "node:crypto"; import fs$1 from "node:fs/promises"; //#region src/infra/browser-handoff.ts const HANDOFF_DIR_PREFIX = "openclaw-browser-handoff-"; const HANDOFF_FILE_NAME = "handoff.html"; const HANDOFF_CLEANUP_DELAY_MS = 6e5; /** Create the private handoff directory with platform-appropriate owner-only access. */ async function createHandoffDirectory() { if (process.platform !== "win32") return await fs$1.mkdtemp(path.join(resolvePreferredOpenClawTmpDir(), HANDOFF_DIR_PREFIX)); const e = path.join(resolvePreferredOpenClawTmpDir(), `${HANDOFF_DIR_PREFIX}${randomUUID()}`); createPrivateWindowsDirectory(e); return e; } /** Write the handoff document with platform-appropriate owner-only access. */ async function writeHandoffDocument(e, t) { if (process.platform !== "win32") { await fs$1.writeFile(e, t, { mode: 384, flag: "wx" }); return; } const n = createPrivateWindowsFile(e); try { fs.writeFileSync(n.fd, t); } finally { n.close(); } } async function removeHandoffDirectory(e) { await fs$1.rm(e, { recursive: true, force: true }).catch(() => {}); } /** * Open a credential-bearing URL in the user's browser without exposing it in * any process's command line. The URL is delivered through a private one-time * handoff document; only that document's file path reaches the browser opener. * Returns true when the handoff document was handed off successfully. */ async function openUrlPrivately(e) { const t = URL.parse(e); if (t?.protocol !== "http:" && t?.protocol !== "https:") return false; const n = t.toString(); let r; try { r = await createHandoffDirectory(); } catch { return false; } const i = path.join(r, HANDOFF_FILE_NAME); try { const e = n.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&#39;"); await writeHandoffDocument(i, [ "<!doctype html>", "<html lang=\"en\">", "<head>", "<meta charset=\"utf-8\">", `<meta http-equiv="refresh" content="0; url=${e}">`, "<title>OpenClaw</title>", "</head>", "<body>", "<p>Opening the OpenClaw Control UI…</p>", `<p>If nothing happens, <a href="${e}">continue to OpenClaw</a>.</p>`, `<script>location.replace(${JSON.stringify(n).replaceAll("<", "\\u003c")});<\/script>`, "</body>", "</html>", "" ].join("\n")); } catch { await removeHandoffDirectory(r); return false; } if (!await openLocalFile(i).catch(() => false)) { await removeHandoffDirectory(r); return false; } setTimeout(() => { removeHandoffDirectory(r); }, HANDOFF_CLEANUP_DELAY_MS).unref?.(); return true; } //#endregion export { openUrlPrivately as t };