Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getPuppeteer = async () => {
try {
const puppeteer = require('puppeteer');
return await puppeteer.launch();
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log('Error(This package is used for local development) ', JSON.stringify(error, null, 2));
try {
const chromium = require('chrome-aws-lambda')
return await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
} catch (_error) {
throw error;
}
}
}
}
const takeScreenshot = async (feature, periods, accessibleColours) => {
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: {
width: 800,
height: 600,
isLandscape: true
},
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.goto(
`https://caniuse.bitsofco.de/embed/index.html?feat=${feature}&periods=${periods}&accessible-colours=${accessibleColours}&screenshot=true`,
{ waitUntil: 'networkidle2' }
);
module.exports = async () => {
const config = {
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
}
const browser = await chromium.puppeteer.launch(config)
global.__BROWSER__ = browser
await fs.mkdirs(DIR)
await fs.writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint())
}
executablePath =>
new MrPuppetshot(
{
executablePath,
args: [...chromeLambda.args, ...CHROMIUM_ARGS],
headless: true,
defaultViewport,
ignoreHTTPSErrors: IGNORE_HTTPS_ERRORS,
timeout: TIMEOUT
},
chromeLambda.puppeteer
)
);
async setup() {
await super.setup()
const wsEndpoint = await fs.readFile(wsEndpointDir, 'utf8')
if (!wsEndpoint) throw new Error('wsEndpoint not found')
this.global.browser = await chromium.puppeteer.connect({
browserWSEndpoint: wsEndpoint,
})
}
exports.handler = async (event, context) => {
const pageToScreenshot = JSON.parse(event.body).pageToScreenshot;
if (!pageToScreenshot) return {
statusCode: 400,
body: JSON.stringify({ message: 'Page URL not defined' })
}
const browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
const page = await browser.newPage();
await page.goto(pageToScreenshot, { waitUntil: 'networkidle2' });
const screenshot = await page.screenshot({ encoding: 'binary' });
await browser.close();
return {
statusCode: 200,