import { run , ToolingLog } from '@kbn/dev-utils' ; import { REPO_ROOT } from '@kbn/utils' ; import chalk from 'chalk' ; import cheerio from 'cheerio' ; import fs from 'fs' ; import fetch from 'node-fetch' ; import path from 'path' ; import { PackageJson } from 'type-fest' ; type PuppeteerRelease = string ; type ChromiumRevision = string ; type ChromiumCommit = string ; const forkCompatibilityMap = { '5.4.1-patch.1' : '5.4.1' , } ; async function getPuppeteerRelease ( log ) { const puppeteerPackageJson = JSON . parse ( fs . readFileSync ( path . resolve ( REPO_ROOT , 'node_modules' , 'puppeteer' , 'package.json' ) , 'utf8' ) ) ; const { version } = puppeteerPackageJson ; if ( version == null ) { throw new Error ( 'Could not get the Puppeteer version! Check node_modules/puppteer/package.json' ) ; } const puppeteerRelease = forkCompatibilityMap [ version ] ?? version ; log . info ( `Kibana is using Puppeteer ${ version } (${ puppeteerRelease })` ) ; return puppeteerRelease ; } async function getChromiumRevision ( kibanaPuppeteerVersion , log ) { const url = `https://raw.githubusercontent.com/puppeteer/puppeteer/v${ kibanaPuppeteerVersion }/src/revisions.ts` ; let body ; try { log . info ( `Fetching code from Puppeteer source: ${ url }` ) ; const rawSource = await fetch ( url ) ; body = await rawSource . text ( ) ; } catch ( err ) { log . error ( err ) ; throw new Error ( `Could not fetch ${ url }. Check the URL in a browser and try again.` ) ; } let revision ; const lines = body . split ( '\n' ) ; let cursor = lines . length ; while ( -- cursor >= 0 ) { const test = lines [ cursor ] . match ( /^\s+chromium: '(\S+)',$/ ) ; if ( test != null ) { log . debug ( `Parsed revision from source text: \`${ lines [ cursor ] }\`` ) ; [ , revision ] = test ; break ; } } if ( revision == null ) { throw new Error ( `Could not find a Chromium revision listed in Puppeteer source! Check ${ url } in a browser` ) ; } log . info ( `Found Chromium revision ${ revision } from Puppeteer ${ kibanaPuppeteerVersion }` ) ; return revision ; } async function getChromiumCommit ( revision , log ) { const url = `https://crrev.com/${ revision }` ; log . info ( `Fetching ${ url }` ) ; const pageText = await fetch ( url ) ; const $ = cheerio . load ( await pageText . text ( ) ) ; let commit = null ; const matches = $ ( 'title' ) . text ( ) . match ( /\S{40}/ ) ; if ( matches != null ) { log . debug ( `Parsed commit hash from page title: \`${ $ ( 'title' ) . text ( ) }\`` ) ; [ commit ] = matches ; } if ( commit == null ) { throw new Error ( `Could not find a Chromium commit! Check ${ url } in a browser.` ) ; } log . info ( `Found Chromium commit ${ commit } from revision ${ revision }.` ) ; return commit ; } run ( async ( { log , flags : { _ : [ puppeteerVersionArg ] , } , } ) => { try { let puppeteerVersion ; if ( puppeteerVersionArg ) { puppeteerVersion = puppeteerVersionArg ; } else { puppeteerVersion = await getPuppeteerRelease ( log ) ; } const chromiumRevision = await getChromiumRevision ( puppeteerVersion , log ) ; await getChromiumCommit ( chromiumRevision , log ) ; } catch ( err ) { log . error ( err ) ; } } , { description : chalk `
      Display the Chromium git commit that correlates to a given Puppeteer release.

      -  node scripts/chromium_version 5.5.0  {dim # gets the Chromium commit for Puppeteer v5.5.0}
      -  node scripts/chromium_version       {dim  # gets the Chromium commit for the Kibana dependency version of Puppeteer}

      You can use https://omahaproxy.appspot.com/ to look up the Chromium release that first shipped with that commit.
    ` , } ) ;