Skip to content

Providing a Browser

@scwrap/browser provides the Browser Capability: browserRun and browserUse in a Worker release, nodeBrowser for tests and scripts, and playwrightBrowser for any other Playwright connection.

A browser Implementation requires Browser, or Browser<Country> when it opens pages in a country. Its Release provides one from @scwrap/browser.

ProviderImport fromRuns inCovers
browserRun@scwrap/browser/browser-runA Worker releaseNo location: Browser only.
browserUse, browserUseWith@scwrap/browser/browser-useA Worker releaseEvery country.
nodeBrowser@scwrap/browser/nodeNode: tests and scriptsNo location.
playwrightBrowser@scwrap/browserWherever its connection worksThe countries you name.

Every provider opens a new browser connection and context for each Browser.open or Browser.openIn call, and closes both when the caller's Scope closes.

A browser Scraper on an instance is a Worker release with services: browserRun or services: browserUse. The Scraper's code runs in the release Worker; only Chromium runs elsewhere.

browserRun ​

browserRun opens pages in Cloudflare Browser Run. Each call asks the instance's front desk for a Browser Run session, which goes to the release Worker's BROWSER binding (Bindings), and launches a fresh Chromium through it with @cloudflare/playwright. The Scraper needs the browser:browser-run grant (Grants); without it, opening fails with BrowserError. Outside an instance, opening always fails.

Example (A Worker release for QuotesBrowserLive from Adding a browser)

ts
import { browserRun } from "@scwrap/browser/browser-run";
import { Deployment } from "@scwrap/deploy";
import { Quotes, QuotesBrowserLive } from "@scwrap/quotes";

export default Deployment.worker({
  implementation: QuotesBrowserLive,
  module: import.meta.url,
  scraper: Quotes,
  services: browserRun,
});

Browser Run needs no key of its own: the binding belongs to the instance's account.

browserRun provides Browser, which is Browser<never>. Browser Run sends requests from Cloudflare's network with headers that identify it as a bot, and it cannot open a page in a country. A Scraper that calls Browser.openIn does not compile with it (The location is part of the type). Use it for public pages that accept that traffic.

browserUse ​

browserUse opens pages in a Browser Use cloud Chromium, connected over the Chrome DevTools Protocol. It covers every country: for Browser.openIn({ location }) it asks Browser Use for a proxy in that country (proxyCountryCode, with GB sent as uk), and for Browser.open it asks for none.

Example (A Worker release for a Scraper that opens pages in France)

ts
import { browserUse } from "@scwrap/browser/browser-use";
import { Deployment } from "@scwrap/deploy";
import { Quotes, QuotesFranceLive } from "@scwrap/quotes";

export default Deployment.worker({
  implementation: QuotesFranceLive,
  module: import.meta.url,
  scraper: Quotes,
  services: browserUse,
});

For each open, browserUse:

  1. Gets a session with the location's proxy country and a timeout. On an instance it asks the front desk, which holds the Browser Use key and needs the Scraper's browser:browser-use grant (Grants). Outside an instance, such as in a script, it reads BROWSER_USE_API_KEY from Config and calls Browser Use itself; without the key, opening fails with BrowserError and the reason Browser Use is not configured.
  2. Creation that is refused, fails or takes more than 30 seconds fails the open with BrowserError; it is not retried.
  3. Annotates the current span with browserUse.location (the country, or any) and browserUse.session.id.
  4. Connects to the session with @cloudflare/playwright and opens the page.
  5. When the scope closes, stops the session and reads back that it is stopped, retrying twice.

On an instance, the Run's owner also stops every Browser Use session the Run opened and did not stop, once the Run ends, and keeps retrying a stop that fails (Browser Use sessions end with their Run).

browserUse gives each session a 10-minute timeout at Browser Use, its last resort against a session that was never stopped. browserUseWith({ timeoutMinutes }) builds the same provider with another timeout:

ts
browserUseWith({ timeoutMinutes: 5 })

timeoutMinutes is a whole number from 1 to 240, the range Browser Use accepts; on an instance the front desk refuses any other value, and the open fails with BrowserError. A longer session timeout does not make a Run longer: the Run still ends at its maximum duration, 10 minutes unless the Release sets maxRunDuration, at most 14, and ending it closes the page's scope and stops the session (Run duration). The session timeout only bounds a session that nobody managed to stop.

nodeBrowser ​

nodeBrowser launches a local Chromium with Playwright, for acceptance tests and scripts. It uses Playwright's own Chromium when one is installed (pnpm --filter <package> exec playwright install chromium), and /usr/bin/google-chrome otherwise. It covers no location.

prepare receives each Playwright BrowserContext before the page opens. A test uses it to answer every request from fixture pages with context.route:

Example (packages/browser/test/node.test.ts)

ts
const served = (status: number) =>
  nodeBrowser({
    prepare: async (context) => {
      await context.route("**/*", async (route) => {
        await route.fulfill({ body: page, contentType: "text/html", status });
      });
    },
  });

A prepare that rejects fails the open with BrowserError.

nodeBrowser needs playwright installed next to @scwrap/browser; it is an optional peer dependency. Without prepare, the same provider runs a Scraper from a script:

ts
await Effect.runPromise(
  Scraper.run(Quotes, { maxPages: 2 }).pipe(
    Stream.runCollect,
    Effect.provide(QuotesBrowserLive.pipe(Layer.provide(nodeBrowser())))
  )
);

playwrightBrowser ​

playwrightBrowser builds a provider from any Playwright connection. nodeBrowser is built with it, and browserRun and browserUse open their pages the same way.

ts
playwrightBrowser<Country>({ connect, prepare? })
OptionMeaning
connect(location)Returns a Promise of a connected Playwright Browser. It receives the location of openIn, or undefined for open.
prepare(context)Optional. An Effect run on each new BrowserContext before the page opens.
CountryThe countries the provider covers. It defaults to never, no location.

For each open it connects, creates a context with the timezoneId and userAgent options, runs prepare, opens a page and sets the page's default timeout to the timeout option, 10 seconds when unset. Every Playwright failure becomes a BrowserError with the step's operation.

Example (A vendor that serves browsers in Germany and France)

ts
import { playwrightBrowser } from "@scwrap/browser";
import { chromium } from "playwright";

export const vendorBrowser = playwrightBrowser<"DE" | "FR">({
  connect: (location) =>
    chromium.connectOverCDP(
      `wss://browsers.example/session?country=${location ?? "any"}`
    ),
});

Name only the countries the connection really serves. A provider that covers a country it ignores makes a Scraper appear to work from an address the site will later refuse.

Source ​