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;
}
}
}
}
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,
body: JSON.stringify({
message: `Complete screenshot of ${pageToScreenshot}`,
export async function runPuppeteer(url: string) {
const browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
try {
let page = await browser.newPage();
await page.goto(url);
const title = await page.title();
const screenshot = await page.screenshot({ encoding: "binary" });
return { title, screenshot };
} finally {
if (browser !== null) {
await browser.close();
}
}
}
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())
}
return macro_response;
} else {
var response_object = {
"Status": "SUCCESS",
"PhysicalResourceId": event.LogicalResourceId,
"StackId": event.StackId,
"RequestId": event.RequestId,
"LogicalResourceId": event.LogicalResourceId,
"Data": {}
};
try {
browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});
let page = await browser.newPage();
if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_Instance") {
await login(page);
response_object.Data = await createinstance(page, event.ResourceProperties);
} else if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_ContactFlow") {
await login(page);
await open(page, event.ResourceProperties);
response_object.Data = await createflow(page, event.ResourceProperties);
} else if (event.RequestType == "Create" && event.ResourceType == "Custom::AWS_Connect_PhoneNumber") {
await login(page);
await open(page, event.ResourceProperties);