From 0562aca09fd095c6137c2b316e984a5a740be43f Mon Sep 17 00:00:00 2001 From: Sparticuz Date: Wed, 28 Dec 2022 10:27:37 -0500 Subject: [PATCH] Refactor executablePath a little bit Thanks @aquarius6666 for the initial code! --- source/index.ts | 50 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/source/index.ts b/source/index.ts index 5dcb249..3f74607 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,4 +1,4 @@ -import { access, createWriteStream, existsSync, mkdirSync, readdirSync, symlink, unlinkSync } from 'fs'; +import { access, createWriteStream, existsSync, mkdirSync, symlink } from 'fs'; import { IncomingMessage } from 'http'; import LambdaFS from './lambdafs'; import { join } from 'path'; @@ -176,38 +176,44 @@ 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. + * Inflates the included version of Chromium + * @param input The location of the `bin` folder + * @returns The path to the `chromium` binary */ - 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}`); - } - } + static async executablePath(input?: string): Promise { + /** + * If the `chromium` binary already exists in /tmp/chromium, return it. + */ + if (existsSync('/tmp/chromium') === true && input === undefined) { return Promise.resolve('/tmp/chromium'); } - 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.`); - } + + /** + * If input is defined, use that as the location of the brotli files, + * otherwise, the default location is ../bin. + * A custom location is needed for workflows that using custom packaging. + */ + input ??= join(__dirname, '..', 'bin'); + + /** + * If the input directory doesn't exist, throw an error. + */ + if (!existsSync(input)) { + 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()); + const result = await Promise.all(promises); + return result.shift(); } /**