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({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
ignoreHTTPSErrors: true,
});

View File

@ -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,
});

View File

@ -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,
});

View File

@ -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<string> {
if (existsSync('/tmp/chromium') === true) {
static executablePath(input?: string): Promise<string> {
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());