All files / src/core/bindings BrowserBinding.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70              28x 28x                                                                                                     28x         8x   8x      
/**
 * @module
 *
 * @author Bayu Dwiyan Satria
 * @version 1.0.0
 * @since 1.3.0
 */
import { Binding } from '@/core/bindings/Binding'
import { lazySettings } from '@/utils/lazySettings'
import { BrowserSettings } from '@/types/BrowserSettings'
 
import type { CloudflareEnv } from '@/types/CloudflareEnv'
 
/**
 * Accessor for the Browser Rendering binding.
 *
 * @remarks
 * Browser Rendering was the one binding in this package with no accessor, which
 * meant the one Worker using it reached `env.BROWSER` directly. An absent
 * binding therefore arrived at the browser driver as `undefined` and failed
 * somewhere inside it, in that library's terms rather than in terms of the
 * deployment that forgot to declare it. This class makes that a
 * `MissingBindingError` naming `BROWSER`, like every other resource.
 *
 * ## Why there is no `BrowserService`
 *
 * Every other binding here has a capability service beside it. This one does
 * not, and that is deliberate. Doing anything with a browser beyond holding the
 * binding means driving it — launching, navigating, evaluating — which needs
 * `@cloudflare/puppeteer`, a real runtime dependency rather than a type. Taking
 * it on would put a browser automation library in the dependency tree of every
 * Worker that installs this package to get a KV accessor.
 *
 * The driving itself also has exactly one consumer today, and a capability with
 * one implementation and one caller has not yet earned the interface. If a
 * second appears, the shape to copy is `./middlewares`: a separate subpath
 * behind an optional peer, so a consumer that does not render pages never
 * resolves the dependency.
 *
 * `Fetcher` rather than a browser-library type for the same reason — it is what
 * the runtime hands over, and naming it costs no dependency.
 *
 * @example
 * ```ts
 * const browser = new BrowserBinding()
 *
 * if (!browser.isBound(env)) {
 *   return fail('This deployment cannot render pages')
 * }
 *
 * const session = await puppeteer.launch(browser.resolve(env))
 * ```
 *
 * @class
 *
 * @author Bayu Dwiyan Satria
 * @version 1.0.0
 * @since 1.3.0
 */
export class BrowserBinding<E extends CloudflareEnv = CloudflareEnv> extends Binding<Fetcher, E> {
  /**
   * @param settings Optional overrides. Defaults to the resolved `browser` module.
   */
  constructor(settings?: BrowserSettings) {
    const settle = lazySettings<BrowserSettings>('browser', settings)
 
    super(() => settle().binding)
  }
}