paulirish / pwmetrics

Progressive web metrics at your fingertipz
Apache License 2.0
1.25k stars 74 forks source link

Cannot specify --extra-headers CLI option, errors with "Invalid parameters (headers: object expected)" #231

Open mikehenrty opened 4 years ago

mikehenrty commented 4 years ago

Config / CLI options

Using a JS config file, I am correctly able to specify additional request headers, like so:

module.exports = {
  flags: {
    chromeFlags: '--headless --disable-gpu --no-sandbox"',
    extraHeaders: {
      'My-Special-Header': 'data'
    },
  }
}

But when I try to speficy these extra headers using CLI options like:

pwmetrics --extra-headers "{\"My-Special-Header\":\"data\"}" https://www.example.com --chrome-flags="--headless --disable-gpu --no-sandbox"

I see this error Protocol error (Network.setExtraHTTPHeaders): Invalid parameters (headers: object expected)

I believe the problem is here: https://github.com/paulirish/pwmetrics/blob/f8a784fd6930a1e8b79c1a00e7cd129178638edf/lib/index.ts#L61

The option flag for pwmetrics gets captured as a string, and then pass straight into the NodeJS Lighthouse Runner as a string. Since the runner expects an object rather than string, we see the aforementioned error: https://github.com/paulirish/pwmetrics/blob/f8a784fd6930a1e8b79c1a00e7cd129178638edf/lib/lh-runner.ts#L43

One possible solution would be to special case the opts.flags.extraHeaders argument, and JSON.parse it.

Note, it seems other projects have had similar issues: https://github.com/sitespeedio/plugin-lighthouse/issues/30

Environment

  1. pwmetrics version: 4.2.3 (lighthouse 5.2.0) Note sure if relevant, but I also have Lighthouse 6 installed globally through yarn

  2. Chrome version: "chrome-launcher": "^0.10.2"

  3. OS version: Linux, 5.6.15-arch