Skip to content

Commit

Permalink
Update to Node 18 Runtime and update used Chromium Version (#85)
Browse files Browse the repository at this point in the history
* Use Node 18 Runtime

* Use @sparticuz/chromium

* Update Spatie Script to use pupeteer-core

* Upgrade browsershot.js to aws-sdk-v3

* Update AWS Layers

* Fix fs.writeFileSync Call

* Fix Buffer constructor deprecation

* Mark sidecar-browsershot.layer config as deprecated
  • Loading branch information
stefanzweifel authored Sep 20, 2023
1 parent 5ea5dcf commit 67845de
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions config/sidecar-browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* AWS Layer to use by Lambda. Defaults to "shelfio/chrome-aws-lambda-layer" in your AWS region.
* Must contain "chrome-aws-lambda".
* @see https://github.com/shelfio/chrome-aws-lambda-layer
* @deprecated
*/
'layer' => env('SIDECAR_BROWSERSHOT_LAYER'),
];
32 changes: 19 additions & 13 deletions resources/lambda/browsershot.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const fs = require('fs');
const { execSync } = require('child_process');
const chromium = require('@sparticuz/chrome-aws-lambda');
const AWS = require('aws-sdk');
const chromium = require('@sparticuz/chromium');
const { S3 } = require("@aws-sdk/client-s3");

exports.handle = async function (event) {
if (event.warming) {
return;
}

// sparticuz/chromium specific settings.
// https://github.com/Sparticuz/chromium#usage
// Use legacy headless mode.
chromium.setHeadlessMode = true;
// Disable webgl.
chromium.setGraphicsMode = false;

// Add Emoji Font to Chromium
await chromium.font( '/var/task/NotoColorEmoji.ttf');
await chromium.font('/var/task/NotoColorEmoji.ttf');

// Constant file where we write out options.
const options = '/tmp/browsershot.js';
Expand All @@ -30,7 +37,7 @@ exports.handle = async function (event) {
event.url = 'file:///tmp/index.html';
} else if (event.options.s3Source) {
// If the source is S3, then download the file into a temporary file to be used as the URL.
const s3 = new AWS.S3({
const s3 = new S3({
region: event.options.s3Source.region,
accessKeyId: event.options.s3Source.key,
secretAccessKey: event.options.s3Source.secret,
Expand All @@ -41,15 +48,14 @@ exports.handle = async function (event) {
Key: event.options.s3Source.path,
}

const result = await s3.getObject(params).promise();
const result = await s3.getObject(params);

fs.writeFileSync('/tmp/index.html', result.Body);
fs.writeFileSync('/tmp/index.html', result.Body.toString());

event.url = 'file:///tmp/index.html';
}

// Get the executable path from the chrome layer.
event.options.executablePath = await chromium.executablePath;
event.options.executablePath = await chromium.executablePath();

// Combine the developers args with the ones from the layer.
event.options.args = [
Expand Down Expand Up @@ -84,7 +90,7 @@ exports.handle = async function (event) {
if (event.options.s3) {
const accessKeyId = event.options.s3.key;
const secretAccessKey = event.options.s3.secret;
const s3 = new AWS.S3({
const s3 = new S3({
region: event.options.s3.region,
accessKeyId: event.options.s3.key,
secretAccessKey: event.options.s3.secret,
Expand All @@ -107,15 +113,15 @@ exports.handle = async function (event) {
Bucket: event.options.s3.bucket,
Key: event.options.s3.path,
Body: contents,
ContentType: type
ContentType: type,
}

const result = await s3.putObject(params).promise();
const result = await s3.putObject(params);

return result.ETag
return result.ETag;
}

return new Buffer(contents).toString('base64');
return new Buffer.from(contents).toString('base64');
}

// Otherwise, return the string.
Expand Down
23 changes: 17 additions & 6 deletions src/Functions/BrowsershotFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Hammerstone\Sidecar\Architecture;
use Hammerstone\Sidecar\LambdaFunction;
use Hammerstone\Sidecar\Package;
use Hammerstone\Sidecar\Runtime;
use Hammerstone\Sidecar\WarmingConfig;

class BrowsershotFunction extends LambdaFunction
Expand Down Expand Up @@ -49,13 +48,13 @@ protected function modifiedBrowserJs()
// Remove their reference.
$browser = str_replace('const puppet = (pup || require(\'puppeteer\'));', '', $browser);

// Add ours.
return "const puppet = require('@sparticuz/chrome-aws-lambda').puppeteer; \n" . $browser;
// Use pupeteer-core instead.
return "const puppet = require('puppeteer-core'); \n" . $browser;
}

public function runtime()
{
return Runtime::NODEJS_14;
return 'nodejs18.x';
}

public function memory()
Expand All @@ -68,7 +67,7 @@ public function memory()
*/
public function storage()
{
// Default to the main sidecar config value if the sidecar-browsershot config hasn't been updated to include this new key.
// Defaults to the main sidecar config value if the sidecar-browsershot config hasn't been updated to include this new key.
return config('sidecar-browsershot.storage', parent::storage());
}

Expand All @@ -93,7 +92,19 @@ public function layers()

$region = config('sidecar.aws_region');

if ($region === 'ap-northeast-2') {
$chromeAwsLambdaVersion = 36;
} else {
$chromeAwsLambdaVersion = 37;
}


// Add Layers that each contain `puppeteer-core` and `@sparticuz/chromium`
// https://github.com/stefanzweifel/sidecar-browsershot-layer
// https://github.com/shelfio/chrome-aws-lambda-layer
return ["arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:31"];
return [
"arn:aws:lambda:{$region}:821527532446:layer:sidecar-browsershot-layer:1",
"arn:aws:lambda:{$region}:764866452798:layer:chrome-aws-lambda:{$chromeAwsLambdaVersion}",
];
}
}

0 comments on commit 67845de

Please sign in to comment.