lighthouse/docs/architecture.md

2.5 KiB

Architecture

Some incomplete notes

Lighthouse Architecture

Components & Terminology

  • Driver - Interfaces with Chrome Debugging Protocol (API viewer)
  • Gatherers - Uses Driver to collect information about the page. Minimal post-processing.
    • Artifacts - output of a gatherer
  • Audits - Using the Artifacts as input, Audits evaluate a test and assign pass/fail/scoring.
    • Computed Artifacts - Generated on-demand from artifacts, these add additional meaning, and are often shared amongst multiple audits.
  • Categories - Grouping audit results into a user-facing section of the report (eg. Best Practices). Applies weighting and overall scoring to the section.

Protocol

  • Interacting with Chrome: The Chrome protocol connection maintained via WebSocket for the CLI chrome.debuggger API when in the Chrome extension.
  • Event binding & domains: Some domains must be enable()d so they issue events. Once enabled, they flush any events that represent state. As such, network events will only issue after the domain is enabled. All the protocol agents resolve their Domain.enable() callback after they have flushed any pending events. See example:
// will NOT work
driver.sendCommand('Security.enable').then(_ => {
  driver.on('Security.securityStateChanged', state => { /* ... */ });
})

// WILL work! happy happy. :)
driver.on('Security.securityStateChanged', state => { /* ... */ }); // event binding is synchronous
driver.sendCommand('Security.enable');

Audits

The return value of each audit takes this shape:

Promise.resolve({
  name: 'audit-name',
  description: 'whatnot',
  // value: The score. Typically a boolean, but can be number 0-100
  value: 0,
  // rawValue: Could be anything, as long as it can easily be stringified and displayed,
  //   e.g. 'your score is bad because you wrote ${rawValue}'
  rawValue: {},
  // debugString: Some *specific* error string for helping the user figure out why they failed here.
  //   The reporter can handle *general* feedback on how to fix, e.g. links to the docs
  debugString: 'Your manifest 404ed',
});