Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
blame on this site's /mcp
CLI
abso blame client/src/open-browser.ts --json
JSON
/christopher/abso-core/blame/client/src/open-browser.ts?format=json
54 linesspeed 100%Last change speed in version 62,

54 lines from 2 changes in 2 versions.

speed60abso login opens the approve page in the browser Like gh auth login and wrangler login: abso login opens the approve page in the default browser with the code already in it, so nobody copies a link by hand. It still prints the link and the code, which stand when the open fails or is skipped. - Windows opens with rundll32 url.dll,FileProtocolHandler, macOS with open, Linux with xdg-open. There's no shell, so a link with & in it arrives whole (cmd's start would cut it). - No browser with --no-browser or ABSO_NO_BROWSER, over SSH (SSH_CONNECTION or SSH_TTY), under CI, or on Linux and other Unixes with neither DISPLAY nor WAYLAND_DISPLAY. - Only http and https links are opened, so a link from a server is never handed to the desktop as a file or a program. - The opener starts detached and isn't waited on; a failure is silent. Tests: open-browser.test.ts (each skip, each platform's command, web links only, a failing opener is silent), and two login tests with an injected opener.Compare1// Opening a page in the person's own browser, as `gh auth login` and `wrangler login` do.
2// `abso login` opens the approve page with the code already in it, and still prints the
3// link and the code: an open that fails, or is skipped, is silent, and the printed link stands.
4
5import { spawn } from "node:child_process";
6
7/** Why not to open a browser here, or null to open one. */
8export function browserSkip(env: Readonly<Record<string, string | undefined>>, platform: string): string | null {
9 if (env.ABSO_NO_BROWSER) return "ABSO_NO_BROWSER is set";
10 // Over SSH the browser would open on the far machine's desktop, if it has one.
11 if (env.SSH_CONNECTION || env.SSH_TTY) return "this is an SSH session";
12 if (env.CI && env.CI !== "false" && env.CI !== "0") return "this is CI";
13 if (platform !== "win32" && platform !== "darwin" && !env.DISPLAY && !env.WAYLAND_DISPLAY)
14 return "there's no display";
15 return null;
18/**
19 * The program that opens `url` in the default browser, and its arguments. No shell, so nothing in the url is
20 * read as shell syntax. On Windows that rules out cmd's start, which cuts a url at its first &.
21 */
22export function openCommand(url: string, platform: string): [string, string[]] {
23 if (platform === "win32") return ["rundll32", ["url.dll,FileProtocolHandler", url]];
24 if (platform === "darwin") return ["open", [url]];
25 return ["xdg-open", [url]];
28/** Only web pages are opened: a link from a server is never handed to the desktop as a file or another program. */
29export function isWebLink(url: string): boolean {
30 try {
31 const u = new URL(url);
32 return u.protocol === "https:" || u.protocol === "http:";
33 } catch {
34 return false;
35 }
38/**
39 * Opens `url` in the default browser and doesn't wait. Answers whether it started an opener; a failure
40 * (no such program, refused) is silent.
41 */
42export function openInBrowser(url: string, platform: string = process.platform, run: typeof spawn = spawn): boolean {
43 if (!isWebLink(url)) return false;
speed62Open the link as it was checked openInBrowser hands the opener the parsed link (new URL(url).href), the same one the web-link check read, rather than the raw text.Compare44 // The link as the check parsed it, so what opens is exactly what was checked.
45 const [command, args] = openCommand(new URL(url).href, platform);
speed60abso login opens the approve page in the browser Like gh auth login and wrangler login: abso login opens the approve page in the default browser with the code already in it, so nobody copies a link by hand. It still prints the link and the code, which stand when the open fails or is skipped. - Windows opens with rundll32 url.dll,FileProtocolHandler, macOS with open, Linux with xdg-open. There's no shell, so a link with & in it arrives whole (cmd's start would cut it). - No browser with --no-browser or ABSO_NO_BROWSER, over SSH (SSH_CONNECTION or SSH_TTY), under CI, or on Linux and other Unixes with neither DISPLAY nor WAYLAND_DISPLAY. - Only http and https links are opened, so a link from a server is never handed to the desktop as a file or a program. - The opener starts detached and isn't waited on; a failure is silent. Tests: open-browser.test.ts (each skip, each platform's command, web links only, a failing opener is silent), and two login tests with an injected opener.Compare46 try {
47 const child = run(command, args, { detached: true, stdio: "ignore", windowsHide: true });
48 child.on("error", () => {});
49 child.unref();
50 return true;
51 } catch {
52 return false;
53 }