From 5d81cf2367df697398b05faa26cd09713b8cfc79 Mon Sep 17 00:00:00 2001 From: Vu Nguyen Date: Thu, 15 Dec 2022 13:50:48 +0700 Subject: [PATCH] feat: add alternative location for executablePath --- _/amazon/handlers/index.js | 2 +- source/index.ts | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/_/amazon/handlers/index.js b/_/amazon/handlers/index.js index 20e40f2..f95457d 100644 --- a/_/amazon/handlers/index.js +++ b/_/amazon/handlers/index.js @@ -10,7 +10,7 @@ exports.handler = async (event, context) => { const browser = await puppeteer.launch({ args: chromium.args, defaultViewport: chromium.defaultViewport, - executablePath: await chromium.executablePath, + executablePath: await chromium.executablePath(), headless: chromium.headless, ignoreHTTPSErrors: true, }); diff --git a/source/index.ts b/source/index.ts index 56e0a58..5dcb249 100644 --- a/source/index.ts +++ b/source/index.ts @@ -179,8 +179,9 @@ class Chromium { * Inflates the current version of Chromium and returns the path to the binary. * If not running on AWS Lambda nor Google Cloud Functions, `null` is returned instead. */ - static get executablePath(): Promise { - if (existsSync('/tmp/chromium') === true) { + static executablePath(input?: string): Promise { + + if (existsSync('/tmp/chromium') === true && input === undefined) { for (const file of readdirSync('/tmp')) { if (file.startsWith('core.chromium') === true) { unlinkSync(`/tmp/${file}`); @@ -189,15 +190,21 @@ class Chromium { return Promise.resolve('/tmp/chromium'); } - - const input = join(__dirname, '..', 'bin'); + let _input = join(__dirname, '..', 'bin'); + if (input !== undefined) { + if (existsSync(input)) { + _input = join(input, 'bin'); + } else { + throw new Error(`The input directory "${input}" does not exist.`); + } + } const promises = [ - LambdaFS.inflate(`${input}/chromium.br`), - LambdaFS.inflate(`${input}/swiftshader.tar.br`), + LambdaFS.inflate(`${_input}/chromium.br`), + LambdaFS.inflate(`${_input}/swiftshader.tar.br`), ]; if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { - promises.push(LambdaFS.inflate(`${input}/aws.tar.br`)); + promises.push(LambdaFS.inflate(`${_input}/aws.tar.br`)); } return Promise.all(promises).then((result) => result.shift());