ensure-correct-devtools-protocol-package.mts (2868B)
1 /** 2 * @license 3 * Copyright 2020 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * This script ensures that the pinned version of devtools-protocol in 9 * package.json is the right version for the current revision of Chrome that 10 * Puppeteer ships with. 11 * 12 * The devtools-protocol package publisher runs every hour and checks if there 13 * are protocol changes. If there are, it will be versioned with the revision 14 * number of the commit that last changed the .pdl files. 15 * 16 * Chrome branches/releases are figured out at a later point in time, so it's 17 * not true that each Chrome revision will have an exact matching revision 18 * version of devtools-protocol. To ensure we're using a devtools-protocol that 19 * is aligned with our revision, we want to find the largest package number 20 * that's \<= the revision that Puppeteer is using. 21 * 22 * This script uses npm's `view` function to list all versions in a range and 23 * find the one closest to our Chrome revision. 24 */ 25 26 import {execSync} from 'child_process'; 27 28 import packageJson from '../package.json' with {type: 'json'}; 29 import {PUPPETEER_REVISIONS} from '../lib/esm/puppeteer/revisions.js'; 30 31 async function main() { 32 const currentProtocolPackageInstalledVersion = 33 packageJson.dependencies['devtools-protocol']; 34 35 /** 36 * Ensure that the devtools-protocol version is pinned. 37 */ 38 if (/^[^0-9]/.test(currentProtocolPackageInstalledVersion)) { 39 console.log( 40 `ERROR: devtools-protocol package is not pinned to a specific version.\n`, 41 ); 42 process.exit(1); 43 } 44 45 const chromeVersion = PUPPETEER_REVISIONS.chrome; 46 // find the right revision for our Chrome version. 47 const req = await fetch( 48 `https://googlechromelabs.github.io/chrome-for-testing/known-good-versions.json`, 49 ); 50 const releases = await req.json(); 51 const chromeRevision = releases.versions.find(release => { 52 return release.version === chromeVersion; 53 }).revision; 54 console.log(`Revisions for ${chromeVersion}: ${chromeRevision}`); 55 56 const command = `npm view "devtools-protocol@<=0.0.${chromeRevision}" version | tail -1`; 57 58 console.log( 59 'Checking npm for devtools-protocol revisions:\n', 60 `'${command}'`, 61 '\n', 62 ); 63 64 const output = execSync(command, { 65 encoding: 'utf8', 66 }); 67 68 const bestRevisionFromNpm = output.split(' ')[1]!.replace(/'|\n/g, ''); 69 70 if (currentProtocolPackageInstalledVersion !== bestRevisionFromNpm) { 71 console.log(`ERROR: bad devtools-protocol revision detected: 72 73 Current Puppeteer Chrome revision: ${chromeRevision} 74 Current devtools-protocol version in package.json: ${currentProtocolPackageInstalledVersion} 75 Expected devtools-protocol version: ${bestRevisionFromNpm}`); 76 77 process.exit(1); 78 } 79 80 console.log( 81 `Correct devtools-protocol version found (${bestRevisionFromNpm}).`, 82 ); 83 process.exit(0); 84 } 85 86 void main();