Merge pull request #26 from aqaurius6666/master

Partially fixes #18
This commit is contained in:
Kyle McNally 2022-12-28 09:11:39 -05:00 committed by GitHub
commit 4cda3ab393
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 12 deletions

View File

@ -41,7 +41,7 @@ exports.handler = async (event, context, callback) => {
browser = await puppeteer.launch({ browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, headless: chromium.headless,
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });

View File

@ -49,7 +49,7 @@ test("Check the page title of example.com", async (t) => {
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, headless: chromium.headless,
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });
@ -74,7 +74,7 @@ const chromium = require('@sparticuz/chromium');
test("Check the page title of example.com", async (t) => { test("Check the page title of example.com", async (t) => {
const browser = await playwright.launch({ const browser = await playwright.launch({
args: chromium.args, args: chromium.args,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, 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 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. 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 ## Google Cloud Functions
@ -198,7 +222,7 @@ exports.handler = async (event, context, callback) => {
+ browser = await puppeteer.launch({ + browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, headless: chromium.headless,
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });

View File

@ -10,7 +10,7 @@ exports.handler = async (event, context) => {
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, headless: chromium.headless,
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });

View File

@ -179,8 +179,9 @@ class Chromium {
* Inflates the current version of Chromium and returns the path to the binary. * 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. * If not running on AWS Lambda nor Google Cloud Functions, `null` is returned instead.
*/ */
static get executablePath(): Promise<string> { static executablePath(input?: string): Promise<string> {
if (existsSync('/tmp/chromium') === true) {
if (existsSync('/tmp/chromium') === true && input === undefined) {
for (const file of readdirSync('/tmp')) { for (const file of readdirSync('/tmp')) {
if (file.startsWith('core.chromium') === true) { if (file.startsWith('core.chromium') === true) {
unlinkSync(`/tmp/${file}`); unlinkSync(`/tmp/${file}`);
@ -189,15 +190,21 @@ class Chromium {
return Promise.resolve('/tmp/chromium'); return Promise.resolve('/tmp/chromium');
} }
let _input = join(__dirname, '..', 'bin');
const 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 = [ const promises = [
LambdaFS.inflate(`${input}/chromium.br`), LambdaFS.inflate(`${_input}/chromium.br`),
LambdaFS.inflate(`${input}/swiftshader.tar.br`), LambdaFS.inflate(`${_input}/swiftshader.tar.br`),
]; ];
if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { 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()); return Promise.all(promises).then((result) => result.shift());