Skip to content

browser.ts#

View Markdown

Source

6 documented symbols. Read the signatures first, then expand each item for parameters, return types, and examples.

6 symbols 2 functions 3 interfaces 1 modules 1 parameters 4 members 2 returns

Reference#

modulebrowserChromium browser session with automatic cleanup via Explicit Resource Management. Usage: await using session = await op…

Chromium browser session with automatic cleanup via Explicit Resource Management.

Usage: await using session = await openBrowser(); const png = await session.renderPage(html, 1200, 630); // browser.close() is called automatically when session goes out of scope

View source

interfaceOgBrowserPool extends AsyncDisposableA set of independent sessions, closed together.

A set of independent sessions, closed together.

Signature

export interface OgBrowserPool extends AsyncDisposable

View source

Members

Properties
NameTypeDescription
sessions OgBrowserSession[]
interfaceOgBrowserSession extends AsyncDisposableA browser session that can render HTML pages to PNG. Implements AsyncDisposable…

A browser session that can render HTML pages to PNG. Implements AsyncDisposable for automatic cleanup via await using.

Signature

export interface OgBrowserSession extends AsyncDisposable

View source

Members

Methods
renderPage()
renderPage(html: string, width: number, height: number, publicDir?: string): Promise<Buffer>;
Parameters
  • html
  • width
  • height
  • publicDir optional
Returns
Promise<Buffer>
fnopenBrowser(): Promise<OgBrowserSession | null>Opens a Chromium browser and returns a session for rendering OG images. Returns…

Opens a Chromium browser and returns a session for rendering OG images. Returns null if Playwright/Chromium is not available.

The session implements AsyncDisposable — use await using for automatic cleanup:

await using session = await openBrowser();
if (!session) return;
const png = await session.renderPage(html, 1200, 630);

Signature

export async function openBrowser(): Promise<OgBrowserSession | null>

View source

Returns

Promise<OgBrowserSession | null>
fnopenBrowserPool(size: number): Promise<OgBrowserPool | null>Opens up to size independent browsers. Rendering does not parallelize inside on…

Opens up to size independent browsers.

Rendering does not parallelize inside one browser: every page shares a single connection to it, so eight pages in one browser finish no sooner than one does. Measured on 120 cards, milliseconds per image:

browsers 1 2 4 8 pages 1 84.6 — 25.6 14.7 pages 8 80.5 42.0 23.1 —

Pages buy nothing; browsers scale nearly linearly. Each one costs its own process, so this is bounded by the caller's concurrency, and concurrency: 1 keeps the old single-process footprint.

Returns null when Chromium cannot be launched at all. A launch that fails after the first still yields a working pool, one browser smaller.

Signature

export async function openBrowserPool(size: number): Promise<OgBrowserPool | null>

View source

Parameters

  • size number

Returns

Promise<OgBrowserPool | null>
interfacePooledPageA pooled page and the public directory its route handler was set up for.

A pooled page and the public directory its route handler was set up for.

Signature

interface PooledPage

View source

Members

Properties
NameTypeDescription
page Page
routedForoptional string