File size: 844 Bytes
ecdd7f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const Viz = require('viz.js');
const { Module, render } = require('viz.js/full.render.js');

async function renderGraphviz(graphStr, opts) {
  const { format, engine, width, height } = opts || {};
  const viz = new Viz({ Module, render });
  const result = await viz.renderString(graphStr, {
    // Built-in format options don't work great. Hardcode to svg and convert it
    // to other supported formats later.
    format: 'svg',
    engine,
  });
  if (format === 'png') {
    // Defer require of sharp as it is not supported by docker container.
    const sharp = require('sharp');
    const img = sharp(Buffer.from(result));
    if (width && height) {
      img.resize({
        width,
        height,
        fit: 'contain',
      });
    }
    return img.png().toBuffer();
  }
  return result;
}

module.exports = {
  renderGraphviz,
};