@@ -2,23 +2,46 @@ import { unlink } from "node:fs";
2
2
import { https } from "follow-redirects" ;
3
3
import { tmpdir } from "node:os" ;
4
4
import { extract } from "tar-fs" ;
5
- import { parse } from "node:url" ;
6
- import type { UrlWithStringQuery } from "node:url" ;
7
5
8
- interface FollowRedirOptions extends UrlWithStringQuery {
6
+ interface FollowRedirOptions extends URL {
9
7
maxBodyLength : number ;
10
8
}
11
9
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
+ */
12
34
export const isValidUrl = ( input : string ) => {
13
35
try {
14
36
return ! ! new URL ( input ) ;
15
- } catch ( err ) {
37
+ } catch {
16
38
return false ;
17
39
}
18
40
} ;
19
41
20
42
/**
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
22
45
* AWS_EXECUTION_ENV is for native Lambda instances
23
46
* AWS_LAMBDA_JS_RUNTIME is for netlify instances
24
47
* @returns boolean indicating if the running instance is inside a Lambda container
@@ -40,15 +63,22 @@ export const isRunningInAwsLambda = () => {
40
63
return false ;
41
64
} ;
42
65
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
+ */
43
74
export const isRunningInAwsLambdaNode20 = ( ) => {
44
75
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" ) )
52
82
) {
53
83
return true ;
54
84
}
@@ -57,7 +87,7 @@ export const isRunningInAwsLambdaNode20 = () => {
57
87
58
88
export const downloadAndExtract = async ( url : string ) =>
59
89
new Promise < string > ( ( resolve , reject ) => {
60
- const getOptions = parse ( url ) as FollowRedirOptions ;
90
+ const getOptions = new URL ( url ) as FollowRedirOptions ;
61
91
getOptions . maxBodyLength = 60 * 1024 * 1024 ; // 60mb
62
92
const destDir = `${ tmpdir ( ) } /chromium-pack` ;
63
93
const extractObj = extract ( destDir ) ;
0 commit comments