{"draft":null,"from":2,"to":318,"files":[{"path":"client/src/open-browser.ts","added":54,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/open-browser.ts\n@@ -0,0 +1,54 @@\n+// Opening a page in the person's own browser, as `gh auth login` and `wrangler login` do.\n+// `abso login` opens the approve page with the code already in it, and still prints the\n+// link and the code: an open that fails, or is skipped, is silent, and the printed link stands.\n+\n+import { spawn } from \"node:child_process\";\n+\n+/** Why not to open a browser here, or null to open one. */\n+export function browserSkip(env: Readonly<Record<string, string | undefined>>, platform: string): string | null {\n+\tif (env.ABSO_NO_BROWSER) return \"ABSO_NO_BROWSER is set\";\n+\t// Over SSH the browser would open on the far machine's desktop, if it has one.\n+\tif (env.SSH_CONNECTION || env.SSH_TTY) return \"this is an SSH session\";\n+\tif (env.CI && env.CI !== \"false\" && env.CI !== \"0\") return \"this is CI\";\n+\tif (platform !== \"win32\" && platform !== \"darwin\" && !env.DISPLAY && !env.WAYLAND_DISPLAY)\n+\t\treturn \"there's no display\";\n+\treturn null;\n+}\n+\n+/**\n+ * The program that opens `url` in the default browser, and its arguments. No shell, so nothing in the url is\n+ * read as shell syntax. On Windows that rules out cmd's start, which cuts a url at its first &.\n+ */\n+export function openCommand(url: string, platform: string): [string, string[]] {\n+\tif (platform === \"win32\") return [\"rundll32\", [\"url.dll,FileProtocolHandler\", url]];\n+\tif (platform === \"darwin\") return [\"open\", [url]];\n+\treturn [\"xdg-open\", [url]];\n+}\n+\n+/** Only web pages are opened: a link from a server is never handed to the desktop as a file or another program. */\n+export function isWebLink(url: string): boolean {\n+\ttry {\n+\t\tconst u = new URL(url);\n+\t\treturn u.protocol === \"https:\" || u.protocol === \"http:\";\n+\t} catch {\n+\t\treturn false;\n+\t}\n+}\n+\n+/**\n+ * Opens `url` in the default browser and doesn't wait. Answers whether it started an opener; a failure\n+ * (no such program, refused) is silent.\n+ */\n+export function openInBrowser(url: string, platform: string = process.platform, run: typeof spawn = spawn): boolean {\n+\tif (!isWebLink(url)) return false;\n+\t// The link as the check parsed it, so what opens is exactly what was checked.\n+\tconst [command, args] = openCommand(new URL(url).href, platform);\n+\ttry {\n+\t\tconst child = run(command, args, { detached: true, stdio: \"ignore\", windowsHide: true });\n+\t\tchild.on(\"error\", () => {});\n+\t\tchild.unref();\n+\t\treturn true;\n+\t} catch {\n+\t\treturn false;\n+\t}\n+}"}],"unmatched":[]}