Skip to content

Commit

Permalink
More robust environment setup
Browse files Browse the repository at this point in the history
fixes #315, closes #316
thank you @tibbing
  • Loading branch information
Sparticuz committed Nov 13, 2024
1 parent 8486f47 commit 45ba3cc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
50 changes: 41 additions & 9 deletions source/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
44 changes: 6 additions & 38 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 {
Expand All @@ -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<string> {
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`);
Expand Down

0 comments on commit 45ba3cc

Please sign in to comment.