{"version":3,"file":"renderHTML.mjs","names":[],"sources":["../../../../src/middlewares/web/utils/renderHTML.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport type { Response } from 'express';\nimport LRUCache from 'lru-cache';\n\nimport { HEADERS } from '@verdaccio/core';\nimport type { ConfigYaml, TemplateUIOptions } from '@verdaccio/types';\n\nimport type { Manifest } from './manifest';\nimport renderTemplate from './template';\nimport type { AssetManifest } from './template';\n\n// Cache for rendered HTML templates: max 500 entries, 1 hour TTL\nconst cache = new LRUCache({ max: 500, ttl: 1000 * 60 * 60 });\n\nconst debug = buildDebug('verdaccio:web:render');\n\nconst defaultManifestFiles: Manifest = {\n  js: ['vendors.js', 'main.js'],\n  ico: 'favicon.ico',\n  css: [],\n};\n\nexport default function renderHTML(\n  config: ConfigYaml,\n  manifest: AssetManifest,\n  manifestFiles: Manifest | null | undefined,\n  options: TemplateUIOptions,\n  res: Response\n) {\n  // @ts-ignore\n  const needHtmlCache = [undefined, null].includes(config?.web?.html_cache)\n    ? true\n    : config?.web?.html_cache;\n\n  const scriptsBodyBefore = config?.web?.scriptsBodyBefore || config?.web?.scriptsbodyBefore || [];\n  const scriptsBodyAfter = config?.web?.scriptsBodyAfter || [];\n  const metaScripts = config?.web?.metaScripts || [];\n\n  let webPage;\n\n  const cacheKey = `template:${JSON.stringify(options)}`;\n\n  try {\n    webPage = cache.get(cacheKey);\n    if (!webPage) {\n      webPage = renderTemplate(\n        {\n          manifest: manifestFiles ?? defaultManifestFiles,\n          options,\n          scriptsBodyAfter,\n          metaScripts,\n          scriptsBodyBefore,\n        },\n        manifest\n      );\n\n      if (needHtmlCache) {\n        cache.set(cacheKey, webPage);\n        debug('set template cache');\n      }\n    } else {\n      debug('reuse template cache');\n    }\n  } catch (error: any) {\n    throw new Error(`theme could not be load, stack ${error.stack}`);\n  }\n  res.setHeader('Content-Type', HEADERS.TEXT_HTML);\n  res.send(webPage);\n  debug('web rendered');\n}\n"],"mappings":";;;;;AAYA,IAAM,QAAQ,IAAI,SAAS;CAAE,KAAK;CAAK,KAAK;AAAe,CAAC;AAE5D,IAAM,QAAQ,WAAW,sBAAsB;AAE/C,IAAM,uBAAiC;CACrC,IAAI,CAAC,cAAc,SAAS;CAC5B,KAAK;CACL,KAAK,CAAC;AACR;AAEA,SAAwB,WACtB,QACA,UACA,eACA,SACA,KACA;CAEA,MAAM,gBAAgB,CAAC,KAAA,GAAW,IAAI,CAAC,CAAC,SAAS,QAAQ,KAAK,UAAU,IACpE,OACA,QAAQ,KAAK;CAEjB,MAAM,oBAAoB,QAAQ,KAAK,qBAAqB,QAAQ,KAAK,qBAAqB,CAAC;CAC/F,MAAM,mBAAmB,QAAQ,KAAK,oBAAoB,CAAC;CAC3D,MAAM,cAAc,QAAQ,KAAK,eAAe,CAAC;CAEjD,IAAI;CAEJ,MAAM,WAAW,YAAY,KAAK,UAAU,OAAO;CAEnD,IAAI;EACF,UAAU,MAAM,IAAI,QAAQ;EAC5B,IAAI,CAAC,SAAS;GACZ,UAAU,eACR;IACE,UAAU,iBAAiB;IAC3B;IACA;IACA;IACA;GACF,GACA,QACF;GAEA,IAAI,eAAe;IACjB,MAAM,IAAI,UAAU,OAAO;IAC3B,MAAM,oBAAoB;GAC5B;EACF,OACE,MAAM,sBAAsB;CAEhC,SAAS,OAAY;EACnB,MAAM,IAAI,MAAM,kCAAkC,MAAM,OAAO;CACjE;CACA,IAAI,UAAU,gBAAgB,QAAQ,SAAS;CAC/C,IAAI,KAAK,OAAO;CAChB,MAAM,cAAc;AACtB"}