simonhaenisch/md-to-pdf

feature: use browser context to reduce cpu load on many files

chamabreu opened this issue ยท 3 comments

Problem:
If i start md2pdf with a lot of md files, I get some warnings:

> md2pdf ../mds/**/*.md
(node:40269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
(node:40269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:40269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:40269) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGHUP listeners added to [process]. Use emitter.setMaxListeners() to increase limit

For each file a seperate pupeteer.launch() function is called in generate-output.ts.
This registers a lot of system listeners.
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });

Solution:

Use one single browser instance and create for each convertToPdf just an browser context.
const listrContext = await browser.createIncognitoBrowserContext();

Pass this Context down the functions and use this, to create and manipulate the pages with puppeteer.

I just added this feature locally, but i can not push due to 403.

How can i push my Branch and open a PR for Review?

Hey thanks i've actually been wanting to do that for a while but never got around to it (i've personally never used it with multiple files ๐Ÿ˜…). Usually you'll have to fork the repo first and then you can make a PR from your fork's branch to this repo. But I'll just add you as a collaborator temporarily.

or maybe not just temporarily, i think there will come more... #137 ๐Ÿ˜„

Just ended up merging this now, will do a release so you can test it more easily (: