Class: Unmagic::Browser::Driver::Cloudflare

Inherits:
Base
  • Object
show all
Defined in:
lib/unmagic/browser/driver/cloudflare.rb,
lib/unmagic/browser/driver/cloudflare/transport.rb

Overview

Cloudflare Browser Rendering — no browser runs on your own servers.

It's the only driver with two ways in, and it uses both. A one-off page grab goes to the REST API, which renders and returns in one request and holds no session at all. A Session connects over CDP to a real remote browser, which is what multi-step work needs — and what occupies one of your account's concurrent slots until it's closed.

Examples:

Unmagic::Browser.new(driver: Unmagic::Browser::Driver::Cloudflare.new(
  account_id: ENV["CLOUDFLARE_ACCOUNT_ID"],
  token:      ENV["CLOUDFLARE_API_TOKEN"],
))

Both credentials come from the environment

Unmagic::Browser.new(driver: :cloudflare)

Defined Under Namespace

Classes: Transport

Constant Summary collapse

API_ROOT =

Where the REST rendering endpoints live.

"https://api.cloudflare.com/client/v4/accounts/%<account_id>s/browser-rendering"
CDP_ENDPOINT =

Cloudflare's own devtools endpoint. Closing the browser rather than merely disconnecting is what frees the concurrency slot immediately; an abandoned session holds one until the service's own idle timeout reaps it.

"wss://api.cloudflare.com/client/v4/accounts/%<account_id>s/browser-rendering/devtools/browser"
REST_TIMEOUT =

Seconds to wait on a REST render before giving up. Cloudflare's own ceiling is lower than this; the margin is for the round trip.

60
TOKEN_VARIABLES =

Where the token comes from when it isn't passed, most specific first. CLOUDFLARE_API_TOKEN is Cloudflare's own name for it — the one wrangler and their docs use — so it's what a machine already set up for Cloudflare will have. The narrower name wins when both are set, so a token scoped to just Browser Rendering can be kept apart from the account-wide one.

["CLOUDFLARE_BROWSER_RENDERING_TOKEN", "CLOUDFLARE_API_TOKEN"].freeze

Constants inherited from Base

Base::DEFAULT_TIMEOUT, Base::WAIT_UNTIL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#name, #open_connection

Constructor Details

#initialize(account_id: nil, token: nil) ⇒ Cloudflare

Returns a new instance of Cloudflare.

Parameters:

  • account_id (String, nil) (defaults to: nil)

    defaults to $CLOUDFLARE_ACCOUNT_ID

  • token (String, nil) (defaults to: nil)

    an API token with Browser Rendering access, defaulting to the first of TOKEN_VARIABLES that's set

Raises:



58
59
60
61
62
63
64
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 58

def initialize(account_id: nil, token: nil)
  super()
  @account_id =  || ENV["CLOUDFLARE_ACCOUNT_ID"]
  @token = token || TOKEN_VARIABLES.filter_map { |name| presence(ENV[name]) }.first
  require_credential(@account_id, "account_id", ["CLOUDFLARE_ACCOUNT_ID"])
  require_credential(@token, "token", TOKEN_VARIABLES)
end

Instance Attribute Details

#account_idString (readonly)

Returns the Cloudflare account the browsers belong to.

Returns:

  • (String)

    the Cloudflare account the browsers belong to



44
45
46
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 44

def 
  @account_id
end

Instance Method Details

#closevoid

This method returns an undefined value.

Nothing to tear down: one-offs never open a browser, and every session owns its own.



101
102
103
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 101

def close
  nil
end

#html(url) ⇒ String

Returns the page's HTML, after JavaScript has run.

Parameters:

  • url (String)

    the page to render

Returns:

  • (String)

    the page's HTML, after JavaScript has run



74
75
76
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 74

def html(url)
  json_request("content", url: url)
end

#markdown(url) ⇒ String

Returns the page as Markdown, rendered by Cloudflare.

Parameters:

  • url (String)

    the page to render

Returns:

  • (String)

    the page as Markdown, rendered by Cloudflare



68
69
70
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 68

def markdown(url)
  json_request("markdown", url: url)
end

#pdf(url) ⇒ String

Returns PDF bytes.

Parameters:

  • url (String)

    the page to render

Returns:

  • (String)

    PDF bytes



93
94
95
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 93

def pdf(url)
  binary_request("pdf", url: url)
end

#screenshot(url, full_page: false, selector: nil) ⇒ String

Returns PNG bytes.

Parameters:

  • url (String)

    the page to render

  • full_page (Boolean) (defaults to: false)

    capture the whole scrollable page

  • selector (String, nil) (defaults to: nil)

    capture just the first element matching this CSS

Returns:

  • (String)

    PNG bytes



82
83
84
85
86
87
88
89
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 82

def screenshot(url, full_page: false, selector: nil)
  binary_request(
    "screenshot",
    url: url,
    selector: selector,
    screenshotOptions: { fullPage: full_page, type: "png" },
  )
end