feat: add CDPServer to drive lightpanda with Playwright/Puppeteer - #1
Merged
Conversation
Add `CDPServer` / `AsyncCDPServer`, which spawn `lightpanda serve` (the browser's native Chrome DevTools Protocol server) on a free localhost port and expose `ws_endpoint` / `http_endpoint` for Playwright's `connect_over_cdp`, Puppeteer's `connect`, chromedp, etc. No Chromium and no runtime dependencies are involved. It is a separate process from `Browser`, since the binary cannot serve MCP and CDP from one process. The spawn loop in client.py becomes a shared `_spawn(binary, mode, ...)` helper with an optional readiness probe (CDP probes `/json/version`) and a fixed-port path that reports a port collision clearly. Tests cover the process lifecycle with the standard library, plus real `connect_over_cdp` sessions through both endpoints using the `playwright` package as a CDP client (dev group only, no browser download; skipped when absent).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
CDPServer/AsyncCDPServer, which spawnlightpanda serve(the browser's native Chrome DevTools Protocol server) on a free localhost port and exposews_endpoint/http_endpointfor any CDP client:The same endpoint works for Node Puppeteer (
puppeteer.connect({ browserWSEndpoint })), Node Playwright (chromium.connectOverCDP), chromedp, etc. No Chromium is involved and the package still has zero runtime dependencies.It is a separate process from
Browser: the binary refusesmcp --porttogether with--cdp-port, and MCP sessions and CDP browser contexts are unrelated anyway.How
client.py: the spawn loop moves out ofClient.__init__into a shared_spawn(binary, mode, ...)helper._reserve_port(port=0)serves both the free-port path (retried on a bind collision, as before) and a caller-pinnedport=, which reports "port N is already in use" instead of an opaque startup failure.cdp.py:CDPServer(sync, context manager,__del__cleanup, Linux PDEATHSIG via the shared spawn path) andAsyncCDPServer(lazystart()overasyncio.to_thread, mirroringAsyncBrowser's shape).127.0.0.1literal on purpose: the server only upgrades on path/, rejects anyOriginheader, and only accepts an IP-literal orlocalhostHost.Tests
tests/test_cdp.py(stdlib only): endpoints and/json/version, pinned port, port in use,serveflag passthrough,close(), async lazy start. The PDEATHSIG test intest_browser.pyis now parametrized overBrowserandCDPServer.tests/test_cdp_playwright.py: realconnect_over_cdpsessions through both the ws and http endpoints.playwrightis added to thedevdependency group and the CI wheel-test venv only;connect_over_cdpneeds the pip package, not a browser download, so there is noplaywright installstep. The tests skip when it is absent.puppeteer-corefrom the demo repo against aCDPServerendpoint.Docs
README gets a "Drive it with Playwright or Puppeteer" section; the package docstring points at
CDPServer. The docs site's Python reference will need a section for the new classes once this ships.