├── .github └── workflows │ └── test.yml ├── .gitignore ├── .vscode └── settings.json ├── CHANGELOG.md ├── Dockerfile ├── LICENSE ├── README.md ├── eslint.config.js ├── lib ├── detect.js ├── download.js ├── index.js ├── install.js ├── options.js └── util.js ├── package.json └── test └── test.js /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: ["master"] 6 | 7 | pull_request: 8 | branches: ["master"] 9 | 10 | workflow_dispatch: 11 | 12 | permissions: 13 | contents: read 14 | 15 | jobs: 16 | 17 | test: 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | os: 22 | - ubuntu-latest 23 | - windows-latest 24 | - macos-latest 25 | 26 | runs-on: ${{ matrix.os }} 27 | name: Test on ${{ matrix.os }} 28 | 29 | steps: 30 | - name: Checkout 31 | uses: actions/checkout@v4 32 | 33 | - name: Use Node.js version 34 | uses: actions/setup-node@v4 35 | with: 36 | node-version: 20 37 | cache: '' 38 | 39 | - run: npm install 40 | - run: npm run test 41 | env: 42 | FORCE_COLOR: true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /package-lock.json 3 | /.temp 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[javascript]": { 3 | "editor.defaultFormatter": "dbaeumer.vscode-eslint" 4 | }, 5 | "editor.codeActionsOnSave": { 6 | "source.fixAll.eslint": "explicit" 7 | }, 8 | "editor.formatOnSave": true, 9 | "eslint.format.enable": true, 10 | "eslint.validate": [ 11 | "javascript" 12 | ] 13 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | > major version following puppeteer-core 2 | 3 | + v24.0.1 4 | - support PUPPETEER_REVISION and PUPPETEER_DEFAULT_HOST 5 | 6 | + v24.0.0 7 | - bump puppeteer-core@24.0.0 and @puppeteer/browsers@2.7.0 8 | 9 | + v23.0.0 10 | - updated puppeteer-core to ^23.1.0 11 | 12 | + v22.0.0 13 | - updated puppeteer-core to ^22.0.0 14 | 15 | + v21.0.0 16 | - updated puppeteer-core to ^21.4.1 17 | 18 | + v20.0.0 19 | - updated puppeteer-core to ^20.0.0 20 | - updated headless="new" 21 | - replaced puppeteer.BrowserFetcher (Deprecated) with @puppeteer/browsers 22 | 23 | + v19.3.2 24 | - added env PUPPETEER_EXECUTABLE_PATH to skip download when installation 25 | 26 | + v19.3.1 27 | - fixed minor issue for docker build 28 | 29 | + v19.3.0 30 | - updated ChromiumRevision to 1108766 from 1095492 31 | 32 | + v19.2.0 33 | - added new option downloadPath 34 | - code refactoring 35 | 36 | + v19.1.0 37 | - moved .stats.json to [user home dir]/.pcr-stats.json 38 | 39 | + v19.0.0 40 | - updated puppeteer-core to ^19.0.0 41 | - updated gauge to ^5.0.0 42 | 43 | + v18.0.0 44 | - updated puppeteer-core to ^18.0.5 45 | 46 | + v17.0.0 47 | - updated puppeteer-core to ^17.0.0 48 | 49 | + v16.0.0 50 | - updated puppeteer-core to ^16.0.0 51 | 52 | + v15.0.0 53 | - updated puppeteer-core to ^15.0.0 54 | 55 | + v14.0.0 56 | - updated puppeteer-core to ^14.0.0 57 | 58 | + v12.0.0 59 | - updated puppeteer-core to ^12.0.1 60 | 61 | + v11.0.0 62 | - updated puppeteer-core to ^11.0.0 63 | 64 | + v10.0.0 65 | - updated puppeteer-core to ^10.0.0 66 | 67 | + v9.0.0 68 | - updated puppeteer-core to ^9.1.1 69 | 70 | + v8.1.1 71 | - supported reading option from root package.json with "pcr" object 72 | - updated PCR(option) API to support using stats cache 73 | 74 | + v8.0.0 75 | - updated puppeteer-core to v8.0.0 76 | 77 | + v7.0.0 78 | - updated puppeteer-core to v7.1.0 79 | 80 | + v5.2.0 81 | - updated puppeteer-core to v5.2.1 82 | 83 | + v5.0.1 84 | - updated puppeteer-core to v5 85 | - added sync API getStats() 86 | 87 | + v4.0.0 88 | - updated puppeteer-core to v4 89 | 90 | + v3.2.0 91 | - updated puppeteer-core to v3 92 | 93 | + v3.1.0 94 | - updated puppeteer-core version to 2.1.1 95 | - auto detect host response time and download from quicker one 96 | 97 | + v3.0.1 98 | + v2.0.2 99 | - added option cacheRevisions to cache multiple revisions 100 | 101 | + v3.0.0 102 | - updated puppeteer-core version to v2.0.0 103 | 104 | + v2.0.1 105 | - updated puppeteer-core version to v1.19.0 106 | - refactoring with async/await 107 | - fixed requesting timeout 108 | 109 | + v1.0.12 110 | - updated puppeteer-core version to v1.18.1 111 | - fixed a gauge log issue 112 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:18.15 2 | 3 | RUN apt-get update 4 | RUN apt-get install -y ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils 5 | 6 | 7 | WORKDIR /workspace/my-project/ 8 | 9 | #ENV PUPPETEER_SKIP_DOWNLOAD=true 10 | 11 | #COPY package.json . 12 | #COPY lib ./lib/ 13 | #COPY test ./test/ 14 | #RUN ls -l 15 | #RUN npm install --dd 16 | 17 | 18 | RUN npm init -y 19 | RUN npm install puppeteer-chromium-resolver --dd -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 cenfun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Puppeteer Chromium Resolver 3 | > Tool to resolve puppeteer and chromium faster, detect local installed chromium, download chromium with custom mirror host, cache chromium revision out of node_modules, test chromium headless being launchable. 4 | 5 |  6 |  7 |  8 | [](https://www.npmjs.com/package/puppeteer-chromium-resolver) 9 |  10 | 11 | * Tool to customize [puppeteer](https://github.com/GoogleChrome/puppeteer) 12 | * Detect local chromium automatically 13 | * Download chromium from custom mirror host 14 | * Cache chromium to custom local folder 15 | * Try launching chromium and resolve launchable and version 16 | * Resolve chromium executablePath and puppeteer 17 | 18 | 19 | ## Install 20 | ```sh 21 | npm i puppeteer-chromium-resolver 22 | ``` 23 | ## Usage 24 | ### [Async] dynamic detection and downloading chromium 25 | ```js 26 | const PCR = require("puppeteer-chromium-resolver"); 27 | const options = {}; 28 | const stats = await PCR(options); 29 | const browser = await stats.puppeteer.launch({ 30 | headless: false, 31 | args: ["--no-sandbox"], 32 | executablePath: stats.executablePath 33 | }).catch(function(error) { 34 | console.log(error); 35 | }); 36 | const page = await browser.newPage(); 37 | await page.goto("https://www.npmjs.com/package/puppeteer-chromium-resolver"); 38 | await browser.close(); 39 | ``` 40 | 41 | ### [Sync] chromium will be pre-downloaded when PCR installation, so calling getStats() will get PCR stats from previous installation. 42 | ```js 43 | const PCR = require("puppeteer-chromium-resolver"); 44 | const options = {}; 45 | const stats = PCR.getStats(options); 46 | if (stats) { 47 | stats.puppeteer.launch({ 48 | headless: false, 49 | args: ["--no-sandbox"], 50 | executablePath: stats.executablePath 51 | }).then(function(browser){ 52 | //... 53 | }).catch(function(error) { 54 | console.log(error); 55 | }); 56 | } 57 | ``` 58 | 59 | ## Default Options 60 | ```js 61 | const options = { 62 | // the chromium revision to use 63 | // default is puppeteer.PUPPETEER_REVISIONS.chromium 64 | // or you can use PUPPETEER_REVISION environment variable 65 | revision: '', 66 | 67 | // additional path to detect local chromium copy (separate with a comma if multiple paths) 68 | detectionPath: '', 69 | 70 | // custom path to download chromium to local, require dir permission: 0o777 71 | // default is user home dir 72 | downloadPath: '', 73 | 74 | // the folder name for chromium snapshots (maybe there are multiple versions) 75 | folderName: '.chromium-browser-snapshots', 76 | 77 | // the stats file name, cache stats info for latest installation 78 | statsName: '.pcr-stats.json', 79 | 80 | // default hosts are ['https://storage.googleapis.com'] 81 | // or you can use PUPPETEER_DEFAULT_HOST environment variable 82 | hosts: [], 83 | 84 | cacheRevisions: 2, 85 | retry: 3, 86 | silent: false 87 | }; 88 | 89 | ``` 90 | see [lib/options.js](/lib/options.js) 91 | 92 | ### Option from root package.json with "pcr" object 93 | ```json 94 | { 95 | // ... 96 | "pcr": { 97 | "revision": "1138907" 98 | } 99 | } 100 | ``` 101 | 102 | ## Return Stats 103 | |Property | Type | | 104 | | :--------------| :------ | :---------------------- | 105 | |revision | String |current chromium revision | 106 | |executablePath | String |chromium executable path | 107 | |chromiumVersion | String |chromium version | 108 | |launchable | Boolean |chromium launchable | 109 | |puppeteerVersion| String |puppeteer version | 110 | |puppeteer | Object |puppeteer module | 111 | 112 | 113 | ## Test Cases 114 | see [test/test.js](/test/test.js) 115 | 116 | ### How to make puppeteer work with puppeteer-chromium-resolver 117 | * 1, Sets env PUPPETEER_SKIP_DOWNLOAD to skip download Chromium when installation (.npmrc) 118 | ``` 119 | PUPPETEER_SKIP_DOWNLOAD=true 120 | ``` 121 | * 2, Sets env PUPPETEER_EXECUTABLE_PATH to PCR executablePath globally or pass in launch option executablePath 122 | ```js 123 | (async () => { 124 | 125 | const PCR = require("puppeteer-chromium-resolver"); 126 | const puppeteer = require("puppeteer"); 127 | const stats = await PCR({}); 128 | //update global env 129 | process.env.PUPPETEER_EXECUTABLE_PATH = stats.executablePath; 130 | 131 | //or specify executablePath 132 | const browser = await puppeteer.launch({ 133 | executablePath: stats.executablePath, 134 | headless: false 135 | }); 136 | 137 | })(); 138 | ``` 139 | 140 | ## Troubleshooting 141 | - CentOS: error while loading shared libraries: libatk-1.0.so.0: cannot open shared objecsuch file or directory 142 | ```sh 143 | # Install dependencies: 144 | yum install -y alsa-lib.x86_64 \ 145 | atk.x86_64 \ 146 | cups-libs.x86_64 \ 147 | gtk3.x86_64 \ 148 | ipa-gothic-fonts \ 149 | libXcomposite.x86_64 \ 150 | libXcursor.x86_64 \ 151 | libXdamage.x86_64 \ 152 | libXext.x86_64 \ 153 | libXi.x86_64 \ 154 | libXrandr.x86_64 \ 155 | libXScrnSaver.x86_64 \ 156 | libXtst.x86_64 \ 157 | pango.x86_64 \ 158 | xorg-x11-fonts-100dpi \ 159 | xorg-x11-fonts-75dpi \ 160 | xorg-x11-fonts-cyrillic \ 161 | xorg-x11-fonts-misc \ 162 | xorg-x11-fonts-Type1 \ 163 | xorg-x11-utils 164 | 165 | # After installing dependencies you need to update nss library: 166 | yum update nss -y 167 | ``` 168 | - Debian (e.g. Ubuntu): error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory 169 | ```sh 170 | sudo apt-get install -y ca-certificates fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils 171 | ``` 172 | more [https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) 173 | 174 | ## CHANGELOG 175 | [CHANGELOG.md](CHANGELOG.md) -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | // https://eslint.org/docs/rules/ 2 | 3 | const plus = require('eslint-config-plus'); 4 | 5 | // https://eslint.org/docs/latest/use/configure/configuration-files 6 | module.exports = [ 7 | plus 8 | ]; 9 | -------------------------------------------------------------------------------- /lib/detect.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const EC = require('eight-colors'); 4 | const Util = require('./util.js'); 5 | 6 | const getDetectionPath = (options) => { 7 | let detectionPath = options.detectionPath; 8 | if (Array.isArray(detectionPath)) { 9 | return detectionPath; 10 | } 11 | detectionPath = `${detectionPath}`; 12 | if (detectionPath) { 13 | return detectionPath.split(','); 14 | } 15 | return []; 16 | }; 17 | 18 | 19 | const getDetectionList = (options) => { 20 | // from user custom 21 | const detectionList = getDetectionPath(options); 22 | 23 | // from chromium snapshots dir 24 | detectionList.push(options.snapshotsDir); 25 | 26 | // from current folder and up 5 level folder 27 | const maxLevel = 5; 28 | let level = 0; 29 | let current = process.cwd(); 30 | while (current && level < maxLevel) { 31 | const dir = path.resolve(current, options.folderName); 32 | detectionList.push(dir); 33 | const parent = path.resolve(current, '../'); 34 | if (parent === current) { 35 | current = ''; 36 | } else { 37 | current = parent; 38 | } 39 | level += 1; 40 | } 41 | 42 | // all detection list 43 | return detectionList; 44 | }; 45 | 46 | const detectionPathHandler = (options, detectionPath) => { 47 | detectionPath = path.resolve(detectionPath); 48 | 49 | const revision = options.revision; 50 | 51 | const executablePath = Util.computeExecutablePath({ 52 | buildId: revision, 53 | cacheDir: detectionPath 54 | }); 55 | 56 | if (!executablePath || !fs.existsSync(executablePath)) { 57 | return; 58 | } 59 | 60 | return executablePath; 61 | 62 | }; 63 | 64 | const detectionHandler = (options) => { 65 | 66 | const detectionList = getDetectionList(options); 67 | // console.log(detectionList); 68 | 69 | for (const detectionPath of detectionList) { 70 | const executablePath = detectionPathHandler(options, detectionPath); 71 | if (executablePath) { 72 | return executablePath; 73 | } 74 | } 75 | return null; 76 | }; 77 | 78 | // ========================================================================================= 79 | 80 | module.exports = (options) => { 81 | const executablePath = detectionHandler(options); 82 | if (executablePath) { 83 | options.executablePath = executablePath; 84 | Util.output(`Found local chromium: ${EC.green(options.revision)}`); 85 | return true; 86 | } 87 | Util.output(EC.yellow('Not found local chromium')); 88 | return false; 89 | }; 90 | -------------------------------------------------------------------------------- /lib/download.js: -------------------------------------------------------------------------------- 1 | const EC = require('eight-colors'); 2 | const Util = require('./util.js'); 3 | 4 | const BrowserPlatform = { 5 | LINUX: 'linux', 6 | MAC: 'mac', 7 | MAC_ARM: 'mac_arm', 8 | WIN32: 'win32', 9 | WIN64: 'win64' 10 | }; 11 | 12 | function folder(platform) { 13 | switch (platform) { 14 | case BrowserPlatform.LINUX: 15 | return 'Linux_x64'; 16 | case BrowserPlatform.MAC_ARM: 17 | return 'Mac_Arm'; 18 | case BrowserPlatform.MAC: 19 | return 'Mac'; 20 | case BrowserPlatform.WIN32: 21 | return 'Win'; 22 | case BrowserPlatform.WIN64: 23 | return 'Win_x64'; 24 | default: 25 | return ''; 26 | } 27 | } 28 | 29 | function archive(platform, buildId) { 30 | switch (platform) { 31 | case BrowserPlatform.LINUX: 32 | return 'chrome-linux'; 33 | case BrowserPlatform.MAC_ARM: 34 | case BrowserPlatform.MAC: 35 | return 'chrome-mac'; 36 | case BrowserPlatform.WIN32: 37 | case BrowserPlatform.WIN64: 38 | // Windows archive name changed at r591479. 39 | return parseInt(buildId, 10) > 591479 ? 'chrome-win' : 'chrome-win32'; 40 | default: 41 | return ''; 42 | } 43 | } 44 | 45 | const resolveDownloadPath = (platform, buildId) => { 46 | return [folder(platform), buildId, `${archive(platform, buildId)}.zip`]; 47 | }; 48 | 49 | // baseUrl 'https://storage.googleapis.com/chromium-browser-snapshots' 50 | const resolveDownloadUrl = (platform, buildId, baseUrl) => { 51 | return `${baseUrl}/${resolveDownloadPath(platform, buildId).join('/')}`; 52 | }; 53 | 54 | const getBaseUrl = (host) => { 55 | return `${host}/chromium-browser-snapshots`; 56 | }; 57 | 58 | const downloadFromHost = async (options, host) => { 59 | Util.output(`Downloading from: ${host} ...`); 60 | Util.createGauge(); 61 | 62 | let failed = false; 63 | const installedBrowser = await Util.install({ 64 | baseUrl: getBaseUrl(host), 65 | buildId: options.revision, 66 | cacheDir: options.snapshotsDir, 67 | downloadProgressCallback: (downloadedBytes, totalBytes) => { 68 | Util.showProgress(downloadedBytes, totalBytes); 69 | } 70 | }).catch((error) => { 71 | failed = true; 72 | Util.output(error, true); 73 | }); 74 | 75 | await Util.delay(500); 76 | 77 | Util.closeGauge(); 78 | 79 | if (failed || !installedBrowser) { 80 | return; 81 | } 82 | 83 | Util.output(`Chromium downloaded: ${installedBrowser.buildId}`); 84 | 85 | return installedBrowser; 86 | }; 87 | 88 | // ========================================================================================= 89 | 90 | const downloadStart = async (options, list) => { 91 | 92 | for (const item of list) { 93 | const installedBrowser = await downloadFromHost(options, item.host); 94 | if (installedBrowser) { 95 | return installedBrowser; 96 | } 97 | } 98 | 99 | if (options.retryNum < options.retry) { 100 | options.retryNum += 1; 101 | Util.output('Retry Chromium downloading ... '); 102 | return downloadStart(options, list); 103 | } 104 | }; 105 | 106 | // ========================================================================================= 107 | 108 | const preDownloadFromHost = async (options, host) => { 109 | const time_start = Date.now(); 110 | 111 | const { platform, revision } = options; 112 | const baseUrl = getBaseUrl(host); 113 | 114 | const downloadUrl = resolveDownloadUrl(platform, revision, baseUrl); 115 | const ok = await Util.headRequest(downloadUrl); 116 | const time = Date.now() - time_start; 117 | 118 | const available = ok ? EC.green(ok) : EC.red(ok); 119 | Util.output(`Head request: ${downloadUrl} - ${available} (${time}ms)`); 120 | 121 | return { 122 | host, 123 | time, 124 | ok, 125 | downloadUrl 126 | }; 127 | }; 128 | 129 | const preDownloadStart = async (options) => { 130 | const list = await Promise.all(options.hosts.map((host) => preDownloadFromHost(options, host))); 131 | // console.log(list); 132 | 133 | // sort list 134 | list.sort((a, b) => { 135 | // ok first 136 | if (a.ok && !b.ok) { 137 | return -1; 138 | } 139 | if (!a.ok && b.ok) { 140 | return 1; 141 | } 142 | // time small first 143 | return a.time - b.time; 144 | }); 145 | // console.log(list); 146 | 147 | return list; 148 | }; 149 | 150 | // ========================================================================================= 151 | 152 | module.exports = async (options) => { 153 | 154 | const list = await preDownloadStart(options); 155 | 156 | options.retryNum = 0; 157 | const installedBrowser = await downloadStart(options, list); 158 | if (installedBrowser) { 159 | options.installedBrowser = installedBrowser; 160 | options.executablePath = Util.computeExecutablePath({ 161 | platform: installedBrowser.platform, 162 | buildId: installedBrowser.buildId, 163 | cacheDir: options.snapshotsDir 164 | }); 165 | } else { 166 | Util.output(`ERROR: Failed to download Chromium after retry ${options.retryNum} times.`, true); 167 | } 168 | 169 | }; 170 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const fs = require('fs'); 3 | const os = require('os'); 4 | const EC = require('eight-colors'); 5 | const puppeteer = require('puppeteer-core'); 6 | 7 | const Util = require('./util.js'); 8 | const defaultOptions = require('./options.js'); 9 | const detectHandler = require('./detect.js'); 10 | const downloadHandler = require('./download.js'); 11 | 12 | // ========================================================================================= 13 | 14 | const getHosts = (options) => { 15 | let hosts = options.hosts; 16 | if (!Util.isList(hosts)) { 17 | // default hosts 18 | const defaultHosts = process.env.PUPPETEER_DEFAULT_HOST || 'https://storage.googleapis.com'; 19 | hosts = [ defaultHosts ]; 20 | } 21 | return hosts; 22 | }; 23 | 24 | const getCacheDir = (options) => { 25 | const downloadPath = options.downloadPath; 26 | if (!downloadPath) { 27 | return os.homedir(); 28 | } 29 | const cacheDir = path.resolve(downloadPath); 30 | if (fs.existsSync(cacheDir)) { 31 | return cacheDir; 32 | } 33 | try { 34 | fs.mkdirSync(cacheDir, { 35 | recursive: true 36 | }); 37 | } catch (e) { 38 | // console.error(e); 39 | Util.output(`Failed to create download dir: ${Util.formatPath(cacheDir)}`, true); 40 | } 41 | return cacheDir; 42 | }; 43 | 44 | const getSnapshotsDir = (options) => { 45 | const folderName = options.folderName || '.chromium-browser-snapshots'; 46 | const snapshotsDir = path.resolve(options.cacheDir, folderName); 47 | if (fs.existsSync(snapshotsDir)) { 48 | return snapshotsDir; 49 | } 50 | try { 51 | fs.mkdirSync(snapshotsDir, { 52 | recursive: true 53 | }); 54 | } catch (e) { 55 | // console.error(e); 56 | Util.output(`Failed to create download dir: ${Util.formatPath(snapshotsDir)}`, true); 57 | } 58 | return snapshotsDir; 59 | }; 60 | 61 | // ========================================================================================= 62 | 63 | const getOptionsFromPackage = () => { 64 | let config; 65 | try { 66 | config = require(path.resolve('package.json')); 67 | } catch (e) { 68 | // empty 69 | } 70 | if (!config || !config.pcr) { 71 | return; 72 | } 73 | // console.log(config.pcr); 74 | return config.pcr; 75 | }; 76 | 77 | const getOptions = (options = {}) => { 78 | const optionsFromPackage = getOptionsFromPackage(); 79 | options = Object.assign(defaultOptions, optionsFromPackage, options); 80 | 81 | options.hosts = getHosts(options); 82 | 83 | // init cache dir, for snapshots and stats file 84 | options.cacheDir = getCacheDir(options); 85 | // for snapshots 86 | options.snapshotsDir = getSnapshotsDir(options); 87 | 88 | return options; 89 | }; 90 | 91 | // ========================================================================================= 92 | 93 | const launchHandler = async (options) => { 94 | options.launchable = false; 95 | if (!options.executablePath) { 96 | return; 97 | } 98 | const browser = await puppeteer.launch({ 99 | headless: 'new', 100 | // fix root issue 101 | args: ['--no-sandbox'], 102 | executablePath: options.executablePath 103 | }).catch((error) => { 104 | // output(error, true); 105 | console.log(error); 106 | }); 107 | if (browser) { 108 | options.launchable = true; 109 | options.chromiumVersion = await browser.version(); 110 | // delay close, fix unknown log in console 111 | await browser.close(); 112 | await Util.delay(500); 113 | } 114 | }; 115 | 116 | // ========================================================================================= 117 | const getPuppeteerVersion = () => { 118 | const version = Util.getPuppeteerVersion(); 119 | if (!version) { 120 | Util.output('Failed to get puppeteer version', true); 121 | return; 122 | } 123 | Util.output(`Puppeteer version: ${EC.magenta(version)}`); 124 | return version; 125 | }; 126 | 127 | const getBrowserPlatform = () => { 128 | const platform = Util.detectBrowserPlatform(); 129 | Util.output(`Browser platform: ${EC.magenta(platform)}`); 130 | return platform; 131 | }; 132 | 133 | const getChromiumRevision = async (options) => { 134 | let revision = options.revision || process.env.PUPPETEER_REVISION; 135 | if (!revision) { 136 | revision = await Util.resolveBuildId(options.platform); 137 | } 138 | Util.output(`Chromium revision: ${EC.magenta(revision)}`); 139 | return revision; 140 | }; 141 | 142 | const statsInfoHandler = (options) => { 143 | 144 | const statsInfo = { 145 | puppeteerVersion: options.puppeteerVersion, 146 | platform: options.platform, 147 | revision: options.revision 148 | }; 149 | 150 | // cache snapshots and stats file 151 | statsInfo.cacheDir = Util.formatPath(options.cacheDir); 152 | 153 | // cache snapshots 154 | statsInfo.snapshotsDir = Util.formatPath(options.snapshotsDir); 155 | 156 | // Chromium executablePath 157 | let executablePath = options.executablePath; 158 | if (executablePath) { 159 | executablePath = Util.formatPath(executablePath); 160 | statsInfo.executablePath = executablePath; 161 | executablePath = fs.existsSync(executablePath) ? EC.green(executablePath) : EC.red(executablePath); 162 | Util.output(`Chromium executablePath: ${executablePath}`); 163 | } 164 | 165 | const chromiumVersion = options.chromiumVersion; 166 | if (chromiumVersion) { 167 | statsInfo.chromiumVersion = chromiumVersion; 168 | Util.output(`Chromium version: ${EC.magenta(chromiumVersion)}`); 169 | } 170 | 171 | if (typeof options.launchable === 'boolean') { 172 | statsInfo.launchable = options.launchable; 173 | const launchable = statsInfo.launchable ? EC.green('true') : EC.red('false'); 174 | Util.output(`Chromium launchable: ${launchable}`); 175 | } 176 | 177 | // save new stats 178 | saveStats(options, statsInfo); 179 | 180 | // re-exports 181 | statsInfo.puppeteer = puppeteer; 182 | 183 | return statsInfo; 184 | }; 185 | 186 | // ========================================================================================= 187 | 188 | const getStatsPath = (options = {}) => { 189 | const statsDir = options.cacheDir || os.homedir(); 190 | const statsName = options.statsName || '.pcr-stats.json'; 191 | const statsPath = path.resolve(statsDir, statsName); 192 | return statsPath; 193 | }; 194 | 195 | const saveStats = (options, statsInfo) => { 196 | const statsPath = getStatsPath(options); 197 | const stats = { 198 | ... statsInfo 199 | }; 200 | try { 201 | fs.writeFileSync(statsPath, JSON.stringify(stats, null, 4)); 202 | } catch (e) { 203 | Util.output(`Failed to save stats: ${Util.formatPath(statsPath)}`, true); 204 | return; 205 | } 206 | Util.output(`Stats saved: ${Util.formatPath(statsPath)}`); 207 | }; 208 | 209 | const getStats = (options) => { 210 | const statsPath = getStatsPath(options); 211 | let stats; 212 | try { 213 | stats = JSON.parse(fs.readFileSync(statsPath)); 214 | } catch (e) { 215 | // console.error(e); 216 | Util.output('Not found PCR stats cache, try npm install again.', true); 217 | } 218 | if (stats) { 219 | stats.puppeteer = puppeteer; 220 | } 221 | return stats; 222 | }; 223 | 224 | // ========================================================================================= 225 | 226 | const PCR = async (options) => { 227 | 228 | options = getOptions(options); 229 | 230 | // from stats cache 231 | Util.silent = true; 232 | const stats = getStats(options); 233 | if (stats && fs.existsSync(stats.executablePath)) { 234 | // if has custom revision should be matched 235 | if (!options.revision || (options.revision && options.revision === stats.revision)) { 236 | return stats; 237 | } 238 | } 239 | 240 | // try to detect and install 241 | 242 | Util.silent = options.silent; 243 | 244 | options.puppeteerVersion = getPuppeteerVersion(); 245 | 246 | options.platform = getBrowserPlatform(); 247 | 248 | // chromium revision to use 249 | options.revision = await getChromiumRevision(options); 250 | 251 | const localChromium = detectHandler(options); 252 | if (!localChromium) { 253 | await downloadHandler(options); 254 | await launchHandler(options); 255 | } 256 | 257 | // force to close gauge 258 | Util.closeGauge(); 259 | 260 | const statsInfo = await statsInfoHandler(options); 261 | // console.log(statsInfo); 262 | 263 | return statsInfo; 264 | }; 265 | 266 | // ========================================================================================= 267 | 268 | // additional API 269 | PCR.getOptions = getOptions; 270 | PCR.getStats = (options) => { 271 | return getStats(getOptions(options)); 272 | }; 273 | 274 | module.exports = PCR; 275 | -------------------------------------------------------------------------------- /lib/install.js: -------------------------------------------------------------------------------- 1 | const EC = require('eight-colors'); 2 | const PCR = require('./index.js'); 3 | const { PUPPETEER_SKIP_DOWNLOAD } = process.env; 4 | // skip download when install 5 | if (PUPPETEER_SKIP_DOWNLOAD) { 6 | EC.logYellow('[PCR] PUPPETEER_SKIP_DOWNLOAD'); 7 | } else { 8 | PCR(); 9 | } 10 | -------------------------------------------------------------------------------- /lib/options.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // the chromium revision to use 3 | // default is puppeteer.PUPPETEER_REVISIONS.chromium 4 | revision: '', 5 | 6 | // additional path to detect local chromium copy (separate with a comma if multiple paths) 7 | detectionPath: '', 8 | 9 | // custom path to download chromium to local, require dir permission: 0o777 10 | // default is user home dir 11 | downloadPath: '', 12 | 13 | // the folder name for chromium snapshots (maybe there are multiple versions) 14 | folderName: '.chromium-browser-snapshots', 15 | 16 | // the stats file name, cache stats info for latest installation 17 | statsName: '.pcr-stats.json', 18 | 19 | // default hosts are ['https://storage.googleapis.com'] 20 | hosts: [], 21 | 22 | cacheRevisions: 2, 23 | retry: 3, 24 | silent: false 25 | }; 26 | -------------------------------------------------------------------------------- /lib/util.js: -------------------------------------------------------------------------------- 1 | const http = require('http'); 2 | const https = require('https'); 3 | const URL = require('url'); 4 | const EC = require('eight-colors'); 5 | const PB = require('@puppeteer/browsers'); 6 | const Gauge = require('gauge'); 7 | 8 | const Util = { 9 | 10 | detectBrowserPlatform: () => { 11 | return PB.detectBrowserPlatform(); 12 | }, 13 | 14 | resolveBuildId: async (platform, tag = 'latest') => { 15 | const buildId = await PB.resolveBuildId(PB.Browser.CHROMIUM, platform, tag).catch((e) => { 16 | // console.log(e); 17 | }); 18 | // console.log('resolveBuildId', buildId); 19 | return buildId || '1337728'; 20 | }, 21 | 22 | install: (options) => { 23 | return PB.install({ 24 | browser: PB.Browser.CHROMIUM, 25 | ... options 26 | }); 27 | }, 28 | 29 | computeExecutablePath: (options) => { 30 | return PB.computeExecutablePath({ 31 | browser: PB.Browser.CHROMIUM, 32 | ... options 33 | }); 34 | }, 35 | 36 | getPuppeteerVersion: () => { 37 | let config; 38 | try { 39 | config = require('puppeteer-core/package.json'); 40 | } catch (e) { 41 | // empty 42 | return; 43 | } 44 | if (config) { 45 | return config.version; 46 | } 47 | }, 48 | 49 | headRequest: (url) => { 50 | return new Promise((resolve) => { 51 | 52 | const urlParsed = URL.parse(url); 53 | 54 | // console.log(urlParsed); 55 | 56 | let isHttps = false; 57 | if (urlParsed.protocol === 'https:') { 58 | isHttps = true; 59 | } 60 | 61 | const options = { 62 | ... urlParsed, 63 | method: 'HEAD' 64 | }; 65 | const request = isHttps ? https.request(options) : http.request(options); 66 | request.setTimeout(3000); 67 | request.end(); 68 | 69 | const onFinish = (value) => { 70 | request.destroy(); 71 | resolve(value); 72 | }; 73 | 74 | request.on('error', (error) => { 75 | // console.error(error); 76 | onFinish(false); 77 | }); 78 | 79 | request.on('timeout', (error) => { 80 | onFinish(false); 81 | }); 82 | 83 | request.on('close', (error) => { 84 | onFinish(false); 85 | }); 86 | 87 | request.on('response', (res) => { 88 | 89 | const { statusCode, headers } = res; 90 | if (statusCode && statusCode >= 300 && statusCode < 400 && headers.location) { 91 | // console.log(`redirection: ${headers.location}`); 92 | onFinish(Util.headRequest(headers.location)); 93 | return; 94 | } 95 | 96 | onFinish(statusCode === 200); 97 | 98 | }); 99 | 100 | }); 101 | }, 102 | 103 | // ===================================================================================== 104 | 105 | silent: false, 106 | output: (msg, isError) => { 107 | if (!Util.silent) { 108 | if (isError) { 109 | console.log(EC.red(`[PCR] ${msg}`)); 110 | } else { 111 | console.log(`[PCR] ${msg}`); 112 | } 113 | } 114 | }, 115 | 116 | delay: function(ms) { 117 | return new Promise((resolve) => { 118 | if (ms) { 119 | setTimeout(resolve, ms); 120 | } else { 121 | setImmediate(resolve); 122 | } 123 | }); 124 | }, 125 | 126 | createGauge: () => { 127 | Util.closeGauge(); 128 | Util.gauge = new Gauge(); 129 | }, 130 | 131 | closeGauge: () => { 132 | if (!Util.gauge) { 133 | return; 134 | } 135 | Util.gauge.disable(); 136 | Util.gauge.hide(); 137 | Util.gauge = null; 138 | }, 139 | 140 | showProgress: (downloadedBytes, totalBytes) => { 141 | let per = 0; 142 | if (totalBytes) { 143 | per = downloadedBytes / totalBytes; 144 | } 145 | if (Util.gauge) { 146 | Util.gauge.show(`Downloading Chromium - ${Util.toMegabytes(downloadedBytes)} / ${Util.toMegabytes(totalBytes)}`, per); 147 | } 148 | }, 149 | 150 | toMegabytes: (bytes) => { 151 | const mb = bytes / 1024 / 1024; 152 | return `${Math.round(mb * 10) / 10} Mb`; 153 | }, 154 | 155 | // \ to / 156 | formatPath: (str) => { 157 | if (str) { 158 | str = str.replace(/\\/g, '/'); 159 | } 160 | return str; 161 | }, 162 | 163 | isList: (data) => { 164 | if (data && data instanceof Array && data.length > 0) { 165 | return true; 166 | } 167 | return false; 168 | } 169 | 170 | }; 171 | 172 | module.exports = Util; 173 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "puppeteer-chromium-resolver", 3 | "version": "24.0.1", 4 | "description": "Tool to resolve puppeteer and chromium faster, detect local installed chromium, download chromium with custom mirror host, cache chromium revision out of node_modules, test chromium headless being launchable.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "install": "node ./lib/install.js", 8 | "test": "npx mocha", 9 | "docker-build": "docker build --progress=plain -t pcr ." 10 | }, 11 | "files": [ 12 | "lib" 13 | ], 14 | "license": "MIT", 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/cenfun/puppeteer-chromium-resolver.git" 18 | }, 19 | "dependencies": { 20 | "@puppeteer/browsers": "^2.8.0", 21 | "eight-colors": "^1.3.1", 22 | "gauge": "^5.0.2", 23 | "puppeteer-core": "^24.4.0" 24 | }, 25 | "devDependencies": { 26 | "eslint": "^9.23.0", 27 | "eslint-config-plus": "^2.0.2", 28 | "mocha": "^11.1.0", 29 | "rimraf": "^6.0.1" 30 | } 31 | } -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const EC = require('eight-colors'); 4 | const rimraf = require('rimraf'); 5 | const assert = require('assert'); 6 | 7 | const PCR = require(path.resolve(__dirname, '../lib/index.js')); 8 | 9 | // utils 10 | const cleanSnapshotsDir = (options) => { 11 | const snapshotsDir = options.snapshotsDir; 12 | if (fs.existsSync(snapshotsDir)) { 13 | console.log(`${EC.cyan('[clean]')} remove chromium snapshots dir: ${EC.magenta(snapshotsDir)} ...`); 14 | rimraf.nativeSync(snapshotsDir); 15 | } 16 | }; 17 | 18 | const cleanStatsFile = (options) => { 19 | const statsPath = path.resolve(options.cacheDir, options.statsName); 20 | if (fs.existsSync(statsPath)) { 21 | console.log(`${EC.cyan('[clean]')} remove stats cache: ${EC.magenta(statsPath)} ...`); 22 | rimraf.nativeSync(statsPath); 23 | } 24 | }; 25 | 26 | const cleanAll = (options) => { 27 | cleanSnapshotsDir(options); 28 | cleanStatsFile(options); 29 | }; 30 | 31 | describe('puppeteer-chromium-resolver', function() { 32 | this.timeout(5 * 60 * 1000); 33 | 34 | it('reinstall with default options', async () => { 35 | 36 | const mergedOptions = PCR.getOptions(); 37 | cleanAll(mergedOptions); 38 | 39 | const options = {}; 40 | const stats = await PCR(options); 41 | assert(fs.existsSync(stats.executablePath)); 42 | }); 43 | 44 | 45 | it('async PCR with default options', async () => { 46 | const options = {}; 47 | const stats = await PCR(options); 48 | assert(fs.existsSync(stats.executablePath)); 49 | }); 50 | 51 | it('sync getStats', () => { 52 | const stats = PCR.getStats(); 53 | assert(fs.existsSync(stats.executablePath)); 54 | }); 55 | 56 | it('async PCR without stats cache', async () => { 57 | const mergedOptions = PCR.getOptions(); 58 | cleanStatsFile(mergedOptions); 59 | 60 | const options = {}; 61 | const stats = await PCR(options); 62 | assert(fs.existsSync(stats.executablePath)); 63 | }); 64 | 65 | it('async PCR with revision: 1337728', async () => { 66 | const options = { 67 | revision: '1337728' 68 | }; 69 | const stats = await PCR(options); 70 | assert(fs.existsSync(stats.executablePath)); 71 | }); 72 | 73 | it('sync getStats with revision: 1337728', () => { 74 | const options = { 75 | revision: '1337728' 76 | }; 77 | const stats = PCR.getStats(options); 78 | assert(fs.existsSync(stats.executablePath)); 79 | }); 80 | 81 | it('async PCR with downloadPath: .temp', async () => { 82 | const options = { 83 | downloadPath: '.temp' 84 | }; 85 | 86 | const mergedOptions = PCR.getOptions(options); 87 | cleanAll(mergedOptions); 88 | 89 | const stats = await PCR(options); 90 | assert(fs.existsSync(stats.executablePath)); 91 | }); 92 | 93 | it('sync getStats with downloadPath: .temp', () => { 94 | const options = { 95 | downloadPath: '.temp' 96 | }; 97 | const stats = PCR.getStats(options); 98 | assert(fs.existsSync(stats.executablePath)); 99 | }); 100 | 101 | it('async PCR with detectionPath: .temp', async () => { 102 | const options = { 103 | detectionPath: '.temp' 104 | }; 105 | const stats = await PCR(options); 106 | assert(fs.existsSync(stats.executablePath)); 107 | }); 108 | 109 | it('launch browser and open page', async () => { 110 | 111 | const stats = await PCR(); 112 | console.log('puppeteerVersion', stats.puppeteerVersion); 113 | console.log('executablePath', stats.executablePath); 114 | 115 | const browser = await stats.puppeteer.launch({ 116 | // headless: 'new', 117 | // headless: false, 118 | args: ['--no-sandbox'], 119 | executablePath: stats.executablePath 120 | }).catch(function(err) { 121 | console.error(err); 122 | }); 123 | console.log('browser.newPage ...'); 124 | const page = await browser.newPage(); 125 | console.log('page.setContent ...'); 126 | await page.setContent('