Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance for concurrent conversions by re-using browser context #141

Merged
merged 2 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Listr from 'listr';
import path from 'path';
import { PackageJson } from '.';
import { Config, defaultConfig } from './lib/config';
import { closeBrowser } from './lib/generate-output';
import { help } from './lib/help';
import { setProcessAndTermTitle } from './lib/helpers';
import { convertMdToPdf } from './lib/md-to-pdf';
Expand Down Expand Up @@ -126,10 +127,11 @@ async function main(args: typeof cliFlags, config: Config) {

if (stdin) {
await convertMdToPdf({ content: stdin }, config, args)
.then(async () => closeServer(server))
.catch(async (error: Error) => {
.finally(async () => {
await closeBrowser();
await closeServer(server);

})
.catch((error: Error) => {
throw error;
});

Expand All @@ -143,7 +145,7 @@ async function main(args: typeof cliFlags, config: Config) {

await new Listr(files.map(getListrTask), { concurrent: true, exitOnError: false })
.run()
.then(() => {
.then(async () => {
if (args['--watch']) {
console.log(chalk.bgBlue('\n watching for changes \n'));

Expand All @@ -155,7 +157,8 @@ async function main(args: typeof cliFlags, config: Config) {
new Listr([getListrTask(file)], { exitOnError: false }).run().catch(console.error),
);
} else {
server.close();
await closeBrowser();
await closeServer(server);
}
})
.catch((error: Error) => {
Expand Down
18 changes: 16 additions & 2 deletions src/lib/generate-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ interface BasicOutput {
filename: string | undefined;
}

/**
* Store a single browser instance reference so that we can re-use it.
*/
let browserPromise: Promise<puppeteer.Browser> | undefined;

/**
* Close the browser instance.
*/
export const closeBrowser = async () => (await browserPromise)?.close();

/**
* Generate the output (either PDF or HTML).
*/
export async function generateOutput(html: string, relativePath: string, config: PdfConfig): Promise<PdfOutput>;
export async function generateOutput(html: string, relativePath: string, config: HtmlConfig): Promise<HtmlOutput>;
export async function generateOutput(html: string, relativePath: string, config: Config): Promise<Output>;
export async function generateOutput(html: string, relativePath: string, config: Config): Promise<Output> {
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });
if (!browserPromise) {
browserPromise = puppeteer.launch({ devtools: config.devtools, ...config.launch_options });
}

const browser = await browserPromise;

const page = await browser.newPage();

Expand Down Expand Up @@ -65,7 +79,7 @@ export async function generateOutput(html: string, relativePath: string, config:
outputFileContent = await page.pdf(config.pdf_options);
}

await browser.close();
await page.close();

return config.devtools ? (undefined as any) : { filename: config.dest, content: outputFileContent };
}