|
| 1 | +delete process.env.AWS_XRAY_CONTEXT_MISSING; |
| 2 | +const AWSXRay = require('aws-xray-sdk-core'); |
| 3 | + |
| 4 | +AWSXRay.setLogger(console); |
| 5 | + |
| 6 | +async function launchBrowser() { |
| 7 | + try { // for aws-based image |
| 8 | + const chromium = require('chrome-aws-lambda'); |
| 9 | + console.info('launching chrome-aws-lambda browser'); |
| 10 | + const browser = await chromium.puppeteer.launch({ |
| 11 | + args: chromium.args, |
| 12 | + defaultViewport: chromium.defaultViewport, |
| 13 | + executablePath: await chromium.executablePath, |
| 14 | + headless: chromium.headless, |
| 15 | + ignoreHTTPSErrors: true, |
| 16 | + }); |
| 17 | + return browser; |
| 18 | + } catch (e) { // for custom-built image |
| 19 | + console.info('launching puppeteer browser'); |
| 20 | + const puppeteer = require('puppeteer'); |
| 21 | + const browser = await puppeteer.launch({ |
| 22 | + executablePath: '/usr/bin/chromium', |
| 23 | + headless: true, |
| 24 | + dumpio: true, |
| 25 | + args: [ |
| 26 | + '--autoplay-policy=user-gesture-required', |
| 27 | + '--disable-background-networking', |
| 28 | + '--disable-background-timer-throttling', |
| 29 | + '--disable-backgrounding-occluded-windows', |
| 30 | + '--disable-breakpad', |
| 31 | + '--disable-client-side-phishing-detection', |
| 32 | + '--disable-component-update', |
| 33 | + '--disable-default-apps', |
| 34 | + '--disable-dev-shm-usage', |
| 35 | + '--disable-domain-reliability', |
| 36 | + '--disable-extensions', |
| 37 | + '--disable-features=AudioServiceOutOfProcess', |
| 38 | + '--disable-hang-monitor', |
| 39 | + '--disable-ipc-flooding-protection', |
| 40 | + '--disable-notifications', |
| 41 | + '--disable-offer-store-unmasked-wallet-cards', |
| 42 | + '--disable-popup-blocking', |
| 43 | + '--disable-print-preview', |
| 44 | + '--disable-prompt-on-repost', |
| 45 | + '--disable-renderer-backgrounding', |
| 46 | + '--disable-setuid-sandbox', |
| 47 | + '--disable-speech-api', |
| 48 | + '--disable-sync', |
| 49 | + '--disk-cache-size=33554432', |
| 50 | + '--hide-scrollbars', |
| 51 | + '--ignore-gpu-blacklist', |
| 52 | + '--metrics-recording-only', |
| 53 | + '--mute-audio', |
| 54 | + '--no-default-browser-check', |
| 55 | + '--no-first-run', |
| 56 | + '--no-pings', |
| 57 | + '--no-sandbox', |
| 58 | + '--no-zygote', |
| 59 | + '--password-store=basic', |
| 60 | + '--use-gl=swiftshader', |
| 61 | + '--use-mock-keychain', |
| 62 | + '--single-process', |
| 63 | + ], |
| 64 | + }); |
| 65 | + return browser; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function createNewSubsegment(subsegmentName) { |
| 70 | + const segment = AWSXRay.getSegment(); |
| 71 | + if (!segment) return { close() { } }; |
| 72 | + return segment.addNewSubsegment(subsegmentName); |
| 73 | +} |
| 74 | + |
| 75 | +async function lambdaHandler(event, context) { |
| 76 | + console.info(`EVENT ${JSON.stringify(event, null, 2)}`); |
| 77 | + AWSXRay.setContextMissingStrategy('LOG_ERROR'); |
| 78 | + const browserLaunchSubsegment = createNewSubsegment('browser launch'); |
| 79 | + const browser = await launchBrowser(); |
| 80 | + browserLaunchSubsegment.close(); |
| 81 | + console.info('browser launched'); |
| 82 | + const newTabSubsegment = createNewSubsegment('open new tab'); |
| 83 | + const page = await browser.newPage(); |
| 84 | + newTabSubsegment.close(); |
| 85 | + console.info('opened new tab'); |
| 86 | + let extractedText = ''; |
| 87 | + try { |
| 88 | + const pageGotoSubsegment = createNewSubsegment('opening url'); |
| 89 | + await page.goto(event.url, { |
| 90 | + waitUntil: 'networkidle0', |
| 91 | + timeout: 10 * 1000, |
| 92 | + }); |
| 93 | + pageGotoSubsegment.close(); |
| 94 | + console.info('page opened'); |
| 95 | + const textExtractSubsegment = createNewSubsegment('extracting text'); |
| 96 | + extractedText = await page.$eval('*', (el) => el.innerText); |
| 97 | + textExtractSubsegment.close(); |
| 98 | + console.info('text extracted'); |
| 99 | + // context.succeed(extractedText); |
| 100 | + } finally { |
| 101 | + console.info('finally'); |
| 102 | + await page.close(); |
| 103 | + console.info('page closed'); |
| 104 | + await browser.close(); |
| 105 | + console.info('browser closed'); |
| 106 | + } |
| 107 | + return extractedText; |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = { handler: lambdaHandler }; |
0 commit comments