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 pending = []
const {queries} = conf 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 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 = { const imports = {
plugins:Plugins, plugins:Plugins,
templates:Templates, 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`) console.debug(`metrics/compute/${login} > verified SVG, no parsing errors found`)
} }
//Resizing //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 rendered = resized
//Result //Result

View File

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