From 38e10469c5d75a9e23933d8b1c10f96234873a30 Mon Sep 17 00:00:00 2001 From: Sparticuz Date: Mon, 27 Mar 2023 08:46:00 -0400 Subject: [PATCH] Pull out the aws lambda check into it's own function --- source/helper.ts | 4 ++++ source/index.ts | 12 +++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/source/helper.ts b/source/helper.ts index d6c87c1..fa19998 100644 --- a/source/helper.ts +++ b/source/helper.ts @@ -17,6 +17,10 @@ export const isValidUrl = (input: string) => { } }; +export const isRunningInAwsLambda = () => + process.env.AWS_EXECUTION_ENV && + /^AWS_Lambda_nodejs/.test(process.env.AWS_EXECUTION_ENV) === true; + export const downloadAndExtract = async (url: string) => new Promise((resolve, reject) => { const getOptions = parse(url) as FollowRedirOptions; diff --git a/source/index.ts b/source/index.ts index 924b464..b2dd265 100644 --- a/source/index.ts +++ b/source/index.ts @@ -9,7 +9,7 @@ import { https } from "follow-redirects"; import LambdaFS from "./lambdafs"; import { join } from "node:path"; import { URL } from "node:url"; -import { downloadAndExtract, isValidUrl } from "./helper"; +import { downloadAndExtract, isRunningInAwsLambda, isValidUrl } from "./helper"; /** Viewport taken from https://github.com/puppeteer/puppeteer/blob/main/docs/api/puppeteer.viewport.md */ interface Viewport { @@ -44,10 +44,7 @@ interface Viewport { hasTouch?: boolean; } -if ( - process.env.AWS_EXECUTION_ENV !== undefined && - /^AWS_Lambda_nodejs/.test(process.env.AWS_EXECUTION_ENV) === true -) { +if (isRunningInAwsLambda()) { if (process.env.FONTCONFIG_PATH === undefined) { process.env.FONTCONFIG_PATH = "/tmp/aws"; } @@ -295,10 +292,7 @@ class Chromium { promises.push(LambdaFS.inflate(`${input}/swiftshader.tar.br`)); } - if ( - process.env.AWS_EXECUTION_ENV !== undefined && - /^AWS_Lambda_nodejs/.test(process.env.AWS_EXECUTION_ENV) === true - ) { + if (isRunningInAwsLambda()) { promises.push(LambdaFS.inflate(`${input}/aws.tar.br`)); }