Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Error while using javascript #271

Open
dungeon2567 opened this issue Apr 26, 2022 · 0 comments
Open

[BUG] Error while using javascript #271

dungeon2567 opened this issue Apr 26, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@dungeon2567
Copy link

dungeon2567 commented Apr 26, 2022

  • chrome-aws-lambda 10.1.0:
  • puppeteer / puppeteer-core 13.6.0:
  • OS: Linux
  • Node.js Version: 14

Expected Behavior

No error happening

Current Behavior

This error: {
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Invalid or unexpected token",
"trace": [
"Runtime.UserCodeSyntaxError: SyntaxError: Invalid or unexpected token",
" at _loadUserApp (/var/runtime/UserFunction.js:218:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)",
" at Object. (/var/runtime/index.js:43:34)",
" at Module._compile (internal/modules/cjs/loader.js:1085:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
" at Module.load (internal/modules/cjs/loader.js:950:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
" at internal/main/run_main_module.js:17:47"
]
}

Steps to Reproduce

async function launchBrowser() {
    try {
        const chromium = require('chrome-aws-lambda');
        console.info('launching chrome-aws-lambda browser');
        const browser = await chromium.puppeteer.launch({
            args: chromium.args,
            defaultViewport: chromium.defaultViewport,
            executablePath: await chromium.executablePath,
            headless: chromium.headless,
            ignoreHTTPSErrors: true,
        });
        return browser;
    } catch (e) {
        console.log(e);
    }
    finally {
        console.info('launching puppeteer browser');
    }
}

async function lambdaHandler(event, context) {
    console.info(`EVENT ${JSON.stringify(event, null, 2)}`);
    const browser = await launchBrowser();
    console.info('browser launched');
    const page = await browser.newPage();
    console.info('opened new tab');
    let bodyText = '';
    try {
        await page.goto(event.url, {
            waitUntil: 'networkidle0',
            timeout: 10 * 1000,
        });
        console.info('page opened');
        const stream = await page.pdf();
        bodyText = stream.toString("base64");
        console.info('pdf created');
    } finally {
        console.info('finally');
        await page.close();
        console.info('page closed');
        await browser.close();
        console.info('browser closed');
    }

    return {
        statusCode: 200,
        isBase64Encoded: true,
        headers: { "Content-type": "application/pdf" },
        body: bodyText
    };
}


module.exports = { handler: lambdaHandler };

Dockerfile: FROM amazon/aws-lambda-nodejs:latest

COPY ./* ${LAMBDA_TASK_ROOT}
RUN npm install --only=prod

CMD [ "index.handler" ]

@dungeon2567 dungeon2567 added the bug Something isn't working label Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant