Class: Unmagic::Browser::Driver::Cloudflare
- 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.
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_TOKENis 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
-
#account_id ⇒ String
readonly
The Cloudflare account the browsers belong to.
Instance Method Summary collapse
-
#close ⇒ void
Nothing to tear down: one-offs never open a browser, and every session owns its own.
-
#html(url) ⇒ String
The page's HTML, after JavaScript has run.
-
#initialize(account_id: nil, token: nil) ⇒ Cloudflare
constructor
A new instance of Cloudflare.
-
#markdown(url) ⇒ String
The page as Markdown, rendered by Cloudflare.
-
#pdf(url) ⇒ String
PDF bytes.
-
#screenshot(url, full_page: false, selector: nil) ⇒ String
PNG bytes.
Methods inherited from Base
Constructor Details
#initialize(account_id: nil, token: nil) ⇒ Cloudflare
Returns a new instance of Cloudflare.
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 = 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_id ⇒ String (readonly)
Returns the Cloudflare account the browsers belong to.
44 45 46 |
# File 'lib/unmagic/browser/driver/cloudflare.rb', line 44 def account_id @account_id end |
Instance Method Details
#close ⇒ void
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.
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.
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.
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.
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 |