feat: add alternative location for executablePath

This commit is contained in:
Vu Nguyen 2022-12-15 13:50:48 +07:00
parent 8a57bf8e0b
commit 5d81cf2367
2 changed files with 15 additions and 8 deletions

View File

@ -10,7 +10,7 @@ exports.handler = async (event, context) => {
const browser = await puppeteer.launch({ const browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath(),
headless: chromium.headless, headless: chromium.headless,
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });

View File

@ -179,8 +179,9 @@ class Chromium {
* Inflates the current version of Chromium and returns the path to the binary. * 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. * If not running on AWS Lambda nor Google Cloud Functions, `null` is returned instead.
*/ */
static get executablePath(): Promise<string> { static executablePath(input?: string): Promise<string> {
if (existsSync('/tmp/chromium') === true) {
if (existsSync('/tmp/chromium') === true && input === undefined) {
for (const file of readdirSync('/tmp')) { for (const file of readdirSync('/tmp')) {
if (file.startsWith('core.chromium') === true) { if (file.startsWith('core.chromium') === true) {
unlinkSync(`/tmp/${file}`); unlinkSync(`/tmp/${file}`);
@ -189,15 +190,21 @@ class Chromium {
return Promise.resolve('/tmp/chromium'); return Promise.resolve('/tmp/chromium');
} }
let _input = join(__dirname, '..', 'bin');
const 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 = [ const promises = [
LambdaFS.inflate(`${input}/chromium.br`), LambdaFS.inflate(`${_input}/chromium.br`),
LambdaFS.inflate(`${input}/swiftshader.tar.br`), LambdaFS.inflate(`${_input}/swiftshader.tar.br`),
]; ];
if (/^AWS_Lambda_nodejs(?:10|12|14|16|18)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) { 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()); return Promise.all(promises).then((result) => result.shift());