diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md index 405d9a6..65b8647 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -41,7 +41,7 @@ exports.handler = async (event, context, callback) => { browser = await puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, ignoreHTTPSErrors: true, }); diff --git a/README.md b/README.md index 8da084d..8455d4b 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ test("Check the page title of example.com", async (t) => { const browser = await puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, ignoreHTTPSErrors: true, }); @@ -74,7 +74,7 @@ const chromium = require('@sparticuz/chromium'); test("Check the page title of example.com", async (t) => { const browser = await playwright.launch({ args: chromium.args, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, }); @@ -171,6 +171,30 @@ aws s3 cp chromium.zip "s3://${bucketName}/chromiumLayers/chromium${versionNumbe aws lambda publish-layer-version --layer-name chromium --description "Chromium v${versionNumber}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium${versionNumber}.zip" --compatible-runtimes nodejs --compatible-architectures x86_64 ``` +Then you can specify custom Chromium location as following. +```javascript +const test = require("node:test"); +const puppeteer = require("puppeteer-core"); +const chromium = require("@sparticuz/chromium"); + +test("Check the page title of example.com", async (t) => { + const browser = await puppeteer.launch({ + args: chromium.args, + defaultViewport: chromium.defaultViewport, + executablePath: await chromium.executablePath('/opt/chromium'), + headless: chromium.headless, + ignoreHTTPSErrors: true, + }); + + const page = await browser.newPage(); + await page.goto("https://example.com"); + const pageTitle = await page.title(); + await browser.close(); + + assert.strictEqual(pageTitle, "Example Domain"); +}); +``` + Alternatively, you can also download the layer artifact from one of our [CI workflow runs](https://github.com/Sparticuz/chromium/actions/workflows/aws.yml?query=is%3Asuccess+branch%3Amaster). Use the `chromium.zip` INSIDE the artifact as the layer. Also, the artifact will expire from Github after a certain time period. ## Google Cloud Functions @@ -198,7 +222,7 @@ exports.handler = async (event, context, callback) => { + browser = await puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, ignoreHTTPSErrors: true, }); diff --git a/_/amazon/handlers/index.js b/_/amazon/handlers/index.js index 20e40f2..f95457d 100644 --- a/_/amazon/handlers/index.js +++ b/_/amazon/handlers/index.js @@ -10,7 +10,7 @@ exports.handler = async (event, context) => { const browser = await puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, ignoreHTTPSErrors: true, }); diff --git a/source/index.ts b/source/index.ts index 56e0a58..5dcb249 100644 --- a/source/index.ts +++ b/source/index.ts @@ -179,8 +179,9 @@ class Chromium { * Inflates the current version of Chromium and returns the path to the binary. * If not running on AWS Lambda nor Google Cloud Functions, `null` is returned instead. */ - static get executablePath(): Promise { - if (existsSync('/tmp/chromium') === true) { + static executablePath(input?: string): Promise { + + if (existsSync('/tmp/chromium') === true && input === undefined) { for (const file of readdirSync('/tmp')) { if (file.startsWith('core.chromium') === true) { unlinkSync(`/tmp/${file}`); @@ -189,15 +190,21 @@ class Chromium { return Promise.resolve('/tmp/chromium'); } - - const input = join(__dirname, '..', 'bin'); + let _input = join(__dirname, '..', 'bin'); + if (input !== undefined) { + if (existsSync(input)) { + _input = join(input, 'bin'); + } else { + throw new Error(`The input directory "${input}" does not exist.`); + } + } const promises = [ - LambdaFS.inflate(`${input}/chromium.br`), - LambdaFS.inflate(`${input}/swiftshader.tar.br`), + LambdaFS.inflate(`${_input}/chromium.br`), + LambdaFS.inflate(`${_input}/swiftshader.tar.br`), ]; if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { - promises.push(LambdaFS.inflate(`${input}/aws.tar.br`)); + promises.push(LambdaFS.inflate(`${_input}/aws.tar.br`)); } return Promise.all(promises).then((result) => result.shift());