puppeteer-chromium-resolver
Advanced tools
Comparing version 8.0.0 to 8.1.0
122
index.js
@@ -22,6 +22,6 @@ const path = require("path"); | ||
Color[name] = (str) => { | ||
return addColor("3" + i, str, "39"); | ||
return addColor(`3${i}`, str, "39"); | ||
}; | ||
Color.bg[name] = (str) => { | ||
return addColor("4" + i, str, "49"); | ||
return addColor(`4${i}`, str, "49"); | ||
}; | ||
@@ -36,5 +36,5 @@ }); | ||
if (isError) { | ||
console.log(Color.red("[PCR] " + msg)); | ||
console.log(Color.red(`[PCR] ${msg}`)); | ||
} else { | ||
console.log("[PCR] " + msg); | ||
console.log(`[PCR] ${msg}`); | ||
} | ||
@@ -76,3 +76,3 @@ } | ||
output("Chromium downloaded to " + option.userFolder); | ||
output(`Chromium downloaded to ${option.userFolder}`); | ||
@@ -86,3 +86,3 @@ let localRevisions = await browserFetcher.localRevisions(); | ||
localRevisions.length -= option.cacheRevisions; | ||
output("Removing useless revisions " + localRevisions.join(", ")); | ||
output(`Removing useless revisions ${localRevisions.join(", ")}`); | ||
const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision)); | ||
@@ -97,4 +97,4 @@ await Promise.all([... cleanupOldVersions]); | ||
const downloadFromHost = async (option) => { | ||
output("Downloading from host: " + option.host + " ..."); | ||
const downloadFromHost = (option) => { | ||
output(`Downloading from host: ${option.host} ...`); | ||
const browserFetcher = puppeteer.createBrowserFetcher({ | ||
@@ -105,3 +105,3 @@ host: option.host, | ||
//download start now | ||
return await downloadNow(option, browserFetcher); | ||
return downloadNow(option, browserFetcher); | ||
}; | ||
@@ -122,3 +122,3 @@ | ||
output("Retry Chromium downloading ... "); | ||
return await downloadStart(option); | ||
return downloadStart(option); | ||
} | ||
@@ -220,3 +220,3 @@ | ||
} | ||
detectionPath = detectionPath + ""; | ||
detectionPath = `${detectionPath}`; | ||
if (detectionPath) { | ||
@@ -301,3 +301,3 @@ return detectionPath.split(","); | ||
} catch (e) { | ||
output("User path is not writable: " + userFolder); | ||
output(`User path is not writable: ${userFolder}`); | ||
output(e); | ||
@@ -334,2 +334,14 @@ } | ||
const getOptionFromPackage = () => { | ||
let config; | ||
try { | ||
config = require(path.resolve("package.json")); | ||
} catch (e) {} | ||
if (!config || !config.pcr) { | ||
return; | ||
} | ||
//console.log(config.pcr); | ||
return config.pcr; | ||
}; | ||
//========================================================================================= | ||
@@ -368,24 +380,28 @@ | ||
const revisionInfo = option.revisionInfo; | ||
//Chromium | ||
revisionInfo.executablePath = formatPath(revisionInfo.executablePath); | ||
let executablePath = revisionInfo.executablePath; | ||
if (executablePath) { | ||
executablePath = fs.existsSync(executablePath) ? Color.green(executablePath) : Color.red(executablePath); | ||
output(`Chromium executablePath: ${executablePath}`); | ||
} | ||
revisionInfo.folderPath = formatPath(revisionInfo.folderPath); | ||
revisionInfo.userFolder = formatPath(option.userFolder); | ||
//Chromium | ||
revisionInfo.launchable = option.launchable; | ||
revisionInfo.chromiumVersion = option.chromiumVersion; | ||
let launchable = Color.red("false"); | ||
if (revisionInfo.launchable) { | ||
launchable = Color.green("true"); | ||
output("Chromium executablePath: " + revisionInfo.executablePath); | ||
if (revisionInfo.chromiumVersion) { | ||
output("Chromium version: " + revisionInfo.chromiumVersion); | ||
} | ||
if (revisionInfo.chromiumVersion) { | ||
output(`Chromium version: ${revisionInfo.chromiumVersion}`); | ||
} | ||
output("Chromium launchable: " + launchable); | ||
if (typeof option.launchable === "boolean") { | ||
revisionInfo.launchable = option.launchable; | ||
const launchable = revisionInfo.launchable ? Color.green("true") : Color.red("false"); | ||
output(`Chromium launchable: ${launchable}`); | ||
} | ||
//Puppeteer | ||
if (option.puppeteerConf) { | ||
revisionInfo.puppeteerVersion = option.puppeteerConf.version; | ||
output("Puppeteer version: " + revisionInfo.puppeteerVersion); | ||
output(`Puppeteer version: ${revisionInfo.puppeteerVersion}`); | ||
} | ||
@@ -397,2 +413,4 @@ revisionInfo.puppeteer = puppeteer; | ||
//========================================================================================= | ||
const statsPath = path.resolve(__dirname, ".stats.json"); | ||
@@ -403,22 +421,7 @@ const saveStats = (revisionInfo) => { | ||
fs.writeFileSync(statsPath, JSON.stringify(stats, null, 4)); | ||
output("Stats saved: " + path.relative(process.cwd(), statsPath)); | ||
output(`Stats saved: ${path.relative(process.cwd(), statsPath)}`); | ||
}; | ||
const getStats = () => { | ||
let stats; | ||
try { | ||
stats = require(statsPath); | ||
} catch (e) { | ||
output("Not found PCR stats, try npm install again.", true); | ||
} | ||
if (stats) { | ||
stats.puppeteer = puppeteer; | ||
} | ||
return stats; | ||
}; | ||
const PCR = async (option = {}) => { | ||
//========================================================================================= | ||
const resolver = async (option = {}) => { | ||
const defaultOption = { | ||
@@ -434,4 +437,7 @@ revision: "", | ||
}; | ||
option = Object.assign(defaultOption, option); | ||
const optionFromPackage = getOptionFromPackage(); | ||
option = Object.assign(defaultOption, optionFromPackage, option); | ||
outputSilent = option.silent; | ||
@@ -441,3 +447,3 @@ | ||
option.revision = initRevision(option); | ||
output("Chromium revision: " + option.revision); | ||
output(`Chromium revision: ${option.revision}`); | ||
option.userFolder = initUserFolder(option); | ||
@@ -449,6 +455,5 @@ //output("User folder: " + option.userFolder); | ||
await downloadHandler(option); | ||
await launchHandler(option); | ||
} | ||
await launchHandler(option); | ||
const revisionInfo = await revisionHandler(option); | ||
@@ -465,5 +470,28 @@ //console.log(revisionInfo); | ||
const getStats = (silent) => { | ||
let stats; | ||
try { | ||
stats = require(statsPath); | ||
} catch (e) { | ||
if (!silent) { | ||
output("Not found PCR stats cache, try npm install again.", true); | ||
} | ||
} | ||
if (stats) { | ||
stats.puppeteer = puppeteer; | ||
} | ||
return stats; | ||
}; | ||
PCR.get = (option) => { | ||
const stats = getStats(true); | ||
if (stats && fs.existsSync(stats.executablePath)) { | ||
return stats; | ||
} | ||
return PCR(option); | ||
}; | ||
//sync API | ||
resolver.getStats = getStats; | ||
PCR.getStats = getStats; | ||
module.exports = resolver; | ||
module.exports = PCR; |
{ | ||
"name": "puppeteer-chromium-resolver", | ||
"version": "8.0.0", | ||
"version": "8.1.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.", | ||
@@ -21,4 +21,8 @@ "main": "index.js", | ||
"scripts": { | ||
"install": "node install.js" | ||
"install": "node install.js", | ||
"test": "node test/test.js" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^7.24.0" | ||
} | ||
} | ||
} |
@@ -22,9 +22,17 @@ | ||
## Usage | ||
### [Async Case](./test/async.js): dynamic detection and downloading chromium | ||
### [Async] dynamic detection and downloading chromium | ||
```js | ||
(async () => { | ||
const PCR = require("puppeteer-chromium-resolver"); | ||
const stats = await PCR(); | ||
const option = { | ||
revision: "", | ||
detectionPath: "", | ||
folderName: ".chromium-browser-snapshots", | ||
defaultHosts: ["https://storage.googleapis.com", "https://npm.taobao.org/mirrors"], | ||
hosts: [], | ||
cacheRevisions: 2, | ||
retry: 3, | ||
silent: false | ||
}; | ||
const stats = await PCR.get(option); | ||
const browser = await stats.puppeteer.launch({ | ||
@@ -40,60 +48,34 @@ headless: false, | ||
await browser.close(); | ||
})(); | ||
``` | ||
#### Option | ||
```js | ||
const stats = await PCR({ | ||
revision: "", | ||
detectionPath: "", | ||
folderName: '.chromium-browser-snapshots', | ||
hosts: ["https://storage.googleapis.com", "https://npm.taobao.org/mirrors"], | ||
cacheRevisions: 2, | ||
retry: 3, | ||
silent: false | ||
}); | ||
``` | ||
### [Sync Case](./test/sync.js): chromium pre-downloaded when installation, just call API PCR.getStats() | ||
### [Sync] chromium will be pre-downloaded when PCR installation, so calling getStats() API will get PCR stats from previous installation cache. | ||
```js | ||
(async () => { | ||
const PCR = require("puppeteer-chromium-resolver"); | ||
const stats = PCR.getStats(); | ||
if (!stats) { | ||
return; | ||
} | ||
const browser = await stats.puppeteer.launch({ | ||
const PCR = require("puppeteer-chromium-resolver"); | ||
const stats = PCR.getStats(); | ||
if (stats) { | ||
stats.puppeteer.launch({ | ||
headless: false, | ||
args: ["--no-sandbox"], | ||
executablePath: stats.executablePath | ||
}).then(function(browser){ | ||
//... | ||
}).catch(function(error) { | ||
console.log(error); | ||
}); | ||
const page = await browser.newPage(); | ||
await page.goto("https://www.npmjs.com/package/puppeteer-chromium-resolver"); | ||
await browser.close(); | ||
})(); | ||
} | ||
``` | ||
### [Runtime Case] | ||
```js | ||
(async () => { | ||
### Option from root package.json with "pcr" object | ||
```json | ||
{ | ||
"name": "xxx", | ||
"version": "xxx", | ||
"dependencies": {}, | ||
const fs = require("fs"); | ||
const PCR = require("puppeteer-chromium-resolver"); | ||
"pcr": { | ||
"revision": "818858" | ||
} | ||
const getPCRStats = () => { | ||
const stats = PCR.getStats(); | ||
if (fs.existsSync(stats.executablePath)) { | ||
return stats; | ||
} | ||
return PCR(); | ||
}, | ||
const stats = await getPCRStats(); | ||
})(); | ||
} | ||
``` | ||
@@ -148,2 +130,6 @@ | ||
+ v8.1.0 | ||
- supported reading option from root package.json with "pcr" object | ||
- replaced PCR(option) with API PCR.get(option) | ||
+ v8.0.0 | ||
@@ -150,0 +136,0 @@ - updated puppeteer-core to v8.0.0 |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
20013
421
1
170
3