Skip to content

Commit 7571c6f

Browse files
authored
Merge pull request #317 from Sparticuz/chromium/131
2 parents cd11388 + 45ba3cc commit 7571c6f

File tree

5 files changed

+50
-52
lines changed

5 files changed

+50
-52
lines changed

Diff for: _/ansible/inventory.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ instance_size=c7i.12xlarge
1414
ansible_connection=ssh
1515
ansible_python_interpreter=auto_silent
1616
ansible_ssh_private_key_file=ansible.pem
17-
chromium_revision=1343869
17+
chromium_revision=1368529

Diff for: bin/chromium.br

-1.19 MB
Binary file not shown.

Diff for: bin/swiftshader.tar.br

-30.8 KB
Binary file not shown.

Diff for: source/helper.ts

+43-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,46 @@ import { unlink } from "node:fs";
22
import { https } from "follow-redirects";
33
import { tmpdir } from "node:os";
44
import { extract } from "tar-fs";
5-
import { parse } from "node:url";
6-
import type { UrlWithStringQuery } from "node:url";
75

8-
interface FollowRedirOptions extends UrlWithStringQuery {
6+
interface FollowRedirOptions extends URL {
97
maxBodyLength: number;
108
}
119

10+
/**
11+
* Adds the proper folders to the environment
12+
* @param baseLibPath the path to this packages lib folder
13+
*/
14+
export const setupLambdaEnvironment = (baseLibPath: string) => {
15+
// If the FONTCONFIG_PATH is not set, set it to /tmp/fonts
16+
process.env["FONTCONFIG_PATH"] ??= "/tmp/fonts";
17+
18+
// If LD_LIBRARY_PATH is undefined, set it to baseLibPath, otherwise, add it
19+
if (process.env["LD_LIBRARY_PATH"] === undefined) {
20+
process.env["LD_LIBRARY_PATH"] = baseLibPath;
21+
} else if (process.env["LD_LIBRARY_PATH"].startsWith(baseLibPath) !== true) {
22+
process.env["LD_LIBRARY_PATH"] = [
23+
baseLibPath,
24+
...new Set(process.env["LD_LIBRARY_PATH"].split(":")),
25+
].join(":");
26+
}
27+
};
28+
29+
/**
30+
* Determines if the input is a valid URL
31+
* @param input the input to check
32+
* @returns boolean indicating if the input is a valid URL
33+
*/
1234
export const isValidUrl = (input: string) => {
1335
try {
1436
return !!new URL(input);
15-
} catch (err) {
37+
} catch {
1638
return false;
1739
}
1840
};
1941

2042
/**
21-
* Determines if the running instance is inside an AWS Lambda container.
43+
* Determines if the running instance is inside an AWS Lambda container,
44+
* and the nodejs version is less than v20. This is to target AL2 instances
2245
* AWS_EXECUTION_ENV is for native Lambda instances
2346
* AWS_LAMBDA_JS_RUNTIME is for netlify instances
2447
* @returns boolean indicating if the running instance is inside a Lambda container
@@ -40,15 +63,22 @@ export const isRunningInAwsLambda = () => {
4063
return false;
4164
};
4265

66+
/**
67+
* Determines if the running instance is inside an AWS Lambda container,
68+
* and the nodejs version is 20. This is to target AL2023 instances
69+
* AWS_EXECUTION_ENV is for native Lambda instances
70+
* AWS_LAMBDA_JS_RUNTIME is for netlify instances
71+
* CODEBUILD_BUILD_IMAGE is for CodeBuild instances
72+
* @returns boolean indicating if the running instance is inside a Lambda container with nodejs20
73+
*/
4374
export const isRunningInAwsLambdaNode20 = () => {
4475
if (
45-
process.env["AWS_EXECUTION_ENV"] &&
46-
process.env["AWS_EXECUTION_ENV"].includes("20.x")
47-
) {
48-
return true;
49-
} else if (
50-
process.env["AWS_LAMBDA_JS_RUNTIME"] &&
51-
process.env["AWS_LAMBDA_JS_RUNTIME"].includes("20.x")
76+
(process.env["AWS_EXECUTION_ENV"] &&
77+
process.env["AWS_EXECUTION_ENV"].includes("20.x")) ||
78+
(process.env["AWS_LAMBDA_JS_RUNTIME"] &&
79+
process.env["AWS_LAMBDA_JS_RUNTIME"].includes("20.x")) ||
80+
(process.env["CODEBUILD_BUILD_IMAGE"] &&
81+
process.env["CODEBUILD_BUILD_IMAGE"].includes("nodejs20"))
5282
) {
5383
return true;
5484
}
@@ -57,7 +87,7 @@ export const isRunningInAwsLambdaNode20 = () => {
5787

5888
export const downloadAndExtract = async (url: string) =>
5989
new Promise<string>((resolve, reject) => {
60-
const getOptions = parse(url) as FollowRedirOptions;
90+
const getOptions = new URL(url) as FollowRedirOptions;
6191
getOptions.maxBodyLength = 60 * 1024 * 1024; // 60mb
6292
const destDir = `${tmpdir()}/chromium-pack`;
6393
const extractObj = extract(destDir);

Diff for: source/index.ts

+6-38
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isRunningInAwsLambda,
1515
isValidUrl,
1616
isRunningInAwsLambdaNode20,
17+
setupLambdaEnvironment,
1718
} from "./helper";
1819

1920
/** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */
@@ -49,42 +50,11 @@ interface Viewport {
4950
hasTouch?: boolean;
5051
}
5152

53+
// Setup the lambda environment
5254
if (isRunningInAwsLambda()) {
53-
if (process.env["FONTCONFIG_PATH"] === undefined) {
54-
process.env["FONTCONFIG_PATH"] = "/tmp/fonts";
55-
}
56-
57-
if (process.env["LD_LIBRARY_PATH"] === undefined) {
58-
process.env["LD_LIBRARY_PATH"] = "/tmp/al2/lib";
59-
} else if (
60-
process.env["LD_LIBRARY_PATH"].startsWith("/tmp/al2/lib") !== true
61-
) {
62-
process.env["LD_LIBRARY_PATH"] = [
63-
...new Set([
64-
"/tmp/al2/lib",
65-
...process.env["LD_LIBRARY_PATH"].split(":"),
66-
]),
67-
].join(":");
68-
}
69-
}
70-
71-
if (isRunningInAwsLambdaNode20()) {
72-
if (process.env["FONTCONFIG_PATH"] === undefined) {
73-
process.env["FONTCONFIG_PATH"] = "/tmp/fonts";
74-
}
75-
76-
if (process.env["LD_LIBRARY_PATH"] === undefined) {
77-
process.env["LD_LIBRARY_PATH"] = "/tmp/al2023/lib";
78-
} else if (
79-
process.env["LD_LIBRARY_PATH"].startsWith("/tmp/al2023/lib") !== true
80-
) {
81-
process.env["LD_LIBRARY_PATH"] = [
82-
...new Set([
83-
"/tmp/al2023/lib",
84-
...process.env["LD_LIBRARY_PATH"].split(":"),
85-
]),
86-
].join(":");
87-
}
55+
setupLambdaEnvironment("/tmp/al2/lib");
56+
} else if (isRunningInAwsLambdaNode20()) {
57+
setupLambdaEnvironment("/tmp/al2023/lib");
8858
}
8959

9060
class Chromium {
@@ -106,9 +76,7 @@ class Chromium {
10676
* Downloads or symlinks a custom font and returns its basename, patching the environment so that Chromium can find it.
10777
*/
10878
static font(input: string): Promise<string> {
109-
if (process.env["HOME"] === undefined) {
110-
process.env["HOME"] = "/tmp";
111-
}
79+
process.env["HOME"] ??= "/tmp";
11280

11381
if (existsSync(`${process.env["HOME"]}/.fonts`) !== true) {
11482
mkdirSync(`${process.env["HOME"]}/.fonts`);

0 commit comments

Comments
 (0)