neocotic / convert-svg

Node.js packages for converting SVG into other formats using headless Chromium
MIT License
200 stars 47 forks source link

Can't run in loop quickly, fails to open chrome eventually #75

Closed johnnyshankman closed 2 years ago

johnnyshankman commented 2 years ago

Error:

(node:31891) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!

TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (/Users/johnshankman/Documents/rpunks/radioactive-punks/node_modules/puppeteer/lib/Launcher.js:348:14)
    at Interface.<anonymous> (/Users/johnshankman/Documents/rpunks/radioactive-punks/node_modules/puppeteer/lib/Launcher.js:337:50)
    at Interface.emit (events.js:412:35)
    at Interface.close (readline.js:530:8)
    at Socket.onend (readline.js:254:10)
    at Socket.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1334:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:31891) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:31891) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Setup:

Create a for loop that does this over and over again.

const png = await convert(someSvgString, {
    height: 500,
    width: 500
  })
neocotic commented 2 years ago

Can you try again using today's 0.6.0 release to determine if there's any noticeable difference?

I use this library in my brander library to generate hundreds of assets consecutively and haven't experience this issue. That said; I notice that you're using the main convert method which creates a new instance of Chromium each time it's called. It's strongly recommended that you use createConverter to create a single Chromium instance but make sure you destroy it once you're done to free up resources (i.e. kills the orphaned Chromium instance).

For example;

const { createConverter } = require('convert-svg-to-png');
const { readdir } = require('fs/promises');

async function convertSvgFiles(dirPath) {
  const converter = createConverter();

  try {
    const filePaths = await readdir(dirPath);

    for (const filePath of filePaths) {
      await converter.convertFile(filePath);
    }
  } finally {
    await converter.destroy();
  }
}

Please let me know how you get on.

johnnyshankman commented 2 years ago

@neocotic great advice! I didn't know about the createConverter optimization not did I realize I should be destroy() ing after every usage. I think this will fix everything alone, but I'll also try the update as well. Thank you so much.

neocotic commented 2 years ago

Let me know how you get on and hopefully we can close this issue out 🤘