puppeteer-chromium-resolver
Advanced tools
Comparing version 19.3.2 to 20.0.0
@@ -0,3 +1,4 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const EC = require('eight-colors'); | ||
const path = require('path'); | ||
const Util = require('./util.js'); | ||
@@ -47,7 +48,16 @@ | ||
detectionPath = path.resolve(detectionPath); | ||
const browserFetcher = Util.createBrowserFetcher({ | ||
path: detectionPath | ||
const revision = options.revision; | ||
const executablePath = Util.computeExecutablePath({ | ||
buildId: revision, | ||
cacheDir: detectionPath | ||
}); | ||
const revisionInfo = browserFetcher.revisionInfo(options.revision); | ||
return revisionInfo; | ||
if (!executablePath || !fs.existsSync(executablePath)) { | ||
return; | ||
} | ||
return executablePath; | ||
}; | ||
@@ -61,5 +71,5 @@ | ||
for (const detectionPath of detectionList) { | ||
const revisionInfo = detectionPathHandler(options, detectionPath); | ||
if (revisionInfo.local) { | ||
return revisionInfo; | ||
const executablePath = detectionPathHandler(options, detectionPath); | ||
if (executablePath) { | ||
return executablePath; | ||
} | ||
@@ -73,6 +83,6 @@ } | ||
module.exports = (options) => { | ||
const revisionInfo = detectionHandler(options); | ||
if (revisionInfo) { | ||
options.revisionInfo = revisionInfo; | ||
Util.output(`Found local chromium: ${EC.green(revisionInfo.revision)}`); | ||
const executablePath = detectionHandler(options); | ||
if (executablePath) { | ||
options.executablePath = executablePath; | ||
Util.output(`Found local chromium: ${EC.green(options.revision)}`); | ||
return true; | ||
@@ -79,0 +89,0 @@ } |
const EC = require('eight-colors'); | ||
const Util = require('./util.js'); | ||
const cleanLocalRevisions = async (options, browserFetcher) => { | ||
let localRevisions = await browserFetcher.localRevisions(); | ||
if (!Util.isList(localRevisions)) { | ||
return; | ||
const BrowserPlatform = { | ||
LINUX: 'linux', | ||
MAC: 'mac', | ||
MAC_ARM: 'mac_arm', | ||
WIN32: 'win32', | ||
WIN64: 'win64' | ||
}; | ||
function folder(platform) { | ||
switch (platform) { | ||
case BrowserPlatform.LINUX: | ||
return 'Linux_x64'; | ||
case BrowserPlatform.MAC_ARM: | ||
return 'Mac_Arm'; | ||
case BrowserPlatform.MAC: | ||
return 'Mac'; | ||
case BrowserPlatform.WIN32: | ||
return 'Win'; | ||
case BrowserPlatform.WIN64: | ||
return 'Win_x64'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
localRevisions = localRevisions.filter((revision) => revision !== options.revision); | ||
if (localRevisions.length > options.cacheRevisions) { | ||
localRevisions.sort(); | ||
localRevisions.length -= options.cacheRevisions; | ||
Util.output(`Removing previous useless revisions ${localRevisions.join(', ')}`); | ||
const cleanupOldVersions = localRevisions.map((revision) => browserFetcher.remove(revision)); | ||
await Promise.all(cleanupOldVersions); | ||
function archive(platform, buildId) { | ||
switch (platform) { | ||
case BrowserPlatform.LINUX: | ||
return 'chrome-linux'; | ||
case BrowserPlatform.MAC_ARM: | ||
case BrowserPlatform.MAC: | ||
return 'chrome-mac'; | ||
case BrowserPlatform.WIN32: | ||
case BrowserPlatform.WIN64: | ||
// Windows archive name changed at r591479. | ||
return parseInt(buildId, 10) > 591479 ? 'chrome-win' : 'chrome-win32'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
const resolveDownloadPath = (platform, buildId) => { | ||
return [folder(platform), buildId, `${archive(platform, buildId)}.zip`]; | ||
}; | ||
// baseUrl 'https://storage.googleapis.com/chromium-browser-snapshots' | ||
const resolveDownloadUrl = (platform, buildId, baseUrl) => { | ||
return `${baseUrl}/${resolveDownloadPath(platform, buildId).join('/')}`; | ||
}; | ||
const getBaseUrl = (host) => { | ||
return `${host}/chromium-browser-snapshots`; | ||
}; | ||
const downloadFromHost = async (options, host) => { | ||
Util.output(`Downloading from: ${host} ...`); | ||
const browserFetcher = Util.createBrowserFetcher({ | ||
host: host, | ||
path: options.snapshotsDir | ||
}); | ||
Util.createGauge(); | ||
let failed = false; | ||
const revisionInfo = await browserFetcher.download(options.revision, (downloadedBytes, totalBytes) => { | ||
Util.showProgress(downloadedBytes, totalBytes); | ||
const installedBrowser = await Util.install({ | ||
baseUrl: getBaseUrl(host), | ||
buildId: options.revision, | ||
cacheDir: options.snapshotsDir, | ||
downloadProgressCallback: (downloadedBytes, totalBytes) => { | ||
Util.showProgress(downloadedBytes, totalBytes); | ||
} | ||
}).catch((error) => { | ||
@@ -38,14 +75,13 @@ failed = true; | ||
await Util.delay(500); | ||
Util.closeGauge(); | ||
if (failed || !revisionInfo) { | ||
if (failed || !installedBrowser) { | ||
return; | ||
} | ||
Util.output(`Chromium downloaded: ${revisionInfo.folderPath}`); | ||
Util.output(`Chromium downloaded: ${installedBrowser.buildId}`); | ||
await cleanLocalRevisions(options, browserFetcher); | ||
return revisionInfo; | ||
return installedBrowser; | ||
}; | ||
@@ -58,5 +94,5 @@ | ||
for (const item of list) { | ||
const revisionInfo = await downloadFromHost(options, item.host); | ||
if (revisionInfo) { | ||
return revisionInfo; | ||
const installedBrowser = await downloadFromHost(options, item.host); | ||
if (installedBrowser) { | ||
return installedBrowser; | ||
} | ||
@@ -75,21 +111,13 @@ } | ||
const preDownloadFromHost = async (options, host) => { | ||
const browserFetcher = Util.createBrowserFetcher({ | ||
host: host, | ||
path: options.snapshotsDir | ||
}); | ||
const info = browserFetcher.revisionInfo(options.revision); | ||
const url = info.url; | ||
const time_start = Date.now(); | ||
const ok = await Util.headRequest(url); | ||
const { platform, revision } = options; | ||
const baseUrl = getBaseUrl(host); | ||
// canDownload has issue for now | ||
// const ok = await browserFetcher.canDownload(options.revision); | ||
const downloadUrl = resolveDownloadUrl(platform, revision, baseUrl); | ||
const ok = await Util.headRequest(downloadUrl); | ||
const time = Date.now() - time_start; | ||
const available = ok ? EC.green(ok) : EC.red(ok); | ||
Util.output(`HEAD request: ${url} - ${available} (${time}ms)`); | ||
Util.output(`Head request: ${downloadUrl} - ${available} (${time}ms)`); | ||
@@ -99,3 +127,4 @@ return { | ||
time, | ||
ok | ||
ok, | ||
downloadUrl | ||
}; | ||
@@ -105,7 +134,4 @@ }; | ||
const preDownloadStart = async (options) => { | ||
const list = []; | ||
for (const host of options.hosts) { | ||
const info = await preDownloadFromHost(options, host); | ||
list.push(info); | ||
} | ||
const list = await Promise.all(options.hosts.map((host) => preDownloadFromHost(options, host))); | ||
// console.log(list); | ||
@@ -136,6 +162,10 @@ // sort list | ||
options.retryNum = 0; | ||
const revisionInfo = await downloadStart(options, list); | ||
// console.log(revisionInfo); | ||
if (revisionInfo) { | ||
options.revisionInfo = revisionInfo; | ||
const installedBrowser = await downloadStart(options, list); | ||
if (installedBrowser) { | ||
options.installedBrowser = installedBrowser; | ||
options.executablePath = Util.computeExecutablePath({ | ||
platform: installedBrowser.platform, | ||
buildId: installedBrowser.buildId, | ||
cacheDir: options.snapshotsDir | ||
}); | ||
} else { | ||
@@ -142,0 +172,0 @@ Util.output(`ERROR: Failed to download Chromium after retry ${options.retryNum} times.`, true); |
@@ -84,2 +84,3 @@ const path = require('path'); | ||
options.cacheDir = getCacheDir(options); | ||
// for snapshots | ||
options.snapshotsDir = getSnapshotsDir(options); | ||
@@ -94,6 +95,10 @@ | ||
options.launchable = false; | ||
if (!options.executablePath) { | ||
return; | ||
} | ||
const browser = await puppeteer.launch({ | ||
headless: 'new', | ||
// fix root issue | ||
args: ['--no-sandbox'], | ||
executablePath: options.revisionInfo.executablePath | ||
executablePath: options.executablePath | ||
}).catch((error) => { | ||
@@ -123,4 +128,13 @@ // output(error, true); | ||
const getChromiumRevision = (options) => { | ||
const revision = options.revision || Util.getPuppeteerChromiumRevision(); | ||
const getBrowserPlatform = () => { | ||
const platform = Util.detectBrowserPlatform(); | ||
Util.output(`Browser platform: ${EC.magenta(platform)}`); | ||
return platform; | ||
}; | ||
const getChromiumRevision = async (options) => { | ||
let revision = options.revision; | ||
if (!revision) { | ||
revision = await Util.resolveBuildId(options.platform); | ||
} | ||
Util.output(`Chromium revision: ${EC.magenta(revision)}`); | ||
@@ -130,15 +144,21 @@ return revision; | ||
const revisionHandler = (options) => { | ||
const statsInfoHandler = (options) => { | ||
const revisionInfo = options.revisionInfo; | ||
if (!revisionInfo) { | ||
return; | ||
} | ||
const statsInfo = { | ||
puppeteerVersion: options.puppeteerVersion, | ||
platform: options.platform, | ||
revision: options.revision | ||
}; | ||
revisionInfo.puppeteerVersion = options.puppeteerVersion; | ||
// cache snapshots and stats file | ||
statsInfo.cacheDir = Util.formatPath(options.cacheDir); | ||
// Chromium | ||
revisionInfo.executablePath = Util.formatPath(revisionInfo.executablePath); | ||
let executablePath = revisionInfo.executablePath; | ||
// cache snapshots | ||
statsInfo.snapshotsDir = Util.formatPath(options.snapshotsDir); | ||
// Chromium executablePath | ||
let executablePath = options.executablePath; | ||
if (executablePath) { | ||
executablePath = Util.formatPath(executablePath); | ||
statsInfo.executablePath = executablePath; | ||
executablePath = fs.existsSync(executablePath) ? EC.green(executablePath) : EC.red(executablePath); | ||
@@ -148,14 +168,11 @@ Util.output(`Chromium executablePath: ${executablePath}`); | ||
revisionInfo.folderPath = Util.formatPath(revisionInfo.folderPath); | ||
revisionInfo.snapshotsDir = Util.formatPath(options.snapshotsDir); | ||
revisionInfo.cacheDir = Util.formatPath(options.cacheDir); | ||
revisionInfo.chromiumVersion = options.chromiumVersion; | ||
if (revisionInfo.chromiumVersion) { | ||
Util.output(`Chromium version: ${EC.magenta(revisionInfo.chromiumVersion)}`); | ||
const chromiumVersion = options.chromiumVersion; | ||
if (chromiumVersion) { | ||
statsInfo.chromiumVersion = chromiumVersion; | ||
Util.output(`Chromium version: ${EC.magenta(chromiumVersion)}`); | ||
} | ||
if (typeof options.launchable === 'boolean') { | ||
revisionInfo.launchable = options.launchable; | ||
const launchable = revisionInfo.launchable ? EC.green('true') : EC.red('false'); | ||
statsInfo.launchable = options.launchable; | ||
const launchable = statsInfo.launchable ? EC.green('true') : EC.red('false'); | ||
Util.output(`Chromium launchable: ${launchable}`); | ||
@@ -165,8 +182,8 @@ } | ||
// save new stats | ||
saveStats(options, revisionInfo); | ||
saveStats(options, statsInfo); | ||
// re-exports | ||
revisionInfo.puppeteer = puppeteer; | ||
statsInfo.puppeteer = puppeteer; | ||
return revisionInfo; | ||
return statsInfo; | ||
}; | ||
@@ -183,6 +200,6 @@ | ||
const saveStats = (options, revisionInfo) => { | ||
const saveStats = (options, statsInfo) => { | ||
const statsPath = getStatsPath(options); | ||
const stats = { | ||
... revisionInfo | ||
... statsInfo | ||
}; | ||
@@ -235,4 +252,6 @@ try { | ||
options.platform = getBrowserPlatform(); | ||
// chromium revision to use | ||
options.revision = getChromiumRevision(options); | ||
options.revision = await getChromiumRevision(options); | ||
@@ -248,6 +267,6 @@ const localChromium = detectHandler(options); | ||
const revisionInfo = await revisionHandler(options); | ||
// console.log(revisionInfo); | ||
const statsInfo = await statsInfoHandler(options); | ||
// console.log(statsInfo); | ||
return revisionInfo; | ||
return statsInfo; | ||
}; | ||
@@ -262,4 +281,3 @@ | ||
}; | ||
PCR.createBrowserFetcher = Util.createBrowserFetcher; | ||
module.exports = PCR; |
@@ -5,3 +5,3 @@ const http = require('http'); | ||
const EC = require('eight-colors'); | ||
const puppeteer = require('puppeteer-core'); | ||
const PB = require('@puppeteer/browsers'); | ||
const Gauge = require('gauge'); | ||
@@ -11,14 +11,28 @@ | ||
getPuppeteerChromiumRevision: () => { | ||
const revisions = puppeteer.PUPPETEER_REVISIONS; | ||
if (revisions) { | ||
return revisions.chromium; | ||
} | ||
return '1108766'; | ||
detectBrowserPlatform: () => { | ||
return PB.detectBrowserPlatform(); | ||
}, | ||
createBrowserFetcher: (options) => { | ||
return new puppeteer.BrowserFetcher(options); | ||
resolveBuildId: async (platform, tag = 'latest') => { | ||
const buildId = await PB.resolveBuildId(PB.Browser.CHROMIUM, platform, tag).catch((e) => { | ||
// console.log(e); | ||
}); | ||
// console.log('resolveBuildId', buildId); | ||
return buildId || '1138907'; | ||
}, | ||
install: (options) => { | ||
return PB.install({ | ||
browser: PB.Browser.CHROMIUM, | ||
... options | ||
}); | ||
}, | ||
computeExecutablePath: (options) => { | ||
return PB.computeExecutablePath({ | ||
browser: PB.Browser.CHROMIUM, | ||
... options | ||
}); | ||
}, | ||
getPuppeteerVersion: () => { | ||
@@ -25,0 +39,0 @@ let config; |
{ | ||
"name": "puppeteer-chromium-resolver", | ||
"version": "19.3.2", | ||
"version": "20.0.0", | ||
"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.", | ||
@@ -20,12 +20,13 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@puppeteer/browsers": "^1.0.0", | ||
"eight-colors": "^1.0.3", | ||
"gauge": "^5.0.0", | ||
"puppeteer-core": "^19.8.1" | ||
"gauge": "^5.0.1", | ||
"puppeteer-core": "^20.1.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.37.0", | ||
"eslint": "^8.39.0", | ||
"eslint-config-plus": "^1.0.6", | ||
"mocha": "^10.2.0", | ||
"rimraf": "^4.4.1" | ||
"rimraf": "^5.0.0" | ||
} | ||
} |
@@ -95,3 +95,3 @@ | ||
"pcr": { | ||
"revision": "869685" | ||
"revision": "1138907" | ||
} | ||
@@ -106,5 +106,2 @@ } | ||
|executablePath | String |chromium executable path | | ||
|folderPath | String |chromium folder path | | ||
|local | Boolean |exists local chromium | | ||
|url | String |chromium download url | | ||
|chromiumVersion | String |chromium version | | ||
@@ -177,16 +174,3 @@ |launchable | Boolean |chromium launchable | | ||
## Dependencies | ||
```sh | ||
nmls -p | ||
┌───────────────────────────────┬─────────┬─────────┬──────┬───────────┬────────┐ | ||
│ Name │ Version │ Size │ Deps │ Deps Size │ Nested │ | ||
├───────────────────────────────┼─────────┼─────────┼──────┼───────────┼────────┤ | ||
│ └ puppeteer-chromium-resolver │ 19.3.0 │ 27.4 KB │ 56 │ 12.6 MB │ 0 │ | ||
│ └ dependencies │ │ │ │ │ │ | ||
│ ├ eight-colors │ 1.0.3 │ 14.9 KB │ 0 │ 0 B │ 0 │ | ||
│ ├ gauge │ 5.0.0 │ 42.1 KB │ 11 │ 113.1 KB │ 0 │ | ||
│ └ puppeteer-core │ 19.8.0 │ 3.94 MB │ 42 │ 8.51 MB │ 0 │ | ||
└───────────────────────────────┴─────────┴─────────┴──────┴───────────┴────────┘ | ||
``` | ||
## CHANGELOG | ||
[CHANGELOG.md](CHANGELOG.md) |
28837
606
4
173
4
+ Added@puppeteer/browsers@^1.0.0
+ Added@puppeteer/browsers@1.4.61.9.1(transitive)
+ Added@tootallnate/quickjs-emscripten@0.23.0(transitive)
+ Addedagent-base@7.1.3(transitive)
+ Addedast-types@0.13.4(transitive)
+ Addedb4a@1.6.7(transitive)
+ Addedbare-events@2.5.4(transitive)
+ Addedbasic-ftp@5.0.5(transitive)
+ Addedchromium-bidi@0.4.16(transitive)
+ Addedcross-fetch@4.0.0(transitive)
+ Addeddata-uri-to-buffer@6.0.2(transitive)
+ Addeddegenerator@5.0.1(transitive)
+ Addeddevtools-protocol@0.0.1147663(transitive)
+ Addedescodegen@2.1.0(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedestraverse@5.3.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedfast-fifo@1.3.2(transitive)
+ Addedget-uri@6.0.4(transitive)
+ Addedhttp-proxy-agent@7.0.2(transitive)
+ Addedhttps-proxy-agent@7.0.6(transitive)
+ Addedip-address@9.0.5(transitive)
+ Addedjsbn@1.1.0(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addednetmask@2.0.2(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedpac-proxy-agent@7.1.0(transitive)
+ Addedpac-resolver@7.0.1(transitive)
+ Addedproxy-agent@6.3.06.3.1(transitive)
+ Addedpuppeteer-core@20.9.0(transitive)
+ Addedsmart-buffer@4.2.0(transitive)
+ Addedsocks@2.8.3(transitive)
+ Addedsocks-proxy-agent@8.0.5(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedsprintf-js@1.1.3(transitive)
+ Addedstreamx@2.22.0(transitive)
+ Addedtar-fs@3.0.4(transitive)
+ Addedtar-stream@3.1.7(transitive)
+ Addedtext-decoder@1.2.3(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addedyargs@17.7.2(transitive)
- Removed@puppeteer/browsers@0.5.0(transitive)
- Removedagent-base@6.0.2(transitive)
- Removedbl@4.1.0(transitive)
- Removedchownr@1.1.4(transitive)
- Removedchromium-bidi@0.4.7(transitive)
- Removedcross-fetch@3.1.5(transitive)
- Removeddevtools-protocol@0.0.1107588(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedhttps-proxy-agent@5.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removednode-fetch@2.6.7(transitive)
- Removedpuppeteer-core@19.11.1(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedtar-fs@2.1.1(transitive)
- Removedtar-stream@2.2.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedgauge@^5.0.1
Updatedpuppeteer-core@^20.1.0