diff --git a/_/ansible/inventory.ini b/_/ansible/inventory.ini index a6bf621..8d8ad71 100644 --- a/_/ansible/inventory.ini +++ b/_/ansible/inventory.ini @@ -14,4 +14,4 @@ instance_size=c7i.12xlarge ansible_connection=ssh ansible_python_interpreter=auto_silent ansible_ssh_private_key_file=ansible.pem -chromium_revision=1343869 +chromium_revision=1368529 diff --git a/bin/chromium.br b/bin/chromium.br index 6c34e30..ce197b1 100644 Binary files a/bin/chromium.br and b/bin/chromium.br differ diff --git a/bin/swiftshader.tar.br b/bin/swiftshader.tar.br index 9e2f1e9..cb6051b 100644 Binary files a/bin/swiftshader.tar.br and b/bin/swiftshader.tar.br differ diff --git a/source/helper.ts b/source/helper.ts index cc03c92..83bd539 100644 --- a/source/helper.ts +++ b/source/helper.ts @@ -2,23 +2,46 @@ import { unlink } from "node:fs"; import { https } from "follow-redirects"; import { tmpdir } from "node:os"; import { extract } from "tar-fs"; -import { parse } from "node:url"; -import type { UrlWithStringQuery } from "node:url"; -interface FollowRedirOptions extends UrlWithStringQuery { +interface FollowRedirOptions extends URL { maxBodyLength: number; } +/** + * Adds the proper folders to the environment + * @param baseLibPath the path to this packages lib folder + */ +export const setupLambdaEnvironment = (baseLibPath: string) => { + // If the FONTCONFIG_PATH is not set, set it to /tmp/fonts + process.env["FONTCONFIG_PATH"] ??= "/tmp/fonts"; + + // If LD_LIBRARY_PATH is undefined, set it to baseLibPath, otherwise, add it + if (process.env["LD_LIBRARY_PATH"] === undefined) { + process.env["LD_LIBRARY_PATH"] = baseLibPath; + } else if (process.env["LD_LIBRARY_PATH"].startsWith(baseLibPath) !== true) { + process.env["LD_LIBRARY_PATH"] = [ + baseLibPath, + ...new Set(process.env["LD_LIBRARY_PATH"].split(":")), + ].join(":"); + } +}; + +/** + * Determines if the input is a valid URL + * @param input the input to check + * @returns boolean indicating if the input is a valid URL + */ export const isValidUrl = (input: string) => { try { return !!new URL(input); - } catch (err) { + } catch { return false; } }; /** - * Determines if the running instance is inside an AWS Lambda container. + * Determines if the running instance is inside an AWS Lambda container, + * and the nodejs version is less than v20. This is to target AL2 instances * AWS_EXECUTION_ENV is for native Lambda instances * AWS_LAMBDA_JS_RUNTIME is for netlify instances * @returns boolean indicating if the running instance is inside a Lambda container @@ -40,15 +63,22 @@ export const isRunningInAwsLambda = () => { return false; }; +/** + * Determines if the running instance is inside an AWS Lambda container, + * and the nodejs version is 20. This is to target AL2023 instances + * AWS_EXECUTION_ENV is for native Lambda instances + * AWS_LAMBDA_JS_RUNTIME is for netlify instances + * CODEBUILD_BUILD_IMAGE is for CodeBuild instances + * @returns boolean indicating if the running instance is inside a Lambda container with nodejs20 + */ 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") + (process.env["AWS_EXECUTION_ENV"] && + process.env["AWS_EXECUTION_ENV"].includes("20.x")) || + (process.env["AWS_LAMBDA_JS_RUNTIME"] && + process.env["AWS_LAMBDA_JS_RUNTIME"].includes("20.x")) || + (process.env["CODEBUILD_BUILD_IMAGE"] && + process.env["CODEBUILD_BUILD_IMAGE"].includes("nodejs20")) ) { return true; } @@ -57,7 +87,7 @@ export const isRunningInAwsLambdaNode20 = () => { export const downloadAndExtract = async (url: string) => new Promise((resolve, reject) => { - const getOptions = parse(url) as FollowRedirOptions; + const getOptions = new URL(url) as FollowRedirOptions; getOptions.maxBodyLength = 60 * 1024 * 1024; // 60mb const destDir = `${tmpdir()}/chromium-pack`; const extractObj = extract(destDir); diff --git a/source/index.ts b/source/index.ts index ef38113..662c468 100644 --- a/source/index.ts +++ b/source/index.ts @@ -14,6 +14,7 @@ import { isRunningInAwsLambda, isValidUrl, isRunningInAwsLambdaNode20, + setupLambdaEnvironment, } from "./helper"; /** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */ @@ -49,42 +50,11 @@ interface Viewport { hasTouch?: boolean; } +// Setup the lambda environment if (isRunningInAwsLambda()) { - 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/al2/lib"; - } else if ( - process.env["LD_LIBRARY_PATH"].startsWith("/tmp/al2/lib") !== true - ) { - process.env["LD_LIBRARY_PATH"] = [ - ...new Set([ - "/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(":"); - } + setupLambdaEnvironment("/tmp/al2/lib"); +} else if (isRunningInAwsLambdaNode20()) { + setupLambdaEnvironment("/tmp/al2023/lib"); } class Chromium { @@ -106,9 +76,7 @@ class Chromium { * Downloads or symlinks a custom font and returns its basename, patching the environment so that Chromium can find it. */ static font(input: string): Promise { - if (process.env["HOME"] === undefined) { - process.env["HOME"] = "/tmp"; - } + process.env["HOME"] ??= "/tmp"; if (existsSync(`${process.env["HOME"]}/.fonts`) !== true) { mkdirSync(`${process.env["HOME"]}/.fonts`);