neocotic / convert-svg

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

Failed converting when doing many in parallel #93

Closed paintoshi closed 2 years ago

paintoshi commented 2 years ago

I'm using the library to convert thousands of svgs in an async function. But running into problems, where some just fail. Others get corrupted and I see an array of svgs in the same output image coming from other svgs. Like it's overwriting itself by colliding in memory or on disk. Could it be the temp file that is the problem here? And is there anything I can do about it?

image

paintoshi commented 2 years ago

Found a solution. Creating and destroying puppeteer on every conversion allows async conversion without conflicts.

https://github.com/neocotic/convert-svg/tree/main/packages/convert-svg-to-png#createconverteroptions

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();
  }
}