Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _downloadBrowser (revision) {
const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST; // defaults to https://storage.googleapis.com if null
const browserFetcher = puppeteer.createBrowserFetcher({ host: downloadHost });
// same logic as puppeteer
revision = revision || process.env.PUPPETEER_CHROMIUM_REVISION || require('puppeteer-core/package.json').puppeteer.chromium_revision;
const revisionInfo = browserFetcher.revisionInfo(revision);
// do nothing if the revision is already downloaded.
if (revisionInfo.local)
return;
await browserFetcher.download(revisionInfo.revision, this._downloadProgressCallback(revision));
},
async function downloadChromium() {
const browserFetcher = puppeteer.createBrowserFetcher({
path: chromeTempPath,
host: downloadHost,
})
const revision =
process.env.PUPPETEER_CHROMIUM_REVISION ||
process.env.npm_config_puppeteer_chromium_revision ||
process.env.npm_package_config_puppeteer_chromium_revision ||
pptrCoreJson.puppeteer.chromium_revision
const revisionInfo = browserFetcher.revisionInfo(revision)
// If already downloaded
if (revisionInfo.local) return revisionInfo
try {
console.log(`Downloading Chromium r${revision}...`)
async function downloadChromium(options, targetRevision) {
const browserFetcher = puppeteer.createBrowserFetcher({ path: options.localDataDir });
const revision = targetRevision || require('puppeteer-core/package.json').puppeteer.chromium_revision;
const revisionInfo = browserFetcher.revisionInfo(revision);
// Do nothing if the revision is already downloaded.
if (revisionInfo.local)
return revisionInfo;
// Override current environment proxy settings with npm configuration, if any.
try {
console.log(`Downloading Chromium r${revision}...`);
const newRevisionInfo = await browserFetcher.download(revisionInfo.revision);
console.log('Chromium downloaded to ' + newRevisionInfo.folderPath);
let localRevisions = await browserFetcher.localRevisions();
localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
// Remove previous chromium revisions.
const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
export const downloadBrowserIfNeeded = async (): Promise => {
const browserFetcher = puppeteer.createBrowserFetcher();
const localRevisions = await browserFetcher.localRevisions();
if (localRevisions && localRevisions.length > 0) {
console.log('Found a local revision for browser, exiting install.');
return;
}
console.log('Did not find any local revisions for browser, downloading latest this might take a while.');
await browserFetcher.download(constants.chromiumRevision, (downloaded, total) => {
if (downloaded === total) {
console.log('Chromium successfully downloaded');
}
});
};
async function downloadOrLocatePreferredBrowserRevision() {
const browserFetcher = puppeteer.createBrowserFetcher();
const preferredRevision = puppeteer._launcher._preferredRevision;
const localRevisions = await browserFetcher.localRevisions();
let revisionInfo;
if (localRevisions.includes(preferredRevision)) {
revisionInfo = await browserFetcher.revisionInfo(preferredRevision);
} else {
console.log(`Downloading Chromium ${preferredRevision}`);
revisionInfo = await browserFetcher.download(preferredRevision);
}
return puppeteer.launch({
executablePath: revisionInfo.executablePath
});
}
const getBrowserFetcher = (): BrowserFetcher => {
const downloadHost =
process.env.PUPPETEER_DOWNLOAD_HOST ||
process.env.npm_config_puppeteer_download_host ||
process.env.npm_package_config_puppeteer_download_host;
if (browserFetcher) {
return browserFetcher;
}
browserFetcher = puppeteer.createBrowserFetcher({ host: downloadHost });
return browserFetcher;
};
export const launchBrowser = async (): Promise => {
const browserFetcher = puppeteer.createBrowserFetcher();
const localRevisions = await browserFetcher.localRevisions();
if (localRevisions.length === 0) {
throw new Error('Could not launch browser because there is no local revisions.');
}
let executablePath = null;
executablePath = browserFetcher.revisionInfo(localRevisions[0]).executablePath;
const browser = await puppeteer.launch({
headless: process.env.BROWSER ? false : true,
slowMo: process.env.SLOWMO ? 100 : 0,
defaultViewport: {
width: 1920,
height: 1080,
deviceScaleFactor: 1,
isMobile: false,