feat(app/metrics): add support for postscripts processors

This commit is contained in:
lowlighter
2022-01-25 19:55:10 -05:00
parent 596fe66f33
commit 1f7090eefc
2 changed files with 10 additions and 10 deletions

View File

@@ -25,7 +25,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
const pending = []
const {queries} = conf
const extras = {css:(conf.settings.extras?.css ?? conf.settings.extras?.default) ? q["extras.css"] ?? "" : "", js:(conf.settings.extras?.js ?? conf.settings.extras?.default) ? q["extras.js"] ?? "" : ""}
const data = {q, animated:true, large:false, base:{}, config:{}, errors:[], plugins:{}, computed:{}, extras}
const data = {q, animated:true, large:false, base:{}, config:{}, errors:[], plugins:{}, computed:{}, extras, postscripts:[]}
const imports = {
plugins:Plugins,
templates:Templates,
@@ -189,7 +189,7 @@ export default async function metrics({login, q}, {graphql, rest, plugins, conf,
console.debug(`metrics/compute/${login} > verified SVG, no parsing errors found`)
}
//Resizing
const {resized, mime} = await imports.svg.resize(rendered, {paddings:q["config.padding"] || conf.settings.padding, convert:convert === "svg" ? null : convert, js:extras.js || null})
const {resized, mime} = await imports.svg.resize(rendered, {paddings:q["config.padding"] || conf.settings.padding, convert:convert === "svg" ? null : convert, scripts:[...data.postscripts, extras.js || null].filter(x => x)})
rendered = resized
//Result

View File

@@ -401,7 +401,7 @@ export const svg = {
return {rendered, mime:"application/pdf"}
},
/**Render and resize svg */
async resize(rendered, {paddings, convert, js}) {
async resize(rendered, {paddings, convert, scripts = []}) {
//Instantiate browser if needed
if (!svg.resize.browser) {
svg.resize.browser = await puppeteer.launch()
@@ -436,16 +436,16 @@ export const svg = {
let height, resized, width
try {
({resized, width, height} = await page.evaluate(
async (padding, js) => {
//Execute user JavaScript if provided
if (js) {
async (padding, scripts) => {
//Execute additional JavaScript
for (const script of scripts) {
try {
console.debug(`metrics/svg/resize > executing ${js}`)
await new Function("document", `return (async () => {${js}})()`)(document) //eslint-disable-line no-new-func
console.debug(`metrics/svg/resize > executing ${script}`)
await new Function("document", `return (async () => {${script}})()`)(document) //eslint-disable-line no-new-func
console.debug("metrics/svg/resize > successfully executed user javascript")
}
catch (error) {
console.debug(`an error occured while evaluating user js: ${error}`)
console.debug(`an error occured while evaluating script: ${error}`)
}
}
//Disable animations
@@ -472,7 +472,7 @@ export const svg = {
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width}
},
padding,
js,
scripts,
))
}
catch (error) {