diff --git a/source/helper.ts b/source/helper.ts index de2329d..83bd539 100644 --- a/source/helper.ts +++ b/source/helper.ts @@ -7,16 +7,41 @@ 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 @@ -38,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; } 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`);