Skip to content

Commit

Permalink
Merge pull request #187 from Sparticuz/fix/lambda-node20.x
Browse files Browse the repository at this point in the history
Node 20 lambda support,
fixes new AL2 lambda libs
  • Loading branch information
Sparticuz authored Dec 19, 2023
2 parents d63a8ef + 849e769 commit 247f748
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 35 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x

- name: Install Packages
run: npm ci
Expand All @@ -29,7 +29,7 @@ jobs:
run: make chromium.zip

- name: Upload Layer Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: chromium
path: chromium.zip
Expand All @@ -45,20 +45,21 @@ jobs:
version:
- 16
- 18
- 20
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.x"

- name: Setup AWS SAM CLI
uses: aws-actions/setup-sam@v2

- name: Download Layer Artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: chromium

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.org/

- run: npm ci
Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ pretest:
npm install --prefix _/amazon/handlers puppeteer-core@latest --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false

test:
sam local invoke --template _/amazon/template.yml --event _/amazon/events/example.com.json node18

.fonts.zip:
zip -9 --filesync --move --recurse-paths .fonts.zip .fonts/
sam local invoke --template _/amazon/template.yml --event _/amazon/events/example.com.json node20

%.zip:
npm install --fund=false --package-lock=false
npm run build
mkdir -p nodejs
npm install --prefix nodejs/ tar-fs@2.1.1 follow-redirects@1.15.2 --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false
npm install --prefix nodejs/ tar-fs@3.0.4 follow-redirects@1.15.3 --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false
npm pack
mkdir -p nodejs/node_modules/@sparticuz/chromium/
tar --directory nodejs/node_modules/@sparticuz/chromium/ --extract --file sparticuz-chromium-*.tgz --strip-components=1
Expand Down
6 changes: 3 additions & 3 deletions _/amazon/events/example.com.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"url": "https://example.com",
"expected": {
"title": "Example Domain",
"screenshot": "fdd55bf210cb00e00cadf3098055611d11293d02"
"screenshot": "cf91d541795c36da2e8733ed389e9a7951d5fe7f"
}
},
{
"url": "https://example.com",
"expected": {
"title": "Example Domain",
"screenshot": "fdd55bf210cb00e00cadf3098055611d11293d02"
"screenshot": "cf91d541795c36da2e8733ed389e9a7951d5fe7f"
}
},
{
"url": "https://get.webgl.org",
"expected": {
"remove": "logo-container",
"screenshot": "7a63a9a18f32dcdad78e1e0a03364fade25c85a8"
"screenshot": "2ca647063ec0be49a6191a7716c6a125e0a5b31b"
}
}
]
1 change: 1 addition & 0 deletions _/amazon/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.handler = async (event, context) => {
browser = await puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
dumpio: true,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
ignoreHTTPSErrors: true,
Expand Down
12 changes: 12 additions & 0 deletions _/amazon/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Resources:
CompatibleRuntimes:
- nodejs16.x
- nodejs18.x
- nodejs20.x

node16:
Type: AWS::Serverless::Function
Expand All @@ -37,3 +38,14 @@ Resources:
- AWSLambdaBasicExecutionRole
- AWSXRayDaemonWriteAccess
Tracing: Active
node20:
Type: AWS::Serverless::Function
Properties:
Layers:
- !Ref layer
Handler: handlers/index.handler
Runtime: nodejs20.x
Policies:
- AWSLambdaBasicExecutionRole
- AWSXRayDaemonWriteAccess
Tracing: Active
Binary file added bin/al2.tar.br
Binary file not shown.
Binary file added bin/al2023.tar.br
Binary file not shown.
Binary file removed bin/aws.tar.br
Binary file not shown.
Binary file added bin/fonts.tar.br
Binary file not shown.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sparticuz/chromium",
"version": "119.0.0",
"version": "119.0.1-next.0",
"description": "Chromium Binary for Serverless Platforms",
"keywords": [
"aws",
Expand Down Expand Up @@ -43,10 +43,10 @@
"@tsconfig/node16": "^16.1.1",
"@tsconfig/strictest": "^2.0.2",
"@types/follow-redirects": "^1.14.4",
"@types/node": "^18.18.9",
"@types/node": "^18.19.3",
"@types/tar-fs": "^2.0.4",
"clean-modules": "^3.0.4",
"typescript": "^5.2.2"
"typescript": "^5.3.3"
},
"engines": {
"node": ">= 16"
Expand Down
21 changes: 19 additions & 2 deletions source/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,29 @@ export const isValidUrl = (input: string) => {
export const isRunningInAwsLambda = () => {
if (
process.env["AWS_EXECUTION_ENV"] &&
/^AWS_Lambda_nodejs/.test(process.env["AWS_EXECUTION_ENV"]) === true
process.env["AWS_EXECUTION_ENV"].includes("AWS_Lambda_nodejs") &&
!process.env["AWS_EXECUTION_ENV"].includes("20.x")
) {
return true;
} else if (
process.env["AWS_LAMBDA_JS_RUNTIME"] &&
/^nodejs/.test(process.env["AWS_LAMBDA_JS_RUNTIME"]) === true
process.env["AWS_LAMBDA_JS_RUNTIME"].includes("nodejs") &&
!process.env["AWS_LAMBDA_JS_RUNTIME"].includes("20.x")
) {
return true;
}
return false;
};

export const isRunningInAwsLambdaNode20 = () => {
if (
process.env["AWS_EXECUTION_ENV"] &&
process.env["AWS_EXECUTION_ENV"].includes("20.x")
) {
return true;
} else if (
process.env["AWS_LAMBDA_JS_RUNTIME"] &&
process.env["AWS_LAMBDA_JS_RUNTIME"].includes("20.x")
) {
return true;
}
Expand Down
44 changes: 37 additions & 7 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { https } from "follow-redirects";
import LambdaFS from "./lambdafs";
import { join } from "node:path";
import { URL } from "node:url";
import { downloadAndExtract, isRunningInAwsLambda, isValidUrl } from "./helper";
import {
downloadAndExtract,
isRunningInAwsLambda,
isValidUrl,
isRunningInAwsLambdaNode20,
} from "./helper";

/** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */
interface Viewport {
Expand Down Expand Up @@ -46,17 +51,36 @@ interface Viewport {

if (isRunningInAwsLambda()) {
if (process.env["FONTCONFIG_PATH"] === undefined) {
process.env["FONTCONFIG_PATH"] = "/tmp/aws";
process.env["FONTCONFIG_PATH"] = "/tmp/fonts";
}

if (process.env["LD_LIBRARY_PATH"] === undefined) {
process.env["LD_LIBRARY_PATH"] = "/tmp/aws/lib";
process.env["LD_LIBRARY_PATH"] = "/tmp/al2/lib";
} else if (
process.env["LD_LIBRARY_PATH"].startsWith("/tmp/aws/lib") !== true
process.env["LD_LIBRARY_PATH"].startsWith("/tmp/al2/lib") !== true
) {
process.env["LD_LIBRARY_PATH"] = [
...new Set([
"/tmp/aws/lib",
"/tmp/al2/lib",
...process.env["LD_LIBRARY_PATH"].split(":"),
]),
].join(":");
}
}

if (isRunningInAwsLambdaNode20()) {
if (process.env["FONTCONFIG_PATH"] === undefined) {
process.env["FONTCONFIG_PATH"] = "/tmp/fonts";
}

if (process.env["LD_LIBRARY_PATH"] === undefined) {
process.env["LD_LIBRARY_PATH"] = "/tmp/al2023/lib";
} else if (
process.env["LD_LIBRARY_PATH"].startsWith("/tmp/al2023/lib") !== true
) {
process.env["LD_LIBRARY_PATH"] = [
...new Set([
"/tmp/al2023/lib",
...process.env["LD_LIBRARY_PATH"].split(":"),
]),
].join(":");
Expand Down Expand Up @@ -300,14 +324,20 @@ class Chromium {
}

// Extract the required files
const promises = [LambdaFS.inflate(`${input}/chromium.br`)];
const promises = [
LambdaFS.inflate(`${input}/chromium.br`),
LambdaFS.inflate(`${input}/fonts.tar.br`),
];
if (this.graphics) {
// Only inflate graphics stack if needed
promises.push(LambdaFS.inflate(`${input}/swiftshader.tar.br`));
}
if (isRunningInAwsLambda()) {
// If running in AWS Lambda, extract more required files
promises.push(LambdaFS.inflate(`${input}/aws.tar.br`));
promises.push(LambdaFS.inflate(`${input}/al2.tar.br`));
}
if (isRunningInAwsLambdaNode20()) {
promises.push(LambdaFS.inflate(`${input}/al2023.tar.br`));
}

// Await all extractions
Expand Down

0 comments on commit 247f748

Please sign in to comment.