Repository navigation
feat(selfhost): render_page through an optional Chrome sidecar (#292) - #339
Merged
Merged
Conversation
CLOUDHAL_BROWSER_URL (Chrome's DevTools port, http://chrome:9222) makes the generated workerd config bind an ExternalServer to the admin Worker as BROWSER_CDP. @cloudhal/cloudflare's new devtoolsRenderer drives it with the shared cdpSession: /json/version and the browser socket over Host: localhost, one browser context per render, disposed after it; the shared browser is never closed. adminRender picks Browser Rendering first, else the sidecar, and CLOUDHAL_RENDER=off still turns both off. render_page opens only the page's own signed preview link. RenderDeps.origin (CLOUDHAL_RENDER_ORIGIN, default http://cloudhal:<site port>, the same on every instance) opens that link at the private origin the browser reaches the site at, so a self-host site without a domain renders too (core-ts previewUrlAt). The admin Worker gets increase_websocket_message_size while a sidecar is configured, for DevTools messages above 1 MiB. Recipes: compose's chrome profile (chromedp/headless-shell pinned, no port, its own network shared with Cloudhal only, so it cannot reach the store), chrome.* Helm values (Deployment, Service, NetworkPolicy admitting the release's instances only), Swarm docs. CI: scripts/render-smoke.ts calls render_page over /mcp with an agent key and checks a JPEG comes back and no render page is left; the compose and chart checks cover the new parts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
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.
render_pagenow works on self-host through an optional Chrome sidecar (RFC-0047 §6). Before this, the image carried no browser, so the tool reported itself unavailable.Design
CLOUDHAL_BROWSER_URLnames Chrome's DevTools port, for examplehttp://chrome:9222. The generated workerd config turns it into(name = "browser", external = (address = "chrome:9222", http = ())), bound only to the admin Worker asBROWSER_CDP(packages/cli/src/selfhost/browser-sidecar.ts).@cloudhal/cloudflarebrowser-cdp.tsholdsdevtoolsRenderer. It readsGET /json/version, then opens the/devtools/browser/<id>socket, both withHost: localhost: Chrome refuses DevTools HTTP for any other host name, and the binding delivers the request to the sidecar whatever the URL says. From there the sharedcdpSessiondrives it.adminRender(env)replaces the inline choice inadmin-worker.ts. Browser Rendering (BROWSER) wins when present, otherwise the sidecar.CLOUDHAL_RENDER=offstill turns both off.createAdminWorker>fetchgets shorter, so the size baseline holds.increase_websocket_message_size, so a DevTools message above 1 MiB (a tall screenshot) is not refused. Browser Rendering chunks its messages; a plain Chrome doesn't.The rules from the issue
chromeprofile publishes no port and runs on its ownrendernetwork, shared with Cloudhal and nothing else, so the browser cannot reach SeaweedFS.chrome.enabledadds a Deployment, a Service and a NetworkPolicy that admits only the release's instances on 9222. There is no Ingress.cdpSessionwithoutcloseBrowser:Target.createBrowserContext, thendisposeBrowserContexton close. The shared browser is never closed. The unit test checks the exact commands, and the smoke checks that no page of the render is left in Chrome.previewDoc's token). NewRenderDeps.origin(CLOUDHAL_RENDER_ORIGIN, defaulthttp://cloudhal:<site port>) opens the same signed link at the private origin the sidecar reaches the site at, withpreviewUrlAtin core-ts. This also covers a self-host site with no domain, which until now had no preview link at all. The origin is one value for all instances, because the config must be identical (RFC-0047 §2, invariant 2).CI
scripts/render-smoke.ts. It refuses to run outside GitHub Actions.cloudhal.initializeandtools/call render_pagego over/mcp, and a JPEG of more than 2 KB must come back./json/listmust show no page left over.--profile chrome.chrome.enabled.Tests and docs
packages/cloudflare/test/browser-cdp.test.ts: the requests, the context lifecycle with noBrowser.close, the socket-path check, and which renderer is chosen.packages/mcp-server/test/render-tools.test.ts: the render origin, with and without a domain.packages/cli/test/selfhost-browser.test.ts: the settings and config.render_page. RFC-0047 §6,deploy/docker/README.md,deploy/helm/cloudhal/README.md,deploy/swarm/README.md, AGENTS.md and.env.exampleare updated too.Checked locally
biome check --diagnostic-level=error,pnpm size:checkandactionlint.tscof cli, cloudflare, mcp-server, core-ts and the smoke script.selfhost-browser/selfhost,browser-cdp,render-toolsandpreview-token: all pass.No Docker, compose or helm runs happened locally. This PR's Build edge run is the first time workerd talks to Chrome: the external service binding, the WebSocket upgrade and the compat flag.
Closes #292
🤖 Generated with Claude Code