#!/usr/bin/env bun /** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * `bxc serve` — CLI entrypoint that spawns Bxc as a CDP server on * a local TCP port. Designed to be invoked by external clients (such as * agent-browser's Rust orchestrator) which expect a Chrome-DevTools-Protocol * compatible HTTP + WebSocket endpoint. * * Usage: * * bxc serve --cdp-port [--profile static|fast|http] \ * [--host 127.0.0.1] [--proxy http://...] * * Endpoints exposed: * * GET /json/version -> Browser version + webSocketDebuggerUrl * GET /json/list /json -> Target list (page + browser) * GET /json/protocol -> empty stub * WS /devtools/browser[/] -> CDP browser-level WebSocket * WS /devtools/page/ -> CDP page-level WebSocket (proxied to fast) * * Profile dispatch: * * - static : in-process StaticDomTransport bound to a WebSocket bridge. * - fast : spawn `lightpanda serve --port `, reverse-proxy * every endpoint and WebSocket frame to it. The presence of * this CLI just means we can speak the Bxc CLI surface * while delegating actual CDP handling to Lightpanda. * Forbidden engines : Chrome / Chromium / Firefox / Edge / Safari and * any derivative are not exposed. For server-grade anti-detection use * `launchGhostBrowser` from `src/profiles/ghost/` (Lightpanda + CDP * stealth injects via `Page.addScriptToEvaluateOnNewDocument`). */ import type { Server, ServerWebSocket } from "bun"; import { type CommonOptions } from "./shared.ts"; import type { CDPEvent } from "../transport/InProcessTransport.ts"; import { hasEmbedded, lightpandaAsset } from "../rust/embedded-assets.ts"; import { extractEmbeddedAssetIfNeeded } from "../internal/embedded-loader.ts"; import { homedir } from "node:os"; import { join } from "node:path"; import { resolveBxcConfig } from "../config/resolve.ts"; import { getVendorDir } from "../utils/paths.ts"; import downloadHtml from "../server/download.html" with { type: "text" }; import installSh from "../../install.sh" with { type: "text" }; import installPs1 from "../../install.ps1" with { type: "text" }; // NOTE: StaticDomTransport and HttpProfileTransport are loaded lazily via // dynamic import inside startStatic / startHttp. This prevents FFI libraries // (rust-bridge cdylib, curl-impersonate) from loading when the user picks // profile=fast or profile=stealth, which reduces cold start by ~30-60 ms. // --------------------------------------------------------------------------- // Argv parsing // --------------------------------------------------------------------------- interface CLIOptions { cdpPort: number; autoProfile: boolean; host: string; profile: "static" | "fast" | "http" | "stealth" | "max"; proxy?: string; logLevel: "debug" | "info" | "warn" | "error" | "silent"; } function parseArgs(argv: string[]): CLIOptions { // Skip "bun", "