puppeteer-chromium-resolver
Advanced tools
Comparing version 8.1.0 to 8.1.1
121
index.js
const path = require("path"); | ||
const fs = require("fs"); | ||
const os = require("os"); | ||
const EC = require("eight-colors"); | ||
const puppeteer = require("puppeteer-core"); | ||
@@ -10,22 +11,2 @@ const PingMonitor = require("ping-monitor"); | ||
//========================================================================================= | ||
//https://en.wikipedia.org/wiki/ANSI_escape_code | ||
//color | ||
//0 - 7 | ||
const Color = { | ||
bg: {} | ||
}; | ||
const addColor = (start, str, end) => { | ||
return `\x1b[${start}m${str}\x1b[${end}m`; | ||
}; | ||
const list = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]; | ||
list.forEach((name, i) => { | ||
Color[name] = (str) => { | ||
return addColor(`3${i}`, str, "39"); | ||
}; | ||
Color.bg[name] = (str) => { | ||
return addColor(`4${i}`, str, "49"); | ||
}; | ||
}); | ||
//========================================================================================= | ||
let outputSilent = false; | ||
@@ -36,3 +17,3 @@ const output = (msg, isError) => { | ||
if (isError) { | ||
console.log(Color.red(`[PCR] ${msg}`)); | ||
console.log(EC.red(`[PCR] ${msg}`)); | ||
} else { | ||
@@ -339,2 +320,21 @@ console.log(`[PCR] ${msg}`); | ||
const getOption = (option) => { | ||
const defaultOption = { | ||
revision: "", | ||
detectionPath: "", | ||
folderName: ".chromium-browser-snapshots", | ||
defaultHosts: ["https://storage.googleapis.com", "https://npm.taobao.org/mirrors"], | ||
hosts: [], | ||
cacheRevisions: 2, | ||
retry: 3, | ||
silent: false | ||
}; | ||
const optionFromPackage = getOptionFromPackage(); | ||
option = Object.assign(defaultOption, optionFromPackage, option); | ||
return option; | ||
}; | ||
//========================================================================================= | ||
@@ -377,3 +377,3 @@ | ||
if (executablePath) { | ||
executablePath = fs.existsSync(executablePath) ? Color.green(executablePath) : Color.red(executablePath); | ||
executablePath = fs.existsSync(executablePath) ? EC.green(executablePath) : EC.red(executablePath); | ||
output(`Chromium executablePath: ${executablePath}`); | ||
@@ -387,3 +387,3 @@ } | ||
if (revisionInfo.chromiumVersion) { | ||
output(`Chromium version: ${revisionInfo.chromiumVersion}`); | ||
output(`Chromium version: ${EC.magenta(revisionInfo.chromiumVersion)}`); | ||
} | ||
@@ -393,3 +393,3 @@ | ||
revisionInfo.launchable = option.launchable; | ||
const launchable = revisionInfo.launchable ? Color.green("true") : Color.red("false"); | ||
const launchable = revisionInfo.launchable ? EC.green("true") : EC.red("false"); | ||
output(`Chromium launchable: ${launchable}`); | ||
@@ -401,3 +401,3 @@ } | ||
revisionInfo.puppeteerVersion = option.puppeteerConf.version; | ||
output(`Puppeteer version: ${revisionInfo.puppeteerVersion}`); | ||
output(`Puppeteer version: ${EC.magenta(revisionInfo.puppeteerVersion)}`); | ||
} | ||
@@ -411,4 +411,9 @@ revisionInfo.puppeteer = puppeteer; | ||
const statsPath = path.resolve(__dirname, ".stats.json"); | ||
const getStatsPath = () => { | ||
const statsPath = path.resolve(__dirname, ".stats.json"); | ||
return statsPath; | ||
}; | ||
const saveStats = (revisionInfo) => { | ||
const statsPath = getStatsPath(); | ||
const stats = Object.assign({}, revisionInfo); | ||
@@ -420,19 +425,31 @@ delete stats.puppeteer; | ||
const getStats = () => { | ||
const statsPath = getStatsPath(); | ||
let stats; | ||
try { | ||
stats = JSON.parse(fs.readFileSync(statsPath)); | ||
} catch (e) { | ||
output("Not found PCR stats cache, try npm install again.", true); | ||
} | ||
if (stats) { | ||
stats.puppeteer = puppeteer; | ||
} | ||
return stats; | ||
}; | ||
const PCR = async (option = {}) => { | ||
const defaultOption = { | ||
revision: "", | ||
detectionPath: "", | ||
folderName: ".chromium-browser-snapshots", | ||
defaultHosts: ["https://storage.googleapis.com", "https://npm.taobao.org/mirrors"], | ||
hosts: [], | ||
cacheRevisions: 2, | ||
retry: 3, | ||
silent: false | ||
}; | ||
option = getOption(option); | ||
const optionFromPackage = getOptionFromPackage(); | ||
//from stats cache | ||
outputSilent = true; | ||
const stats = getStats(); | ||
if (stats && fs.existsSync(stats.executablePath)) { | ||
//if has custom revision should be matched | ||
if (!option.revision || (option.revision && option.revision === stats.revision)) { | ||
return stats; | ||
} | ||
} | ||
option = Object.assign(defaultOption, optionFromPackage, option); | ||
//try to detection and install | ||
outputSilent = option.silent; | ||
@@ -442,3 +459,3 @@ | ||
option.revision = initRevision(option); | ||
output(`Chromium revision: ${option.revision}`); | ||
output(`Chromium revision: ${EC.magenta(option.revision)}`); | ||
option.userFolder = initUserFolder(option); | ||
@@ -464,28 +481,4 @@ //output("User folder: " + option.userFolder); | ||
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 | ||
PCR.getStats = getStats; | ||
module.exports = PCR; |
{ | ||
"name": "puppeteer-chromium-resolver", | ||
"version": "8.1.0", | ||
"version": "8.1.1", | ||
"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.", | ||
@@ -11,2 +11,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"eight-colors": "^1.0.0", | ||
"gauge": "^3.0.0", | ||
@@ -28,2 +29,2 @@ "object-assign": "^4.1.1", | ||
} | ||
} | ||
} |
@@ -36,3 +36,3 @@ | ||
}; | ||
const stats = await PCR.get(option); | ||
const stats = await PCR(option); | ||
const browser = await stats.puppeteer.launch({ | ||
@@ -51,3 +51,3 @@ headless: false, | ||
### [Sync] chromium will be pre-downloaded when PCR installation, so calling getStats() API will get PCR stats from previous installation cache. | ||
### [Sync] chromium will be pre-downloaded when PCR installation, so calling getStats() will get PCR stats from previous installation. | ||
```js | ||
@@ -72,4 +72,4 @@ const PCR = require("puppeteer-chromium-resolver"); | ||
{ | ||
"name": "xxx", | ||
"version": "xxx", | ||
"name": "", | ||
"version": "", | ||
"dependencies": {}, | ||
@@ -112,13 +112,11 @@ | ||
const puppeteer = require("puppeteer"); | ||
const stats = PCR.getStats(); //or await PCR(); | ||
//process global setting | ||
const stats = await PCR(); | ||
//update global env | ||
process.env.PUPPETEER_EXECUTABLE_PATH = stats.executablePath; | ||
//or specify executablePath | ||
const browser = await puppeteer.launch({ | ||
//or executablePath: stats.executablePath, | ||
executablePath: stats.executablePath, | ||
headless: false | ||
}); | ||
const page = await browser.newPage(); | ||
await page.goto('https://github.com'); | ||
await browser.close(); | ||
@@ -132,5 +130,5 @@ })(); | ||
+ v8.1.0 | ||
+ v8.1.1 | ||
- supported reading option from root package.json with "pcr" object | ||
- replaced PCR(option) with API PCR.get(option) | ||
- updated PCR(option) API to support using stats cache | ||
@@ -137,0 +135,0 @@ + 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
2
19782
5
412
168
+ Addedeight-colors@^1.0.0
+ Added@types/node@22.13.1(transitive)
+ Addedeight-colors@1.3.1(transitive)
- Removed@types/node@22.13.0(transitive)