Files
shields/lib/svg-to-img.js
Paul Melnikow 6e51178e73 Make more consistent use of async/await (#2219)
In #2028 I suggested that we update as much of the code as possible to make consistent use of async await. This snags a bunch of the utility code and attempts to do that.
2018-10-28 11:34:47 -04:00

40 lines
954 B
JavaScript

'use strict'
const { promisify } = require('util')
const gm = require('gm')
const LruCache = require('./lru-cache')
const imageMagick = gm.subClass({ imageMagick: true })
// The following is an arbitrary limit (~1.5MB, 1.5kB/image).
const imgCache = new LruCache(1000)
async function svgToImg(svg, format) {
const cacheIndex = `${format}${svg}`
if (imgCache.has(cacheIndex)) {
return imgCache.get(cacheIndex)
}
const svgBuffer = Buffer.from(
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${svg}`
)
const chain = imageMagick(svgBuffer, `image.${format}`)
.density(90)
.background(format === 'jpg' ? '#FFFFFF' : 'none')
.flatten()
const toBuffer = chain.toBuffer.bind(chain)
const data = await promisify(toBuffer)(format)
imgCache.set(cacheIndex, data)
return data
}
module.exports = svgToImg
// To simplify testing.
module.exports._imgCache = imgCache
module.exports._imageMagick = imageMagick