Skip to content

Commit

Permalink
Merge pull request #317 from Sparticuz/chromium/131
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz authored Nov 13, 2024
2 parents cd11388 + 45ba3cc commit 7571c6f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
2 changes: 1 addition & 1 deletion _/ansible/inventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified bin/chromium.br
Binary file not shown.
Binary file modified bin/swiftshader.tar.br
Binary file not shown.
56 changes: 43 additions & 13 deletions source/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -57,7 +87,7 @@ export const isRunningInAwsLambdaNode20 = () => {

export const downloadAndExtract = async (url: string) =>
new Promise<string>((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);
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 7571c6f

Please sign in to comment.