openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 2.2 kB
JavaScript
import{r as resolvePreferredOpenClawTmpDir}from"./tmp-openclaw-dir-DapBruir.mjs";import{n as createPrivateWindowsFile,t as createPrivateWindowsDirectory}from"./windows-private-directory-Dh3tjj3q.mjs";import{n as openLocalFile}from"./browser-open-CC7rSZD8.mjs";import fs from"node:fs";import path from"node:path";import{randomUUID}from"node:crypto";import fs$1 from"node:fs/promises";
//!src/infra/browser-handoff.ts
const HANDOFF_DIR_PREFIX=`openclaw-browser-handoff-`;const HANDOFF_FILE_NAME=`handoff.html`;const HANDOFF_CLEANUP_DELAY_MS=6e5;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}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(()=>{})}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(`&`,`&`).replaceAll(`<`,`<`).replaceAll(`>`,`>`).replaceAll(`"`,`"`).replaceAll(`'`,`'`);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 OpenClaw…</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(`
`))}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}export{openUrlPrivately as t};