import { createRenderer } from './types' ; import { readFile , unlink } from 'fs/promises' ; import { spawn , ChildProcess } from 'child_process' ; const call = ( cmd , args , hook ? ) : Promise < { stdout : Buffer ; stderr : Buffer ; } > => new Promise ( ( resolve , reject ) => { const p = spawn ( cmd , args ) ; const stdout = [ ] ; const stderr = [ ] ; p . stdout . on ( 'data' , ( data ) => { stdout . push ( data ) ; } ) ; p . stderr . on ( 'data' , ( data ) => { stderr . push ( data ) ; } ) ; p . once ( 'close' , ( code ) => { const err = Buffer . concat ( stderr ) ; const out = Buffer . concat ( stdout ) ; if ( code === 0 ) { resolve ( { stderr : err , stdout : out , } ) ; return ; } reject ( new Error ( `Exit status not 0: ${ code }\n${ err }` ) ) ; } ) ; if ( hook ) { hook ( p ) ; } } ) ; export default createRenderer ( { init : async ( ) => { const { stdout } = await call ( 'resvg' , [ '--help' ] ) ; if ( ! `${ stdout }` . includes ( '--version' ) ) { throw new Error ( 'resvg bin not valid' ) ; } } , render : async ( svg ) => { const rand = Math . floor ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) ; const outFile = `/tmp/resvg-${ rand }.png` ; await call ( 'resvg' , [ '--skip-system-fonts' , '-' , outFile ] , ( p ) => { if ( p . stdin ) { p . stdin . end ( svg ) ; } } ) ; const res = await readFile ( outFile ) ; unlink ( outFile ) ; return res ; } , } ) ;